-
Notifications
You must be signed in to change notification settings - Fork 131
/
client.go
134 lines (116 loc) · 2.61 KB
/
client.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
131
132
133
134
package sls
import (
"encoding/json"
"fmt"
)
// Error defines sls error
type Error struct {
Code string `json:"errorCode"`
Message string `json:"errorMessage"`
RequestID string `json:"requestID"`
}
// NewClientError new client error
func NewClientError(message string) *Error {
err := new(Error)
err.Code = "ClientError"
err.Message = message
return err
}
func (e Error) String() string {
b, err := json.MarshalIndent(e, "", " ")
if err != nil {
return ""
}
return string(b)
}
func (e Error) Error() string {
return e.String()
}
// Client ...
type Client struct {
Endpoint string // IP or hostname of SLS endpoint
AccessKeyID string
AccessKeySecret string
SecurityToken string
}
func convert(c *Client, projName string) *LogProject {
return &LogProject{
Name: projName,
Endpoint: c.Endpoint,
AccessKeyID: c.AccessKeyID,
AccessKeySecret: c.AccessKeySecret,
SecurityToken: c.SecurityToken,
}
}
// CreateProject create a new loghub project.
func (c *Client) CreateProject(name, description string) (*LogProject, error) {
type Body struct {
ProjectName string `json:"projectName"`
Description string `json:"description"`
}
body, err := json.Marshal(Body{
ProjectName: name,
Description: description,
})
if err != nil {
return nil, err
}
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%d", len(body)),
"Content-Type": "application/json",
"Accept-Encoding": "deflate", // TODO: support lz4
}
uri := "/"
proj := convert(c, name)
_, err = request(proj, "POST", uri, h, body)
if err != nil {
return nil, err
}
return proj, nil
}
// GetProject ...
func (c *Client) GetProject(name string) (*LogProject, error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
uri := "/"
proj := convert(c, name)
_, err := request(proj, "GET", uri, h, nil)
if err != nil {
return nil, err
}
return proj, nil
}
// CheckProjectExist check project exist or not
func (c *Client) CheckProjectExist(name string) (bool, error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
uri := "/"
proj := convert(c, name)
_, err := request(proj, "GET", uri, h, nil)
if err != nil {
if _, ok := err.(*Error); ok {
slsErr := err.(*Error)
if slsErr.Code == "ProjectNotExist" {
return false, nil
}
return false, slsErr
}
return false, err
}
return true, nil
}
// DeleteProject ...
func (c *Client) DeleteProject(name string) error {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
proj := convert(c, name)
uri := "/"
_, err := request(proj, "DELETE", uri, h, nil)
if err != nil {
return err
}
return nil
}