zabbix_host idempotency fix for situation where ZabbixAPI handles host groups case insensitively (#63860)
This commit is contained in:
parent
2fa8f9cfd8
commit
82c63c0ac3
1 changed files with 11 additions and 23 deletions
|
@ -447,13 +447,12 @@ class Host(object):
|
||||||
|
|
||||||
# get group ids by group names
|
# get group ids by group names
|
||||||
def get_group_ids_by_group_names(self, group_names):
|
def get_group_ids_by_group_names(self, group_names):
|
||||||
group_ids = []
|
|
||||||
if self.check_host_group_exist(group_names):
|
if self.check_host_group_exist(group_names):
|
||||||
group_list = self._zapi.hostgroup.get({'output': 'extend', 'filter': {'name': group_names}})
|
return self._zapi.hostgroup.get({'output': 'groupid', 'filter': {'name': group_names}})
|
||||||
for group in group_list:
|
|
||||||
group_id = group['groupid']
|
# get host groups ids by host id
|
||||||
group_ids.append({'groupid': group_id})
|
def get_group_ids_by_host_id(self, host_id):
|
||||||
return group_ids
|
return self._zapi.hostgroup.get({'output': 'groupid', 'hostids': host_id})
|
||||||
|
|
||||||
# get host templates by host id
|
# get host templates by host id
|
||||||
def get_host_templates_by_host_id(self, host_id):
|
def get_host_templates_by_host_id(self, host_id):
|
||||||
|
@ -463,16 +462,6 @@ class Host(object):
|
||||||
template_ids.append(template['templateid'])
|
template_ids.append(template['templateid'])
|
||||||
return template_ids
|
return template_ids
|
||||||
|
|
||||||
# get host groups by host id
|
|
||||||
def get_host_groups_by_host_id(self, host_id):
|
|
||||||
exist_host_groups = []
|
|
||||||
host_groups_list = self._zapi.hostgroup.get({'output': 'extend', 'hostids': host_id})
|
|
||||||
|
|
||||||
if len(host_groups_list) >= 1:
|
|
||||||
for host_groups_name in host_groups_list:
|
|
||||||
exist_host_groups.append(host_groups_name['name'])
|
|
||||||
return exist_host_groups
|
|
||||||
|
|
||||||
# check the exist_interfaces whether it equals the interfaces or not
|
# check the exist_interfaces whether it equals the interfaces or not
|
||||||
def check_interface_properties(self, exist_interface_list, interfaces):
|
def check_interface_properties(self, exist_interface_list, interfaces):
|
||||||
interfaces_port_list = []
|
interfaces_port_list = []
|
||||||
|
@ -506,14 +495,14 @@ class Host(object):
|
||||||
return host['status']
|
return host['status']
|
||||||
|
|
||||||
# check all the properties before link or clear template
|
# check all the properties before link or clear template
|
||||||
def check_all_properties(self, host_id, host_groups, status, interfaces, template_ids,
|
def check_all_properties(self, host_id, group_ids, status, interfaces, template_ids,
|
||||||
exist_interfaces, host, proxy_id, visible_name, description, host_name,
|
exist_interfaces, host, proxy_id, visible_name, description, host_name,
|
||||||
inventory_mode, inventory_zabbix, tls_accept, tls_psk_identity, tls_psk,
|
inventory_mode, inventory_zabbix, tls_accept, tls_psk_identity, tls_psk,
|
||||||
tls_issuer, tls_subject, tls_connect, ipmi_authtype, ipmi_privilege,
|
tls_issuer, tls_subject, tls_connect, ipmi_authtype, ipmi_privilege,
|
||||||
ipmi_username, ipmi_password):
|
ipmi_username, ipmi_password):
|
||||||
# get the existing host's groups
|
# get the existing host's groups
|
||||||
exist_host_groups = self.get_host_groups_by_host_id(host_id)
|
exist_host_groups = sorted(self.get_group_ids_by_host_id(host_id), key=lambda k: k['groupid'])
|
||||||
if set(host_groups) != set(exist_host_groups):
|
if sorted(group_ids, key=lambda k: k['groupid']) != exist_host_groups:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# get the existing status
|
# get the existing status
|
||||||
|
@ -830,8 +819,7 @@ def main():
|
||||||
if not host_groups:
|
if not host_groups:
|
||||||
# if host_groups have not been specified when updating an existing host, just
|
# if host_groups have not been specified when updating an existing host, just
|
||||||
# get the group_ids from the existing host without updating them.
|
# get the group_ids from the existing host without updating them.
|
||||||
host_groups = host.get_host_groups_by_host_id(host_id)
|
group_ids = host.get_group_ids_by_host_id(host_id)
|
||||||
group_ids = host.get_group_ids_by_group_names(host_groups)
|
|
||||||
|
|
||||||
# get existing host's interfaces
|
# get existing host's interfaces
|
||||||
exist_interfaces = host._zapi.hostinterface.get({'output': 'extend', 'hostids': host_id})
|
exist_interfaces = host._zapi.hostinterface.get({'output': 'extend', 'hostids': host_id})
|
||||||
|
@ -861,12 +849,12 @@ def main():
|
||||||
template_ids = list(set(template_ids + host.get_host_templates_by_host_id(host_id)))
|
template_ids = list(set(template_ids + host.get_host_templates_by_host_id(host_id)))
|
||||||
|
|
||||||
if not force:
|
if not force:
|
||||||
for group_id in host.get_group_ids_by_group_names(host.get_host_groups_by_host_id(host_id)):
|
for group_id in host.get_group_ids_by_host_id(host_id):
|
||||||
if group_id not in group_ids:
|
if group_id not in group_ids:
|
||||||
group_ids.append(group_id)
|
group_ids.append(group_id)
|
||||||
|
|
||||||
# update host
|
# update host
|
||||||
if host.check_all_properties(host_id, host_groups, status, interfaces, template_ids,
|
if host.check_all_properties(host_id, group_ids, status, interfaces, template_ids,
|
||||||
exist_interfaces, zabbix_host_obj, proxy_id, visible_name,
|
exist_interfaces, zabbix_host_obj, proxy_id, visible_name,
|
||||||
description, host_name, inventory_mode, inventory_zabbix,
|
description, host_name, inventory_mode, inventory_zabbix,
|
||||||
tls_accept, tls_psk_identity, tls_psk, tls_issuer, tls_subject, tls_connect,
|
tls_accept, tls_psk_identity, tls_psk, tls_issuer, tls_subject, tls_connect,
|
||||||
|
|
Loading…
Reference in a new issue