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

Grafana api #86

Merged
merged 8 commits into from
Oct 25, 2016
Merged

Grafana api #86

merged 8 commits into from
Oct 25, 2016

Conversation

davideme
Copy link
Member

📌 References

🎩 What is the goal?

When user login for the first time it create his user in grafana in addition of the actual flowup db.
Also if his orgs is configure with a google domain, it will automatically join the org.

How is it being implemented?

Using the Grafana API and Play authenticate callback

Copy link
Member

@pedrovgs pedrovgs left a comment

Choose a reason for hiding this comment

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

Good job :)

public GrafanaClient(WSClient ws, @Named("grafana") Configuration grafanaConf) {
this.ws = ws;

this.apiKey = grafanaConf.getString("api_key");
Copy link
Member

Choose a reason for hiding this comment

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

So are we going to use the api_key at the end?

Copy link
Member Author

Choose a reason for hiding this comment

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

I would like to, but there is something wrong in grafana that does not let me.
I will open a ticket and fix that later.

Copy link
Member

Choose a reason for hiding this comment

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

ok

String scheme = grafanaConf.getString("scheme");
String host = grafanaConf.getString("host");
String port = grafanaConf.getString("port");
this.baseUrl = scheme + "://" + host + ":" + port;
Copy link
Member

Choose a reason for hiding this comment

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

Java has a URL class you can use if I'm not wrong.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes but the Play client only accept String, I don't know if it's worth the double transformation, String -> URL -> String

Copy link
Member

Choose a reason for hiding this comment

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

You can pass from string to URL and URL to string. The only benefit you are going to have is validating the URL in construction, but it's not so important here :)


return getWsRequestForUserCreation(adminUserEndpoint).post(userRequest).thenApply(response -> {
Logger.debug(response.getBody());
if (response.getStatus() == Http.Status.OK) {
Copy link
Member

Choose a reason for hiding this comment

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

Don't we need to do something if the request fails?

Choose a reason for hiding this comment

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

sound good move this ok to a method no?

Copy link
Member Author

Choose a reason for hiding this comment

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

@pedrovgs I created a ticket #88, for the MVP I don't want to handle the failure of grafana api.
@flipper83 👍

}

public CompletionStage<GrafanaResponse> deleteUserInDefaultOrganisation(User user) {
String adminUserEndpoint = "/api/orgs/:orgId/users/:userId".replaceFirst(":orgId", "1").replaceFirst(":userId", user.getGrafanaUserId());
Copy link
Member

Choose a reason for hiding this comment

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

As this string is repeated in some methods we could extract it into a constant.

Copy link
Member Author

@davideme davideme Oct 25, 2016

Choose a reason for hiding this comment

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

👍


ObjectNode userRequest = Json.newObject()
.put("loginOrEmail", user.getEmail())
.put("role", "Viewer");
Copy link
Member

Choose a reason for hiding this comment

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

If you don't want to create json manually you can create a class as you tend to do with the responses :)

Copy link
Member Author

Choose a reason for hiding this comment

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

I no strong opinion about that, coming from swift, this seems fine, until I need this model more than once.

}

@NotNull
private String generatePassword() {
Copy link
Member

Choose a reason for hiding this comment

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

Nice password generator!! I'd move this method to a PasswordGeneratorclass just to don't mix responsibilities between this api client and the pass generator :)

}

private void addUserToGrafanaOrg(User user) {
String[] split = user.getEmail().split("@");
Copy link
Member

Choose a reason for hiding this comment

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

If our user has a gmail email is going to share the org with other gmail users?

Copy link
Member Author

@davideme davideme Oct 25, 2016

Choose a reason for hiding this comment

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

No, domain should be set in Org configuration.
We will only allow domain, that are not @gmail.com or @googlemail.com

Copy link
Member Author

Choose a reason for hiding this comment

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

That's for a future PR.

Copy link
Member

Choose a reason for hiding this comment

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

ok, but if we only allow domains that are not @gmail.com how is a regular user w//o working at any organization going to test the platform?

Copy link

@flipper83 flipper83 left a comment

Choose a reason for hiding this comment

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

Good job, you are a Java expert developer bro!

@@ -27,5 +27,10 @@ protected void configure() {
bind(MetricsDatasource.class)
.to(ElasticSearchDatasource.class)
.asEagerSingleton();

Configuration grafanaConf = configuration.getConfig("grafana");

Choose a reason for hiding this comment

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

have sense move this grafana literal to a constant?

Copy link
Member Author

Choose a reason for hiding this comment

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

I will do it, it feels like a java thing to me :D
have sense

this.baseUrl = scheme + "://" + host + ":" + port;
}

public CompletionStage<GrafanaResponse> createUser(final User user) {

Choose a reason for hiding this comment

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

are you using any kind of checkstyle? do you have a typo here, a double space between public and return value.

Copy link
Member Author

Choose a reason for hiding this comment

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

There is no checkstyle in this project I will add one later.

Choose a reason for hiding this comment

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

try to add as soon as possible because fix check style after having a lot of code could be a pain in the ass. @pedrovgs or I can help you with this one.


String grafanaPassword = generatePassword();
ObjectNode userRequest = Json.newObject()
.put("name", user.getName())

Choose a reason for hiding this comment

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

have sense move this request properties to constants?


return getWsRequestForUserCreation(adminUserEndpoint).post(userRequest).thenApply(response -> {
Logger.debug(response.getBody());
if (response.getStatus() == Http.Status.OK) {

Choose a reason for hiding this comment

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

sound good move this ok to a method no?

}

public CompletionStage<GrafanaResponse> addUserToOrganisation(User user, Organization organization) {
String adminUserEndpoint = "/api/orgs/:orgId/users".replaceFirst(":orgId", organization.grafanaId);

Choose a reason for hiding this comment

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

can you move those urls to a constants?


private void addUserToGrafanaOrg(User user) {
String[] split = user.getEmail().split("@");
if (split.length == 2) {

Choose a reason for hiding this comment

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

can you give semantic to this 2 with a constant?

@davideme davideme merged commit 02dd15e into master Oct 25, 2016
@davideme davideme deleted the grafana-api branch October 25, 2016 12:39
@davideme davideme mentioned this pull request Oct 25, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create User in graphana when created on flowup Add User to a org and equivalent in graphana via api request
3 participants