Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2024 xuzhang3 (@xuzhang3), Fred-sun (@Fred-sun)
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_snapshot_info
14 version_added: "2.7.0"
15 short_description: Get the Azure Snapshot instance facts
16 description:
17 - Get or list instance facts of Azure Snapshot.
18 options:
19 resource_group:
20 description:
21 - The name of the resource group.
22 required: true
23 type: str
24 name:
25 description:
26 - Resource name.
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 extends_documentation_fragment:
34 - azure.azcollection.azure
35 author:
36 - xuzhang3 (@xuzhang3)
37 - Fred-sun (@Fred-sun)
38
39 '''
40
41 EXAMPLES = '''
42 - name: Get the snapshot instance by name
43 azure_rm_snapshot_info:
44 resource_group: myResourceGroup
45 name: mySnapshot
46
47 - name: List all snapshots by resource group and filter by tags
48 azure_rm_snapshot_info:
49 resource_group: myResourceGroup
50 name: mySnapshot
51 '''
52
53 RETURN = '''
54 state:
55 description:
56 - The description of the snapshot instance facts.
57 type: complex
58 returned: always
59 contains:
60 id:
61 description:
62 - The Snapshot instance ID.
63 type: str
64 returned: always
65 sample: /subscription/xxx-xxx/resourceGroups/testRG/providers/Microsoft.Compute/snapshots/mySnapshot01
66 location:
67 description:
68 - The Snapshot instance location.
69 type: str
70 returned: always
71 sample: eastus
72 name:
73 description:
74 - The Snapshot instance name.
75 type: str
76 returned: always
77 sample: mySnapshot01
78 properties:
79 description:
80 - The properties of the snapshot instance.
81 type: dict
82 returned: always
83 sample: {
84 "creationData": {
85 "createOption": "Import",
86 "sourceUri": "https://vmforimagerpfx01.blob.core.windows.net/vhds/vmforimagerpfx01.vhd"
87 },
88 "diskSizeBytes": 32213303296,
89 "diskSizeGB": 30,
90 "diskState": "Unattached",
91 "encryption": {
92 "type": "EncryptionAtRestWithPlatformKey"
93 },
94 "incremental": false,
95 "networkAccessPolicy": "AllowAll",
96 "provisioningState": "Succeeded",
97 "publicNetworkAccess": "Enabled",
98 "timeCreated": "2024-08-01T07:08:32.1635314+00:00",
99 "uniqueId": "85607f7a-ce44-40bd-a523-a7e2a10d72c3"
100 }
101 sku:
102 description:
103 - The snapshots sku option.
104 type: complex
105 returned: always
106 contains:
107 name:
108 description:
109 - The Sku name.
110 type: str
111 returned: always
112 sample: Standard_LRS
113 tier:
114 description:
115 - The Sku tier.
116 type: str
117 returned: always
118 sample: Standard
119 tags:
120 description:
121 - Resource tags.
122 type: dict
123 returned: always
124 sample: {'key1': 'value1'}
125 type:
126 description:
127 - Resource type.
128 type: str
129 returned: always
130 sample: Microsoft.Compute/snapshots
131 '''
132
133 import json
134 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
135 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_rest import GenericRestClient
136
137
138 class AzureRMSnapshotsInfo(AzureRMModuleBaseExt):
139 def __init__(self):
140 self.module_arg_spec = dict(
141 resource_group=dict(
142 type='str',
143 required=True
144 ),
145 name=dict(
146 type='str',
147 ),
148 tags=dict(
149 type='list',
150 elements='str'
151 )
152 )
153
154 self.resource_group = None
155 self.name = None
156 self.tags = None
157
158 self.results = dict(changed=False)
159 self.mgmt_client = None
160 self.url = None
161
162 self.query_parameters = {}
163 self.status_code = [200, 201, 202]
164 self.query_parameters['api-version'] = '2022-03-02'
165 self.header_parameters = {}
166 self.header_parameters['Content-Type'] = 'application/json; charset=utf-8'
167
168 super(AzureRMSnapshotsInfo, self).__init__(derived_arg_spec=self.module_arg_spec,
169 supports_check_mode=True,
170 facts_module=True,
171 supports_tags=False)
172
173 def exec_module(self, **kwargs):
174 for key in list(self.module_arg_spec.keys()):
175 setattr(self, key, kwargs[key])
176
177 response = None
178
179 self.mgmt_client = self.get_mgmt_svc_client(GenericRestClient,
180 base_url=self._cloud_environment.endpoints.resource_manager)
181
182 if self.name is not None:
183 self.results['state'] = self.get()
184 else:
185 self.results['state'] = self.list_by_resourcegroup()
186
187 return self.results
188
189 def get(self):
190 self.url = ('/subscriptions' +
191 '/{{ subscription_id }}' +
192 '/resourceGroups' +
193 '/{{ resource_group }}' +
194 '/providers' +
195 '/Microsoft.Compute' +
196 '/snapshots' +
197 '/{{ snapshot_name }}')
198 self.url = self.url.replace('{{ subscription_id }}', self.subscription_id)
199 self.url = self.url.replace('{{ resource_group }}', self.resource_group)
200 self.url = self.url.replace('{{ snapshot_name }}', self.name)
201 response = None
202 try:
203 response = self.mgmt_client.query(url=self.url,
204 method='GET',
205 query_parameters=self.query_parameters,
206 header_parameters=self.header_parameters,
207 body=None,
208 expected_status_codes=self.status_code,
209 polling_timeout=600,
210 polling_interval=30)
211 response = json.loads(response.body())
212 self.log("Response : {0}".format(response))
213 except Exception as e:
214 self.log('Did not find the Snapshot instance.')
215 if response and self.has_tags(response.get('tags'), self.tags):
216 return [response]
217 else:
218 return []
219
220 def list_by_resourcegroup(self):
221 self.url = ('/subscriptions' +
222 '/{{ subscription_id }}' +
223 '/resourceGroups' +
224 '/{{ resource_group }}' +
225 '/providers' +
226 '/Microsoft.Compute' +
227 '/snapshots')
228 self.url = self.url.replace('{{ subscription_id }}', self.subscription_id)
229 self.url = self.url.replace('{{ resource_group }}', self.resource_group)
230 response = None
231 results = []
232
233 try:
234 response = self.mgmt_client.query(url=self.url,
235 method='GET',
236 query_parameters=self.query_parameters,
237 header_parameters=self.header_parameters,
238 body=None,
239 expected_status_codes=self.status_code,
240 polling_timeout=600,
241 polling_interval=30)
242 response = json.loads(response.body())
243 self.log("Response : {0}".format(response))
244 # self.log("Snapshot instance : {0} found".format(response.name))
245 except Exception as e:
246 self.log('Did not find the Snapshot instance.')
247 for item in response['value']:
248 if self.has_tags(item.get('tags'), self.tags):
249 results.append(item)
250
251 return results
252
253
254 def main():
255 AzureRMSnapshotsInfo()
256
257
258 if __name__ == '__main__':
259 main()