github.com/cbroglie/terraform@v0.7.0-rc3.0.20170410193827-735dfc416d46/builtin/providers/alicloud/config.go (about) 1 package alicloud 2 3 import ( 4 "fmt" 5 6 "github.com/denverdino/aliyungo/common" 7 "github.com/denverdino/aliyungo/ecs" 8 "github.com/denverdino/aliyungo/rds" 9 "github.com/denverdino/aliyungo/slb" 10 ) 11 12 // Config of aliyun 13 type Config struct { 14 AccessKey string 15 SecretKey string 16 Region common.Region 17 } 18 19 // AliyunClient of aliyun 20 type AliyunClient struct { 21 Region common.Region 22 ecsconn *ecs.Client 23 rdsconn *rds.Client 24 // use new version 25 ecsNewconn *ecs.Client 26 vpcconn *ecs.Client 27 slbconn *slb.Client 28 } 29 30 // Client for AliyunClient 31 func (c *Config) Client() (*AliyunClient, error) { 32 err := c.loadAndValidate() 33 if err != nil { 34 return nil, err 35 } 36 37 ecsconn, err := c.ecsConn() 38 if err != nil { 39 return nil, err 40 } 41 42 ecsNewconn, err := c.ecsConn() 43 if err != nil { 44 return nil, err 45 } 46 ecsNewconn.SetVersion(EcsApiVersion20160314) 47 48 rdsconn, err := c.rdsConn() 49 if err != nil { 50 return nil, err 51 } 52 53 slbconn, err := c.slbConn() 54 if err != nil { 55 return nil, err 56 } 57 58 vpcconn, err := c.vpcConn() 59 if err != nil { 60 return nil, err 61 } 62 63 return &AliyunClient{ 64 Region: c.Region, 65 ecsconn: ecsconn, 66 ecsNewconn: ecsNewconn, 67 vpcconn: vpcconn, 68 slbconn: slbconn, 69 rdsconn: rdsconn, 70 }, nil 71 } 72 73 const BusinessInfoKey = "Terraform" 74 75 func (c *Config) loadAndValidate() error { 76 err := c.validateRegion() 77 if err != nil { 78 return err 79 } 80 81 return nil 82 } 83 84 func (c *Config) validateRegion() error { 85 86 for _, valid := range common.ValidRegions { 87 if c.Region == valid { 88 return nil 89 } 90 } 91 92 return fmt.Errorf("Not a valid region: %s", c.Region) 93 } 94 95 func (c *Config) ecsConn() (*ecs.Client, error) { 96 client := ecs.NewECSClient(c.AccessKey, c.SecretKey, c.Region) 97 client.SetBusinessInfo(BusinessInfoKey) 98 99 _, err := client.DescribeRegions() 100 101 if err != nil { 102 return nil, err 103 } 104 105 return client, nil 106 } 107 108 func (c *Config) rdsConn() (*rds.Client, error) { 109 client := rds.NewRDSClient(c.AccessKey, c.SecretKey, c.Region) 110 client.SetBusinessInfo(BusinessInfoKey) 111 return client, nil 112 } 113 114 func (c *Config) slbConn() (*slb.Client, error) { 115 client := slb.NewSLBClient(c.AccessKey, c.SecretKey, c.Region) 116 client.SetBusinessInfo(BusinessInfoKey) 117 return client, nil 118 } 119 120 func (c *Config) vpcConn() (*ecs.Client, error) { 121 client := ecs.NewVPCClient(c.AccessKey, c.SecretKey, c.Region) 122 client.SetBusinessInfo(BusinessInfoKey) 123 return client, nil 124 125 }