Support BYOK for OSS storage driver
Change-Id: I423ad03e63bd38aded3abfcba49079ff2fbb3b74 Signed-off-by: Li Yi <denverdino@gmail.com>
This commit is contained in:
parent
40b7b5830a
commit
90bed67126
15 changed files with 1216 additions and 110 deletions
419
vendor/github.com/denverdino/aliyungo/common/client.go
generated
vendored
419
vendor/github.com/denverdino/aliyungo/common/client.go
generated
vendored
|
@ -3,35 +3,206 @@ package common
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/denverdino/aliyungo/util"
|
||||
)
|
||||
|
||||
// RemovalPolicy.N add index to array item
|
||||
// RemovalPolicy=["a", "b"] => RemovalPolicy.1="a" RemovalPolicy.2="b"
|
||||
type FlattenArray []string
|
||||
|
||||
// string contains underline which will be replaced with dot
|
||||
// SystemDisk_Category => SystemDisk.Category
|
||||
type UnderlineString string
|
||||
|
||||
// A Client represents a client of ECS services
|
||||
type Client struct {
|
||||
AccessKeyId string //Access Key Id
|
||||
AccessKeySecret string //Access Key Secret
|
||||
securityToken string
|
||||
debug bool
|
||||
httpClient *http.Client
|
||||
endpoint string
|
||||
version string
|
||||
serviceCode string
|
||||
regionID Region
|
||||
businessInfo string
|
||||
userAgent string
|
||||
}
|
||||
|
||||
// NewClient creates a new instance of ECS client
|
||||
// Initialize properties of a client instance
|
||||
func (client *Client) Init(endpoint, version, accessKeyId, accessKeySecret string) {
|
||||
client.AccessKeyId = accessKeyId
|
||||
client.AccessKeySecret = accessKeySecret + "&"
|
||||
ak := accessKeySecret
|
||||
if !strings.HasSuffix(ak, "&") {
|
||||
ak += "&"
|
||||
}
|
||||
client.AccessKeySecret = ak
|
||||
client.debug = false
|
||||
client.httpClient = &http.Client{}
|
||||
handshakeTimeout, err := strconv.Atoi(os.Getenv("TLSHandshakeTimeout"))
|
||||
if err != nil {
|
||||
handshakeTimeout = 0
|
||||
}
|
||||
if handshakeTimeout == 0 {
|
||||
client.httpClient = &http.Client{}
|
||||
} else {
|
||||
t := &http.Transport{
|
||||
TLSHandshakeTimeout: time.Duration(handshakeTimeout) * time.Second}
|
||||
client.httpClient = &http.Client{Transport: t}
|
||||
}
|
||||
client.endpoint = endpoint
|
||||
client.version = version
|
||||
}
|
||||
|
||||
// Initialize properties of a client instance including regionID
|
||||
func (client *Client) NewInit(endpoint, version, accessKeyId, accessKeySecret, serviceCode string, regionID Region) {
|
||||
client.Init(endpoint, version, accessKeyId, accessKeySecret)
|
||||
client.serviceCode = serviceCode
|
||||
client.regionID = regionID
|
||||
}
|
||||
|
||||
// Intialize client object when all properties are ready
|
||||
func (client *Client) InitClient() *Client {
|
||||
client.debug = false
|
||||
handshakeTimeout, err := strconv.Atoi(os.Getenv("TLSHandshakeTimeout"))
|
||||
if err != nil {
|
||||
handshakeTimeout = 0
|
||||
}
|
||||
if handshakeTimeout == 0 {
|
||||
client.httpClient = &http.Client{}
|
||||
} else {
|
||||
t := &http.Transport{
|
||||
TLSHandshakeTimeout: time.Duration(handshakeTimeout) * time.Second}
|
||||
client.httpClient = &http.Client{Transport: t}
|
||||
}
|
||||
return client
|
||||
}
|
||||
|
||||
func (client *Client) NewInitForAssumeRole(endpoint, version, accessKeyId, accessKeySecret, serviceCode string, regionID Region, securityToken string) {
|
||||
client.NewInit(endpoint, version, accessKeyId, accessKeySecret, serviceCode, regionID)
|
||||
client.securityToken = securityToken
|
||||
}
|
||||
|
||||
//getLocationEndpoint
|
||||
func (client *Client) getEndpointByLocation() string {
|
||||
locationClient := NewLocationClient(client.AccessKeyId, client.AccessKeySecret, client.securityToken)
|
||||
locationClient.SetDebug(true)
|
||||
return locationClient.DescribeOpenAPIEndpoint(client.regionID, client.serviceCode)
|
||||
}
|
||||
|
||||
//NewClient using location service
|
||||
func (client *Client) setEndpointByLocation(region Region, serviceCode, accessKeyId, accessKeySecret, securityToken string) {
|
||||
locationClient := NewLocationClient(accessKeyId, accessKeySecret, securityToken)
|
||||
locationClient.SetDebug(true)
|
||||
ep := locationClient.DescribeOpenAPIEndpoint(region, serviceCode)
|
||||
if ep == "" {
|
||||
ep = loadEndpointFromFile(region, serviceCode)
|
||||
}
|
||||
|
||||
if ep != "" {
|
||||
client.endpoint = ep
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure all necessary properties are valid
|
||||
func (client *Client) ensureProperties() error {
|
||||
var msg string
|
||||
|
||||
if client.endpoint == "" {
|
||||
msg = fmt.Sprintf("endpoint cannot be empty!")
|
||||
} else if client.version == "" {
|
||||
msg = fmt.Sprintf("version cannot be empty!")
|
||||
} else if client.AccessKeyId == "" {
|
||||
msg = fmt.Sprintf("AccessKeyId cannot be empty!")
|
||||
} else if client.AccessKeySecret == "" {
|
||||
msg = fmt.Sprintf("AccessKeySecret cannot be empty!")
|
||||
}
|
||||
|
||||
if msg != "" {
|
||||
return errors.New(msg)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ----------------------------------------------------
|
||||
// WithXXX methods
|
||||
// ----------------------------------------------------
|
||||
|
||||
// WithEndpoint sets custom endpoint
|
||||
func (client *Client) WithEndpoint(endpoint string) *Client {
|
||||
client.SetEndpoint(endpoint)
|
||||
return client
|
||||
}
|
||||
|
||||
// WithVersion sets custom version
|
||||
func (client *Client) WithVersion(version string) *Client {
|
||||
client.SetVersion(version)
|
||||
return client
|
||||
}
|
||||
|
||||
// WithRegionID sets Region ID
|
||||
func (client *Client) WithRegionID(regionID Region) *Client {
|
||||
client.SetRegionID(regionID)
|
||||
return client
|
||||
}
|
||||
|
||||
//WithServiceCode sets serviceCode
|
||||
func (client *Client) WithServiceCode(serviceCode string) *Client {
|
||||
client.SetServiceCode(serviceCode)
|
||||
return client
|
||||
}
|
||||
|
||||
// WithAccessKeyId sets new AccessKeyId
|
||||
func (client *Client) WithAccessKeyId(id string) *Client {
|
||||
client.SetAccessKeyId(id)
|
||||
return client
|
||||
}
|
||||
|
||||
// WithAccessKeySecret sets new AccessKeySecret
|
||||
func (client *Client) WithAccessKeySecret(secret string) *Client {
|
||||
client.SetAccessKeySecret(secret)
|
||||
return client
|
||||
}
|
||||
|
||||
// WithSecurityToken sets securityToken
|
||||
func (client *Client) WithSecurityToken(securityToken string) *Client {
|
||||
client.SetSecurityToken(securityToken)
|
||||
return client
|
||||
}
|
||||
|
||||
// WithDebug sets debug mode to log the request/response message
|
||||
func (client *Client) WithDebug(debug bool) *Client {
|
||||
client.SetDebug(debug)
|
||||
return client
|
||||
}
|
||||
|
||||
// WithBusinessInfo sets business info to log the request/response message
|
||||
func (client *Client) WithBusinessInfo(businessInfo string) *Client {
|
||||
client.SetBusinessInfo(businessInfo)
|
||||
return client
|
||||
}
|
||||
|
||||
// WithUserAgent sets user agent to the request/response message
|
||||
func (client *Client) WithUserAgent(userAgent string) *Client {
|
||||
client.SetUserAgent(userAgent)
|
||||
return client
|
||||
}
|
||||
|
||||
// ----------------------------------------------------
|
||||
// SetXXX methods
|
||||
// ----------------------------------------------------
|
||||
|
||||
// SetEndpoint sets custom endpoint
|
||||
func (client *Client) SetEndpoint(endpoint string) {
|
||||
client.endpoint = endpoint
|
||||
|
@ -42,6 +213,16 @@ func (client *Client) SetVersion(version string) {
|
|||
client.version = version
|
||||
}
|
||||
|
||||
// SetEndpoint sets Region ID
|
||||
func (client *Client) SetRegionID(regionID Region) {
|
||||
client.regionID = regionID
|
||||
}
|
||||
|
||||
//SetServiceCode sets serviceCode
|
||||
func (client *Client) SetServiceCode(serviceCode string) {
|
||||
client.serviceCode = serviceCode
|
||||
}
|
||||
|
||||
// SetAccessKeyId sets new AccessKeyId
|
||||
func (client *Client) SetAccessKeyId(id string) {
|
||||
client.AccessKeyId = id
|
||||
|
@ -57,11 +238,55 @@ func (client *Client) SetDebug(debug bool) {
|
|||
client.debug = debug
|
||||
}
|
||||
|
||||
// SetBusinessInfo sets business info to log the request/response message
|
||||
func (client *Client) SetBusinessInfo(businessInfo string) {
|
||||
if strings.HasPrefix(businessInfo, "/") {
|
||||
client.businessInfo = businessInfo
|
||||
} else if businessInfo != "" {
|
||||
client.businessInfo = "/" + businessInfo
|
||||
}
|
||||
}
|
||||
|
||||
// SetUserAgent sets user agent to the request/response message
|
||||
func (client *Client) SetUserAgent(userAgent string) {
|
||||
client.userAgent = userAgent
|
||||
}
|
||||
|
||||
//set SecurityToken
|
||||
func (client *Client) SetSecurityToken(securityToken string) {
|
||||
client.securityToken = securityToken
|
||||
}
|
||||
|
||||
func (client *Client) initEndpoint() error {
|
||||
// if set any value to "CUSTOMIZED_ENDPOINT" could skip location service.
|
||||
// example: export CUSTOMIZED_ENDPOINT=true
|
||||
if os.Getenv("CUSTOMIZED_ENDPOINT") != "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
if client.serviceCode != "" && client.regionID != "" {
|
||||
endpoint := client.getEndpointByLocation()
|
||||
if endpoint == "" {
|
||||
return GetCustomError("InvalidEndpoint", "endpoint is empty,pls check")
|
||||
}
|
||||
client.endpoint = endpoint
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Invoke sends the raw HTTP request for ECS services
|
||||
func (client *Client) Invoke(action string, args interface{}, response interface{}) error {
|
||||
if err := client.ensureProperties(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//init endpoint
|
||||
if err := client.initEndpoint(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
request := Request{}
|
||||
request.init(client.version, action, client.AccessKeyId)
|
||||
request.init(client.version, action, client.AccessKeyId, client.securityToken, client.regionID)
|
||||
|
||||
query := util.ConvertToQueryValues(request)
|
||||
util.SetQueryValues(args, &query)
|
||||
|
@ -74,13 +299,15 @@ func (client *Client) Invoke(action string, args interface{}, response interface
|
|||
|
||||
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)
|
||||
}
|
||||
|
||||
// TODO move to util and add build val flag
|
||||
httpReq.Header.Set("X-SDK-Client", `AliyunGO/`+Version+client.businessInfo)
|
||||
|
||||
httpReq.Header.Set("User-Agent", httpReq.UserAgent()+" "+client.userAgent)
|
||||
|
||||
t0 := time.Now()
|
||||
httpResp, err := client.httpClient.Do(httpReq)
|
||||
t1 := time.Now()
|
||||
|
@ -125,6 +352,174 @@ func (client *Client) Invoke(action string, args interface{}, response interface
|
|||
return nil
|
||||
}
|
||||
|
||||
// Invoke sends the raw HTTP request for ECS services
|
||||
func (client *Client) InvokeByFlattenMethod(action string, args interface{}, response interface{}) error {
|
||||
if err := client.ensureProperties(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//init endpoint
|
||||
if err := client.initEndpoint(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
request := Request{}
|
||||
request.init(client.version, action, client.AccessKeyId, client.securityToken, client.regionID)
|
||||
|
||||
query := util.ConvertToQueryValues(request)
|
||||
|
||||
util.SetQueryValueByFlattenMethod(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)
|
||||
|
||||
if err != nil {
|
||||
return GetClientError(err)
|
||||
}
|
||||
|
||||
// TODO move to util and add build val flag
|
||||
httpReq.Header.Set("X-SDK-Client", `AliyunGO/`+Version+client.businessInfo)
|
||||
|
||||
httpReq.Header.Set("User-Agent", httpReq.UserAgent()+" "+client.userAgent)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// Invoke sends the raw HTTP request for ECS services
|
||||
//改进了一下上面那个方法,可以使用各种Http方法
|
||||
//2017.1.30 增加了一个path参数,用来拓展访问的地址
|
||||
func (client *Client) InvokeByAnyMethod(method, action, path string, args interface{}, response interface{}) error {
|
||||
if err := client.ensureProperties(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//init endpoint
|
||||
if err := client.initEndpoint(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
request := Request{}
|
||||
request.init(client.version, action, client.AccessKeyId, client.securityToken, client.regionID)
|
||||
data := util.ConvertToQueryValues(request)
|
||||
util.SetQueryValues(args, &data)
|
||||
|
||||
// Sign request
|
||||
signature := util.CreateSignatureForRequest(method, &data, client.AccessKeySecret)
|
||||
|
||||
data.Add("Signature", signature)
|
||||
// Generate the request URL
|
||||
var (
|
||||
httpReq *http.Request
|
||||
err error
|
||||
)
|
||||
if method == http.MethodGet {
|
||||
requestURL := client.endpoint + path + "?" + data.Encode()
|
||||
//fmt.Println(requestURL)
|
||||
httpReq, err = http.NewRequest(method, requestURL, nil)
|
||||
} else {
|
||||
//fmt.Println(client.endpoint + path)
|
||||
httpReq, err = http.NewRequest(method, client.endpoint+path, strings.NewReader(data.Encode()))
|
||||
httpReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return GetClientError(err)
|
||||
}
|
||||
|
||||
// TODO move to util and add build val flag
|
||||
httpReq.Header.Set("X-SDK-Client", `AliyunGO/`+Version+client.businessInfo)
|
||||
httpReq.Header.Set("User-Agent", httpReq.Header.Get("User-Agent")+" "+client.userAgent)
|
||||
|
||||
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) %v", ECSRequestMethod, client.endpoint, statusCode, t1.Sub(t0), data.Encode())
|
||||
}
|
||||
|
||||
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()
|
||||
|
@ -143,3 +538,13 @@ func GetClientErrorFromString(str string) error {
|
|||
func GetClientError(err error) error {
|
||||
return GetClientErrorFromString(err.Error())
|
||||
}
|
||||
|
||||
func GetCustomError(code, message string) error {
|
||||
return &Error{
|
||||
ErrorResponse: ErrorResponse{
|
||||
Code: code,
|
||||
Message: message,
|
||||
},
|
||||
StatusCode: 400,
|
||||
}
|
||||
}
|
||||
|
|
208
vendor/github.com/denverdino/aliyungo/common/endpoint.go
generated
vendored
Normal file
208
vendor/github.com/denverdino/aliyungo/common/endpoint.go
generated
vendored
Normal file
|
@ -0,0 +1,208 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
// LocationDefaultEndpoint is the default API endpoint of Location services
|
||||
locationDefaultEndpoint = "https://location.aliyuncs.com"
|
||||
locationAPIVersion = "2015-06-12"
|
||||
HTTP_PROTOCOL = "http"
|
||||
HTTPS_PROTOCOL = "https"
|
||||
)
|
||||
|
||||
var (
|
||||
endpoints = make(map[Region]map[string]string)
|
||||
|
||||
SpecailEnpoints = map[Region]map[string]string{
|
||||
APNorthEast1: {
|
||||
"ecs": "https://ecs.ap-northeast-1.aliyuncs.com",
|
||||
"slb": "https://slb.ap-northeast-1.aliyuncs.com",
|
||||
"rds": "https://rds.ap-northeast-1.aliyuncs.com",
|
||||
"vpc": "https://vpc.ap-northeast-1.aliyuncs.com",
|
||||
},
|
||||
APSouthEast2: {
|
||||
"ecs": "https://ecs.ap-southeast-2.aliyuncs.com",
|
||||
"slb": "https://slb.ap-southeast-2.aliyuncs.com",
|
||||
"rds": "https://rds.ap-southeast-2.aliyuncs.com",
|
||||
"vpc": "https://vpc.ap-southeast-2.aliyuncs.com",
|
||||
},
|
||||
APSouthEast3: {
|
||||
"ecs": "https://ecs.ap-southeast-3.aliyuncs.com",
|
||||
"slb": "https://slb.ap-southeast-3.aliyuncs.com",
|
||||
"rds": "https://rds.ap-southeast-3.aliyuncs.com",
|
||||
"vpc": "https://vpc.ap-southeast-3.aliyuncs.com",
|
||||
},
|
||||
MEEast1: {
|
||||
"ecs": "https://ecs.me-east-1.aliyuncs.com",
|
||||
"slb": "https://slb.me-east-1.aliyuncs.com",
|
||||
"rds": "https://rds.me-east-1.aliyuncs.com",
|
||||
"vpc": "https://vpc.me-east-1.aliyuncs.com",
|
||||
},
|
||||
EUCentral1: {
|
||||
"ecs": "https://ecs.eu-central-1.aliyuncs.com",
|
||||
"slb": "https://slb.eu-central-1.aliyuncs.com",
|
||||
"rds": "https://rds.eu-central-1.aliyuncs.com",
|
||||
"vpc": "https://vpc.eu-central-1.aliyuncs.com",
|
||||
},
|
||||
EUWest1: {
|
||||
"ecs": "https://ecs.eu-west-1.aliyuncs.com",
|
||||
"slb": "https://slb.eu-west-1.aliyuncs.com",
|
||||
"rds": "https://rds.eu-west-1.aliyuncs.com",
|
||||
"vpc": "https://vpc.eu-west-1.aliyuncs.com",
|
||||
},
|
||||
Zhangjiakou: {
|
||||
"ecs": "https://ecs.cn-zhangjiakou.aliyuncs.com",
|
||||
"slb": "https://slb.cn-zhangjiakou.aliyuncs.com",
|
||||
"rds": "https://rds.cn-zhangjiakou.aliyuncs.com",
|
||||
"vpc": "https://vpc.cn-zhangjiakou.aliyuncs.com",
|
||||
},
|
||||
Huhehaote: {
|
||||
"ecs": "https://ecs.cn-huhehaote.aliyuncs.com",
|
||||
"slb": "https://slb.cn-huhehaote.aliyuncs.com",
|
||||
"rds": "https://rds.cn-huhehaote.aliyuncs.com",
|
||||
"vpc": "https://vpc.cn-huhehaote.aliyuncs.com",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
//init endpoints from file
|
||||
func init() {
|
||||
|
||||
}
|
||||
|
||||
type LocationClient struct {
|
||||
Client
|
||||
}
|
||||
|
||||
func NewLocationClient(accessKeyId, accessKeySecret, securityToken string) *LocationClient {
|
||||
endpoint := os.Getenv("LOCATION_ENDPOINT")
|
||||
if endpoint == "" {
|
||||
endpoint = locationDefaultEndpoint
|
||||
}
|
||||
|
||||
client := &LocationClient{}
|
||||
client.Init(endpoint, locationAPIVersion, accessKeyId, accessKeySecret)
|
||||
client.securityToken = securityToken
|
||||
return client
|
||||
}
|
||||
|
||||
func NewLocationClientWithSecurityToken(accessKeyId, accessKeySecret, securityToken string) *LocationClient {
|
||||
endpoint := os.Getenv("LOCATION_ENDPOINT")
|
||||
if endpoint == "" {
|
||||
endpoint = locationDefaultEndpoint
|
||||
}
|
||||
|
||||
client := &LocationClient{}
|
||||
client.WithEndpoint(endpoint).
|
||||
WithVersion(locationAPIVersion).
|
||||
WithAccessKeyId(accessKeyId).
|
||||
WithAccessKeySecret(accessKeySecret).
|
||||
WithSecurityToken(securityToken).
|
||||
InitClient()
|
||||
return client
|
||||
}
|
||||
|
||||
func (client *LocationClient) DescribeEndpoint(args *DescribeEndpointArgs) (*DescribeEndpointResponse, error) {
|
||||
response := &DescribeEndpointResponse{}
|
||||
err := client.Invoke("DescribeEndpoint", args, response)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return response, err
|
||||
}
|
||||
|
||||
func (client *LocationClient) DescribeEndpoints(args *DescribeEndpointsArgs) (*DescribeEndpointsResponse, error) {
|
||||
response := &DescribeEndpointsResponse{}
|
||||
err := client.Invoke("DescribeEndpoints", args, response)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return response, err
|
||||
}
|
||||
|
||||
func getProductRegionEndpoint(region Region, serviceCode string) string {
|
||||
if sp, ok := endpoints[region]; ok {
|
||||
if endpoint, ok := sp[serviceCode]; ok {
|
||||
return endpoint
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func setProductRegionEndpoint(region Region, serviceCode string, endpoint string) {
|
||||
endpoints[region] = map[string]string{
|
||||
serviceCode: endpoint,
|
||||
}
|
||||
}
|
||||
|
||||
func (client *LocationClient) DescribeOpenAPIEndpoint(region Region, serviceCode string) string {
|
||||
if endpoint := getProductRegionEndpoint(region, serviceCode); endpoint != "" {
|
||||
return endpoint
|
||||
}
|
||||
defaultProtocols := HTTP_PROTOCOL
|
||||
|
||||
args := &DescribeEndpointsArgs{
|
||||
Id: region,
|
||||
ServiceCode: serviceCode,
|
||||
Type: "openAPI",
|
||||
}
|
||||
|
||||
var endpoint *DescribeEndpointsResponse
|
||||
var err error
|
||||
for index := 0; index < 5; index++ {
|
||||
endpoint, err = client.DescribeEndpoints(args)
|
||||
if err == nil && endpoint != nil && len(endpoint.Endpoints.Endpoint) > 0 {
|
||||
break
|
||||
}
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
|
||||
if err != nil || endpoint == nil || len(endpoint.Endpoints.Endpoint) <= 0 {
|
||||
log.Printf("aliyungo: can not get endpoint from service, use default. endpoint=[%v], error=[%v]\n", endpoint, err)
|
||||
return ""
|
||||
}
|
||||
|
||||
for _, protocol := range endpoint.Endpoints.Endpoint[0].Protocols.Protocols {
|
||||
if strings.ToLower(protocol) == HTTPS_PROTOCOL {
|
||||
defaultProtocols = HTTPS_PROTOCOL
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
ep := fmt.Sprintf("%s://%s", defaultProtocols, endpoint.Endpoints.Endpoint[0].Endpoint)
|
||||
|
||||
setProductRegionEndpoint(region, serviceCode, ep)
|
||||
return ep
|
||||
}
|
||||
|
||||
func loadEndpointFromFile(region Region, serviceCode string) string {
|
||||
data, err := ioutil.ReadFile("./endpoints.xml")
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
var endpoints Endpoints
|
||||
err = xml.Unmarshal(data, &endpoints)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
for _, endpoint := range endpoints.Endpoint {
|
||||
if endpoint.RegionIds.RegionId == string(region) {
|
||||
for _, product := range endpoint.Products.Product {
|
||||
if strings.ToLower(product.ProductName) == serviceCode {
|
||||
return fmt.Sprintf("%s://%s", HTTPS_PROTOCOL, product.DomainName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
54
vendor/github.com/denverdino/aliyungo/common/regions.go
generated
vendored
54
vendor/github.com/denverdino/aliyungo/common/regions.go
generated
vendored
|
@ -5,15 +5,51 @@ 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")
|
||||
USEast1 = Region("us-east-1")
|
||||
Hangzhou = Region("cn-hangzhou")
|
||||
Qingdao = Region("cn-qingdao")
|
||||
Beijing = Region("cn-beijing")
|
||||
Hongkong = Region("cn-hongkong")
|
||||
Shenzhen = Region("cn-shenzhen")
|
||||
Shanghai = Region("cn-shanghai")
|
||||
Zhangjiakou = Region("cn-zhangjiakou")
|
||||
Huhehaote = Region("cn-huhehaote")
|
||||
|
||||
APSouthEast1 = Region("ap-southeast-1")
|
||||
Shanghai = Region("cn-shanghai")
|
||||
APNorthEast1 = Region("ap-northeast-1")
|
||||
APSouthEast2 = Region("ap-southeast-2")
|
||||
APSouthEast3 = Region("ap-southeast-3")
|
||||
APSouthEast5 = Region("ap-southeast-5")
|
||||
|
||||
APSouth1 = Region("ap-south-1")
|
||||
|
||||
USWest1 = Region("us-west-1")
|
||||
USEast1 = Region("us-east-1")
|
||||
|
||||
MEEast1 = Region("me-east-1")
|
||||
|
||||
EUCentral1 = Region("eu-central-1")
|
||||
EUWest1 = Region("eu-west-1")
|
||||
|
||||
ShenZhenFinance = Region("cn-shenzhen-finance-1")
|
||||
ShanghaiFinance = Region("cn-shanghai-finance-1")
|
||||
)
|
||||
|
||||
var ValidRegions = []Region{Hangzhou, Qingdao, Beijing, Shenzhen, Hongkong, Shanghai, USWest1, USEast1, APSouthEast1}
|
||||
var ValidRegions = []Region{
|
||||
Hangzhou, Qingdao, Beijing, Shenzhen, Hongkong, Shanghai, Zhangjiakou, Huhehaote,
|
||||
USWest1, USEast1,
|
||||
APNorthEast1, APSouthEast1, APSouthEast2, APSouthEast3, APSouthEast5,
|
||||
APSouth1,
|
||||
MEEast1,
|
||||
EUCentral1, EUWest1,
|
||||
ShenZhenFinance, ShanghaiFinance,
|
||||
}
|
||||
|
||||
// IsValidRegion checks if r is an Ali supported region.
|
||||
func IsValidRegion(r string) bool {
|
||||
for _, v := range ValidRegions {
|
||||
if r == string(v) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
6
vendor/github.com/denverdino/aliyungo/common/request.go
generated
vendored
6
vendor/github.com/denverdino/aliyungo/common/request.go
generated
vendored
|
@ -20,7 +20,9 @@ const (
|
|||
type Request struct {
|
||||
Format string
|
||||
Version string
|
||||
RegionId Region
|
||||
AccessKeyId string
|
||||
SecurityToken string
|
||||
Signature string
|
||||
SignatureMethod string
|
||||
Timestamp util.ISO6801Time
|
||||
|
@ -30,7 +32,7 @@ type Request struct {
|
|||
Action string
|
||||
}
|
||||
|
||||
func (request *Request) init(version string, action string, AccessKeyId string) {
|
||||
func (request *Request) init(version string, action string, AccessKeyId string, securityToken string, regionId Region) {
|
||||
request.Format = JSONResponseFormat
|
||||
request.Timestamp = util.NewISO6801Time(time.Now().UTC())
|
||||
request.Version = version
|
||||
|
@ -39,6 +41,8 @@ func (request *Request) init(version string, action string, AccessKeyId string)
|
|||
request.SignatureNonce = util.CreateRandomString()
|
||||
request.Action = action
|
||||
request.AccessKeyId = AccessKeyId
|
||||
request.SecurityToken = securityToken
|
||||
request.RegionId = regionId
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
|
|
92
vendor/github.com/denverdino/aliyungo/common/types.go
generated
vendored
92
vendor/github.com/denverdino/aliyungo/common/types.go
generated
vendored
|
@ -13,3 +13,95 @@ const (
|
|||
PrePaid = InstanceChargeType("PrePaid")
|
||||
PostPaid = InstanceChargeType("PostPaid")
|
||||
)
|
||||
|
||||
type DescribeEndpointArgs struct {
|
||||
Id Region
|
||||
ServiceCode string
|
||||
Type string
|
||||
}
|
||||
|
||||
type EndpointItem struct {
|
||||
Protocols struct {
|
||||
Protocols []string
|
||||
}
|
||||
Type string
|
||||
Namespace string
|
||||
Id Region
|
||||
SerivceCode string
|
||||
Endpoint string
|
||||
}
|
||||
|
||||
type DescribeEndpointResponse struct {
|
||||
Response
|
||||
EndpointItem
|
||||
}
|
||||
|
||||
type DescribeEndpointsArgs struct {
|
||||
Id Region
|
||||
ServiceCode string
|
||||
Type string
|
||||
}
|
||||
|
||||
type DescribeEndpointsResponse struct {
|
||||
Response
|
||||
Endpoints APIEndpoints
|
||||
RequestId string
|
||||
Success bool
|
||||
}
|
||||
|
||||
type APIEndpoints struct {
|
||||
Endpoint []EndpointItem
|
||||
}
|
||||
|
||||
type NetType string
|
||||
|
||||
const (
|
||||
Internet = NetType("Internet")
|
||||
Intranet = NetType("Intranet")
|
||||
)
|
||||
|
||||
type TimeType string
|
||||
|
||||
const (
|
||||
Hour = TimeType("Hour")
|
||||
Day = TimeType("Day")
|
||||
Week = TimeType("Week")
|
||||
Month = TimeType("Month")
|
||||
Year = TimeType("Year")
|
||||
)
|
||||
|
||||
type NetworkType string
|
||||
|
||||
const (
|
||||
Classic = NetworkType("Classic")
|
||||
VPC = NetworkType("VPC")
|
||||
)
|
||||
|
||||
type BusinessInfo struct {
|
||||
Pack string `json:"pack,omitempty"`
|
||||
ActivityId string `json:"activityId,omitempty"`
|
||||
}
|
||||
|
||||
//xml
|
||||
type Endpoints struct {
|
||||
Endpoint []Endpoint `xml:"Endpoint"`
|
||||
}
|
||||
|
||||
type Endpoint struct {
|
||||
Name string `xml:"name,attr"`
|
||||
RegionIds RegionIds `xml:"RegionIds"`
|
||||
Products Products `xml:"Products"`
|
||||
}
|
||||
|
||||
type RegionIds struct {
|
||||
RegionId string `xml:"RegionId"`
|
||||
}
|
||||
|
||||
type Products struct {
|
||||
Product []Product `xml:"Product"`
|
||||
}
|
||||
|
||||
type Product struct {
|
||||
ProductName string `xml:"ProductName"`
|
||||
DomainName string `xml:"DomainName"`
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue