Addressed PR comments

This commit is contained in:
Amrita Dutta 2018-11-14 01:47:48 +00:00
parent 2d6b18e63e
commit 67342750a3
6 changed files with 110 additions and 69 deletions

View File

@ -183,20 +183,13 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
NewStepGetOSDisk(azureClient, ui),
NewStepGetAdditionalDisks(azureClient, ui),
NewStepPowerOffCompute(azureClient, ui),
}
// if managed image create snapshot
if b.config.isManagedImage() {
steps = append(steps,
NewStepSnapshotOSDisk(azureClient, ui),
NewStepSnapshotDataDisks(azureClient, ui),
)
}
steps = append(steps,
NewStepSnapshotOSDisk(azureClient, ui, b.config.isManagedImage()),
NewStepSnapshotDataDisks(azureClient, ui, b.config.isManagedImage()),
NewStepCaptureImage(azureClient, ui),
NewStepDeleteResourceGroup(azureClient, ui),
NewStepDeleteOSDisk(azureClient, ui),
NewStepDeleteAdditionalDisks(azureClient, ui),
)
}
} else if b.config.OSType == constants.Target_Windows {
keyVaultDeploymentName := b.stateBag.Get(constants.ArmKeyVaultDeploymentName).(string)
steps = []multistep.Step{
@ -227,21 +220,14 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&packerCommon.StepProvision{},
NewStepGetOSDisk(azureClient, ui),
NewStepGetAdditionalDisks(azureClient, ui),
}
// if managed image create snapshot
if b.config.isManagedImage() {
steps = append(steps,
NewStepSnapshotOSDisk(azureClient, ui),
NewStepSnapshotDataDisks(azureClient, ui),
)
}
steps = append(steps,
NewStepSnapshotOSDisk(azureClient, ui, b.config.isManagedImage()),
NewStepSnapshotDataDisks(azureClient, ui, b.config.isManagedImage()),
NewStepPowerOffCompute(azureClient, ui),
NewStepCaptureImage(azureClient, ui),
NewStepDeleteResourceGroup(azureClient, ui),
NewStepDeleteOSDisk(azureClient, ui),
NewStepDeleteAdditionalDisks(azureClient, ui),
)
}
} else {
return nil, fmt.Errorf("Builder does not support the os_type '%s'", b.config.OSType)
}

View File

