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