forked from influxdata/influxdb-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
67 lines (63 loc) · 1.79 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
// Copyright 2020 InfluxData, Inc. All rights reserved.
// Use of this source code is governed by MIT
// license that can be found in the LICENSE file.
package influxdb2
import (
"bytes"
"context"
"encoding/json"
"errors"
"github.com/influxdata/influxdb-client-go/domain"
"log"
"net/http"
"net/url"
"path"
)
func (c *clientImpl) Setup(ctx context.Context, username, password, org, bucket string, retentionPeriodHours int) (*domain.OnboardingResponse, error) {
if username == "" || password == "" {
return nil, errors.New("a username and password is required for a setup")
}
c.lock.Lock()
defer c.lock.Unlock()
var setupResult *domain.OnboardingResponse
inputData, err := json.Marshal(domain.OnboardingRequest{
Username: username,
Password: password,
Org: org,
Bucket: bucket,
RetentionPeriodHrs: &retentionPeriodHours,
})
if err != nil {
return nil, err
}
if c.options.LogLevel() > 2 {
log.Printf("D! Request:\n%s\n", string(inputData))
}
u, err := url.Parse(c.httpService.ServerApiUrl())
if err != nil {
return nil, err
}
u.Path = path.Join(u.Path, "setup")
perror := c.httpService.PostRequest(ctx, u.String(), bytes.NewReader(inputData), func(req *http.Request) {
req.Header.Add("Content-Type", "application/json; charset=utf-8")
},
func(resp *http.Response) error {
defer func() {
_ = resp.Body.Close()
}()
setupResponse := &domain.OnboardingResponse{}
if err := json.NewDecoder(resp.Body).Decode(setupResponse); err != nil {
return err
}
setupResult = setupResponse
if setupResponse.Auth != nil && *setupResponse.Auth.Token != "" {
c.httpService.SetAuthorization("Token " + *setupResponse.Auth.Token)
}
return nil
},
)
if perror != nil {
return nil, perror
}
return setupResult, nil
}