2017-03-03 04:19:16 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2017-03-04 05:06:32 -05:00
|
|
|
|
|
|
|
"github.com/denverdino/aliyungo/common"
|
2017-03-03 04:19:16 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Interval for checking status in WaitForXXX method
|
|
|
|
const DefaultWaitForInterval = 5
|
|
|
|
|
|
|
|
// Default timeout value for WaitForXXX method
|
|
|
|
const DefaultTimeout = 60
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
common.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ECSDefaultEndpoint is the default API endpoint of ECS services
|
|
|
|
ECSDefaultEndpoint = "https://ecs-cn-hangzhou.aliyuncs.com"
|
|
|
|
ECSAPIVersion = "2014-05-26"
|
2017-11-27 21:22:16 -05:00
|
|
|
ECSServiceCode = "ecs"
|
2017-05-25 06:27:54 -04:00
|
|
|
|
|
|
|
VPCDefaultEndpoint = "https://vpc.aliyuncs.com"
|
|
|
|
VPCAPIVersion = "2016-04-28"
|
|
|
|
VPCServiceCode = "vpc"
|
2017-03-03 04:19:16 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewClient creates a new instance of ECS client
|
|
|
|
func NewClient(accessKeyId, accessKeySecret string) *Client {
|
|
|
|
endpoint := os.Getenv("ECS_ENDPOINT")
|
|
|
|
if endpoint == "" {
|
|
|
|
endpoint = ECSDefaultEndpoint
|
|
|
|
}
|
|
|
|
return NewClientWithEndpoint(endpoint, accessKeyId, accessKeySecret)
|
|
|
|
}
|
|
|
|
|
2017-11-27 21:22:16 -05:00
|
|
|
func NewClientWithRegion(endpoint string, accessKeyId string, accessKeySecret string, regionID common.Region) *Client {
|
|
|
|
client := &Client{}
|
|
|
|
client.NewInit(endpoint, ECSAPIVersion, accessKeyId, accessKeySecret, ECSServiceCode, regionID)
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClientWithEndpoint(endpoint string, accessKeyId string, accessKeySecret string) *Client {
|
|
|
|
client := &Client{}
|
|
|
|
client.Init(endpoint, ECSAPIVersion, accessKeyId, accessKeySecret)
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------
|
|
|
|
// NewECSClient creates a new instance of ECS client
|
|
|
|
// ---------------------------------------
|
2017-03-04 05:06:32 -05:00
|
|
|
func NewECSClient(accessKeyId, accessKeySecret string, regionID common.Region) *Client {
|
2017-11-27 21:22:16 -05:00
|
|
|
return NewECSClientWithSecurityToken(accessKeyId, accessKeySecret, "", regionID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewECSClientWithSecurityToken(accessKeyId string, accessKeySecret string, securityToken string, regionID common.Region) *Client {
|
2017-03-04 05:06:32 -05:00
|
|
|
endpoint := os.Getenv("ECS_ENDPOINT")
|
|
|
|
if endpoint == "" {
|
|
|
|
endpoint = ECSDefaultEndpoint
|
|
|
|
}
|
|
|
|
|
2017-11-27 21:22:16 -05:00
|
|
|
return NewECSClientWithEndpointAndSecurityToken(endpoint, accessKeyId, accessKeySecret, securityToken, regionID)
|
2017-03-04 05:06:32 -05:00
|
|
|
}
|
|
|
|
|
2017-11-27 21:22:16 -05:00
|
|
|
func NewECSClientWithEndpoint(endpoint string, accessKeyId string, accessKeySecret string, regionID common.Region) *Client {
|
|
|
|
return NewECSClientWithEndpointAndSecurityToken(endpoint, accessKeyId, accessKeySecret, "", regionID)
|
2017-03-04 05:06:32 -05:00
|
|
|
}
|
|
|
|
|
2017-11-27 21:22:16 -05:00
|
|
|
func NewECSClientWithEndpointAndSecurityToken(endpoint string, accessKeyId string, accessKeySecret string, securityToken string, regionID common.Region) *Client {
|
2017-03-03 04:19:16 -05:00
|
|
|
client := &Client{}
|
2017-11-27 21:22:16 -05:00
|
|
|
client.WithEndpoint(endpoint).
|
|
|
|
WithVersion(ECSAPIVersion).
|
|
|
|
WithAccessKeyId(accessKeyId).
|
|
|
|
WithAccessKeySecret(accessKeySecret).
|
|
|
|
WithSecurityToken(securityToken).
|
|
|
|
WithServiceCode(ECSServiceCode).
|
|
|
|
WithRegionID(regionID).
|
|
|
|
InitClient()
|
2017-03-03 04:19:16 -05:00
|
|
|
return client
|
|
|
|
}
|
2017-05-25 06:27:54 -04:00
|
|
|
|
2017-11-27 21:22:16 -05:00
|
|
|
// ---------------------------------------
|
|
|
|
// NewVPCClient creates a new instance of VPC client
|
|
|
|
// ---------------------------------------
|
|
|
|
func NewVPCClient(accessKeyId string, accessKeySecret string, regionID common.Region) *Client {
|
|
|
|
return NewVPCClientWithSecurityToken(accessKeyId, accessKeySecret, "", regionID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewVPCClientWithSecurityToken(accessKeyId string, accessKeySecret string, securityToken string, regionID common.Region) *Client {
|
2017-05-25 06:27:54 -04:00
|
|
|
endpoint := os.Getenv("VPC_ENDPOINT")
|
|
|
|
if endpoint == "" {
|
|
|
|
endpoint = VPCDefaultEndpoint
|
|
|
|
}
|
|
|
|
|
2017-11-27 21:22:16 -05:00
|
|
|
return NewVPCClientWithEndpointAndSecurityToken(endpoint, accessKeyId, accessKeySecret, securityToken, regionID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewVPCClientWithEndpoint(endpoint string, accessKeyId string, accessKeySecret string, regionID common.Region) *Client {
|
|
|
|
return NewVPCClientWithEndpointAndSecurityToken(endpoint, accessKeyId, accessKeySecret, "", regionID)
|
2017-05-25 06:27:54 -04:00
|
|
|
}
|
|
|
|
|
2017-11-27 21:22:16 -05:00
|
|
|
func NewVPCClientWithEndpointAndSecurityToken(endpoint string, accessKeyId string, accessKeySecret string, securityToken string, regionID common.Region) *Client {
|
2017-05-25 06:27:54 -04:00
|
|
|
client := &Client{}
|
2017-11-27 21:22:16 -05:00
|
|
|
client.WithEndpoint(endpoint).
|
|
|
|
WithVersion(VPCAPIVersion).
|
|
|
|
WithAccessKeyId(accessKeyId).
|
|
|
|
WithAccessKeySecret(accessKeySecret).
|
|
|
|
WithSecurityToken(securityToken).
|
|
|
|
WithServiceCode(VPCServiceCode).
|
|
|
|
WithRegionID(regionID).
|
|
|
|
InitClient()
|
2017-05-25 06:27:54 -04:00
|
|
|
return client
|
|
|
|
}
|