forked from cloudfoundry/bosh-bootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcp.go
74 lines (59 loc) · 1.74 KB
/
gcp.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
package actors
import (
"context"
"io/ioutil"
"strings"
"golang.org/x/oauth2/google"
acceptance "github.com/cloudfoundry/bosh-bootloader/acceptance-tests"
compute "google.golang.org/api/compute/v1"
. "github.com/onsi/gomega"
)
type GCP struct {
service *compute.Service
projectID string
region string
zone string
}
func NewGCP(config acceptance.Config) GCP {
rawServiceAccountKey, err := ioutil.ReadFile(config.GCPServiceAccountKey)
if err != nil {
rawServiceAccountKey = []byte(config.GCPServiceAccountKey)
}
googleConfig, err := google.JWTConfigFromJSON(rawServiceAccountKey, "https://www.googleapis.com/auth/compute")
if err != nil {
panic(err)
}
service, err := compute.New(googleConfig.Client(context.Background()))
if err != nil {
panic(err)
}
return GCP{
service: service,
projectID: config.GCPProjectID,
region: config.GCPRegion,
zone: config.GCPZone,
}
}
func (g GCP) GetNetwork(networkName string) (*compute.Network, error) {
return g.service.Networks.Get(g.projectID, networkName).Do()
}
func (g GCP) GetTargetPool(targetPoolName string) (*compute.TargetPool, error) {
return g.service.TargetPools.Get(g.projectID, g.region, targetPoolName).Do()
}
func (g GCP) GetTargetHTTPSProxy(name string) (*compute.TargetHttpsProxy, error) {
return g.service.TargetHttpsProxies.Get(g.projectID, name).Do()
}
func (g GCP) NetworkHasBOSHDirector(envID string) bool {
list, err := g.service.Instances.List(g.projectID, g.zone).
Filter("labels.director:bosh-init").
Do()
Expect(err).NotTo(HaveOccurred())
for _, item := range list.Items {
for _, networkInterface := range item.NetworkInterfaces {
if strings.Contains(networkInterface.Network, envID) {
return true
}
}
}
return false
}