-
Notifications
You must be signed in to change notification settings - Fork 180
/
client_provider.go
52 lines (40 loc) · 1.12 KB
/
client_provider.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
package gcp
import (
"context"
"fmt"
"net/http"
"github.com/cloudfoundry/bosh-bootloader/storage"
compute "google.golang.org/api/compute/v1"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
)
func gcpHTTPClientFunc(config *jwt.Config) *http.Client {
return config.Client(context.Background())
}
var gcpHTTPClient = gcpHTTPClientFunc
func NewClient(gcpConfig storage.GCP, basePath string) (Client, error) {
config, err := google.JWTConfigFromJSON([]byte(gcpConfig.ServiceAccountKey), compute.ComputeScope)
if err != nil {
return Client{}, fmt.Errorf("parse service account key: %s", err)
}
if basePath != "" {
config.TokenURL = basePath
}
service, err := compute.New(gcpHTTPClient(config))
if err != nil {
return Client{}, fmt.Errorf("create gcp client: %s", err)
}
if basePath != "" {
service.BasePath = basePath
}
client := Client{
computeClient: gcpComputeClient{service: service},
projectID: gcpConfig.ProjectID,
zone: gcpConfig.Zone,
}
_, err = client.GetRegion(gcpConfig.Region)
if err != nil {
return Client{}, fmt.Errorf("get region: %s", err)
}
return client, nil
}