2017-03-03 03:56:17 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/denverdino/aliyungo/common"
|
|
|
|
"github.com/denverdino/aliyungo/ecs"
|
2017-04-17 09:04:52 -04:00
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2017-03-03 03:56:17 -05:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config of alicloud
|
|
|
|
type AlicloudAccessConfig struct {
|
|
|
|
AlicloudAccessKey string `mapstructure:"access_key"`
|
|
|
|
AlicloudSecretKey string `mapstructure:"secret_key"`
|
|
|
|
AlicloudRegion string `mapstructure:"region"`
|
|
|
|
AlicloudSkipValidation bool `mapstructure:"skip_region_validation"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client for AlicloudClient
|
|
|
|
func (c *AlicloudAccessConfig) Client() (*ecs.Client, error) {
|
|
|
|
if err := c.loadAndValidate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
client := ecs.NewClient(c.AlicloudAccessKey, c.AlicloudSecretKey)
|
2017-03-04 05:06:32 -05:00
|
|
|
client.SetBusinessInfo("Packer")
|
2017-03-03 03:56:17 -05:00
|
|
|
if _, err := client.DescribeRegions(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AlicloudAccessConfig) Prepare(ctx *interpolate.Context) []error {
|
|
|
|
var errs []error
|
|
|
|
if err := c.Config(); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.AlicloudRegion != "" && !c.AlicloudSkipValidation {
|
|
|
|
if c.validateRegion() != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("Unknown alicloud region: %s", c.AlicloudRegion))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AlicloudAccessConfig) Config() error {
|
|
|
|
if c.AlicloudAccessKey == "" {
|
|
|
|
c.AlicloudAccessKey = os.Getenv("ALICLOUD_ACCESS_KEY")
|
|
|
|
}
|
|
|
|
if c.AlicloudSecretKey == "" {
|
|
|
|
c.AlicloudSecretKey = os.Getenv("ALICLOUD_SECRET_KEY")
|
|
|
|
}
|
|
|
|
if c.AlicloudAccessKey == "" || c.AlicloudSecretKey == "" {
|
2017-05-25 22:59:53 -04:00
|
|
|
return fmt.Errorf("ALICLOUD_ACCESS_KEY and ALICLOUD_SECRET_KEY must set in template file " +
|
|
|
|
"or environment variables")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AlicloudAccessConfig) loadAndValidate() error {
|
|
|
|
if err := c.validateRegion(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AlicloudAccessConfig) validateRegion() error {
|
|
|
|
|
|
|
|
for _, valid := range common.ValidRegions {
|
|
|
|
if c.AlicloudRegion == string(valid) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Not a valid alicloud region: %s", c.AlicloudRegion)
|
|
|
|
}
|