Skip to content

Commit

Permalink
Add draft for REST API calls for current org.
Browse files Browse the repository at this point in the history
  • Loading branch information
grafov committed Feb 12, 2017
1 parent 8b456ae commit 61cebe3
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 7 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ with Grafana then client SDK for Go will be uniquely useful.

## Library design principles

1. SDK offers client functionality so it covers Grafana REST API with its requests and responses as close as possible.
1. SDK maps Grafana objects (dashboard, row, panel, datasource) to similar Go structures but not follows exactly all Grafana abstractions.
1. It doesn't use logging, instead API functions can return errors where it need.
1. No external deps except Go stdlib. Another exception is URL slugify, SDK uses external lib "slug" for algorithm compatibility — that is the same package that Grafana server uses.
1. SDK offers client functionality so it covers Grafana REST API with
its requests and responses as close as possible.
1. SDK maps Grafana objects (dashboard, row, panel, datasource) to
similar Go structures but not follows exactly all Grafana
abstractions.
1. It doesn't use logging, instead API functions can return errors
where it need.
1. No external deps except Go stdlib. Another exception is URL
slugify, SDK uses external lib "slug" for algorithm compatibility —
that is the same package that Grafana server uses.

## Examples [![GoDoc](https://godoc.org/github.com/grafana-tools/sdk?status.svg)](https://godoc.org/github.com/grafana-tools/sdk)

Expand Down Expand Up @@ -104,7 +110,7 @@ datasources. State of support for misc API parts noted below.
| Authorization | only API tokens |
| Dashboards | partially |
| Datasources | + |
| Organization (current) | - |
| Organization (current) | partially |
| Organizations | - |
| Users | partially |
| User (actual) | partially |
Expand Down
9 changes: 7 additions & 2 deletions org.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ package sdk
*/

type Org struct {
Id int64
Name string
Id int64 `json:"id"`
Name string `json:"name"`
}

type OrgUser struct {
LoginOrEmail *string `json:"loginOrEmail,omitempty"`
Role *string `json:"role,omitempty"`
}
108 changes: 108 additions & 0 deletions rest-org.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package sdk

// +build draft

/*
Copyright 2016-2017 Alexander I.Grafov <grafov@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
ॐ तारे तुत्तारे तुरे स्व
*/

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)

// GetActualOrg gets current organization.
// It reflects GET /api/org API call.
func (r *Client) GetActualOrg() (Org, error) {
var (
raw []byte
org Org
code int
err error
)
if raw, code, err = r.get("api/org", nil); err != nil {
return org, err
}
if code != http.StatusOK {
return org, fmt.Errorf("HTTP error %d: returns %s", code, raw)
}
dec := json.NewDecoder(bytes.NewReader(raw))
dec.UseNumber()
if err := dec.Decode(&org); err != nil {
return org, fmt.Errorf("unmarshal org: %s\n%s", err, raw)
}
return org, err
}

// UpdateActualOrg updates current organization.
// It reflects PUT /api/org API call.
func (r *Client) UpdateActualOrg(org Org) (StatusMessage, error) {
var (
raw []byte
resp StatusMessage
err error
)
if raw, err = json.Marshal(org); err != nil {
return StatusMessage{}, err
}
if raw, _, err = r.put("api/org", nil, raw); err != nil {
return StatusMessage{}, err
}
if err = json.Unmarshal(raw, &resp); err != nil {
return StatusMessage{}, err
}
return resp, nil
}

// GetActualOrgUsers get all users within the actual organisation.
func (r *Client) GetActualOrgUsers() ([]User, error) {
var (
raw []byte
users []User
code int
err error
)
if raw, code, err = r.get("api/org/users", nil); err != nil {
return nil, err
}
if code != http.StatusOK {
return nil, fmt.Errorf("HTTP error %d: returns %s", code, raw)
}
dec := json.NewDecoder(bytes.NewReader(raw))
dec.UseNumber()
if err := dec.Decode(&users); err != nil {
return nil, fmt.Errorf("unmarshal org: %s\n%s", err, raw)
}
return users, err
}

// DeleteActualOrgUser delete user in actual organisation.
// It reflects DELETE /api/org/users/:userId API call.
func (r *Client) DeleteActualOrgUser(uid uint) (StatusMessage, error) {
var (
raw []byte
reply StatusMessage
err error
)
if raw, _, err = r.delete(fmt.Sprintf("api/org/%d", uid)); err != nil {
return StatusMessage{}, err
}
err = json.Unmarshal(raw, &reply)
return reply, err
}

0 comments on commit 61cebe3

Please sign in to comment.