Skip to content

Commit

Permalink
multiple modules: improve dict.items() loops (#8876)
Browse files Browse the repository at this point in the history
* multiple modules: improve dict.items() loops

* simplify in memset_* modules

* add changelog frag
  • Loading branch information
russoz authored Sep 18, 2024
1 parent 80f48cc commit 6af74d1
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 43 deletions.
16 changes: 16 additions & 0 deletions changelogs/fragments/8876-dict-items-loop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
minor_changes:
- gitlab_deploy_key - better construct when using ``dict.items()`` (https://github.com/ansible-collections/community.general/pull/8876).
- gitlab_group - better construct when using ``dict.items()`` (https://github.com/ansible-collections/community.general/pull/8876).
- gitlab_issue - better construct when using ``dict.items()`` (https://github.com/ansible-collections/community.general/pull/8876).
- gitlab_merge_request - better construct when using ``dict.items()`` (https://github.com/ansible-collections/community.general/pull/8876).
- gitlab_runner - better construct when using ``dict.items()`` (https://github.com/ansible-collections/community.general/pull/8876).
- icinga2_host - replace loop with dict comprehension (https://github.com/ansible-collections/community.general/pull/8876).
- memset_dns_reload - replace loop with ``dict()`` (https://github.com/ansible-collections/community.general/pull/8876).
- memset_memstore_info - replace loop with ``dict()`` (https://github.com/ansible-collections/community.general/pull/8876).
- memset_server_info - replace loop with ``dict()`` (https://github.com/ansible-collections/community.general/pull/8876).
- memset_zone - replace loop with ``dict()`` (https://github.com/ansible-collections/community.general/pull/8876).
- memset_zone_domain - replace loop with ``dict()`` (https://github.com/ansible-collections/community.general/pull/8876).
- memset_zone_record - replace loop with ``dict()`` (https://github.com/ansible-collections/community.general/pull/8876).
- nmcli - better construct when using ``dict.items()`` (https://github.com/ansible-collections/community.general/pull/8876).
- scaleway_user_data - better construct when using ``dict.items()`` (https://github.com/ansible-collections/community.general/pull/8876).
- udm_dns_record - replace loop with ``dict.update()`` (https://github.com/ansible-collections/community.general/pull/8876).
6 changes: 3 additions & 3 deletions plugins/modules/gitlab_deploy_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ def update_deploy_key(self, deploy_key, arguments):
changed = False

for arg_key, arg_value in arguments.items():
if arguments[arg_key] is not None:
if getattr(deploy_key, arg_key) != arguments[arg_key]:
setattr(deploy_key, arg_key, arguments[arg_key])
if arg_value is not None:
if getattr(deploy_key, arg_key) != arg_value:
setattr(deploy_key, arg_key, arg_value)
changed = True

return (changed, deploy_key)
Expand Down
6 changes: 3 additions & 3 deletions plugins/modules/gitlab_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,9 @@ def update_group(self, group, arguments):
changed = False

for arg_key, arg_value in arguments.items():
if arguments[arg_key] is not None:
if getattr(group, arg_key) != arguments[arg_key]:
setattr(group, arg_key, arguments[arg_key])
if arg_value is not None:
if getattr(group, arg_key) != arg_value:
setattr(group, arg_key, arg_value)
changed = True

return (changed, group)
Expand Down
6 changes: 3 additions & 3 deletions plugins/modules/gitlab_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,14 @@ def issue_has_changed(self, issue, options):

if key == 'milestone_id':
old_milestone = getattr(issue, 'milestone')['id'] if getattr(issue, 'milestone') else ""
if options[key] != old_milestone:
if value != old_milestone:
return True
elif key == 'assignee_ids':
if options[key] != sorted([user["id"] for user in getattr(issue, 'assignees')]):
if value != sorted([user["id"] for user in getattr(issue, 'assignees')]):
return True

elif key == 'labels':
if options[key] != sorted(getattr(issue, key)):
if value != sorted(getattr(issue, key)):
return True

elif getattr(issue, key) != value:
Expand Down
6 changes: 3 additions & 3 deletions plugins/modules/gitlab_merge_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,15 @@ def mr_has_changed(self, mr, options):
key = 'force_remove_source_branch'

if key == 'assignee_ids':
if options[key] != sorted([user["id"] for user in getattr(mr, 'assignees')]):
if value != sorted([user["id"] for user in getattr(mr, 'assignees')]):
return True

elif key == 'reviewer_ids':
if options[key] != sorted([user["id"] for user in getattr(mr, 'reviewers')]):
if value != sorted([user["id"] for user in getattr(mr, 'reviewers')]):
return True

elif key == 'labels':
if options[key] != sorted(getattr(mr, key)):
if value != sorted(getattr(mr, key)):
return True

elif getattr(mr, key) != value:
Expand Down
12 changes: 6 additions & 6 deletions plugins/modules/gitlab_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,18 +368,18 @@ def update_runner(self, runner, arguments):
changed = False

for arg_key, arg_value in arguments.items():
if arguments[arg_key] is not None:
if isinstance(arguments[arg_key], list):
if arg_value is not None:
if isinstance(arg_value, list):
list1 = getattr(runner, arg_key)
list1.sort()
list2 = arguments[arg_key]
list2 = arg_value
list2.sort()
if list1 != list2:
setattr(runner, arg_key, arguments[arg_key])
setattr(runner, arg_key, arg_value)
changed = True
else:
if getattr(runner, arg_key) != arguments[arg_key]:
setattr(runner, arg_key, arguments[arg_key])
if getattr(runner, arg_key) != arg_value:
setattr(runner, arg_key, arg_value)
changed = True

return (changed, runner)
Expand Down
4 changes: 1 addition & 3 deletions plugins/modules/icinga2_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,7 @@ def main():
'vars.made_by': "ansible"
}
}

for key, value in variables.items():
data['attrs']['vars.' + key] = value
data['attrs'].update({'vars.' + key: value for key, value in variables.items()})

changed = False
if icinga.exists(name):
Expand Down
4 changes: 1 addition & 3 deletions plugins/modules/memset_dns_reload.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,7 @@ def main():
)

# populate the dict with the user-provided vars.
args = dict()
for key, arg in module.params.items():
args[key] = arg
args = dict(module.params)

retvals = reload_dns(args)

Expand Down
4 changes: 1 addition & 3 deletions plugins/modules/memset_memstore_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ def main():
)

# populate the dict with the user-provided vars.
args = dict()
for key, arg in module.params.items():
args[key] = arg
args = dict(module.params)

retvals = get_facts(args)

Expand Down
4 changes: 1 addition & 3 deletions plugins/modules/memset_server_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,7 @@ def main():
)

# populate the dict with the user-provided vars.
args = dict()
for key, arg in module.params.items():
args[key] = arg
args = dict(module.params)

retvals = get_facts(args)

Expand Down
4 changes: 1 addition & 3 deletions plugins/modules/memset_zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,7 @@ def main():
)

# populate the dict with the user-provided vars.
args = dict()
for key, arg in module.params.items():
args[key] = arg
args = dict(module.params)
args['check_mode'] = module.check_mode

# validate some API-specific limitations.
Expand Down
4 changes: 1 addition & 3 deletions plugins/modules/memset_zone_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,7 @@ def main():
)

# populate the dict with the user-provided vars.
args = dict()
for key, arg in module.params.items():
args[key] = arg
args = dict(module.params)
args['check_mode'] = module.check_mode

# validate some API-specific limitations.
Expand Down
4 changes: 1 addition & 3 deletions plugins/modules/memset_zone_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,7 @@ def main():
)

# populate the dict with the user-provided vars.
args = dict()
for key, arg in module.params.items():
args[key] = arg
args = dict(module.params)
args['check_mode'] = module.check_mode

# perform some Memset API-specific validation
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/nmcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1944,7 +1944,7 @@ def connection_options(self, detect_change=False):
convert_func = self.list_to_string

if callable(convert_func):
options[setting] = convert_func(options[setting])
options[setting] = convert_func(value)

return options

Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/scaleway_user_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def core(module):

# Then we patch keys that are different
for key, value in user_data.items():
if key not in present_user_data or user_data[key] != present_user_data[key]:
if key not in present_user_data or value != present_user_data[key]:

changed = True
if compute_api.module.check_mode:
Expand Down
3 changes: 1 addition & 2 deletions plugins/modules/udm_dns_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ def main():
else:
obj['name'] = name

for k, v in data.items():
obj[k] = v
obj.update(data)
diff = obj.diff()
changed = obj.diff() != []
if not module.check_mode:
Expand Down

0 comments on commit 6af74d1

Please sign in to comment.