37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
gke_version_endpoint.go 1.96 KB
一键复制 编辑 原始数据 按行查看 历史
Nathan Jenan 提交于 2018-01-19 22:04 . Fixing lint, fmt issues
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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.0-rc3

搜索帮助

0d507c66 1850385 C8b1a773 1850385