forked from tsuru/tsuru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gce_client.go
80 lines (70 loc) · 2.3 KB
/
gce_client.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
// Copyright 2017 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package integration
import (
"fmt"
"sort"
"golang.org/x/net/context"
"google.golang.org/api/container/v1"
"google.golang.org/api/option"
"google.golang.org/api/transport"
)
const (
prodAddr = "https://container.googleapis.com/"
userAgent = "gcloud-golang-container/20151008"
)
type gceClient struct {
projectID string
svc *container.Service
}
func newClient(ctx context.Context, projectID string, opts ...option.ClientOption) (*gceClient, error) {
o := []option.ClientOption{
option.WithEndpoint(prodAddr),
option.WithScopes(container.CloudPlatformScope),
option.WithUserAgent(userAgent),
}
o = append(o, opts...)
httpClient, endpoint, err := transport.NewHTTPClient(ctx, o...)
if err != nil {
return nil, fmt.Errorf("dialing: %v", err)
}
svc, err := container.New(httpClient)
if err != nil {
return nil, fmt.Errorf("constructing container client: %v", err)
}
svc.BasePath = endpoint
c := &gceClient{
projectID: projectID,
svc: svc,
}
return c, nil
}
func (c *gceClient) createCluster(ctx context.Context, name, zone, machineType string, nodeCount int64) error {
serverConfig, err := c.svc.Projects.Zones.GetServerconfig(c.projectID, zone).Context(ctx).Do()
if err != nil {
return err
}
var version string
if len(serverConfig.ValidMasterVersions) > 0 {
sort.Sort(sort.Reverse(sort.StringSlice(serverConfig.ValidMasterVersions)))
version = serverConfig.ValidMasterVersions[0]
}
config := &container.CreateClusterRequest{
Cluster: &container.Cluster{
Name: name,
InitialNodeCount: nodeCount,
InitialClusterVersion: version,
NodeConfig: &container.NodeConfig{MachineType: machineType},
},
}
_, err = c.svc.Projects.Zones.Clusters.Create(c.projectID, zone, config).Context(ctx).Do()
return err
}
func (c *gceClient) describeCluster(ctx context.Context, name, zone string) (*container.Cluster, error) {
return c.svc.Projects.Zones.Clusters.Get(c.projectID, zone, name).Context(ctx).Do()
}
func (c *gceClient) deleteCluster(ctx context.Context, name, zone string) error {
_, err := c.svc.Projects.Zones.Clusters.Delete(c.projectID, zone, name).Context(ctx).Do()
return err
}