/
artifact.go
150 lines (128 loc) · 3.73 KB
/
artifact.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package cvm
import (
"context"
"fmt"
"log"
"sort"
"strings"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
)
type Artifact struct {
TencentCloudImages map[string]string
BuilderIdValue string
Client *cvm.Client
// StateData should store data such as GeneratedData
// to be shared with post-processors
StateData map[string]interface{}
}
func (a *Artifact) BuilderId() string {
return a.BuilderIdValue
}
func (*Artifact) Files() []string {
return nil
}
func (a *Artifact) Id() string {
parts := make([]string, 0, len(a.TencentCloudImages))
for region, imageId := range a.TencentCloudImages {
parts = append(parts, fmt.Sprintf("%s:%s", region, imageId))
}
sort.Strings(parts)
return strings.Join(parts, ",")
}
func (a *Artifact) String() string {
parts := make([]string, 0, len(a.TencentCloudImages))
for region, imageId := range a.TencentCloudImages {
parts = append(parts, fmt.Sprintf("%s: %s", region, imageId))
}
sort.Strings(parts)
return fmt.Sprintf("Tencentcloud images(%s) were created.\n\n", strings.Join(parts, "\n"))
}
func (a *Artifact) State(name string) interface{} {
if _, ok := a.StateData[name]; ok {
return a.StateData[name]
}
switch name {
case "atlas.artifact.metadata":
return a.stateAtlasMetadata()
default:
return nil
}
}
func (a *Artifact) Destroy() error {
ctx := context.TODO()
errors := make([]error, 0)
for region, imageId := range a.TencentCloudImages {
log.Printf("Delete tencentcloud image ID(%s) from region(%s)", imageId, region)
describeReq := cvm.NewDescribeImagesRequest()
describeReq.ImageIds = []*string{&imageId}
var describeResp *cvm.DescribeImagesResponse
err := Retry(ctx, func(ctx context.Context) error {
var e error
describeResp, e = a.Client.DescribeImages(describeReq)
return e
})
if err != nil {
errors = append(errors, err)
continue
}
if *describeResp.Response.TotalCount == 0 {
errors = append(errors, fmt.Errorf(
"describe images failed, region(%s) ImageId(%s)", region, imageId))
}
var shareAccountIds []*string = nil
describeShareReq := cvm.NewDescribeImageSharePermissionRequest()
describeShareReq.ImageId = &imageId
var describeShareResp *cvm.DescribeImageSharePermissionResponse
err = Retry(ctx, func(ctx context.Context) error {
var e error
describeShareResp, e = a.Client.DescribeImageSharePermission(describeShareReq)
return e
})
if err != nil {
errors = append(errors, err)
} else {
for _, sharePermission := range describeShareResp.Response.SharePermissionSet {
shareAccountIds = append(shareAccountIds, sharePermission.AccountId)
}
}
if len(shareAccountIds) != 0 {
cancelShareReq := cvm.NewModifyImageSharePermissionRequest()
cancelShareReq.ImageId = &imageId
cancelShareReq.AccountIds = shareAccountIds
CANCEL := "CANCEL"
cancelShareReq.Permission = &CANCEL
err := Retry(ctx, func(ctx context.Context) error {
_, e := a.Client.ModifyImageSharePermission(cancelShareReq)
return e
})
if err != nil {
errors = append(errors, err)
}
}
deleteReq := cvm.NewDeleteImagesRequest()
deleteReq.ImageIds = []*string{&imageId}
err = Retry(ctx, func(ctx context.Context) error {
_, e := a.Client.DeleteImages(deleteReq)
return e
})
if err != nil {
errors = append(errors, err)
}
}
if len(errors) == 1 {
return errors[0]
} else if len(errors) > 1 {
return &packersdk.MultiError{Errors: errors}
} else {
return nil
}
}
func (a *Artifact) stateAtlasMetadata() interface{} {
metadata := make(map[string]string)
for region, imageId := range a.TencentCloudImages {
k := fmt.Sprintf("region.%s", region)
metadata[k] = imageId
}
return metadata
}