diff --git a/builder/azure/arm/builder.go b/builder/azure/arm/builder.go index e278052f6..1dfd66fa5 100644 --- a/builder/azure/arm/builder.go +++ b/builder/azure/arm/builder.go @@ -183,8 +183,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe NewStepGetOSDisk(azureClient, ui), NewStepGetAdditionalDisks(azureClient, ui), NewStepPowerOffCompute(azureClient, ui), - NewStepSnapshotOSDisk(azureClient, ui, b.config.isManagedImage()), - NewStepSnapshotDataDisks(azureClient, ui, b.config.isManagedImage()), + NewStepSnapshotOSDisk(azureClient, ui, b.config), + NewStepSnapshotDataDisks(azureClient, ui, b.config), NewStepCaptureImage(azureClient, ui), NewStepDeleteResourceGroup(azureClient, ui), NewStepDeleteOSDisk(azureClient, ui), @@ -220,8 +220,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &packerCommon.StepProvision{}, NewStepGetOSDisk(azureClient, ui), NewStepGetAdditionalDisks(azureClient, ui), - NewStepSnapshotOSDisk(azureClient, ui, b.config.isManagedImage()), - NewStepSnapshotDataDisks(azureClient, ui, b.config.isManagedImage()), + NewStepSnapshotOSDisk(azureClient, ui, b.config), + NewStepSnapshotDataDisks(azureClient, ui, b.config), NewStepPowerOffCompute(azureClient, ui), NewStepCaptureImage(azureClient, ui), NewStepDeleteResourceGroup(azureClient, ui), diff --git a/builder/azure/arm/config.go b/builder/azure/arm/config.go index 70246b319..35f79e6f8 100644 --- a/builder/azure/arm/config.go +++ b/builder/azure/arm/config.go @@ -56,8 +56,8 @@ var ( reCaptureNamePrefix = regexp.MustCompile("^[A-Za-z0-9][A-Za-z0-9_\\-\\.]{0,23}$") reManagedDiskName = regexp.MustCompile(validManagedDiskName) reResourceGroupName = regexp.MustCompile(validResourceGroupNameRe) - reSnapshotName = regexp.MustCompile("^[A-Za-z0-9_]{10,79}$") - reSnapshotPrefix = regexp.MustCompile("^[A-Za-z0-9_]{10,59}$") + reSnapshotName = regexp.MustCompile("^[A-Za-z0-9_]{1,79}$") + reSnapshotPrefix = regexp.MustCompile("^[A-Za-z0-9_]{1,59}$") ) type PlanInformation struct { diff --git a/builder/azure/arm/config_test.go b/builder/azure/arm/config_test.go index 4b3a7796c..e5b7e6ece 100644 --- a/builder/azure/arm/config_test.go +++ b/builder/azure/arm/config_test.go @@ -669,7 +669,6 @@ func TestConfigShouldRejectMalformedManagedImageOSDiskSnapshotName(t *testing.T) } malformedManagedImageOSDiskSnapshotName := []string{ - "min_ten", "-leading-hyphen", "trailing-hyphen-", "trailing-period.", @@ -720,7 +719,6 @@ func TestConfigShouldRejectMalformedManagedImageDataDiskSnapshotPrefix(t *testin } malformedManagedImageDataDiskSnapshotPrefix := []string{ - "more_ten", "-leading-hyphen", "trailing-hyphen-", "trailing-period.", diff --git a/builder/azure/arm/step_snapshot_data_disks.go b/builder/azure/arm/step_snapshot_data_disks.go index baf56b443..f0f476d5e 100644 --- a/builder/azure/arm/step_snapshot_data_disks.go +++ b/builder/azure/arm/step_snapshot_data_disks.go @@ -13,19 +13,19 @@ import ( ) type StepSnapshotDataDisks struct { - client *AzureClient - create func(ctx context.Context, resourceGroupName string, srcUriVhd string, location string, tags map[string]*string, dstSnapshotName string) error - say func(message string) - error func(e error) - isManagedImage bool + client *AzureClient + create func(ctx context.Context, resourceGroupName string, srcUriVhd string, location string, tags map[string]*string, dstSnapshotName string) error + say func(message string) + error func(e error) + enable func() bool } -func NewStepSnapshotDataDisks(client *AzureClient, ui packer.Ui, isManagedImage bool) *StepSnapshotDataDisks { +func NewStepSnapshotDataDisks(client *AzureClient, ui packer.Ui, config *Config) *StepSnapshotDataDisks { var step = &StepSnapshotDataDisks{ - client: client, - say: func(message string) { ui.Say(message) }, - error: func(e error) { ui.Error(e.Error()) }, - isManagedImage: isManagedImage, + client: client, + say: func(message string) { ui.Say(message) }, + error: func(e error) { ui.Error(e.Error()) }, + enable: func() bool { return config.isManagedImage() && config.ManagedImageDataDiskSnapshotPrefix != "" }, } step.create = step.createDataDiskSnapshot @@ -66,32 +66,38 @@ func (s *StepSnapshotDataDisks) createDataDiskSnapshot(ctx context.Context, reso return err } - s.say(fmt.Sprintf(" -> Managed Image Data Disk Snapshot : '%s'", *(createdSnapshot.ID))) - + s.say(fmt.Sprintf(" -> Snapshot ID : '%s'", *(createdSnapshot.ID))) return nil } func (s *StepSnapshotDataDisks) Run(ctx context.Context, stateBag multistep.StateBag) multistep.StepAction { - if s.isManagedImage { + if !s.enable() { + return multistep.ActionContinue + } - s.say("Taking snapshot of data disk ...") + var resourceGroupName = stateBag.Get(constants.ArmManagedImageResourceGroupName).(string) + var location = stateBag.Get(constants.ArmLocation).(string) + var tags = stateBag.Get(constants.ArmTags).(map[string]*string) + var additionalDisks = stateBag.Get(constants.ArmAdditionalDiskVhds).([]string) + var dstSnapshotPrefix = stateBag.Get(constants.ArmManagedImageDataDiskSnapshotPrefix).(string) - var resourceGroupName = stateBag.Get(constants.ArmManagedImageResourceGroupName).(string) - var location = stateBag.Get(constants.ArmLocation).(string) - var tags = stateBag.Get(constants.ArmTags).(map[string]*string) - var additionalDisks = stateBag.Get(constants.ArmAdditionalDiskVhds).([]string) - var dstSnapshotPrefix = stateBag.Get(constants.ArmManagedImageDataDiskSnapshotPrefix).(string) + if len(additionalDisks) == 1 { + s.say(fmt.Sprintf("Snapshotting data disk ...")) + } else { + s.say(fmt.Sprintf("Snapshotting data disks ...")) + } - for i, disk := range additionalDisks { - dstSnapshotName := dstSnapshotPrefix + strconv.Itoa(i) - err := s.create(ctx, resourceGroupName, disk, location, tags, dstSnapshotName) + for i, disk := range additionalDisks { + s.say(fmt.Sprintf(" -> Data Disk : '%s'", disk)) - if err != nil { - stateBag.Put(constants.Error, err) - s.error(err) + dstSnapshotName := dstSnapshotPrefix + strconv.Itoa(i) + err := s.create(ctx, resourceGroupName, disk, location, tags, dstSnapshotName) - return multistep.ActionHalt - } + if err != nil { + stateBag.Put(constants.Error, err) + s.error(err) + + return multistep.ActionHalt } } diff --git a/builder/azure/arm/step_snapshot_data_disks_test.go b/builder/azure/arm/step_snapshot_data_disks_test.go index e7203aade..66eb2115e 100644 --- a/builder/azure/arm/step_snapshot_data_disks_test.go +++ b/builder/azure/arm/step_snapshot_data_disks_test.go @@ -13,9 +13,9 @@ func TestStepSnapshotDataDisksShouldFailIfSnapshotFails(t *testing.T) { create: func(context.Context, string, string, string, map[string]*string, string) error { return fmt.Errorf("!! Unit Test FAIL !!") }, - say: func(message string) {}, - error: func(e error) {}, - isManagedImage: true, + say: func(message string) {}, + error: func(e error) {}, + enable: func() bool { return true }, } stateBag := createTestStateBagStepSnapshotDataDisks() @@ -30,14 +30,30 @@ func TestStepSnapshotDataDisksShouldFailIfSnapshotFails(t *testing.T) { } } +func TestStepSnapshotDataDisksShouldNotExecute(t *testing.T) { + var testSubject = &StepSnapshotDataDisks{ + create: func(context.Context, string, string, string, map[string]*string, string) error { + return fmt.Errorf("!! Unit Test FAIL !!") + }, + say: func(message string) {}, + error: func(e error) {}, + enable: func() bool { return false }, + } + + var result = testSubject.Run(context.Background(), nil) + if result != multistep.ActionContinue { + t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) + } +} + func TestStepSnapshotDataDisksShouldPassIfSnapshotPasses(t *testing.T) { var testSubject = &StepSnapshotDataDisks{ create: func(context.Context, string, string, string, map[string]*string, string) error { return nil }, - say: func(message string) {}, - error: func(e error) {}, - isManagedImage: true, + say: func(message string) {}, + error: func(e error) {}, + enable: func() bool { return true }, } stateBag := createTestStateBagStepSnapshotDataDisks() diff --git a/builder/azure/arm/step_snapshot_os_disk.go b/builder/azure/arm/step_snapshot_os_disk.go index 7f349c07e..001bb5a45 100644 --- a/builder/azure/arm/step_snapshot_os_disk.go +++ b/builder/azure/arm/step_snapshot_os_disk.go @@ -11,19 +11,19 @@ import ( ) type StepSnapshotOSDisk struct { - client *AzureClient - create func(ctx context.Context, resourceGroupName string, srcUriVhd string, location string, tags map[string]*string, dstSnapshotName string) error - say func(message string) - error func(e error) - isManagedImage bool + client *AzureClient + create func(ctx context.Context, resourceGroupName string, srcUriVhd string, location string, tags map[string]*string, dstSnapshotName string) error + say func(message string) + error func(e error) + enable func() bool } -func NewStepSnapshotOSDisk(client *AzureClient, ui packer.Ui, isManagedImage bool) *StepSnapshotOSDisk { +func NewStepSnapshotOSDisk(client *AzureClient, ui packer.Ui, config *Config) *StepSnapshotOSDisk { var step = &StepSnapshotOSDisk{ - client: client, - say: func(message string) { ui.Say(message) }, - error: func(e error) { ui.Error(e.Error()) }, - isManagedImage: isManagedImage, + client: client, + say: func(message string) { ui.Say(message) }, + error: func(e error) { ui.Error(e.Error()) }, + enable: func() bool { return config.isManagedImage() && config.ManagedImageOSDiskSnapshotName != "" }, } step.create = step.createSnapshot @@ -64,30 +64,31 @@ func (s *StepSnapshotOSDisk) createSnapshot(ctx context.Context, resourceGroupNa return err } - s.say(fmt.Sprintf(" -> Managed Image OS Disk Snapshot : '%s'", *(createdSnapshot.ID))) - + s.say(fmt.Sprintf(" -> Snapshot ID : '%s'", *(createdSnapshot.ID))) return nil } func (s *StepSnapshotOSDisk) Run(ctx context.Context, stateBag multistep.StateBag) multistep.StepAction { - if s.isManagedImage { + if !s.enable() { + return multistep.ActionContinue + } - s.say("Taking snapshot of OS disk ...") + s.say("Snapshotting OS disk ...") - var resourceGroupName = stateBag.Get(constants.ArmManagedImageResourceGroupName).(string) - var location = stateBag.Get(constants.ArmLocation).(string) - var tags = stateBag.Get(constants.ArmTags).(map[string]*string) - var srcUriVhd = stateBag.Get(constants.ArmOSDiskVhd).(string) - var dstSnapshotName = stateBag.Get(constants.ArmManagedImageOSDiskSnapshotName).(string) + var resourceGroupName = stateBag.Get(constants.ArmManagedImageResourceGroupName).(string) + var location = stateBag.Get(constants.ArmLocation).(string) + var tags = stateBag.Get(constants.ArmTags).(map[string]*string) + var srcUriVhd = stateBag.Get(constants.ArmOSDiskVhd).(string) + var dstSnapshotName = stateBag.Get(constants.ArmManagedImageOSDiskSnapshotName).(string) - err := s.create(ctx, resourceGroupName, srcUriVhd, location, tags, dstSnapshotName) + s.say(fmt.Sprintf(" -> OS Disk : '%s'", srcUriVhd)) + err := s.create(ctx, resourceGroupName, srcUriVhd, location, tags, dstSnapshotName) - if err != nil { - stateBag.Put(constants.Error, err) - s.error(err) + if err != nil { + stateBag.Put(constants.Error, err) + s.error(err) - return multistep.ActionHalt - } + return multistep.ActionHalt } return multistep.ActionContinue diff --git a/builder/azure/arm/step_snapshot_os_disk_test.go b/builder/azure/arm/step_snapshot_os_disk_test.go index b7342319b..48d2211d1 100644 --- a/builder/azure/arm/step_snapshot_os_disk_test.go +++ b/builder/azure/arm/step_snapshot_os_disk_test.go @@ -13,9 +13,9 @@ func TestStepSnapshotOSDiskShouldFailIfSnapshotFails(t *testing.T) { create: func(context.Context, string, string, string, map[string]*string, string) error { return fmt.Errorf("!! Unit Test FAIL !!") }, - say: func(message string) {}, - error: func(e error) {}, - isManagedImage: true, + say: func(message string) {}, + error: func(e error) {}, + enable: func() bool { return true }, } stateBag := createTestStateBagStepSnapshotOSDisk() @@ -30,14 +30,30 @@ func TestStepSnapshotOSDiskShouldFailIfSnapshotFails(t *testing.T) { } } +func TestStepSnapshotOSDiskShouldNotExecute(t *testing.T) { + var testSubject = &StepSnapshotOSDisk{ + create: func(context.Context, string, string, string, map[string]*string, string) error { + return fmt.Errorf("!! Unit Test FAIL !!") + }, + say: func(message string) {}, + error: func(e error) {}, + enable: func() bool { return false }, + } + + var result = testSubject.Run(context.Background(), nil) + if result != multistep.ActionContinue { + t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) + } +} + func TestStepSnapshotOSDiskShouldPassIfSnapshotPasses(t *testing.T) { var testSubject = &StepSnapshotOSDisk{ create: func(context.Context, string, string, string, map[string]*string, string) error { return nil }, - say: func(message string) {}, - error: func(e error) {}, - isManagedImage: true, + say: func(message string) {}, + error: func(e error) {}, + enable: func() bool { return true }, } stateBag := createTestStateBagStepSnapshotOSDisk()