@ -912,6 +912,48 @@ func TestConfigShouldAcceptManagedImageOSDiskSnapshotNameAndManagedImageDataDisk
}
}
func TestConfigShouldRejectManagedImageOSDiskSnapshotNameAndManagedImageDataDiskSnapshotPrefixWithCaptureContainerName(t *testing.T) {
config := map[string]interface{}{
"image_offer": "ignore",
"image_publisher": "ignore",
"image_sku": "ignore",
"location": "ignore",
"subscription_id": "ignore",
"communicator": "none",
"capture_container_name": "ignore",
"managed_image_os_disk_snapshot_name": "ignore_ignore",
"managed_image_data_disk_snapshot_prefix": "ignore_ignore",
// Does not matter for this test case, just pick one.
"os_type": constants.Target_Linux,
}
_, _, err := newConfig(config, getPackerConfiguration())
if err == nil {
t.Fatal("expected config to reject Managed Image build with data disk snapshot prefix and OS disk snapshot name with capture container name")
}
}
func TestConfigShouldRejectManagedImageOSDiskSnapshotNameAndManagedImageDataDiskSnapshotPrefixWithCaptureNamePrefix(t *testing.T) {
config := map[string]interface{}{
"image_offer": "ignore",
"image_publisher": "ignore",
"image_sku": "ignore",
"location": "ignore",
"subscription_id": "ignore",
"communicator": "none",
"capture_name_prefix": "ignore",
"managed_image_os_disk_snapshot_name": "ignore_ignore",
"managed_image_data_disk_snapshot_prefix": "ignore_ignore",
// Does not matter for this test case, just pick one.
"os_type": constants.Target_Linux,
}
_, _, err := newConfig(config, getPackerConfiguration())
if err == nil {
t.Fatal("expected config to reject Managed Image build with data disk snapshot prefix and OS disk snapshot name with capture name prefix")
}
}
func TestConfigShouldAcceptPlatformManagedImageBuild(t *testing.T) {
config := map[string]interface{}{
"image_offer": "ignore",

View File

@ -17,13 +17,15 @@ type StepSnapshotDataDisks struct {
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
}
func NewStepSnapshotDataDisks(client *AzureClient, ui packer.Ui) *StepSnapshotDataDisks {
func NewStepSnapshotDataDisks(client *AzureClient, ui packer.Ui, isManagedImage bool) *StepSnapshotDataDisks {
var step = &StepSnapshotDataDisks{
client: client,
say: func(message string) { ui.Say(message) },
error: func(e error) { ui.Error(e.Error()) },
isManagedImage: isManagedImage,
}
step.create = step.createDataDiskSnapshot
@ -70,7 +72,9 @@ func (s *StepSnapshotDataDisks) createDataDiskSnapshot(ctx context.Context, reso
}
func (s *StepSnapshotDataDisks) Run(ctx context.Context, stateBag multistep.StateBag) multistep.StepAction {
s.say("Taking snapshot of OS disk ...")
if s.isManagedImage {
s.say("Taking snapshot of data disk ...")
var resourceGroupName = stateBag.Get(constants.ArmManagedImageResourceGroupName).(string)
var location = stateBag.Get(constants.ArmLocation).(string)
@ -88,7 +92,7 @@ func (s *StepSnapshotDataDisks) Run(ctx context.Context, stateBag multistep.Stat
return multistep.ActionHalt
}
}
}
return multistep.ActionContinue

View File

@ -15,6 +15,7 @@ func TestStepSnapshotDataDisksShouldFailIfSnapshotFails(t *testing.T) {
},
say: func(message string) {},
error: func(e error) {},
isManagedImage: true,
}
stateBag := createTestStateBagStepSnapshotDataDisks()
@ -36,6 +37,7 @@ func TestStepSnapshotDataDisksShouldPassIfSnapshotPasses(t *testing.T) {
},
say: func(message string) {},
error: func(e error) {},
isManagedImage: true,
}
stateBag := createTestStateBagStepSnapshotDataDisks()

View File

@ -15,13 +15,15 @@ type StepSnapshotOSDisk struct {
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
}
func NewStepSnapshotOSDisk(client *AzureClient, ui packer.Ui) *StepSnapshotOSDisk {
func NewStepSnapshotOSDisk(client *AzureClient, ui packer.Ui, isManagedImage bool) *StepSnapshotOSDisk {
var step = &StepSnapshotOSDisk{
client: client,
say: func(message string) { ui.Say(message) },
error: func(e error) { ui.Error(e.Error()) },
isManagedImage: isManagedImage,
}
step.create = step.createSnapshot
@ -68,6 +70,8 @@ func (s *StepSnapshotOSDisk) createSnapshot(ctx context.Context, resourceGroupNa
}
func (s *StepSnapshotOSDisk) Run(ctx context.Context, stateBag multistep.StateBag) multistep.StepAction {
if s.isManagedImage {
s.say("Taking snapshot of OS disk ...")
var resourceGroupName = stateBag.Get(constants.ArmManagedImageResourceGroupName).(string)
@ -84,6 +88,7 @@ func (s *StepSnapshotOSDisk) Run(ctx context.Context, stateBag multistep.StateBa
return multistep.ActionHalt
}
}
return multistep.ActionContinue
}

View File

@ -15,6 +15,7 @@ func TestStepSnapshotOSDiskShouldFailIfSnapshotFails(t *testing.T) {
},
say: func(message string) {},
error: func(e error) {},
isManagedImage: true,
}
stateBag := createTestStateBagStepSnapshotOSDisk()
@ -36,6 +37,7 @@ func TestStepSnapshotOSDiskShouldPassIfSnapshotPasses(t *testing.T) {
},
say: func(message string) {},
error: func(e error) {},
isManagedImage: true,
}
stateBag := createTestStateBagStepSnapshotOSDisk()