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..ef8d4959 --- /dev/null +++ b/changelogs/fragments/318-org_users_by_org_name.yml @@ -0,0 +1,7 @@ +--- + +minor_changes: + - Add parameter `org_name` to `grafana_organization_user` + +trivial: + - Add tests for new `grafana_organization_user`-parameter `org_name` diff --git a/plugins/modules/grafana_organization_user.py b/plugins/modules/grafana_organization_user.py index 72496b84..14195fdb 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 @@ -156,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: @@ -178,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): @@ -232,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'), 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']], ] @@ -246,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'] + 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) 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"