Merge ~gary-wzl77/charm-grafana:fix-1967046 into charm-grafana:master

Proposed by Gary.Wang
Status: Work in progress
Proposed branch: ~gary-wzl77/charm-grafana:fix-1967046
Merge into: charm-grafana:master
Diff against target: 96 lines (+25/-20)
2 files modified
src/README.md (+1/-1)
src/actions/create-user (+24/-19)
Reviewer Review Type Date Requested Status
Alvaro Uria (community) Needs Fixing
BootStack Reviewers mr tracking; do not claim Pending
BootStack Reviewers Pending
BootStack Reviewers Pending
Review via email: mp+417845@code.launchpad.net

Commit message

fix(actions): do not add a user to the organisation again after an account has been created

By default, when creating a user with no parameter `OrgId` is specified
in the post body, the newly created user will be added to the same
organization where the admin account belongs to. Therefore there is
no need to add the user to the organisation again.

Adding the user to the organisation again causes the following error
to occur
```
{"message":"User is already member of this organization","userId":6}
```

This change fixed the issue LP:1967046.

Test: run the following command and check if the user can be added
      successfully
      ```
      juju run-action --wait grafana/0 create-user name="doe" \
           <email address hidden>" \
           login="doe" \
           password="redacted" \
           role="Viewer"
      ```

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Alvaro Uria (aluria) wrote :

Please rebase your change. Functests are failing because of an assert out of scope of this MP.

review: Needs Fixing
Revision history for this message
Gary.Wang (gary-wzl77) wrote :

> Please rebase your change. Functests are failing because of an assert out of
> scope of this MP.
A: Done, could you take another look?

Unmerged commits

5a625b2... by Gary.Wang

fix(README): use the proper parameter in juju run-action

There is no `-w` parameter in juju run-action command but `--wait`.

2c6c1be... by Gary.Wang

fix(action): user proper action key

The action key must start and end with lowercase alphanumeric,
and contain only lowercase alphanumeric, hyphens and periods.
And the underscore is invalid character in the action key.

This change fixed that.

265675d... by Gary.Wang

fix(actions): do not add a user to the organisation again after an account has been created

By default, when creating a user with no parameter `OrgId` is specified
in the post body, the newly created user will be added to the same
organization where the admin account belongs to. Therefore there is
no need to add the user to the organisation again.

Adding the user to the organisation again causes the following error
to occur
```
{"message":"User is already member of this organization","userId":6}
```

This change fixed the issue LP:1967046.

Test: run the following command and check if the user can be added
      successfully
      ```
      juju run-action --wait grafana/0 create-user name="doe" \
           <email address hidden>" \
           login="doe" \
           password="redacted" \
           role="Viewer"
      ```

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/README.md b/src/README.md
2index 1308bee..2fa457c 100644
3--- a/src/README.md
4+++ b/src/README.md
5@@ -64,7 +64,7 @@ juju run-action --wait grafana/0 get-admin-password
6 You can create a user:
7
8 ```
9-juju run-action -w grafana/0 create-user name="John Citizen" \
10+juju run-action --wait grafana/0 create-user name="John Citizen" \
11 email="john@example.com" login="john" password="redacted" \
12 role="Viewer"
13 ```
14diff --git a/src/actions/create-user b/src/actions/create-user
15index 916534c..6a6e7da 100755
16--- a/src/actions/create-user
17+++ b/src/actions/create-user
18@@ -53,11 +53,15 @@ if user_role not in valid_roles:
19 action_fail("Role %s isn't valid, needs to be one of %s" % (user_role, valid_roles))
20 sys.exit(0)
21
22+# Must be an integer
23+org_id=1
24+
25 user_data = {
26 'name': name,
27 'email': email,
28 'login': login,
29- 'password': user_password
30+ 'password': user_password,
31+ 'OrgId': org_id
32 }
33
34 headers = {'Content-Type': 'application/json'}
35@@ -82,14 +86,7 @@ else:
36 sys.exit(0)
37
38 # http://docs.grafana.org/http_api/org/#get-organisation-by-id
39-api_org_url = "/api/orgs/1"
40-
41-# http://docs.grafana.org/http_api/org/#add-a-new-user-to-the-actual-organisation
42-api_org_user_url = "/api/org/users"
43-# {
44-# "role": "Admin",
45-# "loginOrEmail": "admin"
46-# }
47+api_org_url = "/api/orgs/" + str(org_id)
48
49 grafana_api_org_url = grafana + api_org_url
50
51@@ -106,27 +103,35 @@ else:
52 if r_org.status_code == 200:
53 org_data = r_org.json()
54 org_name = org_data['name']
55- org_user_data = {"role": user_role, "loginOrEmail": login}
56-
57- grafana_api_org_user_url = grafana + api_org_user_url
58
59- if requests.utils.urlparse(grafana_api_org_user_url).scheme:
60- r_userorg = requests.post(
61- grafana_api_org_user_url,
62+ # By default, when creating a user with no parameter `OrgId` is specified
63+ # in the post body, the newly created user will be added to the same
64+ # organization where the admin account belongs to. Therefore there is no
65+ # need to add the user to the organisation again but just update account's role.
66+ #
67+ # https://grafana.com/docs/grafana/latest/http_api/org/#updates-the-given-user
68+ user_id = r_create.json()['id']
69+ api_org_users_url = api_org_url + "/users/" + str(user_id)
70+ grafana_api_org_users_url = grafana + api_org_users_url
71+ org_user_data = {"role": user_role}
72+
73+ if requests.utils.urlparse(grafana_api_org_users_url).scheme:
74+ r_userorg = requests.patch(
75+ grafana_api_org_users_url,
76 auth=api_auth,
77 headers=headers,
78 data=json.dumps(org_user_data),
79 verify=verify_ca
80 )
81 else:
82- action_fail("Grafana url %s failed to parse" % (grafana_api_org_user_url))
83+ action_fail("Grafana url %s failed to parse" % (grafana_api_org_users_url))
84 sys.exit(0)
85
86 if r_userorg.status_code == 200:
87- action_set({"organisation_name": org_name})
88- action_set({"organisation_role": user_role})
89+ action_set({"organisation-name": org_name})
90+ action_set({"organisation-role": user_role})
91 else:
92- action_fail("Failed to add user to organisation")
93+ action_fail("Failed to update user's role")
94 sys.exit(0)
95 else:
96 action_fail("Failed to retrieve organisation details")

Subscribers

People subscribed via source and target branches

to all changes: