Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2019 Zim Kalinowski, (@zikalino)
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
11 DOCUMENTATION = '''
12 ---
13 module: azure_rm_devtestlab_info
14 version_added: "0.1.2"
15 short_description: Get Azure DevTest Lab facts
16 description:
17 - Get facts of Azure DevTest Lab.
18
19 options:
20 resource_group:
21 description:
22 - The name of the resource group.
23 type: str
24 name:
25 description:
26 - The name of the lab.
27 type: str
28 tags:
29 description:
30 - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
31 type: list
32 elements: str
33
34 extends_documentation_fragment:
35 - azure.azcollection.azure
36
37 author:
38 - Zim Kalinowski (@zikalino)
39 '''
40
41 EXAMPLES = '''
42 - name: List instances of DevTest Lab by resource group
43 azure_rm_devtestlab_info:
44 resource_group: testrg
45 tags:
46 - key:value
47
48 - name: List instances of DevTest Lab in subscription
49 azure_rm_devtestlab_info:
50
51 - name: Get instance of DevTest Lab
52 azure_rm_devtestlab_info:
53 resource_group: testrg
54 name: testlab
55 '''
56
57 RETURN = '''
58 labs:
59 description:
60 - A list of dictionaries containing facts for Lab.
61 returned: always
62 type: complex
63 contains:
64 id:
65 description:
66 - The identifier of the resource.
67 returned: always
68 type: str
69 sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/microsoft.devtestlab/labs/myLab
70 resource_group:
71 description:
72 - The name of the resource.
73 returned: always
74 type: str
75 sample: testrg
76 name:
77 description:
78 - The name of the resource.
79 returned: always
80 type: str
81 sample: testlab
82 location:
83 description:
84 - The location of the resource.
85 returned: always
86 type: str
87 sample: eastus
88 storage_type:
89 description:
90 - Lab storage type.
91 returned: always
92 type: str
93 sample: standard
94 premium_data_disks:
95 description:
96 - Are premium data disks allowed.
97 returned: always
98 type: bool
99 sample: false
100 provisioning_state:
101 description:
102 - Lab provisioning state.
103 returned: always
104 type: str
105 sample: Succeeded
106 artifacts_storage_account:
107 description:
108 - Artifacts storage account ID.
109 returned: always
110 type: str
111 sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Storage/storageAccounts/myLab6346
112 default_premium_storage_account:
113 description:
114 - Default premium storage account ID.
115 returned: always
116 type: str
117 sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Storage/storageAccounts/myLab6346
118 default_storage_account:
119 description:
120 - Default storage account ID.
121 returned: always
122 type: str
123 sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Storage/storageAccounts/myLab6346
124 premium_data_disk_storage_account:
125 description:
126 - Default storage account ID.
127 returned: always
128 type: str
129 sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Storage/storageAccounts/myLab6346
130 vault_name:
131 description:
132 - Key vault ID.
133 returned: always
134 type: str
135 sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.KeyVault/vaults/myLab6788
136 tags:
137 description:
138 - The tags of the resource.
139 returned: always
140 type: dict
141 sample: "{ 'MyTag': 'MyValue' }"
142 '''
143
144 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
145
146 try:
147 from azure.mgmt.devtestlabs import DevTestLabsClient
148 from azure.core.exceptions import ResourceNotFoundError
149 except ImportError:
150 # This is handled in azure_rm_common
151 pass
152
153
154 class AzureRMDevTestLabInfo(AzureRMModuleBase):
155 def __init__(self):
156 # define user inputs into argument
157 self.module_arg_spec = dict(
158 resource_group=dict(
159 type='str'
160 ),
161 name=dict(
162 type='str'
163 ),
164 tags=dict(
165 type='list',
166 elements='str'
167 )
168 )
169 # store the results of the module operation
170 self.results = dict(
171 changed=False
172 )
173 self.mgmt_client = None
174 self.resource_group = None
175 self.name = None
176 self.tags = None
177 super(AzureRMDevTestLabInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True)
178
179 def exec_module(self, **kwargs):
180
181 for key in self.module_arg_spec:
182 setattr(self, key, kwargs[key])
183 self.mgmt_client = self.get_mgmt_svc_client(DevTestLabsClient,
184 base_url=self._cloud_environment.endpoints.resource_manager)
185
186 if self.resource_group is not None:
187 if self.name is not None:
188 self.results['labs'] = self.get()
189 else:
190 self.results['labs'] = self.list_by_resource_group()
191 else:
192 self.results['labs'] = self.list_by_subscription()
193 return self.results
194
195 def list_by_resource_group(self):
196 response = None
197 results = []
198 try:
199 response = self.mgmt_client.labs.list_by_resource_group(resource_group_name=self.resource_group)
200 self.log("Response : {0}".format(response))
201 except Exception as e:
202 self.log('Could not get facts for Lab.')
203
204 if response is not None:
205 for item in response:
206 if self.has_tags(item.tags, self.tags):
207 results.append(self.format_response(item))
208
209 return results
210
211 def list_by_subscription(self):
212 response = None
213 results = []
214 try:
215 response = self.mgmt_client.labs.list_by_subscription()
216 self.log("Response : {0}".format(response))
217 except Exception as e:
218 self.log('Could not get facts for Lab.')
219
220 if response is not None:
221 for item in response:
222 if self.has_tags(item.tags, self.tags):
223 results.append(self.format_response(item))
224
225 return results
226
227 def get(self):
228 response = None
229 results = []
230 try:
231 response = self.mgmt_client.labs.get(resource_group_name=self.resource_group,
232 name=self.name)
233 self.log("Response : {0}".format(response))
234 except ResourceNotFoundError as e:
235 self.log('Could not get facts for Lab.')
236
237 if response and self.has_tags(response.tags, self.tags):
238 results.append(self.format_response(response))
239
240 return results
241
242 def format_response(self, item):
243 d = item.as_dict()
244 d = {
245 'id': d.get('id', None),
246 'resource_group': self.resource_group,
247 'name': d.get('name', None),
248 'location': d.get('location', '').replace(' ', '').lower(),
249 'storage_type': d.get('lab_storage_type', '').lower(),
250 'premium_data_disks': d.get('premium_data_disks') == 'Enabled',
251 'provisioning_state': d.get('provisioning_state'),
252 'artifacts_storage_account': d.get('artifacts_storage_account'),
253 'default_premium_storage_account': d.get('default_premium_storage_account'),
254 'default_storage_account': d.get('default_storage_account'),
255 'premium_data_disk_storage_account': d.get('premium_data_disk_storage_account'),
256 'vault_name': d.get('vault_name'),
257 'tags': d.get('tags', None)
258 }
259 return d
260
261
262 def main():
263 AzureRMDevTestLabInfo()
264
265
266 if __name__ == '__main__':
267 main()