forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
locationClient.go
57 lines (45 loc) · 1.16 KB
/
locationClient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package locationClient
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
azure "github.com/MSOpenTech/azure-sdk-for-go"
"strings"
)
const (
azureLocationListURL = "locations"
invalidLocationError = "Invalid location: %s. Available locations: %s"
)
func ResolveLocation(location string) error {
if len(location) == 0 {
return fmt.Errorf(azure.ParamNotSpecifiedError, "location")
}
locations, err := GetLocationList()
if err != nil {
return err
}
for _, existingLocation := range locations.Locations {
if existingLocation.Name != location {
continue
}
return nil
}
var availableLocations bytes.Buffer
for _, existingLocation := range locations.Locations {
availableLocations.WriteString(existingLocation.Name + ", ")
}
return errors.New(fmt.Sprintf(invalidLocationError, location, strings.Trim(availableLocations.String(), ", ")))
}
func GetLocationList() (LocationList, error) {
locationList := LocationList{}
response, err := azure.SendAzureGetRequest(azureLocationListURL)
if err != nil {
return locationList, err
}
err = xml.Unmarshal(response, &locationList)
if err != nil {
return locationList, err
}
return locationList, nil
}