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
HTTP API: Allow assigning a specific organization when creating a new user #21775
HTTP API: Allow assigning a specific organization when creating a new user #21775
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks generally good to me, but we need to decide if we want the functionality.
The way I see it: Grafana wants to go down the route of observability for large enterprise, companies like logz.io are starting to offer managed versions of grafana to their users for this purpose (potential stream of revenue), this requires management of a multi-tenant grafana which is currently quite hard (I have done it myself), this PR helps a bit in that direction (and is clearly wanted by the feature request). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thank you for your contribution!
In addition to my inline comment, I would love to include some test for admin/users
endpoint.
More specifically I would like to add the following tests in admin_users_test.go
:
- create user without providing
orgId
- create user by providing
orgId
- create user by providing non-existing
orgId
. To do so you the mock the response ofCreateUserCommand
to returnErrOrgNotFound
similarly to what happens here.
Also for adding the above tests you need to implement a function
func adminCreateUserScenario(desc string, url string, routePattern string, cmd dtos.AdminCreateUserForm, fn scenarioFunc)
similar to the other scenarios defined in admin_users_test.go
.
As an POST example you can check this one.
Please feel free to ping me if you need some additional information.
pkg/services/sqlstore/user_test.go
Outdated
So(err, ShouldNotBeNil) | ||
}) | ||
|
||
setting.AutoAssignOrg = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could do this inside a defer block at the beginning of the test just after setting AutoAssignOrg
to true, like:
defer func() {
setting.AutoAssignOrg = false
}()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@papagian agree, but maybe it would be better to restore to the original value of setting.AutoAssignOrg
, just in case it's true
? Assumptions can be dangerous.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point, that would be even better.
This pull request has been automatically marked as stale because it has not had activity in the last 2 weeks. It will be closed in 30 days if no further activity occurs. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions! |
Sorry had a lot going on, I will try to get it do tomorrow |
Thanks! |
I'm not sure what happened here but it seems like there has been some kind of mistake of keeping up with the master branch. Please fix this PR to only contain your commit or open a new PR with only your commit :) |
Yeah its weird, most likely related to the issues github had today. I will fix it. |
For the future, could you please avoid force pushing to PRs? The comments from previous reviews have been wiped. We squash PRs anyway on commits, so cleaning branch history is unnecessary. Edit: Ah, I guess it was in order to fix the issue mentioned by Carl. Never mind then. |
@marefr @bergquist Can you please weigh in on whether we want this new functionality? I remember we discussed before, but we didn't reach a conclusion I think. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delayed reply. Thanks for the modifications, I have some minor suggestions for fixing the tests.
pkg/api/admin_users_test.go
Outdated
userLogin = cmd.Login | ||
orgId = cmd.OrgId | ||
|
||
if orgId == 1000 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest to declare a const NonExistingOrgID
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@papagian Why should the const be exported? I.e., if it's capitalized it's exported. We should avoid exporting symbols unless there's a real need for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no reason to be exported
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case, it should be named nonExistingOrgID
:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry for being unclear; I thought it goes without saying
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah never mind, I just wanted to be explicit so there's no room for misunderstanding.
pkg/services/sqlstore/user_test.go
Outdated
} | ||
|
||
err := CreateUser(context.Background(), cmd) | ||
So(err, ShouldNotBeNil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additionally, you can check that the error is ErrOrgNotFound
pkg/api/admin_users_test.go
Outdated
createCmd := dtos.AdminCreateUserForm{ | ||
Login: TestLogin, | ||
Password: TestPassword, | ||
OrgId: 1000, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest using the constant nonExistingOrgID
@papagian thanks for the review, I responded where I don't agree. I will fix the other comments. I will wait for your reply! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, although we should resolve the discussions.
This pull request has been automatically marked as stale because it has not had activity in the last 2 weeks. It will be closed in 30 days if no further activity occurs. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions! |
no worries I will get to fix it, didnt have time lately with covid and everything |
@Sytten Great to hear that you're still up for it. I hope your safe given current circumstances. No rush. take your time :) |
This pull request has been automatically marked as stale because it has not had activity in the last 2 weeks. It will be closed in 30 days if no further activity occurs. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions! |
Go away stale bot! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
What this PR does / why we need it:
With this PR, the API can now create user and directly assign it to an organization. Previously, we had to:
This is very common task for multi-tenant grafana and it is currently prone to errors and edge cases (for example a user could get stuck without any org).
Which issue(s) this PR fixes:
Fixes #7114