forked from denverdino/aliyungo
/
client.go
112 lines (92 loc) · 2.69 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
package cms
import (
"net/http"
"os"
"github.com/denverdino/aliyungo/common"
)
type Client struct {
endpoint string
accessKeyId string //Access Key Id
accessKeySecret string //Access Key Secret
debug bool
httpClient *http.Client
version string
internal bool
//region common.Region
securityToken string
}
type CMSClient struct {
common.Client
}
func (client *Client) SetSecurityToken(securityToken string) {
client.securityToken = securityToken
}
func (client *Client) SetDebug(debug bool) {
client.debug = debug
}
// SetTransport sets transport to the http client
func (client *Client) SetTransport(transport http.RoundTripper) {
if client.httpClient == nil {
client.httpClient = &http.Client{}
}
client.httpClient.Transport = transport
}
const (
//TODO 旧的API,暂时保留
DefaultEndpoint = "http://alert.aliyuncs.com"
APIVersion = "2015-08-15"
METHOD_GET = "GET"
METHOD_POST = "POST"
METHOD_PUT = "PUT"
METHOD_DELETE = "DELETE"
CMSDefaultEndpoint = "http://metrics.cn-hangzhou.aliyuncs.com"
CMSAPIVersion = "2017-03-01"
CMSServiceCode = "cms"
)
//TODO 旧的API
// NewClient creates a new instance of ECS client
func NewClient(accessKeyId, accessKeySecret string) *Client {
return &Client{
accessKeyId: accessKeyId,
accessKeySecret: accessKeySecret,
internal: false,
//region: region,
version: APIVersion,
endpoint: DefaultEndpoint,
httpClient: &http.Client{},
}
}
func (client *Client) GetApiUri() string {
return client.endpoint
}
func (client *Client) GetAccessKey() string {
return client.accessKeyId
}
func (client *Client) GetAccessSecret() string {
return client.accessKeySecret
}
// NewClient creates a new instance of CMS client
func NewCMSClient(accessKeyId, accessKeySecret string) *CMSClient {
endpoint := os.Getenv("CMS_ENDPOINT")
if endpoint == "" {
endpoint = CMSDefaultEndpoint
}
return NewClientWithEndpoint(endpoint, accessKeyId, accessKeySecret)
}
func NewClientWithEndpoint(endpoint string, accessKeyId, accessKeySecret string) *CMSClient {
client := &CMSClient{}
client.Init(endpoint, CMSAPIVersion, accessKeyId, accessKeySecret)
return client
}
func NewCMSRegionClient(accessKeyId, accessKeySecret string, regionID common.Region) *CMSClient {
endpoint := os.Getenv("CMS_ENDPOINT")
if endpoint == "" {
endpoint = CMSDefaultEndpoint
}
return NewClientWithRegion(endpoint, accessKeyId, accessKeySecret, regionID)
}
func NewClientWithRegion(endpoint string, accessKeyId, accessKeySecret string, regionID common.Region) *CMSClient {
client := &CMSClient{}
client.NewInit(endpoint, CMSAPIVersion, accessKeyId, accessKeySecret, CMSServiceCode, regionID)
return client
}