# -*- coding: utf-8 -*-
# 批量更新主机接口
# 作者: 陈磊
# 时间: 2019-11-19
from WorkApi.ApiBase import ApiBase
from WorkUtils.UtilsRequest import UtilsRequest
from WorkUtils.UtilsLog import UtilsLog


class HostInterfaceReplace(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 = "/hostinterface/replace"
        self._url = self._host + self._path
        self._params = {}
        self._json = {}
        self.local_json = {}
        self.response = ""

        self.hostids = None
        self.interfaces = None

        self.interfaces_dns = None
        self.interfaces_ip = None
        self.interfaces_main = None
        self.interfaces_port = None
        self.interfaces_type = None
        self.interfaces_useip = None
        self.interfaces_bulk = None

        self.api = UtilsRequest()

    def get_response(self):
        if self.interfaces_ip is not None:
            self.interfaces = []
            for x, y in enumerate(self.interfaces_ip):
                base = ApiBase()
                base.dict_add_key(_key="dns", value=self.interfaces_dns, num=x)
                base.dict_add_key(_key="ip", value=self.interfaces_ip, num=x)
                base.dict_add_key(_key="main", value=self.interfaces_main, num=x)
                base.dict_add_key(_key="port", value=self.interfaces_port, num=x)
                base.dict_add_key(_key="type", value=self.interfaces_type, num=x)
                base.dict_add_key(_key="useip", value=self.interfaces_useip, num=x)
                base.dict_add_key(_key="bulk", value=self.interfaces_bulk, num=x)
                self.interfaces.append(base._json)
        base = ApiBase()
        base.dict_add_key(_key="hostids", value=self.hostids)
        base.dict_add_key(_key="interfaces", value=self.interfaces)
        self.local_json = base._json
        self._json = self.local_json
        self.response = self.api.post(url=self._url, json=self._json)
