-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathsetup.go
130 lines (123 loc) · 3.68 KB
/
setup.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package influxdb
import (
"bytes"
"context"
"encoding/json"
"errors"
"net/http"
)
// Setup sets up a new influxdb server.
// It requires a client be set up with a username and password.
// If successful will add a token to the client.
// RetentionPeriodHrs of zero will result in infinite retention.
func (c *Client) Setup(ctx context.Context, bucket, org string, retentionPeriodHrs int) (*SetupResult, error) {
if c.username == "" || c.password == "" {
return nil, errors.New("a username and password is requred for a setup")
}
inputData, err := json.Marshal(SetupRequest{
Username: c.username,
Password: c.password,
Org: org,
Bucket: bucket,
RetentionPeriodHrs: retentionPeriodHrs,
})
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", c.url.String()+"/setup", bytes.NewBuffer(inputData))
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
req.Header.Add("Content-Type", "application/json; charset=utf-8")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
setupResult := &SetupResult{}
if err := json.NewDecoder(resp.Body).Decode(setupResult); err != nil {
return nil, err
}
if setupResult.Code != "conflict" && resp.StatusCode == http.StatusCreated && setupResult.Auth.Token != "" {
c.l.Lock()
c.authorization = "Token " + setupResult.Auth.Token
c.l.Unlock()
}
return setupResult, nil
}
// SetupRequest is a request to setup a new influx instance.
type SetupRequest struct {
Username string `json:"username"`
Password string `json:"password"`
Org string `json:"org"`
Bucket string `json:"bucket"`
RetentionPeriodHrs int `json:"retentionPeriodHrs"`
}
// SetupResult is the result of setting up a new influx instance.
type SetupResult struct {
Code string `json:"code"`
Message string `json:"message"`
User struct {
Links struct {
Logs string `json:"logs"`
Self string `json:"self"`
} `json:"links"`
ID string `json:"id"`
Name string `json:"name"`
} `json:"user"`
Bucket struct {
ID string `json:"id"`
OrganizationID string `json:"organizationID"`
Organization string `json:"organization"`
Name string `json:"name"`
RetentionRules []struct {
Type string `json:"type"`
EverySeconds int `json:"everySeconds"`
} `json:"retentionRules"`
Links struct {
Labels string `json:"labels"`
Logs string `json:"logs"`
Members string `json:"members"`
Org string `json:"org"`
Owners string `json:"owners"`
Self string `json:"self"`
Write string `json:"write"`
} `json:"links"`
} `json:"bucket"`
Org struct {
Links struct {
Buckets string `json:"buckets"`
Dashboards string `json:"dashboards"`
Labels string `json:"labels"`
Logs string `json:"logs"`
Members string `json:"members"`
Owners string `json:"owners"`
Secrets string `json:"secrets"`
Self string `json:"self"`
Tasks string `json:"tasks"`
} `json:"links"`
ID string `json:"id"`
Name string `json:"name"`
} `json:"org"`
Auth struct {
ID string `json:"id"`
Token string `json:"token"`
Status string `json:"status"`
Description string `json:"description"`
OrgID string `json:"orgID"`
Org string `json:"org"`
UserID string `json:"userID"`
User string `json:"user"`
Permissions []struct {
Action string `json:"action"`
Resource struct {
Type string `json:"type"`
} `json:"resource"`
} `json:"permissions"`
Links struct {
Self string `json:"self"`
User string `json:"user"`
} `json:"links"`
} `json:"auth"`
}