Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_devtestlabvirtualmachine_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_devtestlabvirtualmachine_info
14 version_added: "0.1.2"
15 short_description: Get Azure DevTest Lab Virtual Machine facts
16 description:
17 - Get facts of Azure DevTest Lab Virtual Machine.
18
19 options:
20 resource_group:
21 description:
22 - The name of the resource group.
23 required: True
24 type: str
25 lab_name:
26 description:
27 - The name of the lab.
28 required: True
29 type: str
30 name:
31 description:
32 - The name of the virtual machine.
33 type: str
34 tags:
35 description:
36 - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
37 type: list
38 elements: str
39
40 extends_documentation_fragment:
41 - azure.azcollection.azure
42
43 author:
44 - Zim Kalinowski (@zikalino)
45
46 '''
47
48 EXAMPLES = '''
49 - name: Get instance of DTL Virtual Machine
50 azure_rm_devtestlabvirtualmachine_info:
51 resource_group: myResourceGroup
52 lab_name: myLab
53 name: myVm
54 tags:
55 - key:value
56 '''
57
58 RETURN = '''
59 virtualmachines:
60 description:
61 - A list of dictionaries containing facts for DevTest Lab Virtual Machine.
62 returned: always
63 type: complex
64 contains:
65 id:
66 description:
67 - The identifier of the virtual machine.
68 returned: always
69 type: str
70 sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/microsoft.devtestlab/labs/myLab/virt
71 ualmachines/myVm"
72 resource_group:
73 description:
74 - Name of the resource group.
75 returned: always
76 type: str
77 sample: myResourceGroup
78 lab_name:
79 description:
80 - Name of the lab.
81 returned: always
82 type: str
83 sample: myLab
84 name:
85 description:
86 - Name of the virtual machine.
87 returned: always
88 type: str
89 sample: myVm
90 is_authentication_with_ssh_key:
91 description:
92 - Indicates whether this virtual machine uses an SSH key for authentication.
93 type: bool
94 returned: always
95 sample: False
96 notes:
97 description:
98 - Notes of the virtual machine.
99 returned: always
100 type: str
101 sample: My VM notes
102 disallow_public_ip_address:
103 description:
104 - Whether public IP should be not allowed.
105 returned: always
106 type: bool
107 sample: false
108 expiration_date:
109 description:
110 - Virtual machine expiration date.
111 returned: always
112 type: str
113 sample: "2029-02-22T01:49:12.117974Z"
114 image:
115 description:
116 - Gallery image reference.
117 returned: always
118 type: complex
119 contains:
120 offer:
121 description:
122 - The offer of the gallery image.
123 returned: when created from gallery image
124 type: str
125 sample: UbuntuServer
126 os_type:
127 description:
128 - Operating system type.
129 returned: when created from gallery image
130 type: str
131 sample: Linux
132 sku:
133 description:
134 - The SKU of the gallery image.
135 returned: when created from gallery image
136 type: str
137 sample: 20_04-lts
138 publisher:
139 description:
140 - The publisher of the gallery image.
141 returned: when created from gallery image
142 type: str
143 sample: Canonical
144 version:
145 description:
146 - The version of the gallery image.
147 returned: when created from gallery image
148 type: str
149 sample: latest
150 os_type:
151 description:
152 - Operating system type.
153 returned: always
154 type: str
155 sample: linux
156 vm_size:
157 description:
158 - Virtual machine size.
159 returned: always
160 type: str
161 sample: Standard_A2_v2
162 user_name:
163 description:
164 - Admin user name.
165 returned: always
166 type: str
167 sample: dtl_admin
168 storage_type:
169 description:
170 - Storage type to use for virtual machine.
171 returned: always
172 type: str
173 sample: standard
174 compute_vm_id:
175 description:
176 - Resource id of compute virtual machine.
177 returned: always
178 type: str
179 sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myLab-myVm-097933/providers/Microsoft.Compute/virtualMachines/myVm
180 compute_vm_resource_group:
181 description:
182 - Resource group where compute virtual machine is created.
183 returned: always
184 type: str
185 sample: myLab-myVm-097933
186 compute_vm_name:
187 description:
188 - Name of compute virtual machine.
189 returned: always
190 type: str
191 sample: myVm
192 fqdn:
193 description:
194 - Fully qualified domain name.
195 returned: always
196 type: str
197 sample: myvm.eastus.cloudapp.azure.com
198 provisioning_state:
199 description:
200 - Provisioning state of the virtual network.
201 returned: always
202 type: str
203 sample: Succeeded
204 tags:
205 description:
206 - The tags of the resource.
207 returned: always
208 type: dict
209 sample: "{ 'foo': 'bar' }"
210 '''
211
212 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
213
214 try:
215 from azure.core.exceptions import ResourceNotFoundError
216 from azure.mgmt.devtestlabs import DevTestLabsClient
217 except ImportError:
218 # This is handled in azure_rm_common
219 pass
220
221
222 class AzureRMDtlVirtualMachineInfo(AzureRMModuleBase):
223 def __init__(self):
224 # define user inputs into argument
225 self.module_arg_spec = dict(
226 resource_group=dict(
227 type='str',
228 required=True
229 ),
230 lab_name=dict(
231 type='str',
232 required=True
233 ),
234 name=dict(
235 type='str'
236 ),
237 tags=dict(
238 type='list',
239 elements='str'
240 )
241 )
242 # store the results of the module operation
243 self.results = dict(
244 changed=False
245 )
246 self.mgmt_client = None
247 self.resource_group = None
248 self.lab_name = None
249 self.name = None
250 self.tags = None
251 super(AzureRMDtlVirtualMachineInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True)
252
253 def exec_module(self, **kwargs):
254
255 for key in self.module_arg_spec:
256 setattr(self, key, kwargs[key])
257 self.mgmt_client = self.get_mgmt_svc_client(DevTestLabsClient,
258 base_url=self._cloud_environment.endpoints.resource_manager)
259
260 if self.name:
261 self.results['virtualmachines'] = self.get()
262 else:
263 self.results['virtualmachines'] = self.list()
264
265 return self.results
266
267 def get(self):
268 response = None
269 results = []
270 try:
271 response = self.mgmt_client.virtual_machines.get(resource_group_name=self.resource_group,
272 lab_name=self.lab_name,
273 name=self.name)
274 self.log("Response : {0}".format(response))
275 except ResourceNotFoundError as e:
276 self.log('Could not get facts for Virtual Machine.')
277 return None
278
279 if response and self.has_tags(response.tags, self.tags):
280 results.append(self.format_response(response))
281
282 return results
283
284 def list(self):
285 response = None
286 results = []
287 try:
288 response = self.mgmt_client.virtual_machines.list(resource_group_name=self.resource_group,
289 lab_name=self.lab_name)
290 self.log("Response : {0}".format(response))
291 except Exception as e:
292 self.log('Could not get facts for Virtual Machine.')
293 return None
294
295 if response is not None:
296 for item in response:
297 if self.has_tags(item.tags, self.tags):
298 results.append(self.format_response(item))
299 return results
300
301 def format_response(self, item):
302 d = item.as_dict()
303 d = {
304 'id': d.get('id', None),
305 'resource_group': self.parse_resource_to_dict(d.get('id')).get('resource_group'),
306 'lab_name': self.parse_resource_to_dict(d.get('id')).get('name'),
307 'name': d.get('name'),
308 'notes': d.get('notes'),
309 'disallow_public_ip_address': d.get('disallow_public_ip_address'),
310 'expiration_date': d.get('expiration_date'),
311 'image': d.get('gallery_image_reference'),
312 'os_type': d.get('os_type').lower(),
313 'vm_size': d.get('size'),
314 'user_name': d.get('user_name'),
315 'storage_type': d.get('storage_type').lower(),
316 'compute_vm_id': d.get('compute_id'),
317 'compute_vm_resource_group': self.parse_resource_to_dict(d.get('compute_id')).get('resource_group'),
318 'compute_vm_name': self.parse_resource_to_dict(d.get('compute_id')).get('name'),
319 'fqdn': d.get('fqdn'),
320 'provisioning_state': d.get('provisioning_state'),
321 'tags': d.get('tags', None),
322 'is_authentication_with_ssh_key': d.get('is_authentication_with_ssh_key')
323 }
324 return d
325
326
327 def main():
328 AzureRMDtlVirtualMachineInfo()
329
330
331 if __name__ == '__main__':
332 main()