2017-01-19 15:08:56 +01:00
|
|
|
package alicloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/denverdino/aliyungo/common"
|
|
|
|
"github.com/denverdino/aliyungo/ecs"
|
2017-03-24 12:04:56 +01:00
|
|
|
"github.com/denverdino/aliyungo/rds"
|
2017-01-19 15:08:56 +01:00
|
|
|
"github.com/denverdino/aliyungo/slb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config of aliyun
|
|
|
|
type Config struct {
|
|
|
|
AccessKey string
|
|
|
|
SecretKey string
|
|
|
|
Region common.Region
|
|
|
|
}
|
|
|
|
|
|
|
|
// AliyunClient of aliyun
|
|
|
|
type AliyunClient struct {
|
|
|
|
Region common.Region
|
|
|
|
ecsconn *ecs.Client
|
2017-03-24 12:04:56 +01:00
|
|
|
rdsconn *rds.Client
|
|
|
|
// use new version
|
|
|
|
ecsNewconn *ecs.Client
|
|
|
|
vpcconn *ecs.Client
|
|
|
|
slbconn *slb.Client
|
2017-01-19 15:08:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Client for AliyunClient
|
|
|
|
func (c *Config) Client() (*AliyunClient, error) {
|
|
|
|
err := c.loadAndValidate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ecsconn, err := c.ecsConn()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-24 12:04:56 +01:00
|
|
|
ecsNewconn, err := c.ecsConn()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ecsNewconn.SetVersion(EcsApiVersion20160314)
|
|
|
|
|
|
|
|
rdsconn, err := c.rdsConn()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-19 15:08:56 +01:00
|
|
|
slbconn, err := c.slbConn()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
vpcconn, err := c.vpcConn()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &AliyunClient{
|
2017-03-24 12:04:56 +01:00
|
|
|
Region: c.Region,
|
|
|
|
ecsconn: ecsconn,
|
|
|
|
ecsNewconn: ecsNewconn,
|
|
|
|
vpcconn: vpcconn,
|
|
|
|
slbconn: slbconn,
|
|
|
|
rdsconn: rdsconn,
|
2017-01-19 15:08:56 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-03-24 12:04:56 +01:00
|
|
|
const BusinessInfoKey = "Terraform"
|
|
|
|
|
2017-01-19 15:08:56 +01:00
|
|
|
func (c *Config) loadAndValidate() error {
|
|
|
|
err := c.validateRegion()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) validateRegion() error {
|
|
|
|
|
|
|
|
for _, valid := range common.ValidRegions {
|
|
|
|
if c.Region == valid {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Not a valid region: %s", c.Region)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) ecsConn() (*ecs.Client, error) {
|
2017-03-24 12:04:56 +01:00
|
|
|
client := ecs.NewECSClient(c.AccessKey, c.SecretKey, c.Region)
|
|
|
|
client.SetBusinessInfo(BusinessInfoKey)
|
|
|
|
|
2017-01-19 15:08:56 +01:00
|
|
|
_, err := client.DescribeRegions()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2017-03-24 12:04:56 +01:00
|
|
|
func (c *Config) rdsConn() (*rds.Client, error) {
|
|
|
|
client := rds.NewRDSClient(c.AccessKey, c.SecretKey, c.Region)
|
|
|
|
client.SetBusinessInfo(BusinessInfoKey)
|
|
|
|
return client, nil
|
|
|
|
}
|
2017-01-19 15:08:56 +01:00
|
|
|
|
2017-03-24 12:04:56 +01:00
|
|
|
func (c *Config) slbConn() (*slb.Client, error) {
|
|
|
|
client := slb.NewSLBClient(c.AccessKey, c.SecretKey, c.Region)
|
|
|
|
client.SetBusinessInfo(BusinessInfoKey)
|
2017-01-19 15:08:56 +01:00
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) vpcConn() (*ecs.Client, error) {
|
2017-03-24 12:04:56 +01:00
|
|
|
client := ecs.NewVPCClient(c.AccessKey, c.SecretKey, c.Region)
|
|
|
|
client.SetBusinessInfo(BusinessInfoKey)
|
2017-01-19 15:08:56 +01:00
|
|
|
return client, nil
|
2017-03-24 12:04:56 +01:00
|
|
|
|
2017-01-19 15:08:56 +01:00
|
|
|
}
|