Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2016, Julien Stroheker <[email protected]>
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_availabilityset_info
13
14 version_added: "0.1.2"
15
16 short_description: Get Azure Availability Set facts
17
18 description:
19 - Get facts for a specific availability set or all availability sets.
20
21 options:
22 name:
23 description:
24 - Limit results to a specific availability set.
25 type: str
26 resource_group:
27 description:
28 - The resource group to search for the desired availability set.
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 - Julien Stroheker (@julienstroheker)
41 '''
42
43 EXAMPLES = '''
44 - name: Get facts for one availability set
45 azure_rm_availabilityset_info:
46 name: Testing
47 resource_group: myResourceGroup
48
49 - name: Get facts for all availability sets in a specific resource group
50 azure_rm_availabilityset_info:
51 resource_group: myResourceGroup
52 tags:
53 - key
54 - key:value
55 '''
56
57 RETURN = '''
58 azure_availabilityset:
59 description: List of availability sets dicts.
60 returned: always
61 type: complex
62 contains:
63 location:
64 description:
65 - Location where the resource lives.
66 type: str
67 sample: eastus2
68 name:
69 description:
70 - Resource name.
71 type: str
72 sample: myAvailabilitySet
73 properties:
74 description:
75 - The properties of the resource.
76 type: dict
77 contains:
78 platformFaultDomainCount:
79 description:
80 - Fault Domain count.
81 type: int
82 sample: 3
83 platformUpdateDomainCount:
84 description:
85 - Update Domain count.
86 type: int
87 sample: 2
88 virtualMachines:
89 description:
90 - A list of references to all virtualmachines in the availability set.
91 type: list
92 sample: []
93 sku:
94 description:
95 - Location where the resource lives.
96 type: str
97 sample: Aligned
98 type:
99 description:
100 - Resource type.
101 type: str
102 sample: "Microsoft.Compute/availabilitySets"
103 tags:
104 description:
105 - Resource tags.
106 type: dict
107 sample: { env: sandbox }
108 '''
109
110 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
111
112 try:
113 from azure.core.exceptions import ResourceNotFoundError
114 except Exception:
115 # handled in azure_rm_common
116 pass
117
118 AZURE_OBJECT_CLASS = 'AvailabilitySet'
119
120
121 class AzureRMAvailabilitySetInfo(AzureRMModuleBase):
122 """Utility class to get availability set facts"""
123
124 def __init__(self):
125
126 self.module_args = dict(
127 name=dict(type='str'),
128 resource_group=dict(type='str'),
129 tags=dict(type='list', elements='str')
130 )
131
132 self.results = dict(
133 changed=False,
134 ansible_info=dict(
135 azure_availabilitysets=[]
136 )
137 )
138
139 self.name = None
140 self.resource_group = None
141 self.tags = None
142
143 super(AzureRMAvailabilitySetInfo, self).__init__(
144 derived_arg_spec=self.module_args,
145 supports_check_mode=True,
146 supports_tags=False,
147 facts_module=True
148 )
149
150 def exec_module(self, **kwargs):
151
152 for key in self.module_args:
153 setattr(self, key, kwargs[key])
154
155 if self.name and not self.resource_group:
156 self.fail("Parameter error: resource group required when filtering by name.")
157 if self.name:
158 self.results['ansible_info']['azure_availabilitysets'] = self.get_item()
159 else:
160 self.results['ansible_info']['azure_availabilitysets'] = self.list_items()
161
162 return self.results
163
164 def get_item(self):
165 """Get a single availability set"""
166
167 self.log('Get properties for {0}'.format(self.name))
168
169 item = None
170 result = []
171
172 try:
173 item = self.compute_client.availability_sets.get(self.resource_group, self.name)
174 except ResourceNotFoundError:
175 pass
176
177 if item and self.has_tags(item.tags, self.tags):
178 avase = self.serialize_obj(item, AZURE_OBJECT_CLASS)
179 avase['name'] = item.name
180 avase['type'] = item.type
181 avase['sku'] = item.sku.name
182 result = [avase]
183
184 return result
185
186 def list_items(self):
187 """Get all availability sets"""
188
189 self.log('List all availability sets')
190
191 try:
192 response = self.compute_client.availability_sets.list(self.resource_group)
193 except ResourceNotFoundError as exc:
194 self.fail('Failed to list all items - {0}'.format(str(exc)))
195
196 results = []
197 for item in response:
198 if self.has_tags(item.tags, self.tags):
199 avase = self.serialize_obj(item, AZURE_OBJECT_CLASS)
200 avase['name'] = item.name
201 avase['type'] = item.type
202 avase['sku'] = item.sku.name
203 results.append(avase)
204
205 return results
206
207
208 def main():
209 """Main module execution code path"""
210
211 AzureRMAvailabilitySetInfo()
212
213
214 if __name__ == '__main__':
215 main()