2018-12-20 11:09:44 -05:00
|
|
|
package cvm
|
|
|
|
|
|
|
|
import (
|
2019-10-21 01:21:21 -04:00
|
|
|
"context"
|
2018-12-20 11:09:44 -05:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2020-11-19 15:07:02 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2018-12-20 11:09:44 -05:00
|
|
|
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Artifact struct {
|
|
|
|
TencentCloudImages map[string]string
|
|
|
|
BuilderIdValue string
|
|
|
|
Client *cvm.Client
|
2020-01-30 05:27:58 -05:00
|
|
|
|
|
|
|
// StateData should store data such as GeneratedData
|
|
|
|
// to be shared with post-processors
|
|
|
|
StateData map[string]interface{}
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2019-10-21 01:21:21 -04:00
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
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)
|
2019-10-21 01:21:21 -04:00
|
|
|
|
|
|
|
return fmt.Sprintf("Tencentcloud images(%s) were created.\n\n", strings.Join(parts, "\n"))
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Artifact) State(name string) interface{} {
|
2020-01-30 05:27:58 -05:00
|
|
|
if _, ok := a.StateData[name]; ok {
|
|
|
|
return a.StateData[name]
|
|
|
|
}
|
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
switch name {
|
|
|
|
case "atlas.artifact.metadata":
|
|
|
|
return a.stateAtlasMetadata()
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Artifact) Destroy() error {
|
2019-10-21 01:21:21 -04:00
|
|
|
ctx := context.TODO()
|
2018-12-20 11:09:44 -05:00
|
|
|
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}
|
2019-10-21 01:21:21 -04:00
|
|
|
var describeResp *cvm.DescribeImagesResponse
|
|
|
|
err := Retry(ctx, func(ctx context.Context) error {
|
|
|
|
var e error
|
|
|
|
describeResp, e = a.Client.DescribeImages(describeReq)
|
|
|
|
return e
|
|
|
|
})
|
2018-12-20 11:09:44 -05:00
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
continue
|
|
|
|
}
|
2019-10-21 01:21:21 -04:00
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
if *describeResp.Response.TotalCount == 0 {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"describe images failed, region(%s) ImageId(%s)", region, imageId))
|
|
|
|
}
|
|
|
|
|
2019-10-21 01:21:21 -04:00
|
|
|
var shareAccountIds []*string = nil
|
2018-12-20 11:09:44 -05:00
|
|
|
describeShareReq := cvm.NewDescribeImageSharePermissionRequest()
|
|
|
|
describeShareReq.ImageId = &imageId
|
2019-10-21 01:21:21 -04:00
|
|
|
var describeShareResp *cvm.DescribeImageSharePermissionResponse
|
|
|
|
err = Retry(ctx, func(ctx context.Context) error {
|
|
|
|
var e error
|
|
|
|
describeShareResp, e = a.Client.DescribeImageSharePermission(describeShareReq)
|
|
|
|
return e
|
|
|
|
})
|
2018-12-20 11:09:44 -05:00
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
} else {
|
|
|
|
for _, sharePermission := range describeShareResp.Response.SharePermissionSet {
|
|
|
|
shareAccountIds = append(shareAccountIds, sharePermission.AccountId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 00:38:50 -04:00
|
|
|
if len(shareAccountIds) != 0 {
|
2018-12-20 11:09:44 -05:00
|
|
|
cancelShareReq := cvm.NewModifyImageSharePermissionRequest()
|
|
|
|
cancelShareReq.ImageId = &imageId
|
|
|
|
cancelShareReq.AccountIds = shareAccountIds
|
|
|
|
CANCEL := "CANCEL"
|
|
|
|
cancelShareReq.Permission = &CANCEL
|
2019-10-21 01:21:21 -04:00
|
|
|
err := Retry(ctx, func(ctx context.Context) error {
|
|
|
|
_, e := a.Client.ModifyImageSharePermission(cancelShareReq)
|
|
|
|
return e
|
|
|
|
})
|
2018-12-20 11:09:44 -05:00
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteReq := cvm.NewDeleteImagesRequest()
|
|
|
|
deleteReq.ImageIds = []*string{&imageId}
|
2019-10-21 01:21:21 -04:00
|
|
|
err = Retry(ctx, func(ctx context.Context) error {
|
|
|
|
_, e := a.Client.DeleteImages(deleteReq)
|
|
|
|
return e
|
|
|
|
})
|
2018-12-20 11:09:44 -05:00
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errors) == 1 {
|
|
|
|
return errors[0]
|
|
|
|
} else if len(errors) > 1 {
|
2020-11-19 15:07:02 -05:00
|
|
|
return &packersdk.MultiError{Errors: errors}
|
2018-12-20 11:09:44 -05:00
|
|
|
} 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
|
|
|
|
}
|
2019-10-21 01:21:21 -04:00
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
return metadata
|
|
|
|
}
|