# -*- coding: utf-8 -*-
# 更新脚本
# 作者: 陈磊
# 时间: 2019-10-22
from WorkUtils.UtilsRequest import UtilsRequest
from WorkUtils.UtilsLog import UtilsLog


class ScriptUpdate(object):
    def __init__(self, _host, scriptid=None, command=None, name=None, confirmation=None, description=None, execute_on=None, groupid=None,
                 host_access=None, _type=None, usrgrpid=None):
        """
        :param _host: 域名
        :param scriptid: 脚本ID
        :param command : 命令
        :param name  : 脚本名称
        :param confirmation: 确认
        :param description: 描述
        :param execute_on: 在哪里运行脚本
        :param groupid: 可以在其上运行脚本的主机组的ID
        :param host_access: 运行脚本所需的主机权限
        :param _type: 脚本类型
        :param usrgrpid: 允许运行脚本的用户组的ID
        :return:
        """
        self.log = UtilsLog()
        self.log.info("调用更新脚本")
        self.log.info(self.__class__)

        self._host = _host
        self._headers = {}
        self._path = "/script/update"
        self._url = self._host + self._path
        self._params = {}
        self._json = {}
        self.response = ""

        self.scriptid = scriptid
        self.command = command
        self.name = name
        self.confirmation = confirmation
        self.description = description
        self.execute_on = execute_on
        self.groupid = groupid
        self.host_access = host_access
        self._type = _type
        self.usrgrpid = usrgrpid

        self.api = UtilsRequest()

    def get_response(self):
        self._json = {
            "scriptid": self.scriptid,
            "command": self.command,
            "name": self.name,
            "confirmation": self.confirmation,
            "description": self.description,
            "execute_on": self.execute_on,
            "groupid": self.groupid,
            "host_access": self.host_access,
            "_type": self._type,
            "usrgrpid": self.usrgrpid
        }
        self.response = self.api.post(url=self._url, json=self._json)
