Follow variable varName <-> statebag var_name convention

This commit is contained in:
DanHam 2018-04-27 20:09:19 +01:00
parent df5c67090e
commit c828015bc3
No known key found for this signature in database
GPG Key ID: 58E79AEDD6AA987E
5 changed files with 26 additions and 26 deletions

View File

@ -28,7 +28,7 @@ type StepCompactDisk struct {
func (s StepCompactDisk) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui)
diskPaths := state.Get("disk_full_paths").([]string)
diskFullPaths := state.Get("disk_full_paths").([]string)
if s.Skip {
log.Println("Skipping disk compaction step...")
@ -36,22 +36,22 @@ func (s StepCompactDisk) Run(_ context.Context, state multistep.StateBag) multis
}
ui.Say("Compacting all attached virtual disks...")
for i, diskPath := range diskPaths {
for i, diskFullPath := range diskFullPaths {
ui.Message(fmt.Sprintf("Compacting virtual disk %d", i+1))
// Get the file size of the virtual disk prior to compaction
fi, err := os.Stat(diskPath)
fi, err := os.Stat(diskFullPath)
if err != nil {
state.Put("error", fmt.Errorf("Error getting virtual disk file info pre compaction: %s", err))
return multistep.ActionHalt
}
diskFileSizeStart := fi.Size()
// Defragment and compact the disk
if err := driver.CompactDisk(diskPath); err != nil {
if err := driver.CompactDisk(diskFullPath); err != nil {
state.Put("error", fmt.Errorf("Error compacting disk: %s", err))
return multistep.ActionHalt
}
// Get the file size of the virtual disk post compaction
fi, err = os.Stat(diskPath)
fi, err = os.Stat(diskFullPath)
if err != nil {
state.Put("error", fmt.Errorf("Error getting virtual disk file info post compaction: %s", err))
return multistep.ActionHalt

View File

@ -23,8 +23,8 @@ func TestStepCompactDisk(t *testing.T) {
t.Fatalf("Error creating fake vmdk file: %s", err)
}
diskPath := diskFile.Name()
defer os.Remove(diskPath)
diskFullPath := diskFile.Name()
defer os.Remove(diskFullPath)
content := []byte("I am the fake vmdk's contents")
if _, err := diskFile.Write(content); err != nil {
@ -35,7 +35,7 @@ func TestStepCompactDisk(t *testing.T) {
}
// Set up required state
state.Put("disk_full_paths", []string{diskPath})
state.Put("disk_full_paths", []string{diskFullPath})
driver := state.Get("driver").(*DriverMock)
@ -51,7 +51,7 @@ func TestStepCompactDisk(t *testing.T) {
if !driver.CompactDiskCalled {
t.Fatal("should've called")
}
if driver.CompactDiskPath != diskPath {
if driver.CompactDiskPath != diskFullPath {
t.Fatal("should call with right path")
}
}
@ -61,8 +61,8 @@ func TestStepCompactDisk_skip(t *testing.T) {
step := new(StepCompactDisk)
step.Skip = true
diskPaths := []string{"foo"}
state.Put("disk_full_paths", diskPaths)
diskFullPaths := []string{"foo"}
state.Put("disk_full_paths", diskFullPaths)
driver := state.Get("driver").(*DriverMock)

View File

@ -31,26 +31,26 @@ func (stepCreateDisk) Run(_ context.Context, state multistep.StateBag) multistep
// Users can configure disks at several locations in the template so
// first collate all the disk requirements
var diskPaths, diskSizes []string
var diskFullPaths, diskSizes []string
// The 'main' or 'default' disk
diskPaths = append(diskPaths, filepath.Join(config.OutputDir, config.DiskName+".vmdk"))
diskFullPaths = append(diskFullPaths, filepath.Join(config.OutputDir, config.DiskName+".vmdk"))
diskSizes = append(diskSizes, fmt.Sprintf("%dM", uint64(config.DiskSize)))
// Additional disks
if len(config.AdditionalDiskSize) > 0 {
for i, diskSize := range config.AdditionalDiskSize {
path := filepath.Join(config.OutputDir, fmt.Sprintf("%s-%d.vmdk", config.DiskName, i+1))
diskPaths = append(diskPaths, path)
diskFullPaths = append(diskFullPaths, path)
size := fmt.Sprintf("%dM", uint64(diskSize))
diskSizes = append(diskSizes, size)
}
}
// Create all required disks
for i, diskPath := range diskPaths {
log.Printf("[INFO] Creating disk with Path: %s and Size: %s", diskPath, diskSizes[i])
for i, diskFullPath := range diskFullPaths {
log.Printf("[INFO] Creating disk with Path: %s and Size: %s", diskFullPath, diskSizes[i])
// Additional disks currently use the same adapter type and disk
// type as specified for the main disk
if err := driver.CreateDisk(diskPath, diskSizes[i], config.DiskAdapterType, config.DiskTypeId); err != nil {
if err := driver.CreateDisk(diskFullPath, diskSizes[i], config.DiskAdapterType, config.DiskTypeId); err != nil {
err := fmt.Errorf("Error creating disk: %s", err)
state.Put("error", err)
ui.Error(err.Error())
@ -59,7 +59,7 @@ func (stepCreateDisk) Run(_ context.Context, state multistep.StateBag) multistep
}
// Stash the disk paths so we can retrieve later e.g. when compacting
state.Put("disk_full_paths", diskPaths)
state.Put("disk_full_paths", diskFullPaths)
return multistep.ActionContinue
}

View File

@ -115,13 +115,13 @@ func (s *StepCloneVMX) Run(_ context.Context, state multistep.StateBag) multiste
}
// Write out the relative, host filesystem paths to the disks
var diskPaths []string
var diskFullPaths []string
for _, diskFilename := range diskFilenames {
log.Printf("Found attached disk with filename: %s", diskFilename)
diskPaths = append(diskPaths, filepath.Join(s.OutputDir, diskFilename))
diskFullPaths = append(diskFullPaths, filepath.Join(s.OutputDir, diskFilename))
}
if len(diskPaths) == 0 {
if len(diskFullPaths) == 0 {
state.Put("error", fmt.Errorf("Could not enumerate disk info from the vmx file"))
return multistep.ActionHalt
}
@ -139,7 +139,7 @@ func (s *StepCloneVMX) Run(_ context.Context, state multistep.StateBag) multiste
// Stash all required information in our state bag
state.Put("vmx_path", vmxPath)
state.Put("disk_full_paths", diskPaths)
state.Put("disk_full_paths", diskFullPaths)
state.Put("vmnetwork", networkType)
return multistep.ActionContinue

View File

@ -43,9 +43,9 @@ func TestStepCloneVMX(t *testing.T) {
// Set up expected mock disk file paths
diskFilenames := []string{scsiFilename, sataFilename, ideFilename, nvmeFilename}
var diskPaths []string
var diskFullPaths []string
for _, diskFilename := range diskFilenames {
diskPaths = append(diskPaths, filepath.Join(td, diskFilename))
diskFullPaths = append(diskFullPaths, filepath.Join(td, diskFilename))
}
// Create the source
@ -91,8 +91,8 @@ func TestStepCloneVMX(t *testing.T) {
if stateDiskPaths, ok := state.GetOk("disk_full_paths"); !ok {
t.Fatal("should set disk_full_paths")
} else {
assert.ElementsMatchf(t, stateDiskPaths.([]string), diskPaths,
"%s\nshould contain the same elements as:\n%s", stateDiskPaths.([]string), diskPaths)
assert.ElementsMatchf(t, stateDiskPaths.([]string), diskFullPaths,
"%s\nshould contain the same elements as:\n%s", stateDiskPaths.([]string), diskFullPaths)
}
// Test we got the network type