-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
066ebcd
commit af1d316
Showing
11 changed files
with
181 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
provider "netdata" { | ||
url = "https://app.netdata.cloud" | ||
authtoken = "authtoken" | ||
authtoken = "<authtoken>" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package client | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
var ErrNotFound = errors.New("not found") | ||
|
||
type Client struct { | ||
HostURL string | ||
HTTPClient *http.Client | ||
AuthToken string | ||
} | ||
|
||
func NewClient(url, authtoken string) *Client { | ||
c := Client{ | ||
HostURL: url, | ||
AuthToken: "Bearer " + authtoken, | ||
HTTPClient: &http.Client{Timeout: 10 * time.Second}, | ||
} | ||
|
||
return &c | ||
} | ||
|
||
func (c *Client) doRequest(req *http.Request) ([]byte, error) { | ||
req.Header.Set("Authorization", c.AuthToken) | ||
req.Header.Set("Accept", "application/json") | ||
|
||
res, err := c.HTTPClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer res.Body.Close() | ||
|
||
body, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
statusOK := res.StatusCode >= 200 && res.StatusCode < 300 | ||
if !statusOK { | ||
return nil, fmt.Errorf("status: %d, body: %s", res.StatusCode, body) | ||
} | ||
|
||
return body, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package client | ||
|
||
type SpaceInfo struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
func (c *Client) GetSpaces() (*[]SpaceInfo, error) { | ||
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/v3/spaces", c.HostURL), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := c.doRequest(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var spaces []SpaceInfo | ||
err = json.Unmarshal(body, &spaces) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &spaces, nil | ||
} | ||
|
||
func (c *Client) GetSpaceByID(id string) (*SpaceInfo, error) { | ||
spaces, err := c.GetSpaces() | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, space := range *spaces { | ||
if space.ID == id { | ||
return &space, nil | ||
} | ||
} | ||
return nil, ErrNotFound | ||
} | ||
|
||
func (c *Client) CreateSpace(name, description string) (*SpaceInfo, error) { | ||
reqBody, err := json.Marshal(map[string]string{"name": name}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req, err := http.NewRequest("POST", fmt.Sprintf("%s/api/v1/spaces", c.HostURL), strings.NewReader(string(reqBody))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
respBody, err := c.doRequest(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var space SpaceInfo | ||
err = json.Unmarshal(respBody, &space) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = c.UpdateSpaceByID(space.ID, name, description) | ||
if err != nil { | ||
return nil, err | ||
} | ||
space.Name = name | ||
space.Description = description | ||
|
||
return &space, nil | ||
} | ||
|
||
func (c *Client) UpdateSpaceByID(id, name, description string) error { | ||
if id == "" { | ||
return fmt.Errorf("id is empty") | ||
} | ||
reqBody, err := json.Marshal(map[string]string{ | ||
"name": name, | ||
"description": description, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
req, err := http.NewRequest("PATCH", fmt.Sprintf("%s/api/v1/spaces/%s", c.HostURL, id), strings.NewReader(string(reqBody))) | ||
if err != nil { | ||
return fmt.Errorf("req %+v", req) | ||
} | ||
_, err = c.doRequest(req) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (c *Client) DeleteSpaceByID(id string) error { | ||
if id == "" { | ||
return fmt.Errorf("id is empty") | ||
} | ||
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/api/v1/spaces/%s", c.HostURL, id), nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = c.doRequest(req) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters