From 2933d07bbb00f698e0dba983c265dee7426395f8 Mon Sep 17 00:00:00 2001 From: Nemental <15136847+Nemental@users.noreply.github.com> Date: Fri, 3 Nov 2023 15:10:15 +0100 Subject: [PATCH 1/8] docs: added org_name --- plugins/modules/grafana_organization_user.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plugins/modules/grafana_organization_user.py b/plugins/modules/grafana_organization_user.py index 72496b84..7d82e962 100644 --- a/plugins/modules/grafana_organization_user.py +++ b/plugins/modules/grafana_organization_user.py @@ -57,6 +57,12 @@ default: 1 description: - Organization ID. + - Mutually exclusive with `org_name`. + org_name: + type: str + description: + - Organization name. + - Mutually exclusive with `org_id`. extends_documentation_fragment: - community.grafana.basic_auth From 5eba512d820497ade05bcd0232710ed73023ed8d Mon Sep 17 00:00:00 2001 From: Nemental <15136847+Nemental@users.noreply.github.com> Date: Fri, 3 Nov 2023 16:29:56 +0100 Subject: [PATCH 2/8] feat: init get org id by org name --- plugins/modules/grafana_organization_user.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/plugins/modules/grafana_organization_user.py b/plugins/modules/grafana_organization_user.py index 7d82e962..db3db75f 100644 --- a/plugins/modules/grafana_organization_user.py +++ b/plugins/modules/grafana_organization_user.py @@ -162,6 +162,12 @@ def _api_call(self, method, path, payload): data = json.dumps(payload) return fetch_url(self._module, self.grafana_url + '/api/' + path, headers=self.headers, method=method, data=data) + def _organization_by_name(self, org_name): + r, info = self._api_call('GET', 'orgs/name/%s' % org_name, None) + if info['status'] != 200: + raise GrafanaAPIException("Unable to retrieve organization: %s" % info) + return json.loads(to_text(r.read())) + def _organization_users(self, org_id): r, info = self._api_call('GET', 'orgs/%d/users' % org_id, None) if info['status'] != 200: @@ -238,12 +244,16 @@ def main(): argument_spec.pop('grafana_api_key') argument_spec.update( org_id=dict(type='int', default=1), + org_name=dict(type='str', required=False), login=dict(type='str', required=True), role=dict(type='str', choices=['viewer', 'editor', 'admin'], default='viewer'), ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=False, + mutually_exclusive=[ + ('org_id', 'org_name'), + ], required_if=[ ['state', 'present', ['role']], ] @@ -252,6 +262,10 @@ def main(): org_id = module.params['org_id'] login = module.params['login'] iface = GrafanaOrganizationUserInterface(module) + if module.params['org_name']: + org_name = module.params['org_name'] + result = iface._organization_by_name(org_name) + module.exit_json(failed=False, **result) if module.params['state'] == 'present': role = module.params['role'].capitalize() result = iface.create_or_update_user(org_id, login, role) From c33b87dac92b2ebf36ae6aad996ac6bcfacc8b7a Mon Sep 17 00:00:00 2001 From: Nemental <15136847+Nemental@users.noreply.github.com> Date: Fri, 3 Nov 2023 17:00:07 +0100 Subject: [PATCH 3/8] fix: get orga users by login --- plugins/modules/grafana_organization_user.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/grafana_organization_user.py b/plugins/modules/grafana_organization_user.py index db3db75f..11b93def 100644 --- a/plugins/modules/grafana_organization_user.py +++ b/plugins/modules/grafana_organization_user.py @@ -190,7 +190,7 @@ def _remove_organization_user(self, org_id, user_id): def _organization_user_by_login(self, org_id, login): for user in self._organization_users(org_id): - if user['name'] == login or user['email'] == login: + if login in (user['login'], user['email']): return user def create_or_update_user(self, org_id, login, role): From 663e81a51d028faf6dc2e8f42318a770d73b4c18 Mon Sep 17 00:00:00 2001 From: Nemental <15136847+Nemental@users.noreply.github.com> Date: Fri, 3 Nov 2023 17:15:01 +0100 Subject: [PATCH 4/8] fix: set org_id for further use --- plugins/modules/grafana_organization_user.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/modules/grafana_organization_user.py b/plugins/modules/grafana_organization_user.py index 11b93def..d0f85d1c 100644 --- a/plugins/modules/grafana_organization_user.py +++ b/plugins/modules/grafana_organization_user.py @@ -264,8 +264,8 @@ def main(): iface = GrafanaOrganizationUserInterface(module) if module.params['org_name']: org_name = module.params['org_name'] - result = iface._organization_by_name(org_name) - module.exit_json(failed=False, **result) + organization = iface._organization_by_name(org_name) + org_id = organization['id'] if module.params['state'] == 'present': role = module.params['role'].capitalize() result = iface.create_or_update_user(org_id, login, role) From e63acf9aae962c2f8f863dadf38347e06dc972ff Mon Sep 17 00:00:00 2001 From: Nemental <15136847+Nemental@users.noreply.github.com> Date: Fri, 3 Nov 2023 17:30:29 +0100 Subject: [PATCH 5/8] chore: required is default false --- plugins/modules/grafana_organization_user.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/grafana_organization_user.py b/plugins/modules/grafana_organization_user.py index d0f85d1c..14195fdb 100644 --- a/plugins/modules/grafana_organization_user.py +++ b/plugins/modules/grafana_organization_user.py @@ -244,7 +244,7 @@ def main(): argument_spec.pop('grafana_api_key') argument_spec.update( org_id=dict(type='int', default=1), - org_name=dict(type='str', required=False), + org_name=dict(type='str'), login=dict(type='str', required=True), role=dict(type='str', choices=['viewer', 'editor', 'admin'], default='viewer'), ) From c802be98eaaf5a37b36db787792a9b3d1e0f93db Mon Sep 17 00:00:00 2001 From: Nemental <15136847+Nemental@users.noreply.github.com> Date: Sun, 5 Nov 2023 22:07:42 +0100 Subject: [PATCH 6/8] docs: added fragment --- changelogs/fragments/318-org_users_by_org_name.yml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelogs/fragments/318-org_users_by_org_name.yml diff --git a/changelogs/fragments/318-org_users_by_org_name.yml b/changelogs/fragments/318-org_users_by_org_name.yml new file mode 100644 index 00000000..d7f59c64 --- /dev/null +++ b/changelogs/fragments/318-org_users_by_org_name.yml @@ -0,0 +1,4 @@ +--- + +minor_changes: + - Add parameter `org_name` to `grafana_organization_user` From c34e2d717371c804e6b5e8b5b05a03a495af52d2 Mon Sep 17 00:00:00 2001 From: Nemental <15136847+Nemental@users.noreply.github.com> Date: Mon, 6 Nov 2023 11:24:50 +0100 Subject: [PATCH 7/8] test: added tests with org_name --- .../grafana_organization_user/tasks/main.yml | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/tests/integration/targets/grafana_organization_user/tasks/main.yml b/tests/integration/targets/grafana_organization_user/tasks/main.yml index 6ac1f131..3ede4f7c 100644 --- a/tests/integration/targets/grafana_organization_user/tasks/main.yml +++ b/tests/integration/targets/grafana_organization_user/tasks/main.yml @@ -92,7 +92,7 @@ state: present register: org -- name: Add user to the organization +- name: Add user to the new organization by org_id community.grafana.grafana_organization_user: url: "{{ grafana_url }}" url_username: "{{ grafana_username }}" @@ -108,3 +108,48 @@ - "result.changed == true" - "result.user.orgId == org.org.id" - "result.user.role == 'Admin'" + +- name: Remove user from new organization by org_id + community.grafana.grafana_organization_user: + url: "{{ grafana_url }}" + url_username: "{{ grafana_username }}" + url_password: "{{ grafana_password }}" + org_id: "{{ org.org.id }}" + login: orgtest + state: absent + register: result +- assert: + that: + - "result.failed == false" + - "result.changed == true" + +- name: Add user to the new organization by org_name + community.grafana.grafana_organization_user: + url: "{{ grafana_url }}" + url_username: "{{ grafana_username }}" + url_password: "{{ grafana_password }}" + org_name: "{{ org.org.name }}" + login: orgtest + role: admin + state: present + register: result +- assert: + that: + - "result.failed == false" + - "result.changed == true" + - "result.user.orgId == org.org.id" + - "result.user.role == 'Admin'" + +- name: Remove user from new organization by org_name + community.grafana.grafana_organization_user: + url: "{{ grafana_url }}" + url_username: "{{ grafana_username }}" + url_password: "{{ grafana_password }}" + org_name: "{{ org.org.name }}" + login: orgtest + state: absent + register: result +- assert: + that: + - "result.failed == false" + - "result.changed == true" From fe724a9b907ae82ce3273f2ef2526708934d964c Mon Sep 17 00:00:00 2001 From: Nemental <15136847+Nemental@users.noreply.github.com> Date: Mon, 6 Nov 2023 11:59:30 +0100 Subject: [PATCH 8/8] docs: mentioned test in fragment --- changelogs/fragments/318-org_users_by_org_name.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/changelogs/fragments/318-org_users_by_org_name.yml b/changelogs/fragments/318-org_users_by_org_name.yml index d7f59c64..ef8d4959 100644 --- a/changelogs/fragments/318-org_users_by_org_name.yml +++ b/changelogs/fragments/318-org_users_by_org_name.yml @@ -2,3 +2,6 @@ minor_changes: - Add parameter `org_name` to `grafana_organization_user` + +trivial: + - Add tests for new `grafana_organization_user`-parameter `org_name`