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

Add environment scope on gitlab project variables (#1197)

* add environment scope on gitlab project variables

* fix sanity code

* apply again test

* environment_scope not defined by default. compatible with old versions of lib.

* environment_scope must be optional

* add changelog

* Update changelogs/fragments/1197_gitlab_project_variable.yml

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

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Pablo Castorino 2021-01-18 03:54:43 -03:00 committed by GitHub
parent 3b9c6d496b
commit 52bb601f31
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 10 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- gitlab_project_variable - add support for ``environment_scope`` on projects variables (https://github.com/ansible-collections/community.general/pull/1197).

View file

@ -54,6 +54,7 @@ options:
- Support for masked values requires GitLab >= 11.10. - Support for masked values requires GitLab >= 11.10.
- A I(value) must be a string or a number. - A I(value) must be a string or a number.
- Field I(variable_type) must be a string with either C(env_var), which is the default, or C(file). - Field I(variable_type) must be a string with either C(env_var), which is the default, or C(file).
- Field I(environment_scope) must be a string defined by scope environment.
- When a value is masked, it must be in Base64 and have a length of at least 8 characters. - When a value is masked, it must be in Base64 and have a length of at least 8 characters.
See GitLab documentation on acceptable values for a masked variable (https://docs.gitlab.com/ce/ci/variables/#masked-variables). See GitLab documentation on acceptable values for a masked variable (https://docs.gitlab.com/ce/ci/variables/#masked-variables).
default: {} default: {}
@ -85,6 +86,7 @@ EXAMPLES = '''
masked: true masked: true
protected: true protected: true
variable_type: env_var variable_type: env_var
environment_scope: '*'
- name: Delete one variable - name: Delete one variable
community.general.gitlab_project_variable: community.general.gitlab_project_variable:
@ -164,27 +166,36 @@ class GitlabProjectVariables(object):
vars_page = self.project.variables.list(page=page_nb) vars_page = self.project.variables.list(page=page_nb)
return variables return variables
def create_variable(self, key, value, masked, protected, variable_type): def create_variable(self, key, value, masked, protected, variable_type, environment_scope):
if self._module.check_mode: if self._module.check_mode:
return return
return self.project.variables.create({"key": key, "value": value, var = {
"masked": masked, "protected": protected, "key": key, "value": value,
"variable_type": variable_type}) "masked": masked, "protected": protected,
"variable_type": variable_type
}
if environment_scope is not None:
var["environment_scope"] = environment_scope
return self.project.variables.create(var)
def update_variable(self, key, var, value, masked, protected, variable_type): def update_variable(self, key, var, value, masked, protected, variable_type, environment_scope):
if var.value == value and var.protected == protected and var.masked == masked and var.variable_type == variable_type: if (var.value == value and var.protected == protected and var.masked == masked
and var.variable_type == variable_type
and (var.environment_scope == environment_scope or environment_scope is None)):
return False return False
if self._module.check_mode: if self._module.check_mode:
return True return True
if var.protected == protected and var.masked == masked and var.variable_type == variable_type: if (var.protected == protected and var.masked == masked
and var.variable_type == variable_type
and (var.environment_scope == environment_scope or environment_scope is None)):
var.value = value var.value = value
var.save() var.save()
return True return True
self.delete_variable(key) self.delete_variable(key)
self.create_variable(key, value, masked, protected, variable_type) self.create_variable(key, value, masked, protected, variable_type, environment_scope)
return True return True
def delete_variable(self, key): def delete_variable(self, key):
@ -208,11 +219,13 @@ def native_python_main(this_gitlab, purge, var_list, state, module):
masked = False masked = False
protected = False protected = False
variable_type = 'env_var' variable_type = 'env_var'
environment_scope = None
elif isinstance(var_list[key], dict): elif isinstance(var_list[key], dict):
value = var_list[key].get('value') value = var_list[key].get('value')
masked = var_list[key].get('masked', False) masked = var_list[key].get('masked', False)
protected = var_list[key].get('protected', False) protected = var_list[key].get('protected', False)
variable_type = var_list[key].get('variable_type', 'env_var') variable_type = var_list[key].get('variable_type', 'env_var')
environment_scope = var_list[key].get('environment_scope')
else: else:
module.fail_json(msg="value must be of type string, integer or dict") module.fail_json(msg="value must be of type string, integer or dict")
@ -225,7 +238,8 @@ def native_python_main(this_gitlab, purge, var_list, state, module):
gitlab_keys[index], gitlab_keys[index],
value, masked, value, masked,
protected, protected,
variable_type) variable_type,
environment_scope)
change = single_change or change change = single_change or change
if single_change: if single_change:
return_value['updated'].append(key) return_value['updated'].append(key)
@ -238,7 +252,7 @@ def native_python_main(this_gitlab, purge, var_list, state, module):
return_value['removed'].append(key) return_value['removed'].append(key)
elif key not in existing_variables and state == 'present': elif key not in existing_variables and state == 'present':
this_gitlab.create_variable(key, value, masked, protected, variable_type) this_gitlab.create_variable(key, value, masked, protected, variable_type, environment_scope)
change = True change = True
return_value['added'].append(key) return_value['added'].append(key)

View file

@ -218,6 +218,36 @@
that: that:
- gitlab_project_variable_state is not changed - gitlab_project_variable_state is not changed
- name: change environment scope
gitlab_project_variable:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
project: "{{ gitlab_project_name }}"
vars:
ACCESS_KEY_ID:
environment_scope: testing
register: gitlab_project_variable_state
- name: state must be changed
assert:
that:
- gitlab_project_variable_state is changed
- name: apply again the environment scope change
gitlab_project_variable:
api_url: "{{ gitlab_host }}"
api_token: "{{ gitlab_login_token }}"
project: "{{ gitlab_project_name }}"
vars:
ACCESS_KEY_ID:
environment_scope: testing
register: gitlab_project_variable_state
- name: state must not be changed
assert:
that:
- gitlab_project_variable_state is not changed
- name: purge all variables at the beginning - name: purge all variables at the beginning
gitlab_project_variable: gitlab_project_variable:
api_url: "{{ gitlab_host }}" api_url: "{{ gitlab_host }}"