[PR #8876/6af74d1b backport][stable-9] multiple modules: improve dict.items() loops (#8882)

multiple modules: improve dict.items() loops (#8876)

* multiple modules: improve dict.items() loops

* simplify in memset_* modules

* add changelog frag

(cherry picked from commit 6af74d1ba6)

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
pull/8890/head
patchback[bot] 2024-09-18 17:50:02 +02:00 committed by GitHub
parent e6edf9cdea
commit 53a941cee7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 44 additions and 43 deletions

View File

@ -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).

View File

@ -196,9 +196,9 @@ class GitLabDeployKey(object):
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)

View File

@ -277,9 +277,9 @@ class GitLabGroup(object):
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)

View File

@ -264,14 +264,14 @@ class GitlabIssue(object):
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:

View File

@ -263,15 +263,15 @@ class GitlabMergeRequest(object):
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:

View File

@ -368,18 +368,18 @@ class GitLabRunner(object):
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)

View File

@ -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):

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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.

View File

@ -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.

View File

@ -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

View File

@ -1944,7 +1944,7 @@ class Nmcli(object):
convert_func = self.list_to_string
if callable(convert_func):
options[setting] = convert_func(options[setting])
options[setting] = convert_func(value)
return options

View File

@ -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:

View File

@ -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: