/
gce.go
41 lines (36 loc) · 869 Bytes
/
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
package gce
import (
"context"
"golang.org/x/oauth2/google"
compute "google.golang.org/api/compute/v1"
)
// Client is a placeholder for GCE stuff.
type Client struct {
Service *compute.Service
ProjectID string
}
// CreateGCECloud creates a new instance of GCECloud.
func New(project string) (*Client, error) {
// Use oauth2.NoContext if there isn't a good context to pass in.
ctx := context.TODO()
client, err := google.DefaultClient(ctx, compute.ComputeScope)
if err != nil {
return nil, err
}
c, err := compute.New(client)
if err != nil {
return nil, err
}
if project == "" {
credentials, err := google.FindDefaultCredentials(ctx, compute.ComputeScope)
if err != nil {
return nil, err
}
project = credentials.ProjectID
}
// TODO validate project and network exist
return &Client{
Service: c,
ProjectID: project,
}, nil
}