Debian Codegraph @

ansible_collections/community/vmware/plugins/modules/vmware_category_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Ansible Project
5 # Copyright: (c) 2018, Abhijeet Kasurde <[email protected]>
6 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
7 # SPDX-License-Identifier: GPL-3.0-or-later
8
9 from __future__ import absolute_import, division, print_function
10 __metaclass__ = type
11
12
13 DOCUMENTATION = r'''
14 ---
15 module: vmware_category_info
16 short_description: Gather info about VMware tag categories
17 description:
18 - This module can be used to gather information about VMware tag categories.
19 - Tag feature is introduced in vSphere 6 version, so this module is not supported in earlier versions of vSphere.
20 author:
21 - Abhijeet Kasurde (@Akasurde)
22 requirements:
23 - vSphere Automation SDK
24 extends_documentation_fragment:
25 - vmware.vmware.base_options
26 - vmware.vmware.additional_rest_options
27
28 '''
29
30 EXAMPLES = r'''
31 - name: Gather info about tag categories
32 community.vmware.vmware_category_info:
33 hostname: "{{ vcenter_hostname }}"
34 username: "{{ vcenter_username }}"
35 password: "{{ vcenter_password }}"
36 delegate_to: localhost
37 register: all_tag_category_info
38
39 - name: Gather category id from given tag category
40 community.vmware.vmware_category_info:
41 hostname: "{{ vcenter_hostname }}"
42 username: "{{ vcenter_username }}"
43 password: "{{ vcenter_password }}"
44 delegate_to: localhost
45 register: tag_category_results
46
47 - set_fact:
48 category_id: "{{ item.category_id }}"
49 loop: "{{ tag_category_results.tag_category_info|json_query(query) }}"
50 vars:
51 query: "[?category_name==`Category0001`]"
52 - debug: var=category_id
53
54 '''
55
56 RETURN = r'''
57 tag_category_info:
58 description: metadata of tag categories
59 returned: always
60 type: list
61 sample: [
62 {
63 "category_associable_types": [],
64 "category_cardinality": "MULTIPLE",
65 "category_description": "awesome description",
66 "category_id": "urn:vmomi:InventoryServiceCategory:e785088d-6981-4b1c-9fb8-1100c3e1f742:GLOBAL",
67 "category_name": "Category0001",
68 "category_used_by": []
69 },
70 {
71 "category_associable_types": [
72 "VirtualMachine"
73 ],
74 "category_cardinality": "SINGLE",
75 "category_description": "another awesome description",
76 "category_id": "urn:vmomi:InventoryServiceCategory:ae5b7c6c-e622-4671-9b96-76e93adb70f2:GLOBAL",
77 "category_name": "template_tag",
78 "category_used_by": []
79 }
80 ]
81 '''
82
83 from ansible.module_utils.basic import AnsibleModule
84 from ansible_collections.community.vmware.plugins.module_utils.vmware_rest_client import VmwareRestClient
85 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import rest_compatible_argument_spec
86
87
88 class VmwareCategoryInfoManager(VmwareRestClient):
89 def __init__(self, module):
90 super(VmwareCategoryInfoManager, self).__init__(module)
91 self.category_service = self.api_client.tagging.Category
92
93 def get_all_tag_categories(self):
94 """Retrieve all tag category information."""
95 global_tag_categories = []
96 for category in self.category_service.list():
97 category_obj = self.category_service.get(category)
98 global_tag_categories.append(
99 dict(
100 category_description=category_obj.description,
101 category_used_by=category_obj.used_by,
102 category_cardinality=str(category_obj.cardinality),
103 category_associable_types=category_obj.associable_types,
104 category_id=category_obj.id,
105 category_name=category_obj.name,
106 )
107 )
108
109 self.module.exit_json(changed=False, tag_category_info=global_tag_categories)
110
111
112 def main():
113 argument_spec = rest_compatible_argument_spec()
114 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
115
116 vmware_category_info = VmwareCategoryInfoManager(module)
117 vmware_category_info.get_all_tag_categories()
118
119
120 if __name__ == '__main__':
121 main()