1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

gitlab modules: minor refactor (#6384)

* gitlab modules: minor refactor

* add changelog frag

* Update plugins/module_utils/gitlab.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/module_utils/gitlab.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* update changelog frag

* remove extraneous bracket

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Alexei Znamensky 2023-05-09 05:40:52 +12:00 committed by GitHub
parent 165182cdbf
commit febe7a2fb4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 85 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- gitlab_group_variable, gitlab_project_variable - refactor function out to module utils (https://github.com/ansible-collections/community.general/pull/6384).

View file

@ -10,6 +10,7 @@ __metaclass__ = type
from ansible.module_utils.basic import missing_required_lib from ansible.module_utils.basic import missing_required_lib
from ansible.module_utils.common.text.converters import to_native from ansible.module_utils.common.text.converters import to_native
from ansible.module_utils.six import integer_types, string_types
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
@ -121,3 +122,38 @@ def filter_returned_variables(gitlab_variables):
if key not in KNOWN: if key not in KNOWN:
item.pop(key) item.pop(key)
return existing_variables return existing_variables
def vars_to_variables(vars, module):
# transform old vars to new variables structure
variables = list()
for item, value in vars.items():
if isinstance(value, (string_types, integer_types, float)):
variables.append(
{
"name": item,
"value": str(value),
"masked": False,
"protected": False,
"variable_type": "env_var",
}
)
elif isinstance(value, dict):
new_item = {
"name": item,
"value": value.get('value'),
"masked": value.get('masked'),
"protected": value.get('protected'),
"variable_type": value.get('variable_type'),
}
if value.get('environment_scope'):
new_item['environment_scope'] = value.get('environment_scope')
variables.append(new_item)
else:
module.fail_json(msg="value must be of type string, integer, float or dict")
return variables

View file

@ -166,52 +166,11 @@ group_variable:
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.api import basic_auth_argument_spec from ansible.module_utils.api import basic_auth_argument_spec
from ansible.module_utils.six import string_types
from ansible.module_utils.six import integer_types
from ansible_collections.community.general.plugins.module_utils.gitlab import ( from ansible_collections.community.general.plugins.module_utils.gitlab import (
auth_argument_spec, gitlab_authentication, ensure_gitlab_package, filter_returned_variables auth_argument_spec, gitlab_authentication, ensure_gitlab_package, filter_returned_variables, vars_to_variables
) )
def vars_to_variables(vars, module):
# transform old vars to new variables structure
variables = list()
for item, value in vars.items():
if (isinstance(value, string_types) or
isinstance(value, (integer_types, float))):
variables.append(
{
"name": item,
"value": str(value),
"masked": False,
"protected": False,
"variable_type": "env_var",
}
)
elif isinstance(value, dict):
new_item = {"name": item, "value": value.get('value')}
new_item = {
"name": item,
"value": value.get('value'),
"masked": value.get('masked'),
"protected": value.get('protected'),
"variable_type": value.get('variable_type'),
}
if value.get('environment_scope'):
new_item['environment_scope'] = value.get('environment_scope')
variables.append(new_item)
else:
module.fail_json(msg="value must be of type string, integer, float or dict")
return variables
class GitlabGroupVariables(object): class GitlabGroupVariables(object):
def __init__(self, module, gitlab_instance): def __init__(self, module, gitlab_instance):

View file

@ -184,8 +184,6 @@ project_variable:
import traceback import traceback
from ansible.module_utils.basic import AnsibleModule, missing_required_lib from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.api import basic_auth_argument_spec from ansible.module_utils.api import basic_auth_argument_spec
from ansible.module_utils.six import string_types
from ansible.module_utils.six import integer_types
GITLAB_IMP_ERR = None GITLAB_IMP_ERR = None
try: try:
@ -196,47 +194,10 @@ except Exception:
HAS_GITLAB_PACKAGE = False HAS_GITLAB_PACKAGE = False
from ansible_collections.community.general.plugins.module_utils.gitlab import ( from ansible_collections.community.general.plugins.module_utils.gitlab import (
auth_argument_spec, gitlab_authentication, ensure_gitlab_package, filter_returned_variables auth_argument_spec, gitlab_authentication, ensure_gitlab_package, filter_returned_variables, vars_to_variables
) )
def vars_to_variables(vars, module):
# transform old vars to new variables structure
variables = list()
for item, value in vars.items():
if (isinstance(value, string_types) or
isinstance(value, (integer_types, float))):
variables.append(
{
"name": item,
"value": str(value),
"masked": False,
"protected": False,
"variable_type": "env_var",
}
)
elif isinstance(value, dict):
new_item = {
"name": item,
"value": value.get('value'),
"masked": value.get('masked'),
"protected": value.get('protected'),
"variable_type": value.get('variable_type'),
}
if value.get('environment_scope'):
new_item['environment_scope'] = value.get('environment_scope')
variables.append(new_item)
else:
module.fail_json(msg="value must be of type string, integer, float or dict")
return variables
class GitlabProjectVariables(object): class GitlabProjectVariables(object):
def __init__(self, module, gitlab_instance): def __init__(self, module, gitlab_instance):
@ -322,7 +283,7 @@ def compare(requested_variables, existing_variables, state):
def native_python_main(this_gitlab, purge, requested_variables, state, module): def native_python_main(this_gitlab, purge, requested_variables, state, module):
change = False change = False
return_value = dict(added=list(), updated=list(), removed=list(), untouched=list()) return_value = dict(added=[], updated=[], removed=[], untouched=[])
gitlab_keys = this_gitlab.list_all_project_variables() gitlab_keys = this_gitlab.list_all_project_variables()
before = [x.attributes for x in gitlab_keys] before = [x.attributes for x in gitlab_keys]
@ -391,7 +352,7 @@ def native_python_main(this_gitlab, purge, requested_variables, state, module):
if module.check_mode: if module.check_mode:
return_value = dict(added=added, updated=updated, removed=return_value['removed'], untouched=untouched) return_value = dict(added=added, updated=updated, removed=return_value['removed'], untouched=untouched)
if return_value['added'] or return_value['removed'] or return_value['updated']: if any(return_value[x] for x in ['added', 'removed', 'updated']):
change = True change = True
gitlab_keys = this_gitlab.list_all_project_variables() gitlab_keys = this_gitlab.list_all_project_variables()
@ -452,7 +413,7 @@ def main():
if state == 'present': if state == 'present':
if any(x['value'] is None for x in variables): if any(x['value'] is None for x in variables):
module.fail_json(msg='value parameter is required in state present') module.fail_json(msg='value parameter is required for all variables in state present')
gitlab_instance = gitlab_authentication(module) gitlab_instance = gitlab_authentication(module)