generated from hashicorp/packer-plugin-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathimage_config.go
84 lines (69 loc) · 2.54 KB
/
image_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
//go:generate packer-sdc struct-markdown
package cvm
import (
"fmt"
"unicode/utf8"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
)
type TencentCloudImageConfig struct {
// The name you want to create your customize image,
// it should be composed of no more than 60 characters, of letters, numbers
// or minus sign.
ImageName string `mapstructure:"image_name" required:"true"`
// Image description. It should no more than 60 characters.
ImageDescription string `mapstructure:"image_description" required:"false"`
// Indicates whether to perform a forced shutdown to
// create an image when soft shutdown fails. Default value is `false`.
ForcePoweroff bool `mapstructure:"force_poweroff" required:"false"`
// Whether enable Sysprep during creating windows image.
Sysprep bool `mapstructure:"sysprep" required:"false"`
// regions that will be copied to after
// your image created.
ImageCopyRegions []string `mapstructure:"image_copy_regions" required:"false"`
// accounts that will be shared to
// after your image created.
ImageShareAccounts []string `mapstructure:"image_share_accounts" required:"false"`
// Key/value pair tags that will be applied to the resulting image.
ImageTags map[string]string `mapstructure:"image_tags" required:"false"`
skipValidation bool
// Skip creating the image. Defaults to `false`.
SkipCreateImage bool `mapstructure:"skip_create_image" required:"false"`
}
func (cf *TencentCloudImageConfig) Prepare(ctx *interpolate.Context) []error {
var errs []error
if cf.ImageName == "" {
errs = append(errs, fmt.Errorf("image_name must be specified"))
} else if utf8.RuneCountInString(cf.ImageName) > 60 {
errs = append(errs, fmt.Errorf("image_name length should not exceed 60 characters"))
}
if utf8.RuneCountInString(cf.ImageDescription) > 60 {
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
}
if cf.ImageTags == nil {
cf.ImageTags = make(map[string]string)
}
if len(errs) > 0 {
return errs
}
return nil
}