2017-03-03 03:56:17 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2017-03-03 03:56:17 -05:00
|
|
|
"fmt"
|
2017-05-25 21:49:35 -04:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-17 09:04:52 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-03-03 03:56:17 -05:00
|
|
|
)
|
|
|
|
|
2018-06-15 00:32:53 -04:00
|
|
|
type stepShareAlicloudImage struct {
|
2017-03-03 03:56:17 -05:00
|
|
|
AlicloudImageShareAccounts []string
|
|
|
|
AlicloudImageUNShareAccounts []string
|
|
|
|
RegionId string
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepShareAlicloudImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-04-25 22:37:49 -04:00
|
|
|
client := state.Get("client").(*ClientWrapper)
|
2017-03-03 03:56:17 -05:00
|
|
|
alicloudImages := state.Get("alicloudimages").(map[string]string)
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
for regionId, imageId := range alicloudImages {
|
|
|
|
modifyImageShareRequest := ecs.CreateModifyImageSharePermissionRequest()
|
|
|
|
modifyImageShareRequest.RegionId = regionId
|
|
|
|
modifyImageShareRequest.ImageId = imageId
|
|
|
|
modifyImageShareRequest.AddAccount = &s.AlicloudImageShareAccounts
|
|
|
|
modifyImageShareRequest.RemoveAccount = &s.AlicloudImageUNShareAccounts
|
|
|
|
|
|
|
|
if _, err := client.ModifyImageSharePermission(modifyImageShareRequest); err != nil {
|
|
|
|
return halt(state, err, "Failed modifying image share permissions")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-06-15 00:32:53 -04:00
|
|
|
func (s *stepShareAlicloudImage) Cleanup(state multistep.StateBag) {
|
2017-03-03 03:56:17 -05:00
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
if !cancelled && !halted {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
client := state.Get("client").(*ClientWrapper)
|
|
|
|
alicloudImages := state.Get("alicloudimages").(map[string]string)
|
|
|
|
|
|
|
|
ui.Say("Restoring image share permission because cancellations or error...")
|
|
|
|
|
|
|
|
for regionId, imageId := range alicloudImages {
|
|
|
|
modifyImageShareRequest := ecs.CreateModifyImageSharePermissionRequest()
|
|
|
|
modifyImageShareRequest.RegionId = regionId
|
|
|
|
modifyImageShareRequest.ImageId = imageId
|
|
|
|
modifyImageShareRequest.AddAccount = &s.AlicloudImageUNShareAccounts
|
|
|
|
modifyImageShareRequest.RemoveAccount = &s.AlicloudImageShareAccounts
|
|
|
|
if _, err := client.ModifyImageSharePermission(modifyImageShareRequest); err != nil {
|
|
|
|
ui.Say(fmt.Sprintf("Restoring image share permission failed: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|