VMware: Make a common API for hostsystem params (#36314)

This fix adds a common API for getting host system managed object
from either cluster name or host system.

Fixes: #36010

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2018-02-19 14:00:09 +05:30 committed by GitHub
parent 4d18586fc9
commit 542d27eee4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 45 additions and 184 deletions

View file

@ -1100,3 +1100,33 @@ class PyVmomi(object):
for pg in host_system.config.network.portgroup:
pgs_list.append(pg)
return pgs_list
def get_all_host_objs(self, cluster_name=None, esxi_host_name=None):
"""
Function to get all host system managed object
Args:
cluster_name: Name of Cluster
esxi_host_name: Name of ESXi server
Returns: A list of all host system managed objects, else empty list
"""
host_obj_list = []
if not self.is_vcenter():
host_obj_list.append(get_all_objs(self.content, [vim.HostSystem])[0])
else:
if cluster_name:
cluster_obj = self.find_cluster_by_name(cluster_name=cluster_name)
if cluster_obj:
host_obj_list = [host for host in cluster_obj.host]
else:
self.module.fail_json(changed=False, msg="Cluster '%s' not found" % cluster_name)
elif esxi_host_name:
esxi_host_obj = self.find_hostsystem_by_name(host_name=esxi_host_name)
if esxi_host_obj:
host_obj_list = [esxi_host_obj]
else:
self.module.fail_json(changed=False, msg="ESXi '%s' not found" % esxi_host_name)
return host_obj_list

View file

@ -111,19 +111,7 @@ class VMwareAccpetanceManager(PyVmomi):
super(VMwareAccpetanceManager, self).__init__(module)
cluster_name = self.params.get('cluster_name', None)
esxi_host_name = self.params.get('esxi_hostname', None)
self.hosts = []
if cluster_name:
cluster_obj = self.find_cluster_by_name(cluster_name=cluster_name)
if cluster_obj:
self.hosts = [host for host in cluster_obj.host]
else:
module.fail_json(changed=False, msg="Cluster '%s' not found" % cluster_name)
elif esxi_host_name:
esxi_host_obj = self.find_hostsystem_by_name(host_name=esxi_host_name)
if esxi_host_obj:
self.hosts = [esxi_host_obj]
else:
module.fail_json(changed=False, msg="ESXi '%s' not found" % esxi_host_name)
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
self.desired_state = self.params.get('state')
self.hosts_facts = {}
self.acceptance_level = self.params.get('acceptance_level')

View file

@ -67,19 +67,7 @@ class VmwareConfigFactsManager(PyVmomi):
super(VmwareConfigFactsManager, self).__init__(module)
cluster_name = self.params.get('cluster_name', None)
esxi_host_name = self.params.get('esxi_hostname', None)
self.hosts = []
if cluster_name:
cluster_obj = self.find_cluster_by_name(cluster_name=cluster_name)
if cluster_obj:
self.hosts = [host for host in cluster_obj.host]
else:
module.fail_json(changed=False, msg="Cluster '%s' not found" % cluster_name)
elif esxi_host_name:
esxi_host_obj = self.find_hostsystem_by_name(host_name=esxi_host_name)
if esxi_host_obj:
self.hosts = [esxi_host_obj]
else:
module.fail_json(changed=False, msg="ESXi '%s' not found" % esxi_host_name)
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
def gather_host_facts(self):
hosts_facts = {}

View file

@ -95,19 +95,7 @@ class VmwareConfigManager(PyVmomi):
cluster_name = self.params.get('cluster_name', None)
esxi_host_name = self.params.get('esxi_hostname', None)
self.options = self.params.get('options', dict())
self.hosts = []
if cluster_name:
cluster_obj = self.find_cluster_by_name(cluster_name=cluster_name)
if cluster_obj:
self.hosts = [host for host in cluster_obj.host]
else:
module.fail_json(changed=False, msg="Cluster '%s' not found" % cluster_name)
elif esxi_host_name:
esxi_host_obj = self.find_hostsystem_by_name(host_name=esxi_host_name)
if esxi_host_obj:
self.hosts = [esxi_host_obj]
else:
module.fail_json(changed=False, msg="ESXi '%s' not found" % esxi_host_name)
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
def set_host_configuration_facts(self):
changed = False

View file

@ -86,19 +86,7 @@ class VmwareDnsFactsManager(PyVmomi):
super(VmwareDnsFactsManager, self).__init__(module)
cluster_name = self.params.get('cluster_name', None)
esxi_host_name = self.params.get('esxi_hostname', None)
self.hosts = []
if cluster_name:
cluster_obj = self.find_cluster_by_name(cluster_name=cluster_name)
if cluster_obj:
self.hosts = [host for host in cluster_obj.host]
else:
module.fail_json(changed=False, msg="Cluster '%s' not found" % cluster_name)
elif esxi_host_name:
esxi_host_obj = self.find_hostsystem_by_name(host_name=esxi_host_name)
if esxi_host_obj:
self.hosts = [esxi_host_obj]
else:
module.fail_json(changed=False, msg="ESXi '%s' not found" % esxi_host_name)
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
def gather_dns_facts(self):
hosts_facts = {}

View file

@ -93,19 +93,7 @@ class FirewallFactsManager(PyVmomi):
super(FirewallFactsManager, self).__init__(module)
cluster_name = self.params.get('cluster_name', None)
esxi_host_name = self.params.get('esxi_hostname', None)
self.hosts = []
if cluster_name:
cluster_obj = self.find_cluster_by_name(cluster_name=cluster_name)
if cluster_obj:
self.hosts = [host for host in cluster_obj.host]
else:
module.fail_json(changed=False, msg="Cluster '%s' not found" % cluster_name)
elif esxi_host_name:
esxi_host_obj = self.find_hostsystem_by_name(host_name=esxi_host_name)
if esxi_host_obj:
self.hosts = [esxi_host_obj]
else:
module.fail_json(changed=False, msg="ESXi '%s' not found" % esxi_host_name)
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
@staticmethod
def normalize_rule_set(rule_obj):

View file

@ -121,19 +121,7 @@ class VmwareFirewallManager(PyVmomi):
cluster_name = self.params.get('cluster_name', None)
esxi_host_name = self.params.get('esxi_hostname', None)
self.options = self.params.get('options', dict())
self.hosts = []
if cluster_name:
cluster_obj = self.find_cluster_by_name(cluster_name=cluster_name)
if cluster_obj:
self.hosts = [host for host in cluster_obj.host]
else:
module.fail_json(changed=False, msg="Cluster '%s' not found" % cluster_name)
elif esxi_host_name:
esxi_host_obj = self.find_hostsystem_by_name(host_name=esxi_host_name)
if esxi_host_obj:
self.hosts = [esxi_host_obj]
else:
module.fail_json(changed=False, msg="ESXi '%s' not found" % esxi_host_name)
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
self.firewall_facts = dict()
self.rule_options = self.module.params.get("rules")
self.gather_rule_set()

View file

@ -125,20 +125,7 @@ class VmwareLockdownManager(PyVmomi):
"as vCenter server." % self.module.params['hostname'])
cluster_name = self.params.get('cluster_name', None)
esxi_host_name = self.params.get('esxi_hostname', None)
self.hosts = []
if cluster_name:
cluster_obj = self.find_cluster_by_name(cluster_name=cluster_name)
if cluster_obj:
self.hosts = [host for host in cluster_obj.host]
else:
module.fail_json(changed=False, msg="Cluster '%s' not found" % cluster_name)
elif esxi_host_name:
for host in esxi_host_name:
esxi_host_obj = self.find_hostsystem_by_name(host_name=host)
if esxi_host_obj:
self.hosts.append(esxi_host_obj)
else:
module.fail_json(changed=False, msg="ESXi '%s' not found" % host)
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
def ensure(self):
"""

View file

@ -106,19 +106,7 @@ class VmwareNtpConfigManager(PyVmomi):
cluster_name = self.params.get('cluster_name', None)
esxi_host_name = self.params.get('esxi_hostname', None)
self.ntp_servers = self.params.get('ntp_servers', list())
self.hosts = []
if cluster_name:
cluster_obj = self.find_cluster_by_name(cluster_name=cluster_name)
if cluster_obj:
self.hosts = [host for host in cluster_obj.host]
else:
module.fail_json(changed=False, msg="Cluster '%s' not found" % cluster_name)
elif esxi_host_name:
esxi_host_obj = self.find_hostsystem_by_name(host_name=esxi_host_name)
if esxi_host_obj:
self.hosts = [esxi_host_obj]
else:
module.fail_json(changed=False, msg="ESXi '%s' not found" % esxi_host_name)
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
self.results = {}
self.desired_state = module.params['state']

View file

@ -77,19 +77,7 @@ class VmwarePackageManager(PyVmomi):
super(VmwarePackageManager, self).__init__(module)
cluster_name = self.params.get('cluster_name', None)
esxi_host_name = self.params.get('esxi_hostname', None)
self.hosts = []
if cluster_name:
cluster_obj = self.find_cluster_by_name(cluster_name=cluster_name)
if cluster_obj:
self.hosts = [host for host in cluster_obj.host]
else:
module.fail_json(changed=False, msg="Cluster '%s' not found" % cluster_name)
elif esxi_host_name:
esxi_host_obj = self.find_hostsystem_by_name(host_name=esxi_host_name)
if esxi_host_obj:
self.hosts = [esxi_host_obj]
else:
module.fail_json(changed=False, msg="ESXi '%s' not found" % esxi_host_name)
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
def gather_package_facts(self):
hosts_facts = {}

View file

@ -71,19 +71,7 @@ class VmwareServiceManager(PyVmomi):
super(VmwareServiceManager, self).__init__(module)
cluster_name = self.params.get('cluster_name', None)
esxi_host_name = self.params.get('esxi_hostname', None)
self.hosts = []
if cluster_name:
cluster_obj = self.find_cluster_by_name(cluster_name=cluster_name)
if cluster_obj:
self.hosts = [host for host in cluster_obj.host]
else:
module.fail_json(changed=False, msg="Cluster '%s' not found" % cluster_name)
elif esxi_host_name:
esxi_host_obj = self.find_hostsystem_by_name(host_name=esxi_host_name)
if esxi_host_obj:
self.hosts = [esxi_host_obj]
else:
module.fail_json(changed=False, msg="ESXi '%s' not found" % esxi_host_name)
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
def gather_host_facts(self):
hosts_facts = {}

View file

@ -118,19 +118,7 @@ class VmwareServiceManager(PyVmomi):
cluster_name = self.params.get('cluster_name', None)
esxi_host_name = self.params.get('esxi_hostname', None)
self.options = self.params.get('options', dict())
self.hosts = []
if cluster_name:
cluster_obj = self.find_cluster_by_name(cluster_name=cluster_name)
if cluster_obj:
self.hosts = [host for host in cluster_obj.host]
else:
module.fail_json(changed=False, msg="Cluster '%s' not found" % cluster_name)
elif esxi_host_name:
esxi_host_obj = self.find_hostsystem_by_name(host_name=esxi_host_name)
if esxi_host_obj:
self.hosts = [esxi_host_obj]
else:
module.fail_json(changed=False, msg="ESXi '%s' not found" % esxi_host_name)
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
self.desired_state = self.params.get('state')
self.desired_policy = self.params.get('service_policy', None)
self.service_name = self.params.get('service_name')

View file

@ -79,19 +79,7 @@ class HostVmnicMgr(PyVmomi):
super(HostVmnicMgr, self).__init__(module)
cluster_name = self.params.get('cluster_name', None)
esxi_host_name = self.params.get('esxi_hostname', None)
self.hosts = []
if cluster_name:
cluster_obj = self.find_cluster_by_name(cluster_name=cluster_name)
if cluster_obj:
self.hosts = [host for host in cluster_obj.host]
else:
module.fail_json(changed=False, msg="Cluster '%s' not found" % cluster_name)
elif esxi_host_name:
esxi_host_obj = self.find_hostsystem_by_name(host_name=esxi_host_name)
if esxi_host_obj:
self.hosts = [esxi_host_obj]
else:
module.fail_json(changed=False, msg="ESXi '%s' not found" % esxi_host_name)
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
def gather_host_vmnic_facts(self):
hosts_vmnic_facts = {}

View file

@ -171,17 +171,7 @@ class PyVmomiHelper(PyVmomi):
self.mac_changes = self.params['network_policy'].get('mac_changes')
self.network_policy = self.create_network_policy()
self.state = self.params['state']
self.host_obj_list = []
if cluster and self.find_cluster_by_name(cluster_name=cluster):
self.host_obj_list = self.get_all_hosts_by_cluster(cluster_name=cluster)
elif hosts:
for host in hosts:
host_system = self.find_hostsystem_by_name(host_name=host)
if host_system:
self.host_obj_list.append(host_system)
else:
self.module.fail_json(msg="Failed to find ESXi %s" % host)
self.host_obj_list = self.get_all_host_objs(cluster_name=cluster, esxi_host_name=hosts)
def process_state(self):
"""

View file

@ -184,9 +184,7 @@ class PyVmomiHelper(PyVmomi):
self.vlan_id = self.params['vlan_id']
self.esxi_host_name = self.params['esxi_hostname']
self.esxi_host_obj = self.find_hostsystem_by_name(host_name=self.esxi_host_name)
if not self.esxi_host_obj:
module.fail_json(changed=False, msg="ESXi '%s' not found" % self.esxi_host_name)
self.esxi_host_obj = self.get_all_host_objs(esxi_host_name=self.esxi_host_name)[0]
self.port_group_obj = self.get_port_group_by_name(host_system=self.esxi_host_obj, portgroup_name=self.port_group_name)
if not self.port_group_obj:

View file

@ -102,19 +102,7 @@ class VmkernelFactsManager(PyVmomi):
super(VmkernelFactsManager, self).__init__(module)
cluster_name = self.params.get('cluster_name', None)
esxi_host_name = self.params.get('esxi_hostname', None)
self.hosts = []
if cluster_name:
cluster_obj = self.find_cluster_by_name(cluster_name=cluster_name)
if cluster_obj:
self.hosts = [host for host in cluster_obj.host]
else:
module.fail_json(changed=False, msg="Cluster '%s' not found" % cluster_name)
elif esxi_host_name:
esxi_host_obj = self.find_hostsystem_by_name(host_name=esxi_host_name)
if esxi_host_obj:
self.hosts = [esxi_host_obj]
else:
module.fail_json(changed=False, msg="ESXi '%s' not found" % esxi_host_name)
self.hosts = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_host_name)
self.service_type_vmks = dict()
self.get_all_vmks_by_service_type()