2019-05-31 08:27:41 -04:00
|
|
|
//go:generate struct-markdown
|
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
package cvm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-01-08 06:05:37 -05:00
|
|
|
"unicode/utf8"
|
2018-12-20 11:09:44 -05:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
|
2018-12-20 11:09:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type TencentCloudImageConfig struct {
|
2019-05-28 11:50:58 -04:00
|
|
|
// The name you want to create your customize image,
|
2020-01-08 06:05:37 -05:00
|
|
|
// it should be composed of no more than 60 characters, of letters, numbers
|
2019-06-06 10:29:25 -04:00
|
|
|
// or minus sign.
|
|
|
|
ImageName string `mapstructure:"image_name" required:"true"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Image description.
|
2019-06-06 10:29:25 -04:00
|
|
|
ImageDescription string `mapstructure:"image_description" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Whether shutdown cvm to create Image. Default value is
|
2019-06-06 10:29:25 -04:00
|
|
|
// false.
|
|
|
|
Reboot bool `mapstructure:"reboot" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Whether to force power off cvm when create image.
|
2019-06-06 10:29:25 -04:00
|
|
|
// Default value is false.
|
|
|
|
ForcePoweroff bool `mapstructure:"force_poweroff" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// Whether enable Sysprep during creating windows image.
|
2019-06-06 10:29:25 -04:00
|
|
|
Sysprep bool `mapstructure:"sysprep" required:"false"`
|
|
|
|
ImageForceDelete bool `mapstructure:"image_force_delete"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// regions that will be copied to after
|
2019-06-06 10:29:25 -04:00
|
|
|
// your image created.
|
|
|
|
ImageCopyRegions []string `mapstructure:"image_copy_regions" required:"false"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// accounts that will be shared to
|
2019-06-06 10:29:25 -04:00
|
|
|
// after your image created.
|
2019-05-28 11:50:58 -04:00
|
|
|
ImageShareAccounts []string `mapstructure:"image_share_accounts" required:"false"`
|
|
|
|
// Do not check region and zone when validate.
|
2019-06-06 10:29:25 -04:00
|
|
|
SkipValidation bool `mapstructure:"skip_region_validation" required:"false"`
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cf *TencentCloudImageConfig) Prepare(ctx *interpolate.Context) []error {
|
|
|
|
var errs []error
|
2019-10-21 01:21:21 -04:00
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
cf.ForcePoweroff = true
|
|
|
|
if cf.ImageName == "" {
|
2019-07-04 00:15:31 -04:00
|
|
|
errs = append(errs, fmt.Errorf("image_name must be specified"))
|
2020-01-08 06:05:37 -05:00
|
|
|
} else if utf8.RuneCountInString(cf.ImageName) > 60 {
|
|
|
|
errs = append(errs, fmt.Errorf("image_name length should not exceed 60 characters"))
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
|
|
|
|
2020-01-08 06:05:37 -05:00
|
|
|
if utf8.RuneCountInString(cf.ImageDescription) > 60 {
|
2018-12-20 11:09:44 -05:00
|
|
|
errs = append(errs, fmt.Errorf("image_description length should not exceed 60 characters"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cf.ImageCopyRegions) > 0 {
|
|
|
|
regionSet := make(map[string]struct{})
|
|
|
|
regions := make([]string, 0, len(cf.ImageCopyRegions))
|
|
|
|
|
|
|
|
for _, region := range cf.ImageCopyRegions {
|
|
|
|
if _, ok := regionSet[region]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
regionSet[region] = struct{}{}
|
|
|
|
|
|
|
|
if !cf.SkipValidation {
|
|
|
|
if err := validRegion(region); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
regions = append(regions, region)
|
|
|
|
}
|
|
|
|
cf.ImageCopyRegions = regions
|
|
|
|
}
|
2019-10-21 01:21:21 -04:00
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
if len(errs) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
2019-10-21 01:21:21 -04:00
|
|
|
return nil
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|