Debian Codegraph @

sls/client.go

raw symbols SCIP
1 package sls
2
3 import (
4 "encoding/json"
5 "fmt"
6 "net/http"
7
8 "github.com/denverdino/aliyungo/common"
9 "github.com/golang/protobuf/proto"
10 //"time"
11 "os"
12 "strconv"
13 )
14
15 type Client struct {
16 accessKeyId string //Access Key Id
17 accessKeySecret string //Access Key Secret
18 securityToken string //sts token
19 debug bool
20 httpClient *http.Client
21 version string
22 internal bool
23 region common.Region
24 endpoint string
25 }
26
27 func (client *Client) SetDebug(debug bool) {
28 client.debug = debug
29 }
30
31 type Project struct {
32 client *Client
33 Name string `json:"projectName,omitempty"`
34 Description string `json:"description,omitempty"`
35 }
36
37 //type LogContent struct {
38 // Key string
39 // Value string
40 //}
41 //
42 //type LogItem struct {
43 // Time time.Time
44 // Contents []*LogContent
45 //}
46 //
47 //type LogGroupItem struct {
48 // Logs []*LogItem
49 // Topic string
50 // Source string
51 //}
52
53 type PutLogsRequest struct {
54 Project string
55 LogStore string
56 LogItems LogGroup
57 HashKey string
58 }
59
60 const (
61 SLSDefaultEndpoint = "log.aliyuncs.com"
62 SLSAPIVersion = "0.6.0"
63 METHOD_GET = "GET"
64 METHOD_POST = "POST"
65 METHOD_PUT = "PUT"
66 METHOD_DELETE = "DELETE"
67 )
68
69 // NewClient creates a new instance of ECS client
70 func NewClient(region common.Region, internal bool, accessKeyId, accessKeySecret string) *Client {
71 endpoint := os.Getenv("SLS_ENDPOINT")
72 if endpoint == "" {
73 endpoint = SLSDefaultEndpoint
74 }
75 return NewClientWithEndpoint(endpoint, region, internal, accessKeyId, accessKeySecret)
76 }
77
78 func NewClientForAssumeRole(region common.Region, internal bool, accessKeyId, accessKeySecret, securityToken string) *Client {
79 endpoint := os.Getenv("SLS_ENDPOINT")
80 if endpoint == "" {
81 endpoint = SLSDefaultEndpoint
82 }
83
84 return &Client{
85 accessKeyId: accessKeyId,
86 accessKeySecret: accessKeySecret,
87 securityToken: securityToken,
88 internal: internal,
89 region: region,
90 version: SLSAPIVersion,
91 endpoint: endpoint,
92 httpClient: &http.Client{},
93 }
94 }
95
96 func NewClientWithEndpoint(endpoint string, region common.Region, internal bool, accessKeyId, accessKeySecret string) *Client {
97 return &Client{
98 accessKeyId: accessKeyId,
99 accessKeySecret: accessKeySecret,
100 internal: internal,
101 region: region,
102 version: SLSAPIVersion,
103 endpoint: endpoint,
104 httpClient: &http.Client{},
105 }
106 }
107
108 func (client *Client) Project(name string) (*Project, error) {
109
110 // newClient := client.forProject(name)
111 //
112 // req := &request{
113 // method: METHOD_GET,
114 // path: "/",
115 // }
116 //
117 // project := &Project{}
118 //
119 // if err := newClient.requestWithJsonResponse(req, project); err != nil {
120 // return nil, err
121 // }
122 // project.client = newClient
123 // return project, nil
124 return &Project{
125 Name: name,
126 client: client.forProject(name),
127 }, nil
128 }
129
130 func (client *Client) forProject(name string) *Client {
131 newclient := *client
132
133 region := string(client.region)
134 if client.internal {
135 region = fmt.Sprintf("%s-intranet", region)
136 }
137 newclient.endpoint = fmt.Sprintf("%s.%s.%s", name, region, client.endpoint)
138 return &newclient
139 }
140
141 func (client *Client) DeleteProject(name string) error {
142 req := &request{
143 method: METHOD_DELETE,
144 path: "/",
145 }
146
147 newClient := client.forProject(name)
148 return newClient.requestWithClose(req)
149 }
150
151 func (client *Client) CreateProject(name string, description string) error {
152 project := &Project{
153 Name: name,
154 Description: description,
155 }
156 data, err := json.Marshal(project)
157 if err != nil {
158 return err
159 }
160
161 req := &request{
162 method: METHOD_POST,
163 path: "/",
164 payload: data,
165 contentType: "application/json",
166 }
167
168 newClient := client.forProject(name)
169 return newClient.requestWithClose(req)
170 }
171
172 //
173 //func marshal() ([]byte, error) {
174 //
175 // logGroups := []*LogGroup{}
176 // tmp := []*LogGroupItem
177 // for _, logGroupItem := range tmp {
178 //
179 // logs := []*Log{}
180 // for _, logItem := range logGroupItem.Logs {
181 // contents := []*Log_Content{}
182 // for key, value := range logItem.Content {
183 // contents = append(contents, &Log_Content{
184 // Key: proto.String(key),
185 // Value: proto.String(value),
186 // })
187 // }
188 //
189 // logs = append(logs, &Log{
190 // Time: proto.Uint32(uint32(LogItem.Time.Unix())),
191 // Contents: contents,
192 // })
193 // }
194 //
195 // logGroup := &LogGroup{
196 // Topic: proto.String(LogGroupItem.Topic),
197 // Source: proto.String(LogGroupItem.Source),
198 // Logs: logs,
199 // }
200 // logGroups = append(logGroups, logGroup)
201 // }
202 //
203 // return proto.Marshal(&LogGroupList{
204 // LogGroupList: logGroups,
205 // })
206 //}
207
208 func (client *Client) PutLogs(putLogRequest *PutLogsRequest) error {
209 if putLogRequest == nil {
210 return nil
211 }
212
213 data, err := proto.Marshal(&putLogRequest.LogItems)
214 if err != nil {
215 return err
216 }
217
218 req := &request{
219 method: METHOD_POST,
220 path: "/logstores/" + putLogRequest.LogStore + "/shards/lb",
221 payload: data,
222 contentType: "application/x-protobuf",
223 headers: map[string]string{"x-log-bodyrawsize": strconv.Itoa(len(data))},
224 }
225
226 newClient := client.forProject(putLogRequest.Project)
227 return newClient.requestWithClose(req)
228
229 }