Navigation Menu

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

HTTP API: Allow assigning a specific organization when creating a new user #21775

Merged
merged 5 commits into from Apr 15, 2020
Merged

HTTP API: Allow assigning a specific organization when creating a new user #21775

merged 5 commits into from Apr 15, 2020

Conversation

Sytten
Copy link
Contributor

@Sytten Sytten commented Jan 28, 2020

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:

  1. Create the user
  2. Remove it from the main org
  3. Add it to another organization

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

Copy link
Contributor

@aknuds1 aknuds1 left a 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.

@Sytten
Copy link
Contributor Author

Sytten commented Jan 28, 2020

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).

@Sytten
Copy link
Contributor Author

Sytten commented Feb 3, 2020

Any update? @papagian @marefr

Copy link
Contributor

@papagian papagian left a 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 of CreateUserCommand to return ErrOrgNotFound 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.

So(err, ShouldNotBeNil)
})

setting.AutoAssignOrg = false
Copy link
Contributor

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
}()

Copy link
Contributor

@aknuds1 aknuds1 Feb 10, 2020

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.

Copy link
Contributor

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.

@stale
Copy link

stale bot commented Feb 24, 2020

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!

@stale stale bot added the stale Issue with no recent activity label Feb 24, 2020
@aknuds1 aknuds1 self-assigned this Feb 24, 2020
@stale stale bot removed the stale Issue with no recent activity label Feb 24, 2020
@aknuds1
Copy link
Contributor

aknuds1 commented Feb 24, 2020

@Sytten Have you considered @papagian's requested changes?

@Sytten
Copy link
Contributor Author

Sytten commented Feb 25, 2020

Sorry had a lot going on, I will try to get it do tomorrow

@aknuds1
Copy link
Contributor

aknuds1 commented Feb 25, 2020

Thanks!

@bergquist
Copy link
Contributor

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 :)

@Sytten
Copy link
Contributor Author

Sytten commented Feb 28, 2020

Yeah its weird, most likely related to the issues github had today. I will fix it.

@Sytten
Copy link
Contributor Author

Sytten commented Feb 28, 2020

@aknuds1 @papagian If you can review the new tests and let me know if thats ok for you, thanks!

@Sytten
Copy link
Contributor Author

Sytten commented Mar 7, 2020

@aknuds1 @papagian just a small reminder of this PR

@aknuds1
Copy link
Contributor

aknuds1 commented Mar 9, 2020

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.

@aknuds1 aknuds1 requested a review from papagian March 9, 2020 13:55
@aknuds1
Copy link
Contributor

aknuds1 commented Mar 9, 2020

@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.

Copy link
Contributor

@papagian papagian left a 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.go Show resolved Hide resolved
userLogin = cmd.Login
orgId = cmd.OrgId

if orgId == 1000 {
Copy link
Contributor

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

Copy link
Contributor

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.

Copy link
Contributor

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

Copy link
Contributor

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 :)

Copy link
Contributor

@papagian papagian Mar 10, 2020

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

Copy link
Contributor

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.

}

err := CreateUser(context.Background(), cmd)
So(err, ShouldNotBeNil)
Copy link
Contributor

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 Show resolved Hide resolved
pkg/api/admin_users_test.go Show resolved Hide resolved
pkg/api/admin_users_test.go Show resolved Hide resolved
createCmd := dtos.AdminCreateUserForm{
Login: TestLogin,
Password: TestPassword,
OrgId: 1000,
Copy link
Contributor

@papagian papagian Mar 9, 2020

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

pkg/api/admin_users_test.go Show resolved Hide resolved
pkg/api/admin_users_test.go Show resolved Hide resolved
pkg/api/admin_users_test.go Show resolved Hide resolved
@Sytten
Copy link
Contributor Author

Sytten commented Mar 9, 2020

@papagian thanks for the review, I responded where I don't agree. I will fix the other comments. I will wait for your reply!

Copy link
Contributor

@aknuds1 aknuds1 left a 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.

@stale
Copy link

stale bot commented Mar 24, 2020

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!

@stale stale bot added the stale Issue with no recent activity label Mar 24, 2020
@Sytten
Copy link
Contributor Author

Sytten commented Mar 24, 2020

no worries I will get to fix it, didnt have time lately with covid and everything

@stale stale bot removed the stale Issue with no recent activity label Mar 24, 2020
@bergquist
Copy link
Contributor

@Sytten Great to hear that you're still up for it. I hope your safe given current circumstances. No rush. take your time :)

@stale
Copy link

stale bot commented Apr 7, 2020

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!

@stale stale bot added the stale Issue with no recent activity label Apr 7, 2020
@bergquist
Copy link
Contributor

Go away stale bot!

@stale stale bot removed the stale Issue with no recent activity label Apr 7, 2020
@Sytten Sytten requested a review from a team as a code owner April 15, 2020 08:24
Copy link
Contributor

@aknuds1 aknuds1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@papagian papagian added this to the 7.0 milestone Apr 15, 2020
@papagian papagian merged commit d721dd1 into grafana:master Apr 15, 2020
@papagian papagian added this to Done in Backend Platform Squad via automation Apr 15, 2020
@marefr marefr changed the title Allow API to assign new user to a specific organization HTTP API: Allow assigning a specific organization when creating a new user Apr 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

[Feature request] - when creating users via API allow association with existing (non-default) org
4 participants