Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(organization_user): manage users by org_name #318

Merged
merged 8 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelogs/fragments/318-org_users_by_org_name.yml
Original file line number Diff line number Diff line change
@@ -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`
22 changes: 21 additions & 1 deletion plugins/modules/grafana_organization_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand Down Expand Up @@ -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']],
]
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand All @@ -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"
Loading