Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gitlab modules: minor refactor #6384

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelogs/fragments/6384-gitlab-refactor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- gitlab module utils - refactor function in from modules (https://github.com/ansible-collections/community.general/pull/6384).
- gitlab_group_variable, gitlab_project_variable - refactor function out to module utils (https://github.com/ansible-collections/community.general/pull/6384).
38 changes: 38 additions & 0 deletions plugins/module_utils/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

from ansible.module_utils.basic import missing_required_lib
from ansible.module_utils.common.text.converters import to_native
from ansible.module_utils.six import string_types
from ansible.module_utils.six import integer_types

from ansible_collections.community.general.plugins.module_utils.version import LooseVersion

Expand Down Expand Up @@ -121,3 +123,39 @@ def filter_returned_variables(gitlab_variables):
if key not in KNOWN:
item.pop(key)
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) 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
43 changes: 1 addition & 42 deletions plugins/modules/gitlab_group_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,52 +166,11 @@

from ansible.module_utils.basic import AnsibleModule
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 (
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):

def __init__(self, module, gitlab_instance):
Expand Down
47 changes: 4 additions & 43 deletions plugins/modules/gitlab_project_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,6 @@
import traceback
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
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
try:
Expand All @@ -196,47 +194,10 @@
HAS_GITLAB_PACKAGE = False

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

def __init__(self, module, gitlab_instance):
Expand Down Expand Up @@ -322,7 +283,7 @@ def compare(requested_variables, existing_variables, state):
def native_python_main(this_gitlab, purge, requested_variables, state, module):

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()
before = [x.attributes for x in gitlab_keys]
Expand Down Expand Up @@ -391,7 +352,7 @@ def native_python_main(this_gitlab, purge, requested_variables, state, module):
if module.check_mode:
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

gitlab_keys = this_gitlab.list_all_project_variables()
Expand Down Expand Up @@ -452,7 +413,7 @@ def main():

if state == 'present':
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)

Expand Down