# -*- coding: utf-8 -*-
# 更新发现规则
# 作者: 陈磊
# 时间: 2019-12-06
from WorkApi.ApiBase import ApiBase
from WorkUtils.UtilsRequest import UtilsRequest
from WorkUtils.UtilsLog import UtilsLog


class DRuleUpdate(object):
    def __init__(self, _host):
        """
        :param _host: 域名
        :return:
        """
        self.log = UtilsLog()
        self.log.info("调用更新发现规则")
        self.log.info(self.__class__)

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

        self.druleid = None
        self.iprange = None
        self.name = None
        self.delay = None
        self.nextcheck = None
        self.proxy_hostid = None
        self.status = None
        self.dchecks = None

        self.dchecks_type = None
        self.dchecks_key_ = None
        self.dchecks_snmp_community = None
        self.dchecks_ports = None
        self.dchecks_host_source = None
        self.dchecks_name_source = None

        self.api = UtilsRequest()

    def get_response(self):
        if self.dchecks_type is not None:
            self.dchecks = []
            for x, y in enumerate(self.dchecks_type):
                base = ApiBase()
                base.dict_add_key(_key="type", value=self.dchecks_type, num=x)
                base.dict_add_key(_key="key_", value=self.dchecks_key_, num=x)
                base.dict_add_key(_key="snmp_community", value=self.dchecks_snmp_community, num=x)
                base.dict_add_key(_key="ports", value=self.dchecks_ports, num=x)
                base.dict_add_key(_key="host_source", value=self.dchecks_host_source, num=x)
                base.dict_add_key(_key="name_source", value=self.dchecks_name_source, num=x)
                self.dchecks.append(base._json)
        else:
            pass

        base = ApiBase()
        base.dict_add_key(_key="druleid", value=self.druleid)
        base.dict_add_key(_key="iprange", value=self.iprange)
        base.dict_add_key(_key="name", value=self.name)
        base.dict_add_key(_key="delay", value=self.delay)
        base.dict_add_key(_key="nextcheck", value=self.nextcheck)
        base.dict_add_key(_key="proxy_hostid", value=self.proxy_hostid)
        base.dict_add_key(_key="status", value=self.status)
        base.dict_add_key(_key="dchecks", value=self.dchecks)
        self._json = base._json

        self.response = self.api.post(url=self._url, json=self._json)
