forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gke_version_endpoint.go
98 lines (75 loc) · 1.96 KB
/
gke_version_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
89
90
91
92
93
94
95
96
97
98
package capabilities
import (
"context"
"encoding/json"
"fmt"
"google.golang.org/api/container/v1"
"net/http"
)
const (
defaultCredentialEnv = "GOOGLE_APPLICATION_CREDENTIALS"
)
// NewGKEVersionsHandler creates a new GKEVersionsHandler
func NewGKEVersionsHandler() *GKEVersionHandler {
return &GKEVersionHandler{}
}
// GKEVersionHandler for listing available Kubernetes versions in GKE
type GKEVersionHandler struct {
}
type errorResponse struct {
Error string `json:"error"`
}
func (g *GKEVersionHandler) 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 = validateRequestBody(writer, &body.capabilitiesRequestBody)
if err != nil {
handleErr(writer, err)
return
}
zone := body.Zone
if zone == "" {
writer.WriteHeader(http.StatusBadRequest)
handleErr(writer, fmt.Errorf("invalid zone"))
return
}
client, err := g.getServiceClient(context.Background(), body.Credentials)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
handleErr(writer, err)
return
}
result, err := client.Projects.Zones.GetServerconfig(body.ProjectID, zone).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 *GKEVersionHandler) getServiceClient(ctx context.Context, credentialContent string) (*container.Service, error) {
client, err := getOAuthClient(ctx, credentialContent)
if err != nil {
return nil, err
}
service, err := container.New(client)
if err != nil {
return nil, err
}
return service, nil
}