Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5 from __future__ import absolute_import, division, print_function
6 __metaclass__ = type
7
8
9 DOCUMENTATION = '''
10 ---
11 module: azure_rm_containerregistryscopemap
12 version_added: "3.7.0"
13 short_description: Managed the Azure Container Registry Scope Map
14 description:
15 - Create, update or delete the Container Registry Scope Map.
16
17 options:
18 resource_group:
19 description:
20 - The name of the resource group to which the container registry belongs.
21 type: str
22 required: true
23 registry_name:
24 description:
25 - The name of the container registry.
26 type: str
27 required: true
28 name:
29 description:
30 - The name of the container registry scope map.
31 type: str
32 required: true
33 description:
34 description:
35 - The user friendly description of the scope map.
36 type: str
37 actions:
38 description:
39 - The list of scoped permissions for registry artifacts.
40 - Sample as C(repositories/repository-name/content/read) or C(repositories/repository-name/metadata/write).
41 type: list
42 elements: str
43 state:
44 description:
45 - Assert the state of the container registry scope map.
46 - Use C(present) to create or update a server and C(absent) to delete it.
47 default: present
48 type: str
49 choices:
50 - present
51 - absent
52
53 extends_documentation_fragment:
54 - azure.azcollection.azure
55
56 author:
57 - magodo (@magodo)
58 - Fred-sun (@Fred-sun)
59
60 '''
61
62 EXAMPLES = '''
63 - name: Get instance of Registry Scope Map
64 azure_rm_containerregistryscopemap:
65 resource_group: myResourceGroup
66 registry_name: myRegistry
67 name: myscopemap
68
69 - name: Delete the container registry scope map
70 azure_rm_containerregistryscopemap:
71 resource_group: myResourceGroup
72 registry_name: myRegistry
73 name: myscopemap
74 '''
75
76 RETURN = '''
77 scope_map:
78 description:
79 - A list of dictionaries containing facts for scope map.
80 returned: always
81 type: complex
82 contains:
83 id:
84 description:
85 - The resource ID.
86 returned: always
87 type: str
88 sample: "/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.ContainerRegistry/registries/acr01/scopeMaps/map01"
89 name:
90 description:
91 - The name of the resource.
92 returned: always
93 type: str
94 sample: map01
95 registry_name:
96 description:
97 - The name of the container registry.
98 type: str
99 returned: always
100 sample: registry01
101 resource_group:
102 description:
103 - The name fo the resource group.
104 type: str
105 returned: always
106 sample: myRG
107 actions:
108 description:
109 - The list of scoped permissions for registry artifacts.
110 type: list
111 returned: always
112 sample: ["repositories/test01/content/read"]
113 creation_date:
114 description:
115 - The creation date of scope map.
116 type: str
117 returned: always
118 sample: "2025-07-21T03:15:59.951533Z"
119 description:
120 description:
121 - The user friendly description of the scope map.
122 type: str
123 returned: always
124 sample: "Just for test"
125 provisioning_state:
126 description:
127 - Provisioning state of the resource.
128 type: str
129 returned: always
130 sample: Success
131 system_data:
132 description:
133 - Metadata pertaining to creation and last modification of the resource.
134 type: str
135 returned: always
136 sample: {
137 "created_at": "2025-07-21T03:15:59.287282Z",
138 "created_by": "[email protected]",
139 "created_by_type": "User",
140 "last_modified_at": "2025-07-21T03:15:59.287282Z",
141 "last_modified_by": "[email protected]",
142 "last_modified_by_type": "User"
143 }
144 type:
145 description:
146 - The type of the resource.
147 type: str
148 returned: always
149 sample: 'Microsoft.ContainerRegistry/registries/scopeMaps'
150 type_properties_type:
151 description:
152 - The type of the scope map.
153 type: str
154 returned: always
155 sample: UserDefined
156 '''
157
158 try:
159 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
160 from azure.core.polling import LROPoller
161 except ImportError:
162 # This is handled in azure_rm_common
163 pass
164
165
166 class AzureRMContainerRegistryScopeMap(AzureRMModuleBaseExt):
167 def __init__(self):
168 # define user inputs into argument
169 self.module_arg_spec = dict(
170 resource_group=dict(
171 type='str',
172 required=True,
173 ),
174 registry_name=dict(
175 type='str',
176 required=True
177 ),
178 name=dict(
179 type='str',
180 required=True,
181 ),
182 description=dict(
183 type='str'
184 ),
185 actions=dict(
186 type='list',
187 elements='str'
188 ),
189 state=dict(
190 type='str',
191 default='present',
192 choices=['present', 'absent']
193 )
194 )
195 # store the results of the module operation
196 self.results = dict(
197 changed=False,
198 diff=None,
199 scope_map=None,
200 )
201 self.resource_group = None
202 self.registry_name = None
203 self.name = None
204 self.description = None
205 self.state = None
206 self.actions = None
207
208 super(AzureRMContainerRegistryScopeMap, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=False)
209
210 def exec_module(self, **kwargs):
211
212 for key in list(self.module_arg_spec):
213 setattr(self, key, kwargs[key])
214
215 # Defaults for variables
216 result = None
217 result_compare = dict(compare=[])
218
219 # Get current container registry token
220 before_dict = self.get()
221
222 # Create dict form input, without None value
223 scope_map_template = dict(description=self.description, actions=self.actions)
224
225 # Filter out all None values
226 scope_map_input = {key: value for key, value in scope_map_template.items() if value is not None}
227
228 # Create/Update if state==present
229 if self.state == 'present':
230 if before_dict:
231 # The container registry already exists, try to update
232 # Dict for update is the union of existing object over written by input data
233 scope_map_update = before_dict | scope_map_input
234 if not self.default_compare({}, scope_map_update, before_dict, '', result_compare):
235 self.results['changed'] = True
236 if self.check_mode:
237 # Check mode, skipping actual creation
238 pass
239 else:
240 result = self.update(scope_map_update)
241 else:
242 self.results['changed'] = True
243 # The container registry scope map not exist, create
244 if self.check_mode:
245 # Check mode, Skipping actual creation
246 pass
247 else:
248 result = self.create(scope_map_input)
249 elif self.state == 'absent' and before_dict:
250 self.results['changed'] = True
251 if not self.check_mode:
252 self.delete()
253 else:
254 # Do not delete in check mode
255 pass
256
257 self.results['diff'] = result_compare
258 self.results['scope_map'] = result
259 return self.results
260
261 def get(self):
262 try:
263 response = self.containerregistrytoken_client.scope_maps.get(resource_group_name=self.resource_group,
264 registry_name=self.registry_name,
265 scope_map_name=self.name)
266 self.log("Response : {0}".format(response))
267 except Exception as e:
268 self.log("Could not get facts for Registry Scope Map: {0}".format(str(e)))
269 return None
270
271 return self.format_item(response)
272
273 def create(self, body):
274 try:
275 response = self.containerregistrytoken_client.scope_maps.begin_create(resource_group_name=self.resource_group,
276 registry_name=self.registry_name,
277 scope_map_name=self.name,
278 scope_map_create_parameters=body)
279 self.log("Response: {0}".format(response))
280 except Exception as e:
281 self.fail("Create {0} fail. Abnormal message as {1}".format(self.name, str(e)))
282
283 if isinstance(response, LROPoller):
284 response = self.get_poller_result(response)
285
286 return self.format_item(response)
287
288 def update(self, body):
289 try:
290 response = self.containerregistrytoken_client.scope_maps.begin_update(resource_group_name=self.resource_group,
291 registry_name=self.registry_name,
292 scope_map_name=self.name,
293 scope_map_update_parameters=body)
294 self.log("Response: {0}".format(response))
295 except Exception as e:
296 self.fail("Update {0} fail. Abnormal message as {1}".format(self.name, str(e)))
297
298 if isinstance(response, LROPoller):
299 response = self.get_poller_result(response)
300
301 return self.format_item(response)
302
303 def delete(self):
304 try:
305 response = self.containerregistrytoken_client.scope_maps.begin_delete(resource_group_name=self.resource_group,
306 registry_name=self.registry_name,
307 scope_map_name=self.name)
308 self.log("Response: {0}".format(response))
309 except Exception as e:
310 self.fail("Deletion {0} fail. Abnormal message as {1}".format(self.name, str(e)))
311
312 if isinstance(response, LROPoller):
313 self.get_poller_result(response)
314
315 def format_item(self, item):
316 result = item.as_dict()
317 result['resource_group'] = self.resource_group
318 result['registry_name'] = self.registry_name
319
320 return result
321
322
323 def main():
324 AzureRMContainerRegistryScopeMap()
325
326
327 if __name__ == '__main__':
328 main()