diff --git a/management/location/client.go b/management/location/client.go index 1908626ab3..b41f585a61 100644 --- a/management/location/client.go +++ b/management/location/client.go @@ -2,14 +2,12 @@ package location import ( "encoding/xml" - "fmt" "github.com/Azure/azure-sdk-for-go/management" ) const ( azureLocationListURL = "locations" - errInvalidLocation = "Invalid location: %s. Available locations: %s" errParamNotSpecified = "Parameter %s is not specified." ) @@ -18,60 +16,14 @@ func NewClient(client management.Client) LocationClient { return LocationClient{client: client} } -func (c LocationClient) ResolveLocation(location string) error { - if location == "" { - return fmt.Errorf(errParamNotSpecified, "location") - } - - locations, err := c.GetLocationList() - if err != nil { - return err - } - - for _, existingLocation := range locations.Locations { - if existingLocation.Name != location { - continue - } - - return nil - } - - return fmt.Errorf(errInvalidLocation, location, locations) -} - -func (c LocationClient) GetLocationList() (LocationList, error) { - locationList := LocationList{} +func (c LocationClient) ListLocations() (ListLocationsResponse, error) { + var l ListLocationsResponse response, err := c.client.SendAzureGetRequest(azureLocationListURL) if err != nil { - return locationList, err + return l, err } - err = xml.Unmarshal(response, &locationList) - if err != nil { - return locationList, err - } - - return locationList, nil -} - -func (c LocationClient) GetLocation(location string) (*Location, error) { - if location == "" { - return nil, fmt.Errorf(errParamNotSpecified, "location") - } - - locations, err := c.GetLocationList() - if err != nil { - return nil, err - } - - for _, existingLocation := range locations.Locations { - if existingLocation.Name != location { - continue - } - - return &existingLocation, nil - } - - return nil, fmt.Errorf(errInvalidLocation, location, locations) + err = xml.Unmarshal(response, &l) + return l, err } diff --git a/management/location/entities.go b/management/location/entities.go index 637e307f44..f2420f7bf1 100644 --- a/management/location/entities.go +++ b/management/location/entities.go @@ -14,7 +14,7 @@ type LocationClient struct { client management.Client } -type LocationList struct { +type ListLocationsResponse struct { XMLName xml.Name `xml:"Locations"` Locations []Location `xml:"Location"` } @@ -27,11 +27,10 @@ type Location struct { VirtualMachineRoleSizes []string `xml:"ComputeCapabilities>VirtualMachinesRoleSizes>RoleSize"` } -func (locationList LocationList) String() string { +func (ll ListLocationsResponse) String() string { var buf bytes.Buffer - - for _, location := range locationList.Locations { - buf.WriteString(fmt.Sprintf("%s, ", location.Name)) + for _, l := range ll.Locations { + fmt.Fprintf(&buf, "%s, ", l.Name) } return strings.Trim(buf.String(), ", ") diff --git a/management/vmutils/integration_test.go b/management/vmutils/integration_test.go index c30b321ad3..a8d21de39c 100644 --- a/management/vmutils/integration_test.go +++ b/management/vmutils/integration_test.go @@ -218,7 +218,7 @@ func GetTestStorageAccount(t *testing.T, client management.Client) storage.Stora if len(ssl.StorageServices) == 0 { t.Log("No storage accounts found, creating a new one") lc := location.NewClient(client) - ll, err := lc.GetLocationList() + ll, err := lc.ListLocations() if err != nil { t.Fatal(err) } diff --git a/management/vmutils/rolesize.go b/management/vmutils/rolesize.go index 272d731955..bedd702fb5 100644 --- a/management/vmutils/rolesize.go +++ b/management/vmutils/rolesize.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/Azure/azure-sdk-for-go/management" - locationclient "github.com/Azure/azure-sdk-for-go/management/location" + lc "github.com/Azure/azure-sdk-for-go/management/location" vm "github.com/Azure/azure-sdk-for-go/management/virtualmachine" ) @@ -31,8 +31,7 @@ func IsRoleSizeValid(vmclient vm.VirtualMachineClient, roleSizeName string) (boo } // IsRoleSizeAvailableInLocation retrieves all available sizes in the specified -// location using location.GetLocation() and returns whether that the provided -// roleSizeName is part of that list. +// location and returns whether that the provided roleSizeName is part of that list. func IsRoleSizeAvailableInLocation(managementclient management.Client, location, roleSizeName string) (bool, error) { if location == "" { return false, fmt.Errorf(errParamNotSpecified, "location") @@ -41,8 +40,8 @@ func IsRoleSizeAvailableInLocation(managementclient management.Client, location, return false, fmt.Errorf(errParamNotSpecified, "roleSizeName") } - locationClient := locationclient.NewClient(managementclient) - locationInfo, err := locationClient.GetLocation(location) + locationClient := lc.NewClient(managementclient) + locationInfo, err := getLocation(locationClient, location) if err != nil { return false, err } @@ -55,3 +54,23 @@ func IsRoleSizeAvailableInLocation(managementclient management.Client, location, return false, nil } + +func getLocation(c lc.LocationClient, location string) (*lc.Location, error) { + if location == "" { + return nil, fmt.Errorf(errParamNotSpecified, "location") + } + + locations, err := c.ListLocations() + if err != nil { + return nil, err + } + + for _, existingLocation := range locations.Locations { + if existingLocation.Name != location { + continue + } + + return &existingLocation, nil + } + return nil, fmt.Errorf("Invalid location: %s. Available locations: %s", location, locations) +}