VMware: refactor REST APIs (#60220)
Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
parent
95aa86fcc3
commit
dae2040717
4 changed files with 155 additions and 92 deletions
|
@ -35,7 +35,6 @@ except ImportError:
|
|||
VSPHERE_IMP_ERR = traceback.format_exc()
|
||||
HAS_VSPHERE = False
|
||||
|
||||
from ansible.module_utils._text import to_native
|
||||
from ansible.module_utils.basic import env_fallback, missing_required_lib
|
||||
|
||||
|
||||
|
@ -128,7 +127,7 @@ class VmwareRestClient(object):
|
|||
|
||||
return client
|
||||
|
||||
def get_tags_for_object(self, tag_service, tag_assoc_svc, dobj):
|
||||
def get_tags_for_object(self, tag_service=None, tag_assoc_svc=None, dobj=None):
|
||||
"""
|
||||
Return list of tag objects associated with an object
|
||||
Args:
|
||||
|
@ -137,13 +136,89 @@ class VmwareRestClient(object):
|
|||
tag_assoc_svc: Tag Association object
|
||||
Returns: List of tag objects associated with the given object
|
||||
"""
|
||||
tag_ids = tag_assoc_svc.list_attached_tags(dobj)
|
||||
# This method returns list of tag objects only,
|
||||
# Please use get_tags_for_dynamic_obj for more object details
|
||||
tags = []
|
||||
if not dobj:
|
||||
return tags
|
||||
|
||||
if not tag_service:
|
||||
tag_service = self.api_client.tagging.Tag
|
||||
|
||||
if not tag_assoc_svc:
|
||||
tag_assoc_svc = self.api_client.tagging.TagAssociation
|
||||
|
||||
tag_ids = tag_assoc_svc.list_attached_tags(dobj)
|
||||
|
||||
for tag_id in tag_ids:
|
||||
tags.append(tag_service.get(tag_id))
|
||||
|
||||
return tags
|
||||
|
||||
def get_vm_tags(self, tag_service, tag_association_svc, vm_mid=None):
|
||||
def get_tags_for_dynamic_obj(self, mid=None, type=None):
|
||||
"""
|
||||
Return list of tag object details associated with object
|
||||
Args:
|
||||
mid: Dynamic object for specified object
|
||||
type: Type of DynamicID to lookup
|
||||
|
||||
Returns: List of tag object details associated with the given object
|
||||
|
||||
"""
|
||||
tags = []
|
||||
if mid is None:
|
||||
return tags
|
||||
dynamic_managed_object = DynamicID(type=type, id=mid)
|
||||
|
||||
temp_tags_model = self.get_tags_for_object(dynamic_managed_object)
|
||||
|
||||
category_service = self.api_client.tagging.Category
|
||||
|
||||
for tag_obj in temp_tags_model:
|
||||
tags.append({
|
||||
'id': tag_obj.id,
|
||||
'category_name': category_service.get(tag_obj.category_id).name,
|
||||
'name': tag_obj.name,
|
||||
'description': tag_obj.description,
|
||||
'category_id': tag_obj.category_id,
|
||||
})
|
||||
|
||||
return tags
|
||||
|
||||
def get_tags_for_cluster(self, cluster_mid=None):
|
||||
"""
|
||||
Return list of tag object associated with cluster
|
||||
Args:
|
||||
cluster_mid: Dynamic object for cluster
|
||||
|
||||
Returns: List of tag object associated with the given cluster
|
||||
|
||||
"""
|
||||
return self.get_tags_for_dynamic_obj(mid=cluster_mid, type='ClusterComputeResource')
|
||||
|
||||
def get_tags_for_hostsystem(self, hostsystem_mid=None):
|
||||
"""
|
||||
Return list of tag object associated with host system
|
||||
Args:
|
||||
hostsystem_mid: Dynamic object for host system
|
||||
|
||||
Returns: List of tag object associated with the given host system
|
||||
|
||||
"""
|
||||
return self.get_tags_for_dynamic_obj(mid=hostsystem_mid, type='HostSystem')
|
||||
|
||||
def get_tags_for_vm(self, vm_mid=None):
|
||||
"""
|
||||
Return list of tag object associated with virtual machine
|
||||
Args:
|
||||
vm_mid: Dynamic object for virtual machine
|
||||
|
||||
Returns: List of tag object associated with the given virtual machine
|
||||
|
||||
"""
|
||||
return self.get_tags_for_dynamic_obj(mid=vm_mid, type='VirtualMachine')
|
||||
|
||||
def get_vm_tags(self, tag_service=None, tag_association_svc=None, vm_mid=None):
|
||||
"""
|
||||
Return list of tag name associated with virtual machine
|
||||
Args:
|
||||
|
@ -154,14 +229,18 @@ class VmwareRestClient(object):
|
|||
Returns: List of tag names associated with the given virtual machine
|
||||
|
||||
"""
|
||||
# This API returns just names of tags
|
||||
# Please use get_tags_for_vm for more tag object details
|
||||
tags = []
|
||||
if vm_mid is None:
|
||||
return tags
|
||||
dynamic_managed_object = DynamicID(type='VirtualMachine', id=vm_mid)
|
||||
|
||||
temp_tags_model = self.get_tags_for_object(tag_service, tag_association_svc, dynamic_managed_object)
|
||||
for t in temp_tags_model:
|
||||
tags.append(t.name)
|
||||
|
||||
for tag_obj in temp_tags_model:
|
||||
tags.append(tag_obj.name)
|
||||
|
||||
return tags
|
||||
|
||||
@staticmethod
|
||||
|
|
|
@ -124,14 +124,9 @@ try:
|
|||
except ImportError:
|
||||
pass
|
||||
|
||||
from ansible.module_utils.vmware_rest_client import VmwareRestClient
|
||||
try:
|
||||
from com.vmware.vapi.std_client import DynamicID
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.vmware import PyVmomi, vmware_argument_spec, find_datacenter_by_name, find_cluster_by_name
|
||||
from ansible.module_utils.vmware_rest_client import VmwareRestClient
|
||||
|
||||
|
||||
class VmwreClusterFactsManager(PyVmomi):
|
||||
|
@ -174,33 +169,6 @@ class VmwreClusterFactsManager(PyVmomi):
|
|||
cluster_objs.append(child)
|
||||
return cluster_objs
|
||||
|
||||
def get_tags_for_object(self, dobj):
|
||||
"""
|
||||
Return tags associated with an object
|
||||
Args:
|
||||
dobj: Dynamic object
|
||||
Returns: List of tags associated with the given object
|
||||
"""
|
||||
vmware_client = VmwareRestClient(self.module)
|
||||
|
||||
cluster_dynamic_obj = DynamicID(type='ClusterComputeResource', id=dobj._moId)
|
||||
self.tag_service = vmware_client.api_client.tagging.Tag
|
||||
self.tag_association_svc = vmware_client.api_client.tagging.TagAssociation
|
||||
self.category_service = vmware_client.api_client.tagging.Category
|
||||
|
||||
tag_ids = self.tag_association_svc.list_attached_tags(cluster_dynamic_obj)
|
||||
tags = []
|
||||
for tag_id in tag_ids:
|
||||
tag_obj = self.tag_service.get(tag_id)
|
||||
tags.append({
|
||||
'id': tag_obj.id,
|
||||
'category_name': self.category_service.get(tag_obj.category_id).name,
|
||||
'name': tag_obj.name,
|
||||
'description': tag_obj.description,
|
||||
'category_id': tag_obj.category_id,
|
||||
})
|
||||
return tags
|
||||
|
||||
def gather_cluster_facts(self):
|
||||
"""
|
||||
Gather facts about cluster
|
||||
|
@ -242,7 +210,8 @@ class VmwreClusterFactsManager(PyVmomi):
|
|||
|
||||
tag_info = []
|
||||
if self.params.get('show_tag'):
|
||||
tag_info = self.get_tags_for_object(cluster)
|
||||
vmware_client = VmwareRestClient(self.module)
|
||||
tag_info = vmware_client.get_tags_for_cluster(cluster_mid=cluster._moId)
|
||||
|
||||
results['clusters'][cluster.name] = dict(
|
||||
enable_ha=das_config.enabled,
|
||||
|
|
|
@ -159,10 +159,6 @@ except ImportError:
|
|||
pass
|
||||
|
||||
from ansible.module_utils.vmware_rest_client import VmwareRestClient
|
||||
try:
|
||||
from com.vmware.vapi.std_client import DynamicID
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
class VMwareHostFactManager(PyVmomi):
|
||||
|
@ -192,7 +188,12 @@ class VMwareHostFactManager(PyVmomi):
|
|||
ansible_facts.update(self.get_vsan_facts())
|
||||
ansible_facts.update(self.get_cluster_facts())
|
||||
if self.params.get('show_tag'):
|
||||
ansible_facts.update(self.get_tag_facts())
|
||||
vmware_client = VmwareRestClient(self.module)
|
||||
tag_info = {
|
||||
'tags': vmware_client.get_tags_for_hostsystem(hostsystem_mid=self.host._moId)
|
||||
}
|
||||
ansible_facts.update(tag_info)
|
||||
|
||||
self.module.exit_json(changed=False, ansible_facts=ansible_facts)
|
||||
|
||||
def get_cluster_facts(self):
|
||||
|
@ -284,38 +285,6 @@ class VMwareHostFactManager(PyVmomi):
|
|||
}
|
||||
return facts
|
||||
|
||||
def get_tag_facts(self):
|
||||
vmware_client = VmwareRestClient(self.module)
|
||||
|
||||
host_dynamic_obj = DynamicID(type='HostSystem', id=self.host._moId)
|
||||
self.tag_service = vmware_client.api_client.tagging.Tag
|
||||
self.tag_association_svc = vmware_client.api_client.tagging.TagAssociation
|
||||
self.category_service = vmware_client.api_client.tagging.Category
|
||||
facts = {
|
||||
'tags': self.get_tags_for_object(host_dynamic_obj)
|
||||
}
|
||||
return facts
|
||||
|
||||
def get_tags_for_object(self, dobj):
|
||||
"""
|
||||
Return tags associated with an object
|
||||
Args:
|
||||
dobj: Dynamic object
|
||||
Returns: List of tags associated with the given object
|
||||
"""
|
||||
tag_ids = self.tag_association_svc.list_attached_tags(dobj)
|
||||
tags = []
|
||||
for tag_id in tag_ids:
|
||||
tag_obj = self.tag_service.get(tag_id)
|
||||
tags.append({
|
||||
'id': tag_obj.id,
|
||||
'category_name': self.category_service.get(tag_obj.category_id).name,
|
||||
'name': tag_obj.name,
|
||||
'description': tag_obj.description,
|
||||
'category_id': tag_obj.category_id,
|
||||
})
|
||||
return tags
|
||||
|
||||
|
||||
def main():
|
||||
argument_spec = vmware_argument_spec()
|
||||
|
|
|
@ -65,6 +65,12 @@ options:
|
|||
- ' folder: /folder1/datacenter1/vm/folder2'
|
||||
type: str
|
||||
version_added: 2.9
|
||||
show_tag:
|
||||
description:
|
||||
- Tags related to virtual machine are shown if set to C(True).
|
||||
default: False
|
||||
type: bool
|
||||
version_added: 2.9
|
||||
extends_documentation_fragment: vmware.documentation
|
||||
'''
|
||||
|
||||
|
@ -105,20 +111,40 @@ EXAMPLES = r'''
|
|||
var: vm_facts.virtual_machines
|
||||
|
||||
- name: Get UUID from given VM Name
|
||||
vmware_vm_facts:
|
||||
hostname: '{{ vcenter_hostname }}'
|
||||
username: '{{ vcenter_username }}'
|
||||
password: '{{ vcenter_password }}'
|
||||
vm_type: vm
|
||||
delegate_to: localhost
|
||||
register: vm_facts
|
||||
block:
|
||||
- name: Get virtual machine facts
|
||||
vmware_vm_facts:
|
||||
hostname: '{{ vcenter_hostname }}'
|
||||
username: '{{ vcenter_username }}'
|
||||
password: '{{ vcenter_password }}'
|
||||
folder: "/datacenter/vm/folder"
|
||||
delegate_to: localhost
|
||||
register: vm_facts
|
||||
|
||||
- debug:
|
||||
msg: "{{ item.uuid }}"
|
||||
with_items:
|
||||
- "{{ vm_facts.virtual_machines | json_query(query) }}"
|
||||
vars:
|
||||
query: "[?guest_name=='DC0_H0_VM0']"
|
||||
- debug:
|
||||
msg: "{{ item.uuid }}"
|
||||
with_items:
|
||||
- "{{ vm_facts.virtual_machines | json_query(query) }}"
|
||||
vars:
|
||||
query: "[?guest_name=='DC0_H0_VM0']"
|
||||
|
||||
- name: Get Tags from given VM Name
|
||||
block:
|
||||
- name: Get virtual machine facts
|
||||
vmware_vm_facts:
|
||||
hostname: '{{ vcenter_hostname }}'
|
||||
username: '{{ vcenter_username }}'
|
||||
password: '{{ vcenter_password }}'
|
||||
folder: "/datacenter/vm/folder"
|
||||
delegate_to: localhost
|
||||
register: vm_facts
|
||||
|
||||
- debug:
|
||||
msg: "{{ item.tags }}"
|
||||
with_items:
|
||||
- "{{ vm_facts.virtual_machines | json_query(query) }}"
|
||||
vars:
|
||||
query: "[?guest_name=='DC0_H0_VM0']"
|
||||
'''
|
||||
|
||||
RETURN = r'''
|
||||
|
@ -148,7 +174,16 @@ virtual_machines:
|
|||
},
|
||||
"attributes": {
|
||||
"job": "backup-prepare"
|
||||
}
|
||||
},
|
||||
"tags": [
|
||||
{
|
||||
"category_id": "urn:vmomi:InventoryServiceCategory:b316cc45-f1a9-4277-811d-56c7e7975203:GLOBAL",
|
||||
"category_name": "cat_0001",
|
||||
"description": "",
|
||||
"id": "urn:vmomi:InventoryServiceTag:43737ec0-b832-4abf-abb1-fd2448ce3b26:GLOBAL",
|
||||
"name": "tag_0001"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
'''
|
||||
|
@ -160,12 +195,17 @@ except ImportError:
|
|||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.vmware import PyVmomi, get_all_objs, vmware_argument_spec, _get_vm_prop
|
||||
from ansible.module_utils.vmware_rest_client import VmwareRestClient
|
||||
|
||||
|
||||
class VmwareVmFacts(PyVmomi):
|
||||
def __init__(self, module):
|
||||
super(VmwareVmFacts, self).__init__(module)
|
||||
|
||||
def get_tag_facts(self, vm_dynamic_obj):
|
||||
vmware_client = VmwareRestClient(self.module)
|
||||
return vmware_client.get_tags_for_vm(vm_id=vm_dynamic_obj._moId)
|
||||
|
||||
def get_vm_attributes(self, vm):
|
||||
return dict((x.name, v.value) for x in self.custom_field_mgr
|
||||
for v in vm.customValue if x.key == v.key)
|
||||
|
@ -226,6 +266,10 @@ class VmwareVmFacts(PyVmomi):
|
|||
if self.module.params.get('show_attribute'):
|
||||
vm_attributes = self.get_vm_attributes(vm)
|
||||
|
||||
vm_tags = list()
|
||||
if self.module.params.get('show_tag'):
|
||||
vm_tags = self.get_tag_facts(vm)
|
||||
|
||||
virtual_machine = {
|
||||
"guest_name": summary.config.name,
|
||||
"guest_fullname": summary.config.guestFullName,
|
||||
|
@ -236,7 +280,8 @@ class VmwareVmFacts(PyVmomi):
|
|||
"vm_network": net_dict,
|
||||
"esxi_hostname": esxi_hostname,
|
||||
"cluster": cluster_name,
|
||||
"attributes": vm_attributes
|
||||
"attributes": vm_attributes,
|
||||
"tags": vm_tags
|
||||
}
|
||||
|
||||
vm_type = self.module.params.get('vm_type')
|
||||
|
@ -255,6 +300,7 @@ def main():
|
|||
argument_spec.update(
|
||||
vm_type=dict(type='str', choices=['vm', 'all', 'template'], default='all'),
|
||||
show_attribute=dict(type='bool', default='no'),
|
||||
show_tag=dict(type='bool', default=False),
|
||||
folder=dict(type='str'),
|
||||
)
|
||||
|
||||
|
|
Loading…
Reference in a new issue