Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2025 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_diskaccess
14 version_added: '3.3.0'
15 short_description: Show the details for the disk access
16 description:
17 - Get or list the details for the disk access.
18
19 options:
20 resource_group:
21 description:
22 - The name of the resource group.
23 type: str
24 required: true
25 name:
26 description:
27 - The disk access name.
28 required: true
29 type: str
30 location:
31 description:
32 - Resource location.
33 type: str
34 extended_location:
35 description:
36 - The extended location where the disk access will be created.
37 type: dict
38 suboptions:
39 name:
40 description:
41 - The name of the extended location.
42 type: str
43 required: true
44 type:
45 description:
46 - The type of the extended location.
47 type: str
48 default: EdgeZone
49 choices:
50 - EdgeZone
51 state:
52 description:
53 - State of the disk access.
54 - Use C(present) to create or update a disk access and use C(absent) to delete.
55 type: str
56 default: present
57 choices:
58 - present
59 - absent
60
61 extends_documentation_fragment:
62 - azure.azcollection.azure
63 - azure.azcollection.azure_tags
64
65 author:
66 - xuzhang3 (@xuzhang3)
67 - Fred Sun (@Fred-sun)
68 '''
69
70 EXAMPLES = '''
71 - name: Create a new disk access
72 azure_rm_diskaccess:
73 resource_group: myRG
74 name: testaccess
75 location: westus2
76
77 - name: Create a new disk access
78 azure_rm_diskaccess:
79 resource_group: myRG
80 name: testaccess
81 location: westus
82 extended_location:
83 name: losangeles
84 type: EdgeZone
85 tags:
86 key1: value1
87
88 - name: Delete the disk access
89 azure_rm_diskaccess:
90 resource_group: myRG
91 name: testaccess
92 state: absent
93 '''
94
95 RETURN = '''
96 state:
97 description:
98 - Details for the disk access.
99 returned: always
100 type: complex
101 contains:
102 id:
103 description:
104 - Resource ID.
105 type: str
106 returned: always
107 sample:
108 resource_group:
109 description:
110 - Resource group name.
111 type: str
112 returned: always
113 sample: myRG
114 name:
115 description:
116 - Resource name.
117 type: str
118 returned: always
119 sample: testaccess
120 type:
121 description:
122 - Resource Type.
123 type: str
124 returned: always
125 sample: "Microsoft.Compute/diskAccesses"
126 tags:
127 description:
128 - The tags of the disk access.
129 type: dict
130 returned: always
131 sample: {key1: value1, key2: value2}
132 provisioning_state:
133 description:
134 - The disk access resource provisioning state.
135 type: str
136 returned: always
137 sample: Successed
138 extended_location:
139 description:
140 - The extended location where the disk access will be created.
141 type: dict
142 returned: always
143 sample: {"name": "portland", "type": "EdgeZone"}
144 '''
145
146 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
147
148 try:
149 from azure.core.exceptions import ResourceNotFoundError
150 from azure.core.polling import LROPoller
151 except ImportError:
152 pass
153
154
155 class AzureRMDiskAccess(AzureRMModuleBase):
156 def __init__(self):
157 # define user inputs into argument
158 self.module_arg_spec = dict(
159 resource_group=dict(
160 type='str',
161 required=True
162 ),
163 name=dict(
164 type='str',
165 required=True
166 ),
167 location=dict(
168 type='str'
169 ),
170 extended_location=dict(
171 type='dict',
172 options=dict(
173 name=dict(type='str', required=True),
174 type=dict(type='str', default='EdgeZone', choices=['EdgeZone']),
175 )
176 ),
177 state=dict(
178 type='str',
179 choices=['present', 'absent'],
180 default='present'
181 )
182 )
183 # store the results of the module operation
184 self.results = dict()
185 self.resource_group = None
186 self.name = None
187 self.location = None
188 self.extended_location = None
189 self.state = None
190
191 super(AzureRMDiskAccess, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=True, facts_module=True)
192
193 def exec_module(self, **kwargs):
194
195 for key in list(self.module_arg_spec) + ['tags']:
196 setattr(self, key, kwargs[key])
197
198 resource_group = self.get_resource_group(self.resource_group)
199 if not self.location:
200 # Set default location
201 self.location = resource_group.location
202
203 changed = False
204 response = self.get_diskaccess()
205
206 if self.state == 'present':
207 if response is not None:
208 if self.extended_location is not None and self.extended_location.get('name') != response['extended_location'].get('name'):
209 self.fail("Extended location cannot be changed")
210
211 update_tags, self.tags = self.update_tags(response.get('tags'))
212 if update_tags:
213 changed = True
214 if not self.check_mode:
215 response = self.update_diskaccess()
216 else:
217 changed = True
218 if not self.check_mode:
219 response = self.create_or_update()
220 else:
221 if not self.check_mode:
222 if response is not None:
223 response = self.delete_diskaccess()
224 changed = True
225 else:
226 changed = False
227 else:
228 changed = True
229
230 self.results['changed'] = changed
231 self.results['state'] = response
232
233 return self.results
234
235 def get_diskaccess(self):
236 try:
237 return self.to_dict(self.disk_client.disk_accesses.get(self.resource_group, self.name))
238 except ResourceNotFoundError:
239 pass
240
241 def create_or_update(self):
242 try:
243 poller = self.disk_client.disk_accesses.begin_create_or_update(self.resource_group, self.name, dict(tags=self.tags,
244 location=self.location,
245 extended_location=self.extended_location))
246 if isinstance(poller, LROPoller):
247 response = self.get_poller_result(poller)
248 return self.to_dict(response)
249
250 except Exception as ec:
251 self.fail('Error when create disk access {0}: {1}'.format(self.name, ec))
252
253 def update_diskaccess(self):
254 try:
255 poller = self.disk_client.disk_accesses.begin_update(self.resource_group, self.name, dict(tags=self.tags))
256 if isinstance(poller, LROPoller):
257 response = self.get_poller_result(poller)
258 return self.to_dict(response)
259 except Exception as ec:
260 self.fail('Error when update disk access {0}: {1}'.format(self.name, ec))
261
262 def delete_diskaccess(self):
263 try:
264 self.disk_client.disk_accesses.begin_delete(self.resource_group, self.name)
265 except Exception as ec:
266 self.fail('Error when delete disk access {0}: {1}'.format(self.name, ec))
267
268 def get_diskaccess(self):
269 try:
270 return self.to_dict(self.disk_client.disk_accesses.get(self.resource_group, self.name))
271 except ResourceNotFoundError:
272 pass
273
274 def to_dict(self, item):
275 if item is None:
276 return None
277 disk_access = dict(
278 resource_group=self.resource_group,
279 name=item.name,
280 id=item.id,
281 type=item.type,
282 location=item.location,
283 provisioning_state=item.provisioning_state,
284 extended_location=dict(),
285 tags=item.tags
286 )
287 if item.extended_location is not None:
288 disk_access['extended_location']['name'] = item.extended_location.name
289 disk_access['extended_location']['type'] = item.extended_location.type
290 return disk_access
291
292
293 def main():
294 AzureRMDiskAccess()
295
296
297 if __name__ == '__main__':
298 main()