-
Notifications
You must be signed in to change notification settings - Fork 72
/
gcloud.go
53 lines (42 loc) · 1.05 KB
/
gcloud.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
package main
import (
"context"
"fmt"
"net/http"
"golang.org/x/oauth2/google"
"google.golang.org/api/compute/v1"
)
type GCloud struct {
Client *http.Client
ProjectID string
Service *compute.Service
Zone string
}
type GCloudClient interface {
DeleteNode(string) error
}
// NewGCloudClient return a GCloud client
func NewGCloudClient(projectId string, zone string) (gcloud GCloudClient, err error) {
client, err := google.DefaultClient(context.Background(), compute.ComputeScope)
if err != nil {
err = fmt.Errorf("Error creating compute client:\n%v", err)
return
}
service, err := compute.New(client)
if err != nil {
err = fmt.Errorf("Error creating compute service:\n%v", err)
return
}
gcloud = &GCloud{
Client: client,
ProjectID: projectId,
Service: service,
Zone: zone,
}
return
}
// DeleteNode delete a GCloud instance from a given node name
func (g *GCloud) DeleteNode(name string) (err error) {
_, err = g.Service.Instances.Delete(g.ProjectID, g.Zone, name).Context(context.Background()).Do()
return
}