From e1a4b50074112e206cb903840d5a19c2ab7dbbf6 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Sat, 5 Feb 2022 21:20:41 +0100 Subject: [PATCH] gitlab_project_variable: Allow delete without value (#4150) (#4157) * value is not required when state is absent * fix integration test. missing value * fix condition * add changelog fragment * fail fast * try required_if on suboptions * revert * Update plugins/modules/source_control/gitlab/gitlab_project_variable.py Co-authored-by: Felix Fontein * fix naming in doc * typo in name Co-authored-by: Felix Fontein (cherry picked from commit 69551ac32500883ff6a8fb5009934cfdf8578528) Co-authored-by: Markus Bergholz --- ...150-gitlab-project-variable-absent-fix.yml | 2 + .../gitlab/gitlab_project_variable.py | 15 +++--- .../gitlab_project_variable/tasks/main.yml | 50 +++++++++++++++++++ 3 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 changelogs/fragments/4150-gitlab-project-variable-absent-fix.yml diff --git a/changelogs/fragments/4150-gitlab-project-variable-absent-fix.yml b/changelogs/fragments/4150-gitlab-project-variable-absent-fix.yml new file mode 100644 index 0000000000..778ee40ce1 --- /dev/null +++ b/changelogs/fragments/4150-gitlab-project-variable-absent-fix.yml @@ -0,0 +1,2 @@ +bugfixes: + - gitlab_project_variable - ``value`` is not necessary when deleting variables (https://github.com/ansible-collections/community.general/pull/4150). diff --git a/plugins/modules/source_control/gitlab/gitlab_project_variable.py b/plugins/modules/source_control/gitlab/gitlab_project_variable.py index 9bab6f169c..f9b8d7b6e1 100644 --- a/plugins/modules/source_control/gitlab/gitlab_project_variable.py +++ b/plugins/modules/source_control/gitlab/gitlab_project_variable.py @@ -74,8 +74,8 @@ options: value: description: - The variable value. + - Required when I(state=present). type: str - required: true masked: description: - Wether variable value is masked or not. @@ -403,7 +403,7 @@ def main(): vars=dict(type='dict', required=False, default=dict(), no_log=True), variables=dict(type='list', elements='dict', required=False, default=list(), options=dict( name=dict(type='str', required=True), - value=dict(type='str', required=True, no_log=True), + value=dict(type='str', no_log=True), masked=dict(type='bool', default=False), protected=dict(type='bool', default=False), environment_scope=dict(type='str', default='*'), @@ -431,18 +431,21 @@ def main(): supports_check_mode=True ) + if not HAS_GITLAB_PACKAGE: + module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR) + purge = module.params['purge'] var_list = module.params['vars'] + state = module.params['state'] if var_list: variables = vars_to_variables(var_list, module) else: variables = module.params['variables'] - state = module.params['state'] - - if not HAS_GITLAB_PACKAGE: - module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR) + if state == 'present': + if any(x['value'] is None for x in variables): + module.fail_json(msg='value parameter is required in state present') gitlab_instance = gitlab_authentication(module) diff --git a/tests/integration/targets/gitlab_project_variable/tasks/main.yml b/tests/integration/targets/gitlab_project_variable/tasks/main.yml index 7b745526a2..18dbf7eccd 100644 --- a/tests/integration/targets/gitlab_project_variable/tasks/main.yml +++ b/tests/integration/targets/gitlab_project_variable/tasks/main.yml @@ -227,6 +227,7 @@ vars: ACCESS_KEY_ID: environment_scope: testing + value: checkmode register: gitlab_project_variable_state - name: state must be changed @@ -242,6 +243,7 @@ vars: ACCESS_KEY_ID: environment_scope: testing + value: checkmode register: gitlab_project_variable_state - name: state must not be changed @@ -644,3 +646,51 @@ - gitlab_project_variable_state.project_variable.untouched|length == 0 - gitlab_project_variable_state.project_variable.removed|length == 0 - gitlab_project_variable_state.project_variable.updated|length == 0 + +- name: throw error when state is present but no value is given + gitlab_project_variable: + api_url: "{{ gitlab_host }}" + api_token: "{{ gitlab_login_token }}" + project: "{{ gitlab_project_name }}" + variables: + - name: delete_me + register: gitlab_project_variable_state + ignore_errors: yes + +- name: verify fail + assert: + that: + - gitlab_project_variable_state.failed + - gitlab_project_variable_state is not changed + +- name: set a new variable to delete it later + gitlab_project_variable: + api_url: "{{ gitlab_host }}" + api_token: "{{ gitlab_login_token }}" + project: "{{ gitlab_project_name }}" + purge: True + variables: + - name: delete_me + value: ansible + register: gitlab_project_variable_state + +- name: verify the change + assert: + that: + - gitlab_project_variable_state.changed + +- name: delete variable without referencing its value + gitlab_project_variable: + api_url: "{{ gitlab_host }}" + api_token: "{{ gitlab_login_token }}" + project: "{{ gitlab_project_name }}" + state: absent + variables: + - name: delete_me + register: gitlab_project_variable_state + +- name: verify deletion + assert: + that: + - gitlab_project_variable_state.changed + - gitlab_project_variable_state.project_variable.removed|length == 1