From ecaec1ac584895dd8588ba3ffa4e74afad2be20b Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Thu, 22 Aug 2019 13:52:29 -0700 Subject: [PATCH] swap out alicloud encryption *bools for trileans --- builder/alicloud/ecs/builder_test.go | 3 +-- builder/alicloud/ecs/image_config.go | 15 ++------------- builder/alicloud/ecs/step_create_image.go | 4 ++-- builder/alicloud/ecs/step_create_instance.go | 5 +++-- builder/alicloud/ecs/step_region_copy_image.go | 11 ++++++----- 5 files changed, 14 insertions(+), 24 deletions(-) diff --git a/builder/alicloud/ecs/builder_test.go b/builder/alicloud/ecs/builder_test.go index 72c3d480f..e67861f5d 100644 --- a/builder/alicloud/ecs/builder_test.go +++ b/builder/alicloud/ecs/builder_test.go @@ -132,8 +132,7 @@ func TestBuilderPrepare_Devices(t *testing.T) { Description: "system disk", DiskName: "system_disk", DiskSize: 60, - RawEncrypted: helperconfig.TriUnset, - Encrypted: nil, + Encrypted: helperconfig.TriUnset, } if !reflect.DeepEqual(b.config.ECSSystemDiskMapping, expected) { t.Fatalf("system disk is not set properly, actual: %v; expected: %v", b.config.ECSSystemDiskMapping, expected) diff --git a/builder/alicloud/ecs/image_config.go b/builder/alicloud/ecs/image_config.go index f59894a14..b0d82dd81 100644 --- a/builder/alicloud/ecs/image_config.go +++ b/builder/alicloud/ecs/image_config.go @@ -17,9 +17,7 @@ type AlicloudDiskDevice struct { Description string `mapstructure:"disk_description"` DeleteWithInstance bool `mapstructure:"disk_delete_with_instance"` Device string `mapstructure:"disk_device"` - RawEncrypted config.Trilean `mapstructure:"disk_encrypted"` - - Encrypted *bool + Encrypted config.Trilean `mapstructure:"disk_encrypted"` } type AlicloudDiskDevices struct { @@ -35,7 +33,7 @@ type AlicloudImageConfig struct { AlicloudImageUNShareAccounts []string `mapstructure:"image_unshare_account"` AlicloudImageDestinationRegions []string `mapstructure:"image_copy_regions"` AlicloudImageDestinationNames []string `mapstructure:"image_copy_names"` - RawImageEncrypted config.Trilean `mapstructure:"image_encrypted"` + ImageEncrypted config.Trilean `mapstructure:"image_encrypted"` AlicloudImageForceDelete bool `mapstructure:"image_force_delete"` AlicloudImageForceDeleteSnapshots bool `mapstructure:"image_force_delete_snapshots"` AlicloudImageForceDeleteInstances bool `mapstructure:"image_force_delete_instances"` @@ -43,8 +41,6 @@ type AlicloudImageConfig struct { AlicloudImageSkipRegionValidation bool `mapstructure:"skip_region_validation"` AlicloudImageTags map[string]string `mapstructure:"tags"` AlicloudDiskDevices `mapstructure:",squash"` - - ImageEncrypted *bool } func (c *AlicloudImageConfig) Prepare(ctx *interpolate.Context) []error { @@ -80,13 +76,6 @@ func (c *AlicloudImageConfig) Prepare(ctx *interpolate.Context) []error { 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 { return errs } diff --git a/builder/alicloud/ecs/step_create_image.go b/builder/alicloud/ecs/step_create_image.go index f69842733..9c8c45ec6 100644 --- a/builder/alicloud/ecs/step_create_image.go +++ b/builder/alicloud/ecs/step_create_image.go @@ -30,7 +30,7 @@ func (s *stepCreateAlicloudImage) Run(ctx context.Context, state multistep.State ui := state.Get("ui").(packer.Ui) tempImageName := config.AlicloudImageName - if config.ImageEncrypted != nil && *config.ImageEncrypted { + if config.ImageEncrypted.True() { tempImageName = fmt.Sprintf("packer_%s", random.AlphaNum(7)) ui.Say(fmt.Sprintf("Creating temporary image for encryption: %s", tempImageName)) } else { @@ -85,7 +85,7 @@ func (s *stepCreateAlicloudImage) Cleanup(state multistep.StateBag) { } config := state.Get("config").(*Config) - encryptedSet := config.ImageEncrypted != nil && *config.ImageEncrypted + encryptedSet := config.ImageEncrypted.True() _, cancelled := state.GetOk(multistep.StateCancelled) _, halted := state.GetOk(multistep.StateHalted) diff --git a/builder/alicloud/ecs/step_create_instance.go b/builder/alicloud/ecs/step_create_instance.go index ad3854751..14d1ac4a2 100644 --- a/builder/alicloud/ecs/step_create_instance.go +++ b/builder/alicloud/ecs/step_create_instance.go @@ -12,6 +12,7 @@ import ( "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/services/ecs" + confighelper "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" ) @@ -174,8 +175,8 @@ func (s *stepCreateAlicloudInstance) buildCreateInstanceRequest(state multistep. dataDisk.Description = imageDisk.Description dataDisk.DeleteWithInstance = strconv.FormatBool(imageDisk.DeleteWithInstance) dataDisk.Device = imageDisk.Device - if imageDisk.Encrypted != nil { - dataDisk.Encrypted = strconv.FormatBool(*imageDisk.Encrypted) + if imageDisk.Encrypted != confighelper.TriUnset { + dataDisk.Encrypted = strconv.FormatBool(imageDisk.Encrypted.True()) } dataDisks = append(dataDisks, dataDisk) diff --git a/builder/alicloud/ecs/step_region_copy_image.go b/builder/alicloud/ecs/step_region_copy_image.go index 8fb30551b..ae5ebc693 100644 --- a/builder/alicloud/ecs/step_region_copy_image.go +++ b/builder/alicloud/ecs/step_region_copy_image.go @@ -7,6 +7,7 @@ import ( "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests" "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/packer" ) @@ -20,7 +21,7 @@ type stepRegionCopyAlicloudImage struct { func (s *stepRegionCopyAlicloudImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { config := state.Get("config").(*Config) - if config.ImageEncrypted != nil { + if config.ImageEncrypted != confighelper.TriUnset { s.AlicloudImageDestinationRegions = append(s.AlicloudImageDestinationRegions, s.RegionId) 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)) for index, destinationRegion := range s.AlicloudImageDestinationRegions { - if destinationRegion == s.RegionId && config.ImageEncrypted == nil { + if destinationRegion == s.RegionId && config.ImageEncrypted == confighelper.TriUnset { continue } @@ -52,8 +53,8 @@ func (s *stepRegionCopyAlicloudImage) Run(ctx context.Context, state multistep.S copyImageRequest.ImageId = srcImageId copyImageRequest.DestinationRegionId = destinationRegion copyImageRequest.DestinationImageName = ecsImageName - if config.ImageEncrypted != nil { - copyImageRequest.Encrypted = requests.NewBoolean(*config.ImageEncrypted) + if config.ImageEncrypted != confighelper.TriUnset { + copyImageRequest.Encrypted = requests.NewBoolean(config.ImageEncrypted.True()) } 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)) } - 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 { return halt(state, err, fmt.Sprintf("Timeout waiting image %s finish copying", alicloudImages[s.RegionId])) }