This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
83 lines (68 loc) · 2.13 KB
/
client.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
package provider
import (
"fmt"
"log"
"net/http"
"runtime"
"github.com/hashicorp/terraform/helper/logging"
"github.com/hashicorp/terraform/helper/pathorcontents"
"github.com/hashicorp/terraform/terraform"
"github.com/pkg/errors"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
billing "google.golang.org/api/cloudbilling/v1"
)
var OAuthScopes = []string{
billing.CloudPlatformScope,
}
// Client is a structure that will be provided to terraform as "meta"
type ProviderClient struct {
billing *billing.APIService
}
func test() error {
return nil
}
func apiClientFromCredentialsFile(crendentialsFile string) (*http.Client, error) {
log.Printf("[INFO] Authenticating using provided credentials file")
fileContent, _, err := pathorcontents.Read(crendentialsFile)
if err != nil {
return nil, err
}
jwtConfig, err := google.JWTConfigFromJSON([]byte(fileContent), OAuthScopes...)
if err != nil {
return nil, err
}
return jwtConfig.Client(context.Background()), nil
}
func apiClientFromDefault() (*http.Client, error) {
log.Printf("[INFO] Authenticating using DefaultClient")
return google.DefaultClient(context.Background(), OAuthScopes...)
}
func (pc *ProviderClient) init(crendentialsFile string) error {
var apiClient *http.Client
var err error
if crendentialsFile != "" {
apiClient, err = apiClientFromCredentialsFile(crendentialsFile)
if err != nil {
return errors.Wrap(err, "Authentication error, invalid credentials file")
}
} else {
apiClient, err = apiClientFromDefault()
if err != nil {
return errors.Wrap(err, "Authentication error, failed to use default credentials")
}
}
// Use a custom user-agent string. This helps google with analytics and it's
// just a nice thing to do.
apiClient.Transport = logging.NewTransport("Google", apiClient.Transport)
userAgent := fmt.Sprintf("(%s %s) Terraform/%s",
runtime.GOOS, runtime.GOARCH, terraform.VersionString())
// Create billing service
billingSvc, err := billing.New(apiClient)
if err != nil {
return errors.Wrap(err, "Couldn't instantiate billing api service")
}
billingSvc.UserAgent = userAgent
pc.billing = billingSvc
return nil
}