| 1 |
package ros |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/md5" |
| 6 |
"encoding/base64" |
| 7 |
"encoding/json" |
| 8 |
"io" |
| 9 |
"io/ioutil" |
| 10 |
"log" |
| 11 |
"net/http" |
| 12 |
"net/url" |
| 13 |
"time" |
| 14 |
|
| 15 |
"fmt" |
| 16 |
|
| 17 |
"github.com/denverdino/aliyungo/common" |
| 18 |
"github.com/denverdino/aliyungo/util" |
| 19 |
) |
| 20 |
|
| 21 |
const ( |
| 22 |
|
| 23 |
ROSDefaultEndpoint = "https://ros.aliyuncs.com" |
| 24 |
ROSAPIVersion = "2015-09-01" |
| 25 |
) |
| 26 |
|
| 27 |
type Client struct { |
| 28 |
AccessKeyId string |
| 29 |
AccessKeySecret string |
| 30 |
SecurityToken string |
| 31 |
endpoint string |
| 32 |
Version string |
| 33 |
debug bool |
| 34 |
userAgent string |
| 35 |
httpClient *http.Client |
| 36 |
} |
| 37 |
|
| 38 |
type Response struct { |
| 39 |
RequestId string `json:"request_id"` |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
func NewClient(accessKeyId, accessKeySecret string) *Client { |
| 44 |
return &Client{ |
| 45 |
AccessKeyId: accessKeyId, |
| 46 |
AccessKeySecret: accessKeySecret, |
| 47 |
endpoint: ROSDefaultEndpoint, |
| 48 |
Version: ROSAPIVersion, |
| 49 |
httpClient: &http.Client{}, |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func NewROSClientWithSecurityToken(accessKeyId string, accessKeySecret string, securityToken string) *Client { |
| 54 |
return &Client{ |
| 55 |
AccessKeyId: accessKeyId, |
| 56 |
AccessKeySecret: accessKeySecret, |
| 57 |
SecurityToken: securityToken, |
| 58 |
endpoint: ROSDefaultEndpoint, |
| 59 |
Version: ROSAPIVersion, |
| 60 |
httpClient: &http.Client{}, |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
func NewClientForAussumeRole(accessKeyId, accessKeySecret, securityToken string) *Client { |
| 65 |
return &Client{ |
| 66 |
AccessKeyId: accessKeyId, |
| 67 |
AccessKeySecret: accessKeySecret, |
| 68 |
SecurityToken: securityToken, |
| 69 |
endpoint: ROSDefaultEndpoint, |
| 70 |
Version: ROSAPIVersion, |
| 71 |
httpClient: &http.Client{}, |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
func (client *Client) SetDebug(debug bool) { |
| 77 |
client.debug = debug |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
func (client *Client) SetUserAgent(userAgent string) { |
| 82 |
client.userAgent = userAgent |
| 83 |
} |
| 84 |
|
| 85 |
func (client *Client) SetSecurityToken(securityToken string) { |
| 86 |
client.SecurityToken = securityToken |
| 87 |
} |
| 88 |
|
| 89 |
type Request struct { |
| 90 |
Method string |
| 91 |
URL string |
| 92 |
Version string |
| 93 |
Region common.Region |
| 94 |
Signature string |
| 95 |
SignatureMethod string |
| 96 |
SignatureNonce string |
| 97 |
Timestamp util.ISO6801Time |
| 98 |
Body []byte |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
func (client *Client) Invoke(region common.Region, method string, path string, query url.Values, args interface{}, response interface{}) error { |
| 103 |
|
| 104 |
var reqBody []byte |
| 105 |
var err error |
| 106 |
var contentType string |
| 107 |
var contentMD5 string |
| 108 |
|
| 109 |
if args != nil { |
| 110 |
reqBody, err = json.Marshal(args) |
| 111 |
if err != nil { |
| 112 |
return err |
| 113 |
} |
| 114 |
contentType = "application/json" |
| 115 |
hasher := md5.New() |
| 116 |
hasher.Write(reqBody) |
| 117 |
contentMD5 = base64.StdEncoding.EncodeToString(hasher.Sum(nil)) |
| 118 |
} |
| 119 |
|
| 120 |
requestURL := client.endpoint + path |
| 121 |
if query != nil && len(query) > 0 { |
| 122 |
requestURL = requestURL + "?" + util.Encode(query) |
| 123 |
} |
| 124 |
var bodyReader io.Reader |
| 125 |
if reqBody != nil { |
| 126 |
bodyReader = bytes.NewReader(reqBody) |
| 127 |
} |
| 128 |
httpReq, err := http.NewRequest(method, requestURL, bodyReader) |
| 129 |
if err != nil { |
| 130 |
return common.GetClientError(err) |
| 131 |
} |
| 132 |
|
| 133 |
if region != "" { |
| 134 |
httpReq.Header["x-acs-region-id"] = []string{string(region)} |
| 135 |
} |
| 136 |
|
| 137 |
if contentType != "" { |
| 138 |
httpReq.Header.Set("Content-Type", contentType) |
| 139 |
} |
| 140 |
if contentMD5 != "" { |
| 141 |
httpReq.Header.Set("Content-MD5", contentMD5) |
| 142 |
} |
| 143 |
|
| 144 |
httpReq.Header.Set("Date", util.GetGMTime()) |
| 145 |
httpReq.Header.Set("Accept", "application/json") |
| 146 |
|
| 147 |
|
| 148 |
httpReq.Header["x-acs-signature-version"] = []string{"1.0"} |
| 149 |
httpReq.Header["x-acs-signature-nonce"] = []string{util.CreateRandomString()} |
| 150 |
httpReq.Header["x-acs-signature-method"] = []string{"HMAC-SHA1"} |
| 151 |
|
| 152 |
if client.userAgent != "" { |
| 153 |
httpReq.Header.Set("User-Agent", client.userAgent) |
| 154 |
} |
| 155 |
|
| 156 |
if client.SecurityToken != "" { |
| 157 |
httpReq.Header["x-acs-security-token"] = []string{client.SecurityToken} |
| 158 |
} |
| 159 |
|
| 160 |
client.signRequest(httpReq) |
| 161 |
|
| 162 |
t0 := time.Now() |
| 163 |
httpResp, err := client.httpClient.Do(httpReq) |
| 164 |
t1 := time.Now() |
| 165 |
if err != nil { |
| 166 |
return common.GetClientError(err) |
| 167 |
} |
| 168 |
statusCode := httpResp.StatusCode |
| 169 |
|
| 170 |
if client.debug { |
| 171 |
fmt.Printf("Invoke %s %s %d (%v)", method, requestURL, statusCode, t1.Sub(t0)) |
| 172 |
} |
| 173 |
|
| 174 |
defer httpResp.Body.Close() |
| 175 |
body, err := ioutil.ReadAll(httpResp.Body) |
| 176 |
|
| 177 |
if err != nil { |
| 178 |
return common.GetClientError(err) |
| 179 |
} |
| 180 |
|
| 181 |
if client.debug { |
| 182 |
var prettyJSON bytes.Buffer |
| 183 |
err = json.Indent(&prettyJSON, body, "", " ") |
| 184 |
log.Println(string(prettyJSON.Bytes())) |
| 185 |
} |
| 186 |
|
| 187 |
if statusCode >= 400 && statusCode <= 599 { |
| 188 |
errorResponse := common.ErrorResponse{} |
| 189 |
err = json.Unmarshal(body, &errorResponse) |
| 190 |
cErr := &common.Error{ |
| 191 |
ErrorResponse: errorResponse, |
| 192 |
StatusCode: statusCode, |
| 193 |
} |
| 194 |
return cErr |
| 195 |
} |
| 196 |
|
| 197 |
if response != nil && len(body) > 0 { |
| 198 |
err = json.Unmarshal(body, response) |
| 199 |
|
| 200 |
if err != nil { |
| 201 |
return common.GetClientError(err) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
return nil |
| 206 |
} |