| 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 |
|
| 11 |
"os" |
| 12 |
"strconv" |
| 13 |
) |
| 14 |
|
| 15 |
type Client struct { |
| 16 |
accessKeyId string |
| 17 |
accessKeySecret string |
| 18 |
securityToken string |
| 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 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 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 |
|
| 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 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 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 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 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 |
} |