Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_devtestlabcustomimage_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_devtestlabcustomimage_info
14 version_added: "0.1.2"
15 short_description: Get Azure DevTest Lab Custom Image facts
16 description:
17 - Get facts of Azure Azure DevTest Lab Custom Image.
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 custom image.
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 Custom Image
50 azure_rm_devtestlabcustomimage_info:
51 resource_group: myResourceGroup
52 lab_name: myLab
53 name: myImage
54
55 - name: List instances of Custom Image in the lab
56 azure_rm_devtestlabcustomimage_info:
57 resource_group: myResourceGroup
58 lab_name: myLab
59 name: myImage
60 '''
61
62 RETURN = '''
63 custom_images:
64 description:
65 - A list of dictionaries containing facts for Custom Image.
66 returned: always
67 type: complex
68 contains:
69 id:
70 description:
71 - The identifier of the artifact source.
72 returned: always
73 type: str
74 sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.DevTestLab/labs/myLab/cu
75 stomimages/myImage"
76 resource_group:
77 description:
78 - Name of the resource group.
79 returned: always
80 type: str
81 sample: myResourceGroup
82 lab_name:
83 description:
84 - Name of the lab.
85 returned: always
86 type: str
87 sample: myLab
88 name:
89 description:
90 - The name of the image.
91 returned: always
92 type: str
93 sample: myImage
94 managed_shapshot_id:
95 description:
96 - Managed snapshot id.
97 returned: always
98 type: str
99 sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/microsoft.compute/snapshots/myImage"
100 source_vm_id:
101 description:
102 - Source VM id.
103 returned: always
104 type: str
105 sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx//resourcegroups/myResourceGroup/providers/microsoft.devtestlab/labs/myLab/v
106 irtualmachines/myLabVm"
107 tags:
108 description:
109 - The tags of the resource.
110 returned: always
111 type: dict
112 sample: "{ 'MyTag':'MyValue' }"
113 '''
114
115 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
116
117 try:
118 from azure.core.exceptions import ResourceNotFoundError
119 from azure.mgmt.devtestlabs import DevTestLabsClient
120 except ImportError:
121 # This is handled in azure_rm_common
122 pass
123
124
125 class AzureRMDtlCustomImageInfo(AzureRMModuleBase):
126 def __init__(self):
127 # define user inputs into argument
128 self.module_arg_spec = dict(
129 resource_group=dict(
130 type='str',
131 required=True
132 ),
133 lab_name=dict(
134 type='str',
135 required=True
136 ),
137 name=dict(
138 type='str',
139 ),
140 tags=dict(
141 type='list',
142 elements='str'
143 )
144 )
145 # store the results of the module operation
146 self.results = dict(
147 changed=False
148 )
149 self.mgmt_client = None
150 self.resource_group = None
151 self.lab_name = None
152 self.name = None
153 self.tags = None
154 super(AzureRMDtlCustomImageInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True)
155
156 def exec_module(self, **kwargs):
157
158 for key in self.module_arg_spec:
159 setattr(self, key, kwargs[key])
160 self.mgmt_client = self.get_mgmt_svc_client(DevTestLabsClient,
161 base_url=self._cloud_environment.endpoints.resource_manager)
162
163 if self.name:
164 self.results['custom_images'] = self.get()
165 else:
166 self.results['custom_images'] = self.list()
167 return self.results
168
169 def get(self):
170 response = None
171 results = []
172 try:
173 response = self.mgmt_client.custom_images.get(resource_group_name=self.resource_group,
174 lab_name=self.lab_name,
175 name=self.name)
176 self.log("Response : {0}".format(response))
177 except ResourceNotFoundError as e:
178 self.log('Could not get facts for Custom Image.')
179
180 if response and self.has_tags(response.tags, self.tags):
181 results.append(self.format_response(response))
182
183 return results
184
185 def list(self):
186 response = None
187 results = []
188 try:
189 response = self.mgmt_client.custom_images.list(resource_group_name=self.resource_group,
190 lab_name=self.lab_name)
191 self.log("Response : {0}".format(response))
192 except Exception as e:
193 self.log('Could not get facts for Custom Image.')
194
195 if response is not None:
196 for item in response:
197 if self.has_tags(item.tags, self.tags):
198 results.append(self.format_response(item))
199
200 return results
201
202 def format_response(self, item):
203 d = item.as_dict()
204 d = {
205 'resource_group': self.resource_group,
206 'lab_name': self.lab_name,
207 'name': d.get('name'),
208 'id': d.get('id'),
209 'managed_snapshot_id': d.get('managed_snapshot_id'),
210 'source_vm_id': d.get('vm', {}).get('source_vm_id'),
211 'tags': d.get('tags')
212 }
213 return d
214
215
216 def main():
217 AzureRMDtlCustomImageInfo()
218
219
220 if __name__ == '__main__':
221 main()