forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gke_zones_endpoint.go
88 lines (67 loc) · 1.75 KB
/
gke_zones_endpoint.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package capabilities
import (
"context"
"encoding/json"
"google.golang.org/api/compute/v1"
"net/http"
)
// NewGKEZonesHandler creates a new GKEZonesHandler
func NewGKEZonesHandler() *GKEZonesHandler {
return &GKEZonesHandler{}
}
// GKEZonesHandler for listing available GKE zones
type GKEZonesHandler struct {
Field string
}
type zoneCapabilitiesRequestBody struct {
capabilitiesRequestBody
Zone string `json:"zone"`
}
func (g *GKEZonesHandler) ServeHTTP(writer http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
writer.WriteHeader(http.StatusMethodNotAllowed)
return
}
writer.Header().Set("Content-Type", "application/json")
var body zoneCapabilitiesRequestBody
err := extractRequestBody(writer, req, &body)
if err != nil {
handleErr(writer, err)
return
}
err = validateCapabilitiesRequestBody(writer, &body.capabilitiesRequestBody)
if err != nil {
handleErr(writer, err)
return
}
client, err := g.getServiceClient(context.Background(), body.Credentials)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
handleErr(writer, err)
return
}
result, err := client.Zones.List(body.ProjectID).Do()
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
handleErr(writer, err)
return
}
serialized, err := json.Marshal(result)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
handleErr(writer, err)
return
}
writer.Write(serialized)
}
func (g *GKEZonesHandler) getServiceClient(ctx context.Context, credentialContent string) (*compute.Service, error) {
client, err := getOAuthClient(ctx, credentialContent)
if err != nil {
return nil, err
}
service, err := compute.New(client)
if err != nil {
return nil, err
}
return service, nil
}