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

Commit 55faaa3

Browse files
authored
move location checking to validate step and check available sizes in location (#267)
1 parent 1eaf690 commit 55faaa3

File tree

2 files changed

+38
-51
lines changed

2 files changed

+38
-51
lines changed

packer/builder/azure/smapi/config.go

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,8 @@ func newConfig(raws ...interface{}) (*Config, []string, error) {
135135
errs = packer.MultiErrorAppend(errs, fmt.Errorf("location must be specified"))
136136
}
137137

138-
sizeIsValid := false
139-
140-
for _, instanceSize := range allowedVMSizes {
141-
if c.InstanceSize == instanceSize {
142-
sizeIsValid = true
143-
break
144-
}
145-
}
146-
147-
if !sizeIsValid {
148-
errs = packer.MultiErrorAppend(errs, fmt.Errorf("instance_size is not valid, must be one of: %v", allowedVMSizes))
138+
if c.InstanceSize == "" {
139+
errs = packer.MultiErrorAppend(errs, fmt.Errorf("instance_size must be specified"))
149140
}
150141

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

187178
return &c, nil, nil
188179
}
189-
190-
// Target sizes
191-
var allowedVMSizes = []string{
192-
"ExtraSmall",
193-
"Small",
194-
"Medium",
195-
"Large",
196-
"ExtraLarge",
197-
"A5",
198-
"A6",
199-
"A7",
200-
"A8",
201-
"A9",
202-
203-
"Standard_D1",
204-
"Standard_D2",
205-
"Standard_D3",
206-
"Standard_D4",
207-
"Standard_D11",
208-
"Standard_D12",
209-
"Standard_D13",
210-
"Standard_D14",
211-
212-
"Standard_DS1",
213-
"Standard_DS2",
214-
"Standard_DS3",
215-
"Standard_DS4",
216-
"Standard_DS11",
217-
"Standard_DS12",
218-
"Standard_DS13",
219-
"Standard_DS14",
220-
221-
"Standard_G1",
222-
"Standard_G2",
223-
"Standard_G3",
224-
"Standard_G4",
225-
"Standard_G5",
226-
}

packer/builder/azure/smapi/step_validate.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/Azure/packer-azure/packer/builder/azure/common/constants"
1212

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

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

35+
locationsResponse, err := location.NewClient(client).ListLocations()
36+
if err != nil {
37+
err = fmt.Errorf("Error checking location: %v", err)
38+
state.Put("error", err)
39+
ui.Error(err.Error())
40+
return multistep.ActionHalt
41+
}
42+
if err := func() error {
43+
for _, l := range locationsResponse.Locations {
44+
if config.Location == l.Name {
45+
ui.Message("Checking instance size availability...")
46+
if !func() bool {
47+
for _, size := range l.VirtualMachineRoleSizes {
48+
if size == config.InstanceSize {
49+
return true
50+
}
51+
}
52+
return false
53+
}() {
54+
sizes := strings.Join(l.VirtualMachineRoleSizes, ",")
55+
return fmt.Errorf("Instance size %q not available in location %q for this subscription, valid instance sizes: %s",
56+
config.InstanceSize, config.Location, sizes)
57+
}
58+
return nil
59+
}
60+
}
61+
return fmt.Errorf("Location %q not available for this subscription, valid locations: %s", config.Location, locationsResponse.String())
62+
}(); err != nil {
63+
state.Put("error", err)
64+
ui.Error(err.Error())
65+
return multistep.ActionHalt
66+
}
67+
3468
role := vmutils.NewVMConfiguration(config.tmpVmName, config.InstanceSize)
3569

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

205239
if sa.StorageServiceProperties.Location != config.Location {
206-
return "", fmt.Errorf("Storage Account %q is not in location %q, but in location %q.",
240+
return "", fmt.Errorf("Storage account %q is not in location %q, but in location %q.",
207241
config.StorageAccount, sa.StorageServiceProperties.Location, config.Location)
208242
}
209243

0 commit comments

Comments
 (0)