/
explorer.go
61 lines (53 loc) · 1.76 KB
/
explorer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package client
import (
"fmt"
"net/http"
"github.com/go-resty/resty/v2"
"github.com/rs/zerolog/log"
)
// ExplorerClient is used to call Explorer API endpoints
type ExplorerClient struct {
APIClient *resty.Client
Config *ExplorerConfig
}
// NewExplorerClient creates a new explorer mock client
func NewExplorerClient(cfg *ExplorerConfig) *ExplorerClient {
return &ExplorerClient{
Config: cfg,
APIClient: resty.New().SetBaseURL(cfg.URL),
}
}
// PostAdminNodes is used to exercise the POST /api/v1/admin/nodes endpoint
// This endpoint is used to create access keys for nodes
func (em *ExplorerClient) PostAdminNodes(nodeName string) (NodeAccessKeys, error) {
em.APIClient.SetHeaders(map[string]string{
"x-explore-admin-password": em.Config.AdminPassword,
"x-explore-admin-username": em.Config.AdminUsername,
"Content-Type": "application/json",
"Accept": "application/json",
})
requestBody := &Name{Name: nodeName}
responseBody := NodeAccessKeys{}
log.Info().Str("Explorer URL", em.Config.URL).Msg("Creating node credentials")
resp, err := em.APIClient.R().SetBody(requestBody).SetResult(&responseBody).Post("/api/v1/admin/nodes")
if resp.StatusCode() != http.StatusCreated {
err = fmt.Errorf("Unexpected Status Code. Expected %d; Got %d", http.StatusCreated, resp.StatusCode())
}
return responseBody, err
}
// Name is the body of the request
type Name struct {
Name string `json:"name"`
}
// NodeAccessKeys is the body of the response
type NodeAccessKeys struct {
ID string `json:"id"`
AccessKey string `json:"accessKey"`
Secret string `json:"secret"`
}
// ExplorerConfig holds config information for ExplorerClient
type ExplorerConfig struct {
URL string
AdminUsername string
AdminPassword string
}