/
gce.go
135 lines (111 loc) · 3.07 KB
/
gce.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package gcloudcleanup
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"cloud.google.com/go/storage"
"cloud.google.com/go/trace"
googlecloudtrace "cloud.google.com/go/trace"
"github.com/pkg/errors"
"go.opencensus.io/plugin/ochttp"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
"google.golang.org/api/compute/v1"
"google.golang.org/api/option"
)
type gceAccountJSON struct {
ClientEmail string `json:"client_email"`
PrivateKey string `json:"private_key"`
}
func buildGoogleComputeService(accountJSON string) (*compute.Service, error) {
if accountJSON == "" {
client, err := google.DefaultClient(context.TODO(), compute.DevstorageFullControlScope, compute.ComputeScope)
if err != nil {
return nil, errors.Wrap(err, "could not build default client")
}
return compute.New(client)
}
a, err := loadGoogleAccountJSON(accountJSON)
if err != nil {
return nil, err
}
config := jwt.Config{
Email: a.ClientEmail,
PrivateKey: []byte(a.PrivateKey),
Scopes: []string{
compute.DevstorageFullControlScope,
compute.ComputeScope,
},
TokenURL: "https://accounts.google.com/o/oauth2/token",
}
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, &http.Client{
Transport: &ochttp.Transport{},
})
client := config.Client(ctx)
cs, err := compute.New(client)
if err != nil {
return nil, err
}
cs.UserAgent = "gcloud-cleanup"
return cs, nil
}
func buildGoogleStorageClient(ctx context.Context, accountJSON string) (*storage.Client, error) {
if accountJSON == "" {
creds, err := google.FindDefaultCredentials(ctx, storage.ScopeReadWrite)
if err != nil {
return nil, errors.Wrap(err, "could not build default client")
}
return storage.NewClient(ctx, option.WithCredentials(creds))
}
credBytes, err := loadBytes(accountJSON)
if err != nil {
return nil, err
}
creds, err := google.CredentialsFromJSON(ctx, credBytes, storage.ScopeReadWrite)
if err != nil {
return nil, err
}
return storage.NewClient(ctx, option.WithCredentials(creds))
}
func buildGoogleCloudCredentials(ctx context.Context, accountJSON string) (*google.Credentials, error) {
if accountJSON == "" {
creds, err := google.FindDefaultCredentials(ctx, googlecloudtrace.ScopeTraceAppend)
return creds, errors.Wrap(err, "could not build default client")
}
credBytes, err := loadBytes(accountJSON)
if err != nil {
return nil, err
}
creds, err := google.CredentialsFromJSON(ctx, credBytes, trace.ScopeTraceAppend)
if err != nil {
return nil, err
}
return creds, nil
}
func loadGoogleAccountJSON(filenameOrJSON string) (*gceAccountJSON, error) {
bytes, err := loadBytes(filenameOrJSON)
if err != nil {
return nil, err
}
a := &gceAccountJSON{}
err = json.Unmarshal(bytes, a)
return a, err
}
func loadBytes(filenameOrJSON string) ([]byte, error) {
var (
bytes []byte
err error
)
if strings.HasPrefix(strings.TrimSpace(filenameOrJSON), "{") {
bytes = []byte(filenameOrJSON)
} else {
bytes, err = ioutil.ReadFile(filenameOrJSON)
if err != nil {
return nil, err
}
}
return bytes, nil
}