From 98486c0ee26463a07f1a85ffe69c6639e763db3b Mon Sep 17 00:00:00 2001 From: S Code Man <30977678+scodeman@users.noreply.github.com> Date: Fri, 9 Oct 2020 20:59:00 +0800 Subject: [PATCH] Gitlab variables pagination (#968) * Workaround increasing per_page to max * Update plugins/modules/source_control/gitlab/gitlab_group_variable.py * Create 968-gitlab_variables-pagination-increase.yml * Update changelogs/fragments/968-gitlab_variables-pagination-increase.yml Co-authored-by: Felix Fontein * Update changelogs/fragments/968-gitlab_variables-pagination-increase.yml Co-authored-by: Felix Fontein * Proper support of pagination * Fix E303 too many blank lines * Add test for pagination * Fix last vars removal test * Apply suggestions from code review Check misalignement fixed Co-authored-by: Felix Fontein --- .../968-gitlab_variables-pagination.yml | 4 + .../gitlab/gitlab_group_variable.py | 9 ++- .../gitlab/gitlab_project_variable.py | 9 ++- .../gitlab_group_variable/tasks/main.yml | 78 ++++++++++++++++++- .../gitlab_project_variable/tasks/main.yml | 78 ++++++++++++++++++- 5 files changed, 170 insertions(+), 8 deletions(-) create mode 100644 changelogs/fragments/968-gitlab_variables-pagination.yml diff --git a/changelogs/fragments/968-gitlab_variables-pagination.yml b/changelogs/fragments/968-gitlab_variables-pagination.yml new file mode 100644 index 0000000000..9f8e930ed6 --- /dev/null +++ b/changelogs/fragments/968-gitlab_variables-pagination.yml @@ -0,0 +1,4 @@ +--- +bugfixes: + - gitlab_project_variable - support for GitLab pagination limitation by iterating over GitLab variable pages (https://github.com/ansible-collections/community.general/pull/968). + - gitlab_group_variable - support for GitLab pagination limitation by iterating over GitLab variable pages (https://github.com/ansible-collections/community.general/pull/968). diff --git a/plugins/modules/source_control/gitlab/gitlab_group_variable.py b/plugins/modules/source_control/gitlab/gitlab_group_variable.py index 74e9215e12..dd20a0b8da 100644 --- a/plugins/modules/source_control/gitlab/gitlab_group_variable.py +++ b/plugins/modules/source_control/gitlab/gitlab_group_variable.py @@ -158,7 +158,14 @@ class GitlabGroupVariables(object): return self.repo.groups.get(group_name) def list_all_group_variables(self): - return self.group.variables.list() + page_nb = 1 + variables = [] + vars_page = self.group.variables.list(page=page_nb) + while len(vars_page) > 0: + variables += vars_page + page_nb += 1 + vars_page = self.group.variables.list(page=page_nb) + return variables def create_variable(self, key, value, masked, protected, variable_type): if self._module.check_mode: diff --git a/plugins/modules/source_control/gitlab/gitlab_project_variable.py b/plugins/modules/source_control/gitlab/gitlab_project_variable.py index 70d88d5f8f..9803f76bc8 100644 --- a/plugins/modules/source_control/gitlab/gitlab_project_variable.py +++ b/plugins/modules/source_control/gitlab/gitlab_project_variable.py @@ -155,7 +155,14 @@ class GitlabProjectVariables(object): return self.repo.projects.get(project_name) def list_all_project_variables(self): - return self.project.variables.list() + page_nb = 1 + variables = [] + vars_page = self.project.variables.list(page=page_nb) + while len(vars_page) > 0: + variables += vars_page + page_nb += 1 + vars_page = self.project.variables.list(page=page_nb) + return variables def create_variable(self, key, value, masked, protected, variable_type): if self._module.check_mode: diff --git a/tests/integration/targets/gitlab_group_variable/tasks/main.yml b/tests/integration/targets/gitlab_group_variable/tasks/main.yml index 38779ace3d..d3b6eb4ce0 100644 --- a/tests/integration/targets/gitlab_group_variable/tasks/main.yml +++ b/tests/integration/targets/gitlab_group_variable/tasks/main.yml @@ -495,6 +495,78 @@ - gitlab_group_variable_state.group_variable.updated|length == 0 - gitlab_group_variable_state.group_variable.removed[0] == "my_test_var" +- name: set complete page and purge existing ones + gitlab_group_variable: + api_url: "{{ gitlab_host }}" + api_token: "{{ gitlab_login_token }}" + group: "{{ gitlab_group_name }}" + vars: + page1_var01: value + page1_var02: value + page1_var03: value + page1_var04: value + page1_var05: value + page1_var06: value + page1_var07: value + page1_var08: value + page1_var09: value + page1_var10: value + page1_var11: value + page1_var12: value + page1_var13: value + page1_var14: value + page1_var15: value + page1_var16: value + page1_var17: value + page1_var18: value + page1_var19: value + page1_var20: value + purge: True + register: gitlab_group_variable_state + +- name: complete page added state must be changed + assert: + that: + - gitlab_group_variable_state is changed + - gitlab_group_variable_state.group_variable.added|length == 20 + - gitlab_group_variable_state.group_variable.untouched|length == 0 + +- name: set complete page and keep existing ones + gitlab_group_variable: + api_url: "{{ gitlab_host }}" + api_token: "{{ gitlab_login_token }}" + group: "{{ gitlab_group_name }}" + vars: + page2_var01: value + page2_var02: value + page2_var03: value + page2_var04: value + page2_var05: value + page2_var06: value + page2_var07: value + page2_var08: value + page2_var09: value + page2_var10: value + page2_var11: value + page2_var12: value + page2_var13: value + page2_var14: value + page2_var15: value + page2_var16: value + page2_var17: value + page2_var18: value + page2_var19: value + page2_var20: value + purge: False + register: gitlab_group_variable_state + +- name: existing page untouched state must be changed + assert: + that: + - gitlab_group_variable_state is changed + - gitlab_group_variable_state.group_variable.added|length == 20 + - gitlab_group_variable_state.group_variable.untouched|length == 20 + - name: check that no variables are left gitlab_group_variable: api_url: "{{ gitlab_host }}" @@ -503,11 +575,11 @@ purge: True register: gitlab_group_variable_state -- name: check that no variables are untoucheded state must be changed +- name: check that no variables are untouched state must be changed assert: that: - - not gitlab_group_variable_state.changed + - gitlab_group_variable_state.changed - gitlab_group_variable_state.group_variable.added|length == 0 - gitlab_group_variable_state.group_variable.untouched|length == 0 - - gitlab_group_variable_state.group_variable.removed|length == 0 + - gitlab_group_variable_state.group_variable.removed|length == 40 - gitlab_group_variable_state.group_variable.updated|length == 0 diff --git a/tests/integration/targets/gitlab_project_variable/tasks/main.yml b/tests/integration/targets/gitlab_project_variable/tasks/main.yml index 338d46f873..2b29619f45 100644 --- a/tests/integration/targets/gitlab_project_variable/tasks/main.yml +++ b/tests/integration/targets/gitlab_project_variable/tasks/main.yml @@ -494,6 +494,78 @@ - gitlab_project_variable_state.project_variable.updated|length == 0 - gitlab_project_variable_state.project_variable.removed[0] == "my_test_var" +- name: set complete page and purge existing ones + gitlab_project_variable: + api_url: "{{ gitlab_host }}" + api_token: "{{ gitlab_login_token }}" + project: "{{ gitlab_project_name }}" + vars: + page1_var01: value + page1_var02: value + page1_var03: value + page1_var04: value + page1_var05: value + page1_var06: value + page1_var07: value + page1_var08: value + page1_var09: value + page1_var10: value + page1_var11: value + page1_var12: value + page1_var13: value + page1_var14: value + page1_var15: value + page1_var16: value + page1_var17: value + page1_var18: value + page1_var19: value + page1_var20: value + purge: True + register: gitlab_project_variable_state + +- name: complete page added state must be changed + assert: + that: + - gitlab_project_variable_state is changed + - gitlab_project_variable_state.project_variable.added|length == 20 + - gitlab_project_variable_state.project_variable.untouched|length == 0 + +- name: set complete page and keep existing ones + gitlab_project_variable: + api_url: "{{ gitlab_host }}" + api_token: "{{ gitlab_login_token }}" + project: "{{ gitlab_project_name }}" + vars: + page2_var01: value + page2_var02: value + page2_var03: value + page2_var04: value + page2_var05: value + page2_var06: value + page2_var07: value + page2_var08: value + page2_var09: value + page2_var10: value + page2_var11: value + page2_var12: value + page2_var13: value + page2_var14: value + page2_var15: value + page2_var16: value + page2_var17: value + page2_var18: value + page2_var19: value + page2_var20: value + purge: False + register: gitlab_project_variable_state + +- name: existing page untouched state must be changed + assert: + that: + - gitlab_project_variable_state is changed + - gitlab_project_variable_state.project_variable.added|length == 20 + - gitlab_project_variable_state.project_variable.untouched|length == 20 + - name: check that no variables are left gitlab_project_variable: api_url: "{{ gitlab_host }}" @@ -502,11 +574,11 @@ purge: True register: gitlab_project_variable_state -- name: check that no variables are untoucheded state must be changed +- name: check that no variables are untouched state must be changed assert: that: - - not gitlab_project_variable_state.changed + - gitlab_project_variable_state.changed - gitlab_project_variable_state.project_variable.added|length == 0 - gitlab_project_variable_state.project_variable.untouched|length == 0 - - gitlab_project_variable_state.project_variable.removed|length == 0 + - gitlab_project_variable_state.project_variable.removed|length == 40 - gitlab_project_variable_state.project_variable.updated|length == 0