swap out alicloud encryption *bools for trileans

This commit is contained in:
Megan Marsh 2019-08-22 13:52:29 -07:00
parent 1a6adadb89
commit ecaec1ac58
5 changed files with 14 additions and 24 deletions

View File

@ -132,8 +132,7 @@ func TestBuilderPrepare_Devices(t *testing.T) {
Description: "system disk", Description: "system disk",
DiskName: "system_disk", DiskName: "system_disk",
DiskSize: 60, DiskSize: 60,
RawEncrypted: helperconfig.TriUnset, Encrypted: helperconfig.TriUnset,
Encrypted: nil,
} }
if !reflect.DeepEqual(b.config.ECSSystemDiskMapping, expected) { if !reflect.DeepEqual(b.config.ECSSystemDiskMapping, expected) {
t.Fatalf("system disk is not set properly, actual: %v; expected: %v", b.config.ECSSystemDiskMapping, expected) t.Fatalf("system disk is not set properly, actual: %v; expected: %v", b.config.ECSSystemDiskMapping, expected)

View File

@ -17,9 +17,7 @@ type AlicloudDiskDevice struct {
Description string `mapstructure:"disk_description"` Description string `mapstructure:"disk_description"`
DeleteWithInstance bool `mapstructure:"disk_delete_with_instance"` DeleteWithInstance bool `mapstructure:"disk_delete_with_instance"`
Device string `mapstructure:"disk_device"` Device string `mapstructure:"disk_device"`
RawEncrypted config.Trilean `mapstructure:"disk_encrypted"` Encrypted config.Trilean `mapstructure:"disk_encrypted"`
Encrypted *bool
} }
type AlicloudDiskDevices struct { type AlicloudDiskDevices struct {
@ -35,7 +33,7 @@ type AlicloudImageConfig struct {
AlicloudImageUNShareAccounts []string `mapstructure:"image_unshare_account"` AlicloudImageUNShareAccounts []string `mapstructure:"image_unshare_account"`
AlicloudImageDestinationRegions []string `mapstructure:"image_copy_regions"` AlicloudImageDestinationRegions []string `mapstructure:"image_copy_regions"`
AlicloudImageDestinationNames []string `mapstructure:"image_copy_names"` AlicloudImageDestinationNames []string `mapstructure:"image_copy_names"`
RawImageEncrypted config.Trilean `mapstructure:"image_encrypted"` ImageEncrypted config.Trilean `mapstructure:"image_encrypted"`
AlicloudImageForceDelete bool `mapstructure:"image_force_delete"` AlicloudImageForceDelete bool `mapstructure:"image_force_delete"`
AlicloudImageForceDeleteSnapshots bool `mapstructure:"image_force_delete_snapshots"` AlicloudImageForceDeleteSnapshots bool `mapstructure:"image_force_delete_snapshots"`
AlicloudImageForceDeleteInstances bool `mapstructure:"image_force_delete_instances"` AlicloudImageForceDeleteInstances bool `mapstructure:"image_force_delete_instances"`
@ -43,8 +41,6 @@ type AlicloudImageConfig struct {
AlicloudImageSkipRegionValidation bool `mapstructure:"skip_region_validation"` AlicloudImageSkipRegionValidation bool `mapstructure:"skip_region_validation"`
AlicloudImageTags map[string]string `mapstructure:"tags"` AlicloudImageTags map[string]string `mapstructure:"tags"`
AlicloudDiskDevices `mapstructure:",squash"` AlicloudDiskDevices `mapstructure:",squash"`
ImageEncrypted *bool
} }
func (c *AlicloudImageConfig) Prepare(ctx *interpolate.Context) []error { func (c *AlicloudImageConfig) Prepare(ctx *interpolate.Context) []error {
@ -80,13 +76,6 @@ func (c *AlicloudImageConfig) Prepare(ctx *interpolate.Context) []error {
c.AlicloudImageDestinationRegions = regions c.AlicloudImageDestinationRegions = regions
} }
c.ImageEncrypted = c.RawImageEncrypted.ToBoolPointer()
c.ECSSystemDiskMapping.Encrypted = c.RawImageEncrypted.ToBoolPointer()
for i := range c.ECSImagesDiskMappings {
c.ECSImagesDiskMappings[i].Encrypted = c.RawImageEncrypted.ToBoolPointer()
}
if len(errs) > 0 { if len(errs) > 0 {
return errs return errs
} }

View File

@ -30,7 +30,7 @@ func (s *stepCreateAlicloudImage) Run(ctx context.Context, state multistep.State
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
tempImageName := config.AlicloudImageName tempImageName := config.AlicloudImageName
if config.ImageEncrypted != nil && *config.ImageEncrypted { if config.ImageEncrypted.True() {
tempImageName = fmt.Sprintf("packer_%s", random.AlphaNum(7)) tempImageName = fmt.Sprintf("packer_%s", random.AlphaNum(7))
ui.Say(fmt.Sprintf("Creating temporary image for encryption: %s", tempImageName)) ui.Say(fmt.Sprintf("Creating temporary image for encryption: %s", tempImageName))
} else { } else {
@ -85,7 +85,7 @@ func (s *stepCreateAlicloudImage) Cleanup(state multistep.StateBag) {
} }
config := state.Get("config").(*Config) config := state.Get("config").(*Config)
encryptedSet := config.ImageEncrypted != nil && *config.ImageEncrypted encryptedSet := config.ImageEncrypted.True()
_, cancelled := state.GetOk(multistep.StateCancelled) _, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted) _, halted := state.GetOk(multistep.StateHalted)

View File

@ -12,6 +12,7 @@ import (
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests" "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses" "github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs" "github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
confighelper "github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
) )
@ -174,8 +175,8 @@ func (s *stepCreateAlicloudInstance) buildCreateInstanceRequest(state multistep.
dataDisk.Description = imageDisk.Description dataDisk.Description = imageDisk.Description
dataDisk.DeleteWithInstance = strconv.FormatBool(imageDisk.DeleteWithInstance) dataDisk.DeleteWithInstance = strconv.FormatBool(imageDisk.DeleteWithInstance)
dataDisk.Device = imageDisk.Device dataDisk.Device = imageDisk.Device
if imageDisk.Encrypted != nil { if imageDisk.Encrypted != confighelper.TriUnset {
dataDisk.Encrypted = strconv.FormatBool(*imageDisk.Encrypted) dataDisk.Encrypted = strconv.FormatBool(imageDisk.Encrypted.True())
} }
dataDisks = append(dataDisks, dataDisk) dataDisks = append(dataDisks, dataDisk)

View File

@ -7,6 +7,7 @@ import (
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests" "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs" "github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
confighelper "github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
) )
@ -20,7 +21,7 @@ type stepRegionCopyAlicloudImage struct {
func (s *stepRegionCopyAlicloudImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { func (s *stepRegionCopyAlicloudImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config) config := state.Get("config").(*Config)
if config.ImageEncrypted != nil { if config.ImageEncrypted != confighelper.TriUnset {
s.AlicloudImageDestinationRegions = append(s.AlicloudImageDestinationRegions, s.RegionId) s.AlicloudImageDestinationRegions = append(s.AlicloudImageDestinationRegions, s.RegionId)
s.AlicloudImageDestinationNames = append(s.AlicloudImageDestinationNames, config.AlicloudImageName) s.AlicloudImageDestinationNames = append(s.AlicloudImageDestinationNames, config.AlicloudImageName)
} }
@ -38,7 +39,7 @@ func (s *stepRegionCopyAlicloudImage) Run(ctx context.Context, state multistep.S
ui.Say(fmt.Sprintf("Coping image %s from %s...", srcImageId, s.RegionId)) ui.Say(fmt.Sprintf("Coping image %s from %s...", srcImageId, s.RegionId))
for index, destinationRegion := range s.AlicloudImageDestinationRegions { for index, destinationRegion := range s.AlicloudImageDestinationRegions {
if destinationRegion == s.RegionId && config.ImageEncrypted == nil { if destinationRegion == s.RegionId && config.ImageEncrypted == confighelper.TriUnset {
continue continue
} }
@ -52,8 +53,8 @@ func (s *stepRegionCopyAlicloudImage) Run(ctx context.Context, state multistep.S
copyImageRequest.ImageId = srcImageId copyImageRequest.ImageId = srcImageId
copyImageRequest.DestinationRegionId = destinationRegion copyImageRequest.DestinationRegionId = destinationRegion
copyImageRequest.DestinationImageName = ecsImageName copyImageRequest.DestinationImageName = ecsImageName
if config.ImageEncrypted != nil { if config.ImageEncrypted != confighelper.TriUnset {
copyImageRequest.Encrypted = requests.NewBoolean(*config.ImageEncrypted) copyImageRequest.Encrypted = requests.NewBoolean(config.ImageEncrypted.True())
} }
imageResponse, err := client.CopyImage(copyImageRequest) imageResponse, err := client.CopyImage(copyImageRequest)
@ -65,7 +66,7 @@ func (s *stepRegionCopyAlicloudImage) Run(ctx context.Context, state multistep.S
ui.Message(fmt.Sprintf("Copy image from %s(%s) to %s(%s)", s.RegionId, srcImageId, destinationRegion, imageResponse.ImageId)) ui.Message(fmt.Sprintf("Copy image from %s(%s) to %s(%s)", s.RegionId, srcImageId, destinationRegion, imageResponse.ImageId))
} }
if config.ImageEncrypted != nil { if config.ImageEncrypted != confighelper.TriUnset {
if _, err := client.WaitForImageStatus(s.RegionId, alicloudImages[s.RegionId], ImageStatusAvailable, time.Duration(ALICLOUD_DEFAULT_LONG_TIMEOUT)*time.Second); err != nil { if _, err := client.WaitForImageStatus(s.RegionId, alicloudImages[s.RegionId], ImageStatusAvailable, time.Duration(ALICLOUD_DEFAULT_LONG_TIMEOUT)*time.Second); err != nil {
return halt(state, err, fmt.Sprintf("Timeout waiting image %s finish copying", alicloudImages[s.RegionId])) return halt(state, err, fmt.Sprintf("Timeout waiting image %s finish copying", alicloudImages[s.RegionId]))
} }