...
1
2
3 package vmutils
4
5
6
7
8 import (
9 "fmt"
10
11 "github.com/Azure/azure-sdk-for-go/services/classic/management"
12 lc "github.com/Azure/azure-sdk-for-go/services/classic/management/location"
13 vm "github.com/Azure/azure-sdk-for-go/services/classic/management/virtualmachine"
14 )
15
16
17
18
19 func IsRoleSizeValid(vmclient vm.VirtualMachineClient, roleSizeName string) (bool, error) {
20 if roleSizeName == "" {
21 return false, fmt.Errorf(errParamNotSpecified, "roleSizeName")
22 }
23
24 roleSizeList, err := vmclient.GetRoleSizeList()
25 if err != nil {
26 return false, err
27 }
28
29 for _, roleSize := range roleSizeList.RoleSizes {
30 if roleSize.Name == roleSizeName {
31 return true, nil
32 }
33 }
34
35 return false, nil
36 }
37
38
39
40 func IsRoleSizeAvailableInLocation(managementclient management.Client, location, roleSizeName string) (bool, error) {
41 if location == "" {
42 return false, fmt.Errorf(errParamNotSpecified, "location")
43 }
44 if roleSizeName == "" {
45 return false, fmt.Errorf(errParamNotSpecified, "roleSizeName")
46 }
47
48 locationClient := lc.NewClient(managementclient)
49 locationInfo, err := getLocation(locationClient, location)
50 if err != nil {
51 return false, err
52 }
53
54 for _, availableRoleSize := range locationInfo.VirtualMachineRoleSizes {
55 if availableRoleSize == roleSizeName {
56 return true, nil
57 }
58 }
59
60 return false, nil
61 }
62
63 func getLocation(c lc.LocationClient, location string) (*lc.Location, error) {
64 if location == "" {
65 return nil, fmt.Errorf(errParamNotSpecified, "location")
66 }
67
68 locations, err := c.ListLocations()
69 if err != nil {
70 return nil, err
71 }
72
73 for _, existingLocation := range locations.Locations {
74 if existingLocation.Name != location {
75 continue
76 }
77
78 return &existingLocation, nil
79 }
80 return nil, fmt.Errorf("Invalid location: %s. Available locations: %s", location, locations)
81 }
82
View as plain text