Skip to content

Commit 9587fa9

Browse files
committed
Modify azure_rm_containerregistrytoke
1 parent d6240c3 commit 9587fa9

File tree

2 files changed

+33
-93
lines changed

2 files changed

+33
-93
lines changed

plugins/modules/azure_rm_containerregistrytoken.py

Lines changed: 32 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -41,53 +41,6 @@
4141
choices:
4242
- enabled
4343
- disabled
44-
credentials:
45-
description:
46-
- The credentials that can be used for authenticating the token.
47-
type: dict
48-
suboptions:
49-
certificates:
50-
description:
51-
- The certificate of the token credentials.
52-
type: list
53-
elements: dict
54-
suboptions:
55-
name:
56-
description:
57-
- The certificate name.
58-
type: str
59-
choices:
60-
- certificate1
61-
- certificate2
62-
expiry:
63-
description:
64-
- The expiry datetime of the certificate.
65-
type: str
66-
thumbprint:
67-
description:
68-
- The thumbprint of the certificate.
69-
type: str
70-
encoded_pem_certificate:
71-
description:
72-
- Base 64 encoded string of the public certificate1 in PEM format that will be used for authenticating the token.
73-
type: str
74-
passwords:
75-
description:
76-
- The password of the token credentials.
77-
type: list
78-
elements: dict
79-
suboptions:
80-
expiry:
81-
description:
82-
- The expiry datetime of the password.
83-
type: str
84-
name:
85-
description:
86-
- The password name C(password1) or C(password2).
87-
type: str
88-
choices:
89-
- password1
90-
- password2
9144
state:
9245
description:
9346
- Assert the state of the container registry token.
@@ -108,11 +61,13 @@
10861
'''
10962

11063
EXAMPLES = '''
111-
- name: Get instance of Registry Token
64+
- name: Create a new Container Registry Token
11265
azure_rm_containerregistrytoken:
11366
resource_group: myResourceGroup
11467
registry_name: myRegistry
11568
name: mytoken
69+
status: enabled
70+
scope_map_id: scopemap_id
11671
11772
- name: Delete the container registry token
11873
azure_rm_containerregistrytoken:
@@ -122,7 +77,7 @@
12277
'''
12378

12479
RETURN = '''
125-
tokens:
80+
registry_token:
12681
description:
12782
- A list of dictionaries containing facts for token.
12883
returned: always
@@ -242,22 +197,6 @@ def __init__(self):
242197
type='str',
243198
choices=['enabled', 'disabled']
244199
),
245-
cred=dict(
246-
type='dict',
247-
aliase=['credentials'],
248-
options=dict(
249-
certificates=dict(
250-
name=dict(type='str', choices=['certificate1', 'certificate2']),
251-
expiry=dict(type='str'),
252-
thumbprint=dict(type='str'),
253-
encoded_pem_certificate=dict(type='str')
254-
),
255-
passwords=dict(
256-
expiry=dict(type='str'),
257-
name=dict(type='str', choices=['password1', 'password2'])
258-
)
259-
)
260-
),
261200
state=dict(
262201
type='str',
263202
default='present',
@@ -267,13 +206,12 @@ def __init__(self):
267206
# store the results of the module operation
268207
self.results = dict(
269208
changed=False,
270-
diff=None,
209+
diff=[],
271210
token=None,
272211
)
273212
self.resource_group = None
274213
self.registry_name = None
275214
self.name = None
276-
self.cred = None
277215
self.status = None
278216
self.scope_map_id = None
279217

@@ -288,53 +226,50 @@ def exec_module(self, **kwargs):
288226

289227
# Defaults for variables
290228
result = None
291-
result_compare = dict(compare=[])
292229

293230
# Get current container registry token
294-
before_dict = self.get()
295-
296-
# Create dict form input, without None value
297-
token_template = dict(scope_map_id=self.scope_map_id,
298-
credentials=self.cred,
299-
status=self.status)
300-
301-
# Filter out all None values
302-
token_input = {key: value for key, value in token_template.items() if value is not None}
231+
old_response = self.get()
303232

304233
# Create/Update if state==present
305234
if self.state == 'present':
306-
if before_dict:
307-
# The container registry already exists, try to update
308-
# Dict for update is the union of existing object over written by input data
309-
token_update = before_dict() | token_input
310-
if not self.default_compare({}, token_update, before_dict, '', result_compare):
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():
311238
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']:
312245
if self.check_mode:
313246
# Check mode, skipping actual creation
314247
pass
315248
else:
316-
result = self.update(token_update)
249+
result = self.update()
250+
else:
251+
result = old_response
317252
else:
318253
self.results['changed'] = True
319254
# The container registry token not exist, create
320255
if self.check_mode:
321256
# Check mode, Skipping actual creation
322257
pass
323258
else:
324-
result = self.create(token_input)
325-
elif self.state == 'absent' and before_dict:
259+
result = self.create()
260+
elif self.state == 'absent' and old_response:
326261
self.results['changed'] = True
327262
if not self.check_mode:
328263
self.delete()
329264
else:
330265
# Do not delete in check mode
331266
pass
332267

333-
self.results['diff'] = result_compare
334-
self.results['token'] = result
268+
self.results['registry_token'] = result
335269
return self.results
336270

337271
def get(self):
272+
# Gets the properties of the specified token
338273
try:
339274
response = self.containerregistrytoken_client.tokens.get(resource_group_name=self.resource_group,
340275
registry_name=self.registry_name,
@@ -344,14 +279,16 @@ def get(self):
344279
self.log("Could not get facts for Registry Token: {0}".format(str(e)))
345280
return None
346281

347-
return response.as_dict()
282+
return self.format_item(response)
348283

349-
def create(self, body):
284+
def create(self):
285+
# Creates a token for a container registry with the specified parameters
350286
try:
351287
response = self.containerregistrytoken_client.tokens.begin_create(resource_group_name=self.resource_group,
352288
registry_name=self.registry_name,
353289
token_name=self.name,
354-
token_create_parameters=body)
290+
token_create_parameters=dict(scope_map_id=self.scope_map_id,
291+
status=self.status))
355292
self.log("Response: {0}".format(response))
356293
except Exception as e:
357294
self.fail("Create {0} failed. Abnormal message as {1}".format(self.name, str(e)))
@@ -361,12 +298,14 @@ def create(self, body):
361298

362299
return self.format_item(response)
363300

364-
def update(self, body):
301+
def update(self):
302+
# Updates a token with the specified parameters
365303
try:
366304
response = self.containerregistrytoken_client.tokens.begin_update(resource_group_name=self.resource_group,
367305
registry_name=self.registry_name,
368306
token_name=self.name,
369-
token_update_parameters=body)
307+
token_update_parameters=dict(scope_map_id=self.scope_map_id,
308+
status=self.status))
370309
self.log("Response: {0}".format(response))
371310
except Exception as e:
372311
self.fail("Update {0} failed. Abnormal message as {1}".format(self.name, str(e)))
@@ -377,6 +316,7 @@ def update(self, body):
377316
return self.format_item(response)
378317

379318
def delete(self):
319+
# Deletes a token from a container registry
380320
try:
381321
response = self.containerregistrytoken_client.tokens.begin_delete(resource_group_name=self.resource_group,
382322
registry_name=self.registry_name,

plugins/modules/azure_rm_containerregistrytoken_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
'''
5454

5555
RETURN = '''
56-
tokens:
56+
registry_tokens:
5757
description:
5858
- A list of dictionaries containing facts for token.
5959
returned: always

0 commit comments

Comments
 (0)