Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
7380665
Add customMetricsNamespaces into data structure (#1)
teemupo Feb 22, 2018
a2ec15b
Add method for fetching org membership
mlclmj Jun 18, 2018
1ad379d
Add methods to add/update/delete org users
mlclmj Jun 18, 2018
7fbea2b
Moved OrgUser methods to separate file
mlclmj Jun 18, 2018
04e16fb
Fix spacing issue
mlclmj Jun 18, 2018
4743232
Add method to retrieve org by name
mlclmj Jun 18, 2018
443facd
Better naming for JSON data
mlclmj Jun 20, 2018
0b75ce3
Add endpoint for updating orgs
mlclmj Jun 20, 2018
a33f220
Add endpoint for getting org by id
mlclmj Jun 20, 2018
9968a4a
Fix arg type
mlclmj Jun 20, 2018
09e688d
Add request method for supporting GET parameters
mlclmj Jun 26, 2018
e3d5e5a
Add method for getting a user by login or email
mlclmj Jun 26, 2018
c8a6813
Add logging of query parameters
mlclmj Jun 26, 2018
4e1736b
Fix variable name
mlclmj Jun 26, 2018
9286081
Add logging
mlclmj Jun 27, 2018
7a4d445
Add generic method to create a new user
mlclmj Jun 27, 2018
646a34a
Return the orgId for newly created orgs
mlclmj Jun 27, 2018
a55e687
Add tests for orgs
mlclmj Jun 27, 2018
bc8a142
Add org_users tests
mlclmj Jun 28, 2018
266ab81
Fix JSON decoding
mlclmj Jun 28, 2018
4a01512
Add tests for user.go
mlclmj Jun 28, 2018
5857fb3
Better handling of create org response
mlclmj Jun 28, 2018
d0e8099
Add JSON definitions for struct fields
mlclmj Jun 28, 2018
f5ae1b6
Return id of newly created user
mlclmj Jun 28, 2018
2af2767
Add testing output and tests for admin.go
mlclmj Jun 28, 2018
1168099
Move fixtures into respective test files
mlclmj Jun 28, 2018
f63ea13
Remove unused imports
mlclmj Jun 28, 2018
f0238e7
Add JSON tags for field names
mlclmj Jun 28, 2018
3f9aece
Change variable name for consistency
mlclmj Jun 28, 2018
d437479
Rework request method
mlclmj Jul 6, 2018
2f8875a
Better request parameter handling
mlclmj Jul 9, 2018
a7599ef
Formatting fix
mlclmj Jul 9, 2018
9ac7c4a
Updated method for handling query parameters
mlclmj Jul 10, 2018
d1dd660
Remove dtos data struct
mlclmj Jul 10, 2018
0a62311
Go formatting
mlclmj Jul 12, 2018
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
30 changes: 18 additions & 12 deletions admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,38 @@ import (
"errors"
"fmt"
"io/ioutil"

"github.com/grafana/grafana/pkg/api/dtos"
)

func (c *Client) CreateUserForm(settings dtos.AdminCreateUserForm) error {
data, err := json.Marshal(settings)
req, err := c.newRequest("POST", "/api/admin/users", bytes.NewBuffer(data))
func (c *Client) CreateUser(user User) (int64, error) {
id := int64(0)
data, err := json.Marshal(user)
req, err := c.newRequest("POST", "/api/admin/users", nil, bytes.NewBuffer(data))
if err != nil {
return err
return id, err
}
resp, err := c.Do(req)
if err != nil {
return err
return id, err
}
if resp.StatusCode != 200 {
return id, errors.New(resp.Status)
}
data, err = ioutil.ReadAll(resp.Body)
if err != nil {
return err
return id, err
}
if resp.StatusCode != 200 {
return errors.New(resp.Status)
created := struct {
Id int64 `json:"id"`
}{}
err = json.Unmarshal(data, &created)
if err != nil {
return id, err
}
return err
return created.Id, err
}

func (c *Client) DeleteUser(id int64) error {
req, err := c.newRequest("DELETE", fmt.Sprintf("/api/admin/users/%d", id), nil)
req, err := c.newRequest("DELETE", fmt.Sprintf("/api/admin/users/%d", id), nil, nil)
if err != nil {
return err
}
Expand Down
39 changes: 39 additions & 0 deletions admin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package gapi

import (
"testing"
)

const (
createUserJSON = `{"id":1,"message":"User created"}`
deleteUserJSON = `{"message":"User deleted"}`
)

func TestCreateUser(t *testing.T) {
server, client := gapiTestTools(200, createUserJSON)
defer server.Close()
user := User{
Email: "admin@localhost",
Login: "admin",
Name: "Administrator",
Password: "password",
}
resp, err := client.CreateUser(user)
if err != nil {
t.Error(err)
}

if resp != 1 {
t.Error("Not correctly parsing returned user message.")
}
}

func TestDeleteUser(t *testing.T) {
server, client := gapiTestTools(200, deleteUserJSON)
defer server.Close()

err := client.DeleteUser(int64(1))
if err != nil {
t.Error(err)
}
}
18 changes: 9 additions & 9 deletions alertnotification.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import (
)

type AlertNotification struct {
Id int64 `json:"id,omitempty"`
Name string `json:"name"`
Type string `json:"type"`
IsDefault bool `json:"isDefault"`
Settings interface{} `json:"settings"`
Id int64 `json:"id,omitempty"`
Name string `json:"name"`
Type string `json:"type"`
IsDefault bool `json:"isDefault"`
Settings interface{} `json:"settings"`
}

func (c *Client) AlertNotification(id int64) (*AlertNotification, error) {
path := fmt.Sprintf("/api/alert-notifications/%d", id)
req, err := c.newRequest("GET", path, nil)
req, err := c.newRequest("GET", path, nil, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -46,7 +46,7 @@ func (c *Client) NewAlertNotification(a *AlertNotification) (int64, error) {
if err != nil {
return 0, err
}
req, err := c.newRequest("POST", "/api/alert-notifications", bytes.NewBuffer(data))
req, err := c.newRequest("POST", "/api/alert-notifications", nil, bytes.NewBuffer(data))
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -77,7 +77,7 @@ func (c *Client) UpdateAlertNotification(a *AlertNotification) error {
if err != nil {
return err
}
req, err := c.newRequest("PUT", path, bytes.NewBuffer(data))
req, err := c.newRequest("PUT", path, nil, bytes.NewBuffer(data))
if err != nil {
return err
}
Expand All @@ -95,7 +95,7 @@ func (c *Client) UpdateAlertNotification(a *AlertNotification) error {

func (c *Client) DeleteAlertNotification(id int64) error {
path := fmt.Sprintf("/api/alert-notifications/%d", id)
req, err := c.newRequest("DELETE", path, nil)
req, err := c.newRequest("DELETE", path, nil, nil)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ func New(auth, baseURL string) (*Client, error) {
}, nil
}

func (c *Client) newRequest(method, requestPath string, body io.Reader) (*http.Request, error) {
func (c *Client) newRequest(method, requestPath string, query url.Values, body io.Reader) (*http.Request, error) {
url := c.baseURL
url.Path = path.Join(url.Path, requestPath)
url.RawQuery = query.Encode()
req, err := http.NewRequest(method, url.String(), body)
if err != nil {
return req, err
Expand Down
6 changes: 3 additions & 3 deletions dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (c *Client) SaveDashboard(model map[string]interface{}, overwrite bool) (*D
if err != nil {
return nil, err
}
req, err := c.newRequest("POST", "/api/dashboards/db", bytes.NewBuffer(data))
req, err := c.newRequest("POST", "/api/dashboards/db", nil, bytes.NewBuffer(data))
if err != nil {
return nil, err
}
Expand All @@ -58,7 +58,7 @@ func (c *Client) SaveDashboard(model map[string]interface{}, overwrite bool) (*D

func (c *Client) Dashboard(slug string) (*Dashboard, error) {
path := fmt.Sprintf("/api/dashboards/db/%s", slug)
req, err := c.newRequest("GET", path, nil)
req, err := c.newRequest("GET", path, nil, nil)
if err != nil {
return nil, err
}
Expand All @@ -83,7 +83,7 @@ func (c *Client) Dashboard(slug string) (*Dashboard, error) {

func (c *Client) DeleteDashboard(slug string) error {
path := fmt.Sprintf("/api/dashboards/db/%s", slug)
req, err := c.newRequest("DELETE", path, nil)
req, err := c.newRequest("DELETE", path, nil, nil)
if err != nil {
return err
}
Expand Down
15 changes: 8 additions & 7 deletions datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ type DataSource struct {

// JSONData is a representation of the datasource `jsonData` property
type JSONData struct {
AssumeRoleArn string `json:"assumeRoleArn,omitempty"`
AuthType string `json:"authType,omitempty"`
DefaultRegion string `json:"defaultRegion,omitempty"`
AssumeRoleArn string `json:"assumeRoleArn,omitempty"`
AuthType string `json:"authType,omitempty"`
CustomMetricsNamespaces string `json:"customMetricsNamespaces,omitempty"`
DefaultRegion string `json:"defaultRegion,omitempty"`
}

// SecureJSONData is a representation of the datasource `secureJsonData` property
Expand All @@ -48,7 +49,7 @@ func (c *Client) NewDataSource(s *DataSource) (int64, error) {
if err != nil {
return 0, err
}
req, err := c.newRequest("POST", "/api/datasources", bytes.NewBuffer(data))
req, err := c.newRequest("POST", "/api/datasources", nil, bytes.NewBuffer(data))
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -79,7 +80,7 @@ func (c *Client) UpdateDataSource(s *DataSource) error {
if err != nil {
return err
}
req, err := c.newRequest("PUT", path, bytes.NewBuffer(data))
req, err := c.newRequest("PUT", path, nil, bytes.NewBuffer(data))
if err != nil {
return err
}
Expand All @@ -97,7 +98,7 @@ func (c *Client) UpdateDataSource(s *DataSource) error {

func (c *Client) DataSource(id int64) (*DataSource, error) {
path := fmt.Sprintf("/api/datasources/%d", id)
req, err := c.newRequest("GET", path, nil)
req, err := c.newRequest("GET", path, nil, nil)
if err != nil {
return nil, err
}
Expand All @@ -122,7 +123,7 @@ func (c *Client) DataSource(id int64) (*DataSource, error) {

func (c *Client) DeleteDataSource(id int64) error {
path := fmt.Sprintf("/api/datasources/%d", id)
req, err := c.newRequest("DELETE", path, nil)
req, err := c.newRequest("DELETE", path, nil, nil)
if err != nil {
return err
}
Expand Down
11 changes: 8 additions & 3 deletions datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"github.com/gobs/pretty"
)

const (
createdDataSourceJSON = `{"id":1,"message":"Datasource added", "name": "test_datasource"}`
)

func gapiTestTools(code int, body string) (*httptest.Server, *Client) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(code)
Expand Down Expand Up @@ -46,9 +50,10 @@ func TestNewDataSource(t *testing.T) {
Access: "access",
IsDefault: true,
JSONData: JSONData{
AssumeRoleArn: "arn:aws:iam::123:role/some-role",
AuthType: "keys",
DefaultRegion: "us-east-1",
AssumeRoleArn: "arn:aws:iam::123:role/some-role",
AuthType: "keys",
CustomMetricsNamespaces: "SomeNamespace",
DefaultRegion: "us-east-1",
},
SecureJSONData: SecureJSONData{
AccessKey: "123",
Expand Down
5 changes: 0 additions & 5 deletions grafana_fixtures_test.go

This file was deleted.

95 changes: 95 additions & 0 deletions org_users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package gapi

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
)

type OrgUser struct {
OrgId int64 `json:"orgId"`
UserId int64 `json:"userId"`
Email string `json:"email"`
Login string `json:"login"`
Role string `json:"role"`
}

func (c *Client) OrgUsers(orgId int64) ([]OrgUser, error) {
users := make([]OrgUser, 0)
req, err := c.newRequest("GET", fmt.Sprintf("/api/orgs/%d/users", orgId), nil, nil)
if err != nil {
return users, err
}
resp, err := c.Do(req)
if err != nil {
return users, err
}
if resp.StatusCode != 200 {
return users, errors.New(resp.Status)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return users, err
}
err = json.Unmarshal(data, &users)
if err != nil {
return users, err
}
return users, err
}

func (c *Client) AddOrgUser(orgId int64, user, role string) error {
dataMap := map[string]string{
"loginOrEmail": user,
"role": role,
}
data, err := json.Marshal(dataMap)
req, err := c.newRequest("POST", fmt.Sprintf("/api/orgs/%d/users", orgId), nil, bytes.NewBuffer(data))
if err != nil {
return err
}
resp, err := c.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return errors.New(resp.Status)
}
return err
}

func (c *Client) UpdateOrgUser(orgId, userId int64, role string) error {
dataMap := map[string]string{
"role": role,
}
data, err := json.Marshal(dataMap)
req, err := c.newRequest("PATCH", fmt.Sprintf("/api/orgs/%d/users/%d", orgId, userId), nil, bytes.NewBuffer(data))
if err != nil {
return err
}
resp, err := c.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return errors.New(resp.Status)
}
return err
}

func (c *Client) RemoveOrgUser(orgId, userId int64) error {
req, err := c.newRequest("DELETE", fmt.Sprintf("/api/orgs/%d/users/%d", orgId, userId), nil, nil)
if err != nil {
return err
}
resp, err := c.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return errors.New(resp.Status)
}
return err
}
Loading