Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_containerregistrytoken.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_containerregistrytoken
12 version_added: "3.7.0"
13 short_description: Managed the Azure Container Registry Token
14 description:
15 - Create, update or delete the Container Registry Token.
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 token.
31 type: str
32 required: true
33 scope_map_id:
34 description:
35 - The resource ID of the scope map to which the token will be associated with.
36 type: str
37 status:
38 description:
39 - The status of the token example enabled or disabled.
40 type: str
41 choices:
42 - enabled
43 - disabled
44 state:
45 description:
46 - Assert the state of the container registry token.
47 - Use C(present) to create or update a server and C(absent) to delete it.
48 default: present
49 type: str
50 choices:
51 - present
52 - absent
53
54 extends_documentation_fragment:
55 - azure.azcollection.azure
56
57 author:
58 - magodo (@magodo)
59 - Fred-sun (@Fred-sun)
60
61 '''
62
63 EXAMPLES = '''
64 - name: Create a new Container Registry Token
65 azure_rm_containerregistrytoken:
66 resource_group: myResourceGroup
67 registry_name: myRegistry
68 name: mytoken
69 status: enabled
70 scope_map_id: scopemap_id
71
72 - name: Delete the container registry token
73 azure_rm_containerregistrytoken:
74 resource_group: myResourceGroup
75 registry_name: myRegistry
76 name: mytoken
77 '''
78
79 RETURN = '''
80 registry_token:
81 description:
82 - A list of dictionaries containing facts for token.
83 returned: always
84 type: complex
85 contains:
86 id:
87 description:
88 - The resource ID.
89 returned: always
90 type: str
91 sample: "/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.ContainerRegistry/registries/registry01/tokens/token01"
92 name:
93 description:
94 - The name of the resource.
95 returned: always
96 type: str
97 sample: token01
98 registry_name:
99 description:
100 - The name of the container registry.
101 type: str
102 returned: always
103 sample: registry01
104 resource_group:
105 description:
106 - The name fo the resource group.
107 type: str
108 returned: always
109 sample: myRG
110 creation_date:
111 description:
112 - The creation date of scope map.
113 type: str
114 returned: always
115 sample: '2025-07-21T03:16:03.828541Z'
116 credentials:
117 description:
118 - The credentials that can be used for authenticating the token.
119 type: str
120 returned: always
121 sample: {
122 "passwords": [
123 {
124 "creation_time": "2025-07-21T03:17:14.528612Z",
125 "expiry": "2026-07-22T03:17:00.551Z",
126 "name": "password1"
127 }
128 ]
129 }
130 provisioning_state:
131 description:
132 - Provisioning state of the container registry token.
133 returned: always
134 type: str
135 sample: Succeeded
136 scope_map_id:
137 description:
138 - The resource ID of the scope map to which the token will be associated with.
139 type: str
140 returned: always
141 sample: "/subscriptions/xxx-xxx/resourceGroups/myRG/providers/Microsoft.ContainerRegistry/registries/registry01/scopeMaps/map01"
142 status:
143 description:
144 - The status of the token example enabled or disabled.
145 type: str
146 returned: always
147 sample: enabled
148 system_data:
149 description:
150 - Metadata pertaining to creation and last modification of the resource.
151 type: dict
152 returned: always
153 sample: {
154 "created_at": "2025-07-21T03:16:50.586036Z",
155 "created_by": "[email protected]",
156 "created_by_type": "User",
157 "last_modified_at": "2025-07-21T03:16:50.586036Z",
158 "last_modified_by": "[email protected]",
159 "last_modified_by_type": "User"
160 }
161 type:
162 description:
163 - The type of the resource.
164 type: str
165 returned: always
166 sample: 'Microsoft.ContainerRegistry/registries/tokens'
167 '''
168
169 try:
170 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt
171 from azure.core.polling import LROPoller
172 except ImportError:
173 # This is handled in azure_rm_common
174 pass
175
176
177 class AzureRMContainerRegistryToken(AzureRMModuleBaseExt):
178 def __init__(self):
179 # define user inputs into argument
180 self.module_arg_spec = dict(
181 resource_group=dict(
182 type='str',
183 required=True,
184 ),
185 registry_name=dict(
186 type='str',
187 required=True
188 ),
189 name=dict(
190 type='str',
191 required=True,
192 ),
193 scope_map_id=dict(
194 type='str'
195 ),
196 status=dict(
197 type='str',
198 choices=['enabled', 'disabled']
199 ),
200 state=dict(
201 type='str',
202 default='present',
203 choices=['present', 'absent']
204 )
205 )
206 # store the results of the module operation
207 self.results = dict(
208 changed=False,
209 diff=[],
210 token=None,
211 )
212 self.resource_group = None
213 self.registry_name = None
214 self.name = None
215 self.status = None
216 self.scope_map_id = None
217
218 self.state = None
219
220 super(AzureRMContainerRegistryToken, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=False)
221
222 def exec_module(self, **kwargs):
223
224 for key in list(self.module_arg_spec):
225 setattr(self, key, kwargs[key])
226
227 # Defaults for variables
228 result = None
229
230 # Get current container registry token
231 old_response = self.get()
232
233 # Create/Update if state==present
234 if self.state == 'present':
235 if old_response:
236 # The container registry token already exists, try to update
237 if self.scope_map_id is not None and self.scope_map_id.lower() != old_response['scope_map_id'].lower():
238 self.results['changed'] = True
239 self.results['diff'].append('scope_map_id')
240 if self.status is not None and self.status != old_response['status'].lower():
241 self.results['changed'] = True
242 self.results['diff'].append('status')
243
244 if self.results['changed']:
245 if self.check_mode:
246 # Check mode, skipping actual creation
247 pass
248 else:
249 result = self.update()
250 else:
251 result = old_response
252 else:
253 self.results['changed'] = True
254 # The container registry token not exist, create
255 if self.check_mode:
256 # Check mode, Skipping actual creation
257 pass
258 else:
259 result = self.create()
260 elif self.state == 'absent' and old_response:
261 self.results['changed'] = True
262 if not self.check_mode:
263 self.delete()
264 else:
265 # Do not delete in check mode
266 pass
267
268 self.results['registry_token'] = result
269 return self.results
270
271 def get(self):
272 # Gets the properties of the specified token
273 try:
274 response = self.containerregistrytoken_client.tokens.get(resource_group_name=self.resource_group,
275 registry_name=self.registry_name,
276 token_name=self.name)
277 self.log("Response : {0}".format(response))
278 except Exception as e:
279 self.log("Could not get facts for Registry Token: {0}".format(str(e)))
280 return None
281
282 return self.format_item(response)
283
284 def create(self):
285 # Creates a token for a container registry with the specified parameters
286 try:
287 response = self.containerregistrytoken_client.tokens.begin_create(resource_group_name=self.resource_group,
288 registry_name=self.registry_name,
289 token_name=self.name,
290 token_create_parameters=dict(scope_map_id=self.scope_map_id,
291 status=self.status))
292 self.log("Response: {0}".format(response))
293 except Exception as e:
294 self.fail("Create {0} failed. Abnormal message as {1}".format(self.name, str(e)))
295
296 if isinstance(response, LROPoller):
297 response = self.get_poller_result(response)
298
299 return self.format_item(response)
300
301 def update(self):
302 # Updates a token with the specified parameters
303 try:
304 response = self.containerregistrytoken_client.tokens.begin_update(resource_group_name=self.resource_group,
305 registry_name=self.registry_name,
306 token_name=self.name,
307 token_update_parameters=dict(scope_map_id=self.scope_map_id,
308 status=self.status))
309 self.log("Response: {0}".format(response))
310 except Exception as e:
311 self.fail("Update {0} failed. Abnormal message as {1}".format(self.name, str(e)))
312
313 if isinstance(response, LROPoller):
314 response = self.get_poller_result(response)
315
316 return self.format_item(response)
317
318 def delete(self):
319 # Deletes a token from a container registry
320 try:
321 response = self.containerregistrytoken_client.tokens.begin_delete(resource_group_name=self.resource_group,
322 registry_name=self.registry_name,
323 token_name=self.name)
324 self.log("Response: {0}".format(response))
325 except Exception as e:
326 self.fail("Deletion {0} failed. Abnormal message as {1}".format(self.name, str(e)))
327
328 if isinstance(response, LROPoller):
329 self.get_poller_result(response)
330
331 def format_item(self, item):
332 result = item.as_dict()
333 result['resource_group'] = self.resource_group
334 result['registry_name'] = self.registry_name
335
336 return result
337
338
339 def main():
340 AzureRMContainerRegistryToken()
341
342
343 if __name__ == '__main__':
344 main()