Merge pull request #140 from ahmetalpbalkan/location

management/location: Refactoring
This commit is contained in:
Paul Meyer 2015-05-18 15:41:41 -07:00
Родитель 32624e7b6c c3a30be4bc
Коммит 0f045e6369
4 изменённых файлов: 34 добавлений и 64 удалений

Просмотреть файл

@ -3,14 +3,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."
)
@ -19,60 +17,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
}

Просмотреть файл

@ -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(), ", ")

Просмотреть файл

@ -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)
}

Просмотреть файл

@ -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)
}