-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlocation.go
42 lines (34 loc) · 964 Bytes
/
location.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
package run
import (
"context"
"fmt"
"net/http"
"github.com/pkg/errors"
)
const (
// regionAPIBaseURL is the base URL for GCP project API, and
// the parameter is project NUMBER (not ID).
regionAPIBaseURL = "https://run.googleapis.com/v1/projects/%s/locations"
)
type locationList struct {
Locations []*location `json:"locations"`
}
type location struct {
ID string `json:"locationId"`
Name string `json:"displayName"`
}
func getLocations(ctx context.Context, projectNumber string) ([]*location, error) {
if projectNumber == "" {
return nil, errors.New("project number is empty")
}
u := fmt.Sprintf(regionAPIBaseURL, projectNumber)
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, errors.Wrap(err, "error client creating request")
}
var list locationList
if err := apiClient.Get(ctx, req, &list); err != nil {
return nil, errors.Wrap(err, "error decoding response")
}
return list.Locations, nil
}