# -*- coding: utf-8 -*-
# 创建主机原型
# 作者: 陈磊
# 时间: 2019-11-11
from WorkUtils.UtilsRequest import UtilsRequest
from WorkUtils.UtilsLog import UtilsLog
from WorkApi.ApiBase import ApiBase


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

        self.hostid = None
        self.host = None
        self.name = None
        self.status = None
        self.templateid = None
        self.tls_connect = None
        self.tls_accept = None
        self.tls_issuer = None
        self.tls_subject = None
        self.tls_psk_identity = None
        self.tls_psk = None
        self.groupLinks = None
        self.groupPrototypes = None

        self.templateids = None
        self.ruleid = None
        self.groupLinks_groupid = None
        self.groupPrototypes_name = None

        self.api = UtilsRequest()

    def get_response(self):
        if self.groupLinks_groupid is not None:
            self.groupLinks = []
            for x, y in enumerate(self.groupLinks_groupid):
                base = ApiBase()
                base.dict_add_key(_key="groupid", value=self.groupLinks_groupid, num=x)
                self.groupLinks.append(base._json)
        else:
            pass

        if self.groupPrototypes_name is not None:
            self.groupPrototypes = []
            for x, y in enumerate(self.groupPrototypes_name):
                base = ApiBase()
                base.dict_add_key(_key="name", value=self.groupPrototypes_name, num=x)
                self.groupPrototypes.append(base._json)
        else:
            pass

        base = ApiBase()
        base.dict_add_key(_key="hostid", value=self.hostid)
        base.dict_add_key(_key="host", value=self.host)
        base.dict_add_key(_key="name", value=self.name)
        base.dict_add_key(_key="status", value=self.status)
        base.dict_add_key(_key="templateids", value=self.templateids)
        base.dict_add_key(_key="tls_connect", value=self.tls_connect)
        base.dict_add_key(_key="tls_accept", value=self.tls_accept)
        base.dict_add_key(_key="tls_issuer", value=self.tls_issuer)
        base.dict_add_key(_key="tls_subject", value=self.tls_subject)
        base.dict_add_key(_key="tls_psk_identity", value=self.tls_psk_identity)
        base.dict_add_key(_key="ruleid", value=self.ruleid)
        base.dict_add_key(_key="groupLinks", value=self.groupLinks)
        base.dict_add_key(_key="groupPrototypes", value=self.groupPrototypes)

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