Move to vendor
Signed-off-by: Olivier Gambier <olivier@docker.com>
This commit is contained in:
parent
c8d8e7e357
commit
77e69b9cf3
1268 changed files with 34 additions and 24 deletions
145
vendor/github.com/denverdino/aliyungo/common/client.go
generated
vendored
Normal file
145
vendor/github.com/denverdino/aliyungo/common/client.go
generated
vendored
Normal file
|
@ -0,0 +1,145 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/denverdino/aliyungo/util"
|
||||
)
|
||||
|
||||
// A Client represents a client of ECS services
|
||||
type Client struct {
|
||||
AccessKeyId string //Access Key Id
|
||||
AccessKeySecret string //Access Key Secret
|
||||
debug bool
|
||||
httpClient *http.Client
|
||||
endpoint string
|
||||
version string
|
||||
}
|
||||
|
||||
// NewClient creates a new instance of ECS client
|
||||
func (client *Client) Init(endpoint, version, accessKeyId, accessKeySecret string) {
|
||||
client.AccessKeyId = accessKeyId
|
||||
client.AccessKeySecret = accessKeySecret + "&"
|
||||
client.debug = false
|
||||
client.httpClient = &http.Client{}
|
||||
client.endpoint = endpoint
|
||||
client.version = version
|
||||
}
|
||||
|
||||
// SetEndpoint sets custom endpoint
|
||||
func (client *Client) SetEndpoint(endpoint string) {
|
||||
client.endpoint = endpoint
|
||||
}
|
||||
|
||||
// SetEndpoint sets custom version
|
||||
func (client *Client) SetVersion(version string) {
|
||||
client.version = version
|
||||
}
|
||||
|
||||
// SetAccessKeyId sets new AccessKeyId
|
||||
func (client *Client) SetAccessKeyId(id string) {
|
||||
client.AccessKeyId = id
|
||||
}
|
||||
|
||||
// SetAccessKeySecret sets new AccessKeySecret
|
||||
func (client *Client) SetAccessKeySecret(secret string) {
|
||||
client.AccessKeySecret = secret + "&"
|
||||
}
|
||||
|
||||
// SetDebug sets debug mode to log the request/response message
|
||||
func (client *Client) SetDebug(debug bool) {
|
||||
client.debug = debug
|
||||
}
|
||||
|
||||
// Invoke sends the raw HTTP request for ECS services
|
||||
func (client *Client) Invoke(action string, args interface{}, response interface{}) error {
|
||||
|
||||
request := Request{}
|
||||
request.init(client.version, action, client.AccessKeyId)
|
||||
|
||||
query := util.ConvertToQueryValues(request)
|
||||
util.SetQueryValues(args, &query)
|
||||
|
||||
// Sign request
|
||||
signature := util.CreateSignatureForRequest(ECSRequestMethod, &query, client.AccessKeySecret)
|
||||
|
||||
// Generate the request URL
|
||||
requestURL := client.endpoint + "?" + query.Encode() + "&Signature=" + url.QueryEscape(signature)
|
||||
|
||||
httpReq, err := http.NewRequest(ECSRequestMethod, requestURL, nil)
|
||||
|
||||
// TODO move to util and add build val flag
|
||||
httpReq.Header.Set("X-SDK-Client", `AliyunGO/`+Version)
|
||||
|
||||
if err != nil {
|
||||
return GetClientError(err)
|
||||
}
|
||||
|
||||
t0 := time.Now()
|
||||
httpResp, err := client.httpClient.Do(httpReq)
|
||||
t1 := time.Now()
|
||||
if err != nil {
|
||||
return GetClientError(err)
|
||||
}
|
||||
statusCode := httpResp.StatusCode
|
||||
|
||||
if client.debug {
|
||||
log.Printf("Invoke %s %s %d (%v)", ECSRequestMethod, requestURL, statusCode, t1.Sub(t0))
|
||||
}
|
||||
|
||||
defer httpResp.Body.Close()
|
||||
body, err := ioutil.ReadAll(httpResp.Body)
|
||||
|
||||
if err != nil {
|
||||
return GetClientError(err)
|
||||
}
|
||||
|
||||
if client.debug {
|
||||
var prettyJSON bytes.Buffer
|
||||
err = json.Indent(&prettyJSON, body, "", " ")
|
||||
log.Println(string(prettyJSON.Bytes()))
|
||||
}
|
||||
|
||||
if statusCode >= 400 && statusCode <= 599 {
|
||||
errorResponse := ErrorResponse{}
|
||||
err = json.Unmarshal(body, &errorResponse)
|
||||
ecsError := &Error{
|
||||
ErrorResponse: errorResponse,
|
||||
StatusCode: statusCode,
|
||||
}
|
||||
return ecsError
|
||||
}
|
||||
|
||||
err = json.Unmarshal(body, response)
|
||||
//log.Printf("%++v", response)
|
||||
if err != nil {
|
||||
return GetClientError(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GenerateClientToken generates the Client Token with random string
|
||||
func (client *Client) GenerateClientToken() string {
|
||||
return util.CreateRandomString()
|
||||
}
|
||||
|
||||
func GetClientErrorFromString(str string) error {
|
||||
return &Error{
|
||||
ErrorResponse: ErrorResponse{
|
||||
Code: "AliyunGoClientFailure",
|
||||
Message: str,
|
||||
},
|
||||
StatusCode: -1,
|
||||
}
|
||||
}
|
||||
|
||||
func GetClientError(err error) error {
|
||||
return GetClientErrorFromString(err.Error())
|
||||
}
|
18
vendor/github.com/denverdino/aliyungo/common/regions.go
generated
vendored
Normal file
18
vendor/github.com/denverdino/aliyungo/common/regions.go
generated
vendored
Normal file
|
@ -0,0 +1,18 @@
|
|||
package common
|
||||
|
||||
// Region represents ECS region
|
||||
type Region string
|
||||
|
||||
// Constants of region definition
|
||||
const (
|
||||
Hangzhou = Region("cn-hangzhou")
|
||||
Qingdao = Region("cn-qingdao")
|
||||
Beijing = Region("cn-beijing")
|
||||
Hongkong = Region("cn-hongkong")
|
||||
Shenzhen = Region("cn-shenzhen")
|
||||
USWest1 = Region("us-west-1")
|
||||
APSouthEast1 = Region("ap-southeast-1")
|
||||
Shanghai = Region("cn-shanghai")
|
||||
)
|
||||
|
||||
var ValidRegions = []Region{Hangzhou, Qingdao, Beijing, Shenzhen, Hongkong, Shanghai, USWest1, APSouthEast1}
|
101
vendor/github.com/denverdino/aliyungo/common/request.go
generated
vendored
Normal file
101
vendor/github.com/denverdino/aliyungo/common/request.go
generated
vendored
Normal file
|
@ -0,0 +1,101 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/denverdino/aliyungo/util"
|
||||
)
|
||||
|
||||
// Constants for Aliyun API requests
|
||||
const (
|
||||
SignatureVersion = "1.0"
|
||||
SignatureMethod = "HMAC-SHA1"
|
||||
JSONResponseFormat = "JSON"
|
||||
XMLResponseFormat = "XML"
|
||||
ECSRequestMethod = "GET"
|
||||
)
|
||||
|
||||
type Request struct {
|
||||
Format string
|
||||
Version string
|
||||
AccessKeyId string
|
||||
Signature string
|
||||
SignatureMethod string
|
||||
Timestamp util.ISO6801Time
|
||||
SignatureVersion string
|
||||
SignatureNonce string
|
||||
ResourceOwnerAccount string
|
||||
Action string
|
||||
}
|
||||
|
||||
func (request *Request) init(version string, action string, AccessKeyId string) {
|
||||
request.Format = JSONResponseFormat
|
||||
request.Timestamp = util.NewISO6801Time(time.Now().UTC())
|
||||
request.Version = version
|
||||
request.SignatureVersion = SignatureVersion
|
||||
request.SignatureMethod = SignatureMethod
|
||||
request.SignatureNonce = util.CreateRandomString()
|
||||
request.Action = action
|
||||
request.AccessKeyId = AccessKeyId
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
RequestId string
|
||||
}
|
||||
|
||||
type ErrorResponse struct {
|
||||
Response
|
||||
HostId string
|
||||
Code string
|
||||
Message string
|
||||
}
|
||||
|
||||
// An Error represents a custom error for Aliyun API failure response
|
||||
type Error struct {
|
||||
ErrorResponse
|
||||
StatusCode int //Status Code of HTTP Response
|
||||
}
|
||||
|
||||
func (e *Error) Error() string {
|
||||
return fmt.Sprintf("Aliyun API Error: RequestId: %s Status Code: %d Code: %s Message: %s", e.RequestId, e.StatusCode, e.Code, e.Message)
|
||||
}
|
||||
|
||||
type Pagination struct {
|
||||
PageNumber int
|
||||
PageSize int
|
||||
}
|
||||
|
||||
func (p *Pagination) SetPageSize(size int) {
|
||||
p.PageSize = size
|
||||
}
|
||||
|
||||
func (p *Pagination) Validate() {
|
||||
if p.PageNumber < 0 {
|
||||
log.Printf("Invalid PageNumber: %d", p.PageNumber)
|
||||
p.PageNumber = 1
|
||||
}
|
||||
if p.PageSize < 0 {
|
||||
log.Printf("Invalid PageSize: %d", p.PageSize)
|
||||
p.PageSize = 10
|
||||
} else if p.PageSize > 50 {
|
||||
log.Printf("Invalid PageSize: %d", p.PageSize)
|
||||
p.PageSize = 50
|
||||
}
|
||||
}
|
||||
|
||||
// A PaginationResponse represents a response with pagination information
|
||||
type PaginationResult struct {
|
||||
TotalCount int
|
||||
PageNumber int
|
||||
PageSize int
|
||||
}
|
||||
|
||||
// NextPage gets the next page of the result set
|
||||
func (r *PaginationResult) NextPage() *Pagination {
|
||||
if r.PageNumber*r.PageSize >= r.TotalCount {
|
||||
return nil
|
||||
}
|
||||
return &Pagination{PageNumber: r.PageNumber + 1, PageSize: r.PageSize}
|
||||
}
|
8
vendor/github.com/denverdino/aliyungo/common/types.go
generated
vendored
Normal file
8
vendor/github.com/denverdino/aliyungo/common/types.go
generated
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
package common
|
||||
|
||||
type InternetChargeType string
|
||||
|
||||
const (
|
||||
PayByBandwidth = InternetChargeType("PayByBandwidth")
|
||||
PayByTraffic = InternetChargeType("PayByTraffic")
|
||||
)
|
3
vendor/github.com/denverdino/aliyungo/common/version.go
generated
vendored
Normal file
3
vendor/github.com/denverdino/aliyungo/common/version.go
generated
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
package common
|
||||
|
||||
const Version = "0.1"
|
Loading…
Add table
Add a link
Reference in a new issue