Skip to content
This repository has been archived by the owner on Feb 13, 2021. It is now read-only.

Commit

Permalink
move location checking to validate step and check available sizes in …
Browse files Browse the repository at this point in the history
…location (#267)
  • Loading branch information
paulmey authored Oct 10, 2016
1 parent 1eaf690 commit 55faaa3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 51 deletions.
51 changes: 2 additions & 49 deletions packer/builder/azure/smapi/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,8 @@ func newConfig(raws ...interface{}) (*Config, []string, error) {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("location must be specified"))
}

sizeIsValid := false

for _, instanceSize := range allowedVMSizes {
if c.InstanceSize == instanceSize {
sizeIsValid = true
break
}
}

if !sizeIsValid {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("instance_size is not valid, must be one of: %v", allowedVMSizes))
if c.InstanceSize == "" {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("instance_size must be specified"))
}

for n := 0; n < len(c.DataDisks); n++ {
Expand Down Expand Up @@ -186,41 +177,3 @@ func newConfig(raws ...interface{}) (*Config, []string, error) {

return &c, nil, nil
}

// Target sizes
var allowedVMSizes = []string{
"ExtraSmall",
"Small",
"Medium",
"Large",
"ExtraLarge",
"A5",
"A6",
"A7",
"A8",
"A9",

"Standard_D1",
"Standard_D2",
"Standard_D3",
"Standard_D4",
"Standard_D11",
"Standard_D12",
"Standard_D13",
"Standard_D14",

"Standard_DS1",
"Standard_DS2",
"Standard_DS3",
"Standard_DS4",
"Standard_DS11",
"Standard_DS12",
"Standard_DS13",
"Standard_DS14",

"Standard_G1",
"Standard_G2",
"Standard_G3",
"Standard_G4",
"Standard_G5",
}
38 changes: 36 additions & 2 deletions packer/builder/azure/smapi/step_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/Azure/packer-azure/packer/builder/azure/common/constants"

"github.com/Azure/azure-sdk-for-go/management"
"github.com/Azure/azure-sdk-for-go/management/location"
"github.com/Azure/azure-sdk-for-go/management/osimage"
"github.com/Azure/azure-sdk-for-go/management/storageservice"
vm "github.com/Azure/azure-sdk-for-go/management/virtualmachine"
Expand All @@ -31,9 +32,42 @@ func (*StepValidate) Run(state multistep.StateBag) multistep.StepAction {

ui.Say("Validating Azure options...")

locationsResponse, err := location.NewClient(client).ListLocations()
if err != nil {
err = fmt.Errorf("Error checking location: %v", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if err := func() error {
for _, l := range locationsResponse.Locations {
if config.Location == l.Name {
ui.Message("Checking instance size availability...")
if !func() bool {
for _, size := range l.VirtualMachineRoleSizes {
if size == config.InstanceSize {
return true
}
}
return false
}() {
sizes := strings.Join(l.VirtualMachineRoleSizes, ",")
return fmt.Errorf("Instance size %q not available in location %q for this subscription, valid instance sizes: %s",
config.InstanceSize, config.Location, sizes)
}
return nil
}
}
return fmt.Errorf("Location %q not available for this subscription, valid locations: %s", config.Location, locationsResponse.String())
}(); err != nil {
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}

role := vmutils.NewVMConfiguration(config.tmpVmName, config.InstanceSize)

ui.Message("Checking Storage Account...")
ui.Message("Checking storage account...")
destinationVhd, err := validateStorageAccount(config, client)
if err != nil {
err = fmt.Errorf("Error checking storage account: %v", err)
Expand Down Expand Up @@ -203,7 +237,7 @@ func validateStorageAccount(config *Config, client management.Client) (string, e
}

if sa.StorageServiceProperties.Location != config.Location {
return "", fmt.Errorf("Storage Account %q is not in location %q, but in location %q.",
return "", fmt.Errorf("Storage account %q is not in location %q, but in location %q.",
config.StorageAccount, sa.StorageServiceProperties.Location, config.Location)
}

Expand Down

0 comments on commit 55faaa3

Please sign in to comment.