2018-12-20 11:09:44 -05:00
|
|
|
package cvm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-10-21 01:21:21 -04:00
|
|
|
"strings"
|
2018-12-20 11:09:44 -05:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
2019-10-21 01:21:21 -04:00
|
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
2018-12-20 11:09:44 -05:00
|
|
|
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepShareImage struct {
|
|
|
|
ShareAccounts []string
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepShareImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-12-20 11:09:44 -05:00
|
|
|
if len(s.ShareAccounts) == 0 {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
client := state.Get("cvm_client").(*cvm.Client)
|
2019-10-21 01:21:21 -04:00
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
imageId := state.Get("image").(*cvm.Image).ImageId
|
2019-10-21 01:21:21 -04:00
|
|
|
Say(state, strings.Join(s.ShareAccounts, ","), "Trying to share image to")
|
2018-12-20 11:09:44 -05:00
|
|
|
|
|
|
|
req := cvm.NewModifyImageSharePermissionRequest()
|
|
|
|
req.ImageId = imageId
|
2019-10-21 01:21:21 -04:00
|
|
|
req.Permission = common.StringPtr("SHARE")
|
2018-12-20 11:09:44 -05:00
|
|
|
accounts := make([]*string, 0, len(s.ShareAccounts))
|
|
|
|
for _, account := range s.ShareAccounts {
|
2019-11-07 05:12:19 -05:00
|
|
|
accounts = append(accounts, common.StringPtr(account))
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
|
|
|
req.AccountIds = accounts
|
2019-10-21 01:21:21 -04:00
|
|
|
err := Retry(ctx, func(ctx context.Context) error {
|
|
|
|
_, e := client.ModifyImageSharePermission(req)
|
|
|
|
return e
|
|
|
|
})
|
2018-12-20 11:09:44 -05:00
|
|
|
if err != nil {
|
2019-10-21 01:21:21 -04:00
|
|
|
return Halt(state, err, "Failed to share image")
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
2019-10-21 01:21:21 -04:00
|
|
|
|
|
|
|
Message(state, "Image shared", "")
|
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepShareImage) Cleanup(state multistep.StateBag) {
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
2019-10-21 01:21:21 -04:00
|
|
|
if !cancelled && !halted {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
client := state.Get("cvm_client").(*cvm.Client)
|
2018-12-20 11:09:44 -05:00
|
|
|
|
2019-10-21 01:21:21 -04:00
|
|
|
imageId := state.Get("image").(*cvm.Image).ImageId
|
|
|
|
SayClean(state, "image share")
|
2018-12-20 11:09:44 -05:00
|
|
|
|
2019-10-21 01:21:21 -04:00
|
|
|
req := cvm.NewModifyImageSharePermissionRequest()
|
|
|
|
req.ImageId = imageId
|
|
|
|
req.Permission = common.StringPtr("CANCEL")
|
|
|
|
accounts := make([]*string, 0, len(s.ShareAccounts))
|
|
|
|
for _, account := range s.ShareAccounts {
|
|
|
|
accounts = append(accounts, &account)
|
|
|
|
}
|
|
|
|
req.AccountIds = accounts
|
|
|
|
err := Retry(ctx, func(ctx context.Context) error {
|
|
|
|
_, e := client.ModifyImageSharePermission(req)
|
|
|
|
return e
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
Error(state, err, fmt.Sprintf("Failed to cancel share image(%s), please delete it manually", *imageId))
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
|
|
|
}
|