Shift names to diskset
Instead of one disk, we'll be working with a set of disks.
This commit is contained in:
parent
c9284d03aa
commit
3a3e6e525b
|
@ -464,12 +464,12 @@ func buildsteps(config Config, info *client.ComputeInfo) []multistep.Step {
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.FromScratch {
|
if config.FromScratch {
|
||||||
addSteps(&StepCreateNewDisk{
|
addSteps(&StepCreateNewDiskset{
|
||||||
ResourceID: config.TemporaryOSDiskID,
|
OSDiskID: config.TemporaryOSDiskID,
|
||||||
DiskSizeGB: config.OSDiskSizeGB,
|
OSDiskSizeGB: config.OSDiskSizeGB,
|
||||||
DiskStorageAccountType: config.OSDiskStorageAccountType,
|
OSDiskStorageAccountType: config.OSDiskStorageAccountType,
|
||||||
HyperVGeneration: config.ImageHyperVGeneration,
|
HyperVGeneration: config.ImageHyperVGeneration,
|
||||||
Location: info.Location})
|
Location: info.Location})
|
||||||
} else {
|
} else {
|
||||||
switch config.sourceType {
|
switch config.sourceType {
|
||||||
case sourcePlatformImage:
|
case sourcePlatformImage:
|
||||||
|
@ -482,13 +482,13 @@ func buildsteps(config Config, info *client.ComputeInfo) []multistep.Step {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
addSteps(
|
addSteps(
|
||||||
&StepCreateNewDisk{
|
&StepCreateNewDiskset{
|
||||||
ResourceID: config.TemporaryOSDiskID,
|
OSDiskID: config.TemporaryOSDiskID,
|
||||||
DiskSizeGB: config.OSDiskSizeGB,
|
OSDiskSizeGB: config.OSDiskSizeGB,
|
||||||
DiskStorageAccountType: config.OSDiskStorageAccountType,
|
OSDiskStorageAccountType: config.OSDiskStorageAccountType,
|
||||||
HyperVGeneration: config.ImageHyperVGeneration,
|
HyperVGeneration: config.ImageHyperVGeneration,
|
||||||
Location: info.Location,
|
Location: info.Location,
|
||||||
PlatformImage: pi,
|
SourcePlatformImage: pi,
|
||||||
|
|
||||||
SkipCleanup: config.SkipCleanup,
|
SkipCleanup: config.SkipCleanup,
|
||||||
})
|
})
|
||||||
|
@ -502,13 +502,13 @@ func buildsteps(config Config, info *client.ComputeInfo) []multistep.Step {
|
||||||
SourceDiskResourceID: config.Source,
|
SourceDiskResourceID: config.Source,
|
||||||
Location: info.Location,
|
Location: info.Location,
|
||||||
},
|
},
|
||||||
&StepCreateNewDisk{
|
&StepCreateNewDiskset{
|
||||||
ResourceID: config.TemporaryOSDiskID,
|
OSDiskID: config.TemporaryOSDiskID,
|
||||||
DiskSizeGB: config.OSDiskSizeGB,
|
OSDiskSizeGB: config.OSDiskSizeGB,
|
||||||
DiskStorageAccountType: config.OSDiskStorageAccountType,
|
OSDiskStorageAccountType: config.OSDiskStorageAccountType,
|
||||||
HyperVGeneration: config.ImageHyperVGeneration,
|
HyperVGeneration: config.ImageHyperVGeneration,
|
||||||
SourceDiskResourceID: config.Source,
|
SourceOSDiskResourceID: config.Source,
|
||||||
Location: info.Location,
|
Location: info.Location,
|
||||||
|
|
||||||
SkipCleanup: config.SkipCleanup,
|
SkipCleanup: config.SkipCleanup,
|
||||||
})
|
})
|
||||||
|
@ -520,9 +520,9 @@ func buildsteps(config Config, info *client.ComputeInfo) []multistep.Step {
|
||||||
SubscriptionID: info.SubscriptionID,
|
SubscriptionID: info.SubscriptionID,
|
||||||
Location: info.Location,
|
Location: info.Location,
|
||||||
},
|
},
|
||||||
&StepCreateNewDisk{
|
&StepCreateNewDiskset{
|
||||||
ResourceID: config.TemporaryOSDiskID,
|
OSDiskID: config.TemporaryOSDiskID,
|
||||||
DiskSizeGB: config.OSDiskSizeGB,
|
OSDiskSizeGB: config.OSDiskSizeGB,
|
||||||
SourceImageResourceID: config.Source,
|
SourceImageResourceID: config.Source,
|
||||||
Location: info.Location,
|
Location: info.Location,
|
||||||
|
|
||||||
|
|
|
@ -151,9 +151,9 @@ func Test_buildsteps(t *testing.T) {
|
||||||
config: Config{FromScratch: true},
|
config: Config{FromScratch: true},
|
||||||
verify: func(steps []multistep.Step, _ *testing.T) {
|
verify: func(steps []multistep.Step, _ *testing.T) {
|
||||||
for _, s := range steps {
|
for _, s := range steps {
|
||||||
if s, ok := s.(*StepCreateNewDisk); ok {
|
if s, ok := s.(*StepCreateNewDiskset); ok {
|
||||||
if s.SourceDiskResourceID == "" &&
|
if s.SourceOSDiskResourceID == "" &&
|
||||||
s.PlatformImage == nil {
|
s.SourcePlatformImage == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
t.Errorf("found misconfigured StepCreateNewDisk: %+v", s)
|
t.Errorf("found misconfigured StepCreateNewDisk: %+v", s)
|
||||||
|
@ -166,10 +166,10 @@ func Test_buildsteps(t *testing.T) {
|
||||||
config: Config{Source: "publisher:offer:sku:version", sourceType: sourcePlatformImage},
|
config: Config{Source: "publisher:offer:sku:version", sourceType: sourcePlatformImage},
|
||||||
verify: func(steps []multistep.Step, _ *testing.T) {
|
verify: func(steps []multistep.Step, _ *testing.T) {
|
||||||
for _, s := range steps {
|
for _, s := range steps {
|
||||||
if s, ok := s.(*StepCreateNewDisk); ok {
|
if s, ok := s.(*StepCreateNewDiskset); ok {
|
||||||
if s.SourceDiskResourceID == "" &&
|
if s.SourceOSDiskResourceID == "" &&
|
||||||
s.PlatformImage != nil &&
|
s.SourcePlatformImage != nil &&
|
||||||
s.PlatformImage.Publisher == "publisher" {
|
s.SourcePlatformImage.Publisher == "publisher" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
t.Errorf("found misconfigured StepCreateNewDisk: %+v", s)
|
t.Errorf("found misconfigured StepCreateNewDisk: %+v", s)
|
||||||
|
@ -197,9 +197,9 @@ func Test_buildsteps(t *testing.T) {
|
||||||
config: Config{Source: "diskresourceid", sourceType: sourceDisk},
|
config: Config{Source: "diskresourceid", sourceType: sourceDisk},
|
||||||
verify: func(steps []multistep.Step, _ *testing.T) {
|
verify: func(steps []multistep.Step, _ *testing.T) {
|
||||||
for _, s := range steps {
|
for _, s := range steps {
|
||||||
if s, ok := s.(*StepCreateNewDisk); ok {
|
if s, ok := s.(*StepCreateNewDiskset); ok {
|
||||||
if s.SourceDiskResourceID == "diskresourceid" &&
|
if s.SourceOSDiskResourceID == "diskresourceid" &&
|
||||||
s.PlatformImage == nil {
|
s.SourcePlatformImage == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
t.Errorf("found misconfigured StepCreateNewDisk: %+v", s)
|
t.Errorf("found misconfigured StepCreateNewDisk: %+v", s)
|
||||||
|
|
|
@ -15,22 +15,26 @@ import (
|
||||||
"github.com/hashicorp/packer/packer"
|
"github.com/hashicorp/packer/packer"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ multistep.Step = &StepCreateNewDisk{}
|
var _ multistep.Step = &StepCreateNewDiskset{}
|
||||||
|
|
||||||
|
type StepCreateNewDiskset struct {
|
||||||
|
OSDiskID string // Disk ID
|
||||||
|
OSDiskSizeGB int32 // optional, ignored if 0
|
||||||
|
OSDiskStorageAccountType string // from compute.DiskStorageAccountTypes
|
||||||
|
|
||||||
type StepCreateNewDisk struct {
|
|
||||||
ResourceID string // Disk ID
|
|
||||||
subscriptionID, resourceGroup, diskName string // split out resource id
|
subscriptionID, resourceGroup, diskName string // split out resource id
|
||||||
|
|
||||||
DiskSizeGB int32 // optional, ignored if 0
|
HyperVGeneration string // For OS disk
|
||||||
DiskStorageAccountType string // from compute.DiskStorageAccountTypes
|
|
||||||
HyperVGeneration string
|
|
||||||
|
|
||||||
Location string
|
// Copy another disk
|
||||||
PlatformImage *client.PlatformImage
|
SourceOSDiskResourceID string
|
||||||
|
|
||||||
SourceDiskResourceID string
|
|
||||||
|
|
||||||
|
// Extract from platform image
|
||||||
|
SourcePlatformImage *client.PlatformImage
|
||||||
|
// Extract from shared image
|
||||||
SourceImageResourceID string
|
SourceImageResourceID string
|
||||||
|
// Location is needed for platform and shared images
|
||||||
|
Location string
|
||||||
|
|
||||||
SkipCleanup bool
|
SkipCleanup bool
|
||||||
}
|
}
|
||||||
|
@ -49,19 +53,19 @@ func parseDiskResourceID(resourceID string) (subscriptionID, resourceGroup, disk
|
||||||
return r.SubscriptionID, r.ResourceGroup, r.ResourceName, nil
|
return r.SubscriptionID, r.ResourceGroup, r.ResourceName, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepCreateNewDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
func (s *StepCreateNewDiskset) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
||||||
azcli := state.Get("azureclient").(client.AzureClientSet)
|
azcli := state.Get("azureclient").(client.AzureClientSet)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
state.Put(stateBagKey_OSDiskResourceID, s.ResourceID)
|
state.Put(stateBagKey_OSDiskResourceID, s.OSDiskID)
|
||||||
ui.Say(fmt.Sprintf("Creating disk '%s'", s.ResourceID))
|
ui.Say(fmt.Sprintf("Creating disk '%s'", s.OSDiskID))
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
s.subscriptionID, s.resourceGroup, s.diskName, err = parseDiskResourceID(s.ResourceID)
|
s.subscriptionID, s.resourceGroup, s.diskName, err = parseDiskResourceID(s.OSDiskID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("StepCreateNewDisk.Run: error: %+v", err)
|
log.Printf("StepCreateNewDisk.Run: error: %+v", err)
|
||||||
err := fmt.Errorf(
|
err := fmt.Errorf(
|
||||||
"error parsing resource id '%s': %v", s.ResourceID, err)
|
"error parsing resource id '%s': %v", s.OSDiskID, err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
ui.Error(err.Error())
|
ui.Error(err.Error())
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
|
@ -75,9 +79,9 @@ func (s *StepCreateNewDisk) Run(ctx context.Context, state multistep.StateBag) m
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.DiskStorageAccountType != "" {
|
if s.OSDiskStorageAccountType != "" {
|
||||||
disk.Sku = &compute.DiskSku{
|
disk.Sku = &compute.DiskSku{
|
||||||
Name: compute.DiskStorageAccountTypes(s.DiskStorageAccountType),
|
Name: compute.DiskStorageAccountTypes(s.OSDiskStorageAccountType),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,21 +89,21 @@ func (s *StepCreateNewDisk) Run(ctx context.Context, state multistep.StateBag) m
|
||||||
disk.DiskProperties.HyperVGeneration = compute.HyperVGeneration(s.HyperVGeneration)
|
disk.DiskProperties.HyperVGeneration = compute.HyperVGeneration(s.HyperVGeneration)
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.DiskSizeGB > 0 {
|
if s.OSDiskSizeGB > 0 {
|
||||||
disk.DiskProperties.DiskSizeGB = to.Int32Ptr(s.DiskSizeGB)
|
disk.DiskProperties.DiskSizeGB = to.Int32Ptr(s.OSDiskSizeGB)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case s.PlatformImage != nil:
|
case s.SourcePlatformImage != nil:
|
||||||
disk.CreationData.CreateOption = compute.FromImage
|
disk.CreationData.CreateOption = compute.FromImage
|
||||||
disk.CreationData.ImageReference = &compute.ImageDiskReference{
|
disk.CreationData.ImageReference = &compute.ImageDiskReference{
|
||||||
ID: to.StringPtr(fmt.Sprintf(
|
ID: to.StringPtr(fmt.Sprintf(
|
||||||
"/subscriptions/%s/providers/Microsoft.Compute/locations/%s/publishers/%s/artifacttypes/vmimage/offers/%s/skus/%s/versions/%s",
|
"/subscriptions/%s/providers/Microsoft.Compute/locations/%s/publishers/%s/artifacttypes/vmimage/offers/%s/skus/%s/versions/%s",
|
||||||
s.subscriptionID, s.Location, s.PlatformImage.Publisher, s.PlatformImage.Offer, s.PlatformImage.Sku, s.PlatformImage.Version)),
|
s.subscriptionID, s.Location, s.SourcePlatformImage.Publisher, s.SourcePlatformImage.Offer, s.SourcePlatformImage.Sku, s.SourcePlatformImage.Version)),
|
||||||
}
|
}
|
||||||
case s.SourceDiskResourceID != "":
|
case s.SourceOSDiskResourceID != "":
|
||||||
disk.CreationData.CreateOption = compute.Copy
|
disk.CreationData.CreateOption = compute.Copy
|
||||||
disk.CreationData.SourceResourceID = to.StringPtr(s.SourceDiskResourceID)
|
disk.CreationData.SourceResourceID = to.StringPtr(s.SourceOSDiskResourceID)
|
||||||
case s.SourceImageResourceID != "":
|
case s.SourceImageResourceID != "":
|
||||||
disk.CreationData.CreateOption = compute.FromImage
|
disk.CreationData.CreateOption = compute.FromImage
|
||||||
disk.CreationData.GalleryImageReference = &compute.ImageDiskReference{
|
disk.CreationData.GalleryImageReference = &compute.ImageDiskReference{
|
||||||
|
@ -118,7 +122,7 @@ func (s *StepCreateNewDisk) Run(ctx context.Context, state multistep.StateBag) m
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("StepCreateNewDisk.Run: error: %+v", err)
|
log.Printf("StepCreateNewDisk.Run: error: %+v", err)
|
||||||
err := fmt.Errorf(
|
err := fmt.Errorf(
|
||||||
"error creating new disk '%s': %v", s.ResourceID, err)
|
"error creating new disk '%s': %v", s.OSDiskID, err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
ui.Error(err.Error())
|
ui.Error(err.Error())
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
|
@ -127,18 +131,18 @@ func (s *StepCreateNewDisk) Run(ctx context.Context, state multistep.StateBag) m
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepCreateNewDisk) Cleanup(state multistep.StateBag) {
|
func (s *StepCreateNewDiskset) Cleanup(state multistep.StateBag) {
|
||||||
if !s.SkipCleanup {
|
if !s.SkipCleanup {
|
||||||
azcli := state.Get("azureclient").(client.AzureClientSet)
|
azcli := state.Get("azureclient").(client.AzureClientSet)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
ui.Say(fmt.Sprintf("Waiting for disk %q detach to complete", s.ResourceID))
|
ui.Say(fmt.Sprintf("Waiting for disk %q detach to complete", s.OSDiskID))
|
||||||
err := NewDiskAttacher(azcli).WaitForDetach(context.Background(), s.ResourceID)
|
err := NewDiskAttacher(azcli).WaitForDetach(context.Background(), s.OSDiskID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ui.Error(fmt.Sprintf("error detaching disk %q: %s", s.ResourceID, err))
|
ui.Error(fmt.Sprintf("error detaching disk %q: %s", s.OSDiskID, err))
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.Say(fmt.Sprintf("Deleting disk %q", s.ResourceID))
|
ui.Say(fmt.Sprintf("Deleting disk %q", s.OSDiskID))
|
||||||
|
|
||||||
f, err := azcli.DisksClient().Delete(context.TODO(), s.resourceGroup, s.diskName)
|
f, err := azcli.DisksClient().Delete(context.TODO(), s.resourceGroup, s.diskName)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -146,7 +150,7 @@ func (s *StepCreateNewDisk) Cleanup(state multistep.StateBag) {
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("StepCreateNewDisk.Cleanup: error: %+v", err)
|
log.Printf("StepCreateNewDisk.Cleanup: error: %+v", err)
|
||||||
ui.Error(fmt.Sprintf("error deleting disk '%s': %v.", s.ResourceID, err))
|
ui.Error(fmt.Sprintf("error deleting disk '%s': %v.", s.OSDiskID, err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -99,14 +99,14 @@ func TestStepCreateNewDisk_Run(t *testing.T) {
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
s := StepCreateNewDisk{
|
s := StepCreateNewDiskset{
|
||||||
ResourceID: tt.fields.ResourceID,
|
OSDiskID: tt.fields.ResourceID,
|
||||||
DiskSizeGB: tt.fields.DiskSizeGB,
|
OSDiskSizeGB: tt.fields.DiskSizeGB,
|
||||||
DiskStorageAccountType: tt.fields.DiskStorageAccountType,
|
OSDiskStorageAccountType: tt.fields.DiskStorageAccountType,
|
||||||
HyperVGeneration: tt.fields.HyperVGeneration,
|
HyperVGeneration: tt.fields.HyperVGeneration,
|
||||||
Location: tt.fields.Location,
|
Location: tt.fields.Location,
|
||||||
PlatformImage: tt.fields.PlatformImage,
|
SourcePlatformImage: tt.fields.PlatformImage,
|
||||||
SourceDiskResourceID: tt.fields.SourceDiskResourceID,
|
SourceOSDiskResourceID: tt.fields.SourceDiskResourceID,
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedPutDiskBody := regexp.MustCompile(`[\s\n]`).ReplaceAllString(tt.fields.expectedPutDiskBody, "")
|
expectedPutDiskBody := regexp.MustCompile(`[\s\n]`).ReplaceAllString(tt.fields.expectedPutDiskBody, "")
|
Loading…
Reference in New Issue