Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_hostgroup_info.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2021 Aparna Patil(@aparna-patil)
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10 DOCUMENTATION = '''
11 ---
12 module: azure_rm_hostgroup_info
13
14 version_added: "1.10.0"
15
16 short_description: Get host group facts
17
18 description:
19 - Get facts for specified dedicated host group or all host groups in a given resource group.
20
21 options:
22 resource_group:
23 description:
24 - Name of the resource group.
25 type: str
26 name:
27 description:
28 - Name of the host group.
29 type: str
30 tags:
31 description:
32 - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
33 type: list
34 elements: str
35
36 extends_documentation_fragment:
37 - azure.azcollection.azure
38
39 author:
40 - Aparna Patil (@aparna-patil)
41
42 '''
43
44 EXAMPLES = '''
45 - name: Get facts for one host group
46 azure_rm_hostgroup_info:
47 resource_group: myAzureResourceGroup
48 name: myhostgroup
49
50 - name: Get facts for all host groups in resource group
51 azure_rm_hostgroup_info:
52 resource_group: myAzureResourceGroup
53 '''
54
55 RETURN = '''
56 hostgroups:
57 description:
58 - Gets a list of dedicated host groups.
59 returned: always
60 type: list
61 elements: dict
62 sample: [
63 {
64 "hosts": null,
65 "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myAzureResourceGroup/providers/
66 Microsoft.Compute/hostGroups/myhostgroup",
67 "location": "eastus",
68 "name": "myhostgroup",
69 "platform_fault_domain_count": 1,
70 "tags": {
71 "key1": "value1"
72 },
73 "zones": [
74 "1"
75 ]
76 }
77 ]
78 '''
79
80 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
81
82 try:
83 from azure.core.exceptions import ResourceNotFoundError
84 except Exception:
85 # This is handled in azure_rm_common
86 pass
87
88 AZURE_OBJECT_CLASS = 'HostGroup'
89
90
91 class AzureRMHostGroupInfo(AzureRMModuleBase):
92
93 def __init__(self):
94
95 # define user inputs into argument
96 self.module_arg_spec = dict(
97 name=dict(type='str'),
98 resource_group=dict(type='str'),
99 tags=dict(type='list', elements='str')
100 )
101
102 # store the results of the module operation
103 self.results = dict(
104 changed=False
105 )
106
107 self.name = None
108 self.resource_group = None
109 self.tags = None
110
111 super(AzureRMHostGroupInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True)
112
113 def exec_module(self, **kwargs):
114
115 for key in self.module_arg_spec:
116 setattr(self, key, kwargs[key])
117
118 results = []
119 # list the conditions and results to return based on user input
120 if self.name is not None:
121 # if there is a host group name provided, return facts about that dedicated host group
122 results = self.get_item()
123 elif self.resource_group:
124 # all the host groups listed in specific resource group
125 results = self.list_resource_group()
126 else:
127 # all the host groups in a subscription
128 results = self.list_items()
129
130 self.results['hostgroups'] = self.curated_items(results)
131
132 return self.results
133
134 def get_item(self):
135 self.log('Get properties for {0}'.format(self.name))
136 item = None
137 results = []
138 # get specific host group
139 try:
140 item = self.compute_client.dedicated_host_groups.get(self.resource_group, self.name)
141 except ResourceNotFoundError:
142 pass
143
144 # serialize result
145 if item and self.has_tags(item.tags, self.tags):
146 results = [item]
147 return results
148
149 def list_resource_group(self):
150 self.log('List all host groups for resource group - {0}'.format(self.resource_group))
151 try:
152 response = self.compute_client.dedicated_host_groups.list_by_resource_group(self.resource_group)
153 except ResourceNotFoundError as exc:
154 self.fail("Failed to list for resource group {0} - {1}".format(self.resource_group, str(exc)))
155
156 results = []
157 for item in response:
158 if self.has_tags(item.tags, self.tags):
159 results.append(item)
160 return results
161
162 def list_items(self):
163 self.log('List all host groups for a subscription ')
164 try:
165 response = self.compute_client.dedicated_host_groups.list_by_subscription()
166 except ResourceNotFoundError as exc:
167 self.fail("Failed to list all items - {0}".format(str(exc)))
168
169 results = []
170 for item in response:
171 if self.has_tags(item.tags, self.tags):
172 results.append(item)
173 return results
174
175 def curated_items(self, raws):
176 return [self.hostgroup_to_dict(item) for item in raws] if raws else []
177
178 def hostgroup_to_dict(self, hostgroup):
179 result = dict(
180 id=hostgroup.id,
181 name=hostgroup.name,
182 location=hostgroup.location,
183 tags=hostgroup.tags,
184 platform_fault_domain_count=hostgroup.platform_fault_domain_count,
185 zones=hostgroup.zones,
186 hosts=[dict(id=x.id) for x in hostgroup.hosts] if hostgroup.hosts else None
187 )
188 return result
189
190
191 def main():
192 AzureRMHostGroupInfo()
193
194
195 if __name__ == '__main__':
196 main()