2017-03-03 03:56:17 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-04-26 03:12:07 -04:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
|
2017-04-17 09:04:52 -04:00
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2019-04-25 22:37:49 -04:00
|
|
|
"github.com/hashicorp/packer/version"
|
2017-03-03 03:56:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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"`
|
2017-11-27 21:22:16 -05:00
|
|
|
SecurityToken string `mapstructure:"security_token"`
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
client *ClientWrapper
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
const Packer = "HashiCorp-Packer"
|
|
|
|
const DefaultRequestReadTimeout = 10 * time.Second
|
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
// Client for AlicloudClient
|
2019-04-25 22:37:49 -04:00
|
|
|
func (c *AlicloudAccessConfig) Client() (*ClientWrapper, error) {
|
|
|
|
if c.client != nil {
|
|
|
|
return c.client, nil
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2017-11-27 21:22:16 -05:00
|
|
|
if c.SecurityToken == "" {
|
|
|
|
c.SecurityToken = os.Getenv("SECURITY_TOKEN")
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
client, err := ecs.NewClientWithStsToken(c.AlicloudRegion, c.AlicloudAccessKey,
|
|
|
|
c.AlicloudSecretKey, c.SecurityToken)
|
|
|
|
if err != nil {
|
2017-03-03 03:56:17 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
client.AppendUserAgent(Packer, version.FormattedVersion())
|
|
|
|
client.SetReadTimeout(DefaultRequestReadTimeout)
|
|
|
|
c.client = &ClientWrapper{client}
|
|
|
|
|
|
|
|
return c.client, nil
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AlicloudAccessConfig) Prepare(ctx *interpolate.Context) []error {
|
|
|
|
var errs []error
|
|
|
|
if err := c.Config(); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
if c.AlicloudRegion == "" {
|
|
|
|
c.AlicloudRegion = os.Getenv("ALICLOUD_REGION")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.AlicloudRegion == "" {
|
|
|
|
errs = append(errs, fmt.Errorf("region option or ALICLOUD_REGION must be provided in template file or environment variables."))
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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 21:49:35 -04:00
|
|
|
return fmt.Errorf("ALICLOUD_ACCESS_KEY and ALICLOUD_SECRET_KEY must be set in template file or environment variables.")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
func (c *AlicloudAccessConfig) ValidateRegion(region string) error {
|
|
|
|
|
|
|
|
supportedRegions, err := c.getSupportedRegions()
|
|
|
|
if err != nil {
|
2017-03-03 03:56:17 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
for _, supportedRegion := range supportedRegions {
|
|
|
|
if region == supportedRegion {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Not a valid alicloud region: %s", region)
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
func (c *AlicloudAccessConfig) getSupportedRegions() ([]string, error) {
|
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
regionsRequest := ecs.CreateDescribeRegionsRequest()
|
|
|
|
regionsResponse, err := client.DescribeRegions(regionsRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-03 03:56:17 -05:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
validRegions := make([]string, len(regionsResponse.Regions.Region))
|
|
|
|
for _, valid := range regionsResponse.Regions.Region {
|
|
|
|
validRegions = append(validRegions, valid.RegionId)
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
return validRegions, nil
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|