management/location: Refactoring
- Removed client side logic from `management/location` - Renamed return structs to reflect operations better - Moved helpers to where they are needed
This commit is contained in:
Родитель
4835857e0b
Коммит
c3a30be4bc
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче