2017-03-03 03:56:17 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-05-25 21:49:35 -04:00
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
"github.com/denverdino/aliyungo/common"
|
|
|
|
"github.com/denverdino/aliyungo/ecs"
|
2017-04-17 09:04:52 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-03-03 03:56:17 -05:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
)
|
|
|
|
|
|
|
|
type setpShareAlicloudImage struct {
|
|
|
|
AlicloudImageShareAccounts []string
|
|
|
|
AlicloudImageUNShareAccounts []string
|
|
|
|
RegionId string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *setpShareAlicloudImage) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
alicloudImages := state.Get("alicloudimages").(map[string]string)
|
|
|
|
for copyedRegion, copyedImageId := range alicloudImages {
|
|
|
|
err := client.ModifyImageSharePermission(
|
|
|
|
&ecs.ModifyImageSharePermissionArgs{
|
|
|
|
RegionId: common.Region(copyedRegion),
|
|
|
|
ImageId: copyedImageId,
|
|
|
|
AddAccount: s.AlicloudImageShareAccounts,
|
|
|
|
RemoveAccount: s.AlicloudImageUNShareAccounts,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
2017-05-25 21:49:35 -04:00
|
|
|
ui.Say(fmt.Sprintf("Failed modifying image share permissions: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *setpShareAlicloudImage) Cleanup(state multistep.StateBag) {
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
|
|
|
if cancelled || halted {
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
client := state.Get("client").(*ecs.Client)
|
|
|
|
alicloudImages := state.Get("alicloudimages").(map[string]string)
|
2017-05-25 21:49:35 -04:00
|
|
|
ui.Say("Restoring image share permission because cancellations or error...")
|
2017-03-03 03:56:17 -05:00
|
|
|
for copyedRegion, copyedImageId := range alicloudImages {
|
|
|
|
err := client.ModifyImageSharePermission(
|
|
|
|
&ecs.ModifyImageSharePermissionArgs{
|
|
|
|
RegionId: common.Region(copyedRegion),
|
|
|
|
ImageId: copyedImageId,
|
|
|
|
AddAccount: s.AlicloudImageUNShareAccounts,
|
|
|
|
RemoveAccount: s.AlicloudImageShareAccounts,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-05-25 21:49:35 -04:00
|
|
|
ui.Say(fmt.Sprintf("Restoring image share permission failed: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|