# -*- coding: utf-8 -*-
# 创建模板组
# 作者: 陈磊
# 时间: 2019-10-23
from WorkUtils.UtilsRequest import UtilsRequest
from WorkUtils.UtilsLog import UtilsLog


class TemplateCreate(object):
    def __init__(self, _host, host=None):
        """
        :param _host: 域名
        :param host: 主机名称
        :return:
        """
        self.log = UtilsLog()
        self.log.info("调用创模板")
        self.log.info(self.__class__)

        self._host = _host
        self._headers = {}
        self._path = "/template/create"
        self._url = self._host + self._path
        self._params = {}
        self._json = {}
        self.response = ""

        self.host = host
        self.groups = []
        self.tags = []
        self.templates = []
        self.macros = []
        self.hosts = []
        self.api = UtilsRequest()

    def groups_add(self, groupid):
        """
        :param groupid:
        :return:
        """
        self.groups.append({
            "groupid": groupid,
        })

    def tags_add(self, tag):
        """
        :param tag:
        :return:
        """
        self.tags.append({
            "tags": "Host name",
            "value": tag
        })

    def templates_add(self, template):
        """
        :param template:
        :return:
        """
        self.templates.append(template)

    def macros_add(self, macro):
        """
        :param macro:
        :return:
        """
        self.macros.append(macro)

    def hosts_add(self, hostid):
        """
        :param hostid:
        :return:
        """
        self.hosts.append({
            "hostid": hostid
        })

    def get_response(self):
        self._json = {
            "host": self.host,
            "tags": self.tags,
            "groups": self.groups,
            "templates": self.templates,
            "macros": self.macros,
            "hosts": self.hosts,
        }
        self.response = self.api.post(url=self._url, json=self._json)
