forked from hashicorp/packer-plugin-tencentcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_config.go
More file actions
101 lines (85 loc) · 3.31 KB
/
image_config.go
File metadata and controls
101 lines (85 loc) · 3.31 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// 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"`
// Key/value pair tags that will be applied to snapshot.
SnapshotTags map[string]string `mapstructure:"snapshot_tags" required:"false"`
skipValidation bool
// Skip creating an image. When set to true, you don't need to enter target image information, share, copy, etc. The default value is false.
SkipCreateImage bool `mapstructure:"skip_create_image" required:"false"`
// After creating the image,
// whether to share it with other accounts in the organization
// where the current account is located.
// The image can be copied to a maximum of 50 accounts,
// with ImageShareAccounts being the priority.
IsShareOrgMembers bool `mapstructure:"is_share_org_members" required:"false"`
// Image family. Example value: business-daily-update.
ImageFamily string `mapstructure:"image_family" required:"false"`
}
func (cf *TencentCloudImageConfig) Prepare(ctx *interpolate.Context) []error {
var errs []error
if cf.SkipCreateImage {
return nil
}
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 cf.SnapshotTags == nil {
cf.SnapshotTags = make(map[string]string)
}
if len(errs) > 0 {
return errs
}
return nil
}