2014-08-25 20:48:20 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2015-04-16 00:17:21 +02:00
|
|
|
"runtime"
|
2015-07-23 23:56:32 +02:00
|
|
|
"strings"
|
2014-08-25 20:48:20 +02:00
|
|
|
|
2015-11-12 23:13:07 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/pathorcontents"
|
2015-10-19 21:38:23 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-02-12 03:21:24 +01:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"golang.org/x/oauth2/google"
|
|
|
|
"golang.org/x/oauth2/jwt"
|
2017-02-20 18:32:24 +01:00
|
|
|
"google.golang.org/api/cloudbilling/v1"
|
2016-08-10 06:44:53 +02:00
|
|
|
"google.golang.org/api/cloudresourcemanager/v1"
|
2015-03-18 18:10:39 +01:00
|
|
|
"google.golang.org/api/compute/v1"
|
2015-07-05 18:39:01 +02:00
|
|
|
"google.golang.org/api/container/v1"
|
2015-04-30 07:32:34 +02:00
|
|
|
"google.golang.org/api/dns/v1"
|
2016-11-08 08:27:32 +01:00
|
|
|
"google.golang.org/api/iam/v1"
|
2015-12-11 18:59:13 +01:00
|
|
|
"google.golang.org/api/pubsub/v1"
|
2016-11-23 07:55:40 +01:00
|
|
|
"google.golang.org/api/servicemanagement/v1"
|
2015-10-23 16:10:41 +02:00
|
|
|
"google.golang.org/api/sqladmin/v1beta4"
|
2015-10-30 22:27:12 +01:00
|
|
|
"google.golang.org/api/storage/v1"
|
2014-08-25 20:48:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the configuration structure used to instantiate the Google
|
|
|
|
// provider.
|
|
|
|
type Config struct {
|
2015-11-12 23:13:07 +01:00
|
|
|
Credentials string
|
2015-07-27 21:35:52 +02:00
|
|
|
Project string
|
|
|
|
Region string
|
2014-08-25 20:48:20 +02:00
|
|
|
|
2017-02-20 18:32:24 +01:00
|
|
|
clientBilling *cloudbilling.Service
|
2016-08-10 06:44:53 +02:00
|
|
|
clientCompute *compute.Service
|
|
|
|
clientContainer *container.Service
|
|
|
|
clientDns *dns.Service
|
|
|
|
clientPubsub *pubsub.Service
|
|
|
|
clientResourceManager *cloudresourcemanager.Service
|
|
|
|
clientStorage *storage.Service
|
|
|
|
clientSqlAdmin *sqladmin.Service
|
2016-11-08 08:27:32 +01:00
|
|
|
clientIAM *iam.Service
|
2016-11-23 07:55:40 +01:00
|
|
|
clientServiceMan *servicemanagement.APIService
|
2014-08-25 20:48:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) loadAndValidate() error {
|
|
|
|
var account accountFile
|
2015-10-19 21:27:41 +02:00
|
|
|
clientScopes := []string{
|
|
|
|
"https://www.googleapis.com/auth/compute",
|
|
|
|
"https://www.googleapis.com/auth/cloud-platform",
|
|
|
|
"https://www.googleapis.com/auth/ndev.clouddns.readwrite",
|
|
|
|
"https://www.googleapis.com/auth/devstorage.full_control",
|
|
|
|
}
|
|
|
|
|
2015-02-12 03:21:24 +01:00
|
|
|
var client *http.Client
|
2015-01-30 20:53:09 +01:00
|
|
|
|
2015-11-12 23:13:07 +01:00
|
|
|
if c.Credentials != "" {
|
|
|
|
contents, _, err := pathorcontents.Read(c.Credentials)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error loading credentials: %s", err)
|
|
|
|
}
|
2015-07-27 21:35:52 +02:00
|
|
|
|
|
|
|
// Assume account_file is a JSON string
|
|
|
|
if err := parseJSON(&account, contents); err != nil {
|
2015-11-12 23:13:07 +01:00
|
|
|
return fmt.Errorf("Error parsing credentials '%s': %s", contents, err)
|
2015-01-30 20:53:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the token for use in our requests
|
|
|
|
log.Printf("[INFO] Requesting Google token...")
|
|
|
|
log.Printf("[INFO] -- Email: %s", account.ClientEmail)
|
|
|
|
log.Printf("[INFO] -- Scopes: %s", clientScopes)
|
|
|
|
log.Printf("[INFO] -- Private Key Length: %d", len(account.PrivateKey))
|
|
|
|
|
2015-02-12 03:21:24 +01:00
|
|
|
conf := jwt.Config{
|
|
|
|
Email: account.ClientEmail,
|
|
|
|
PrivateKey: []byte(account.PrivateKey),
|
|
|
|
Scopes: clientScopes,
|
|
|
|
TokenURL: "https://accounts.google.com/o/oauth2/token",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initiate an http.Client. The following GET request will be
|
|
|
|
// authorized and authenticated on the behalf of
|
|
|
|
// your service account.
|
|
|
|
client = conf.Client(oauth2.NoContext)
|
2015-01-30 20:53:09 +01:00
|
|
|
|
|
|
|
} else {
|
2015-10-29 23:10:44 +01:00
|
|
|
log.Printf("[INFO] Authenticating using DefaultClient")
|
2015-10-19 21:27:41 +02:00
|
|
|
err := error(nil)
|
|
|
|
client, err = google.DefaultClient(oauth2.NoContext, clientScopes...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-02-12 03:21:24 +01:00
|
|
|
}
|
2014-08-25 20:48:20 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 20:07:16 +02:00
|
|
|
versionString := terraform.VersionString()
|
2015-04-30 07:32:34 +02:00
|
|
|
userAgent := fmt.Sprintf(
|
2015-04-16 00:17:21 +02:00
|
|
|
"(%s %s) Terraform/%s", runtime.GOOS, runtime.GOARCH, versionString)
|
|
|
|
|
2015-04-30 07:32:34 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
log.Printf("[INFO] Instantiating GCE client...")
|
|
|
|
c.clientCompute, err = compute.New(client)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.clientCompute.UserAgent = userAgent
|
|
|
|
|
2015-07-05 18:39:01 +02:00
|
|
|
log.Printf("[INFO] Instantiating GKE client...")
|
|
|
|
c.clientContainer, err = container.New(client)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.clientContainer.UserAgent = userAgent
|
|
|
|
|
2015-04-30 07:32:34 +02:00
|
|
|
log.Printf("[INFO] Instantiating Google Cloud DNS client...")
|
|
|
|
c.clientDns, err = dns.New(client)
|
2014-08-25 20:48:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-30 07:32:34 +02:00
|
|
|
c.clientDns.UserAgent = userAgent
|
2014-08-25 20:48:20 +02:00
|
|
|
|
2015-05-21 19:28:27 +02:00
|
|
|
log.Printf("[INFO] Instantiating Google Storage Client...")
|
|
|
|
c.clientStorage, err = storage.New(client)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.clientStorage.UserAgent = userAgent
|
|
|
|
|
2015-10-23 16:10:41 +02:00
|
|
|
log.Printf("[INFO] Instantiating Google SqlAdmin Client...")
|
|
|
|
c.clientSqlAdmin, err = sqladmin.New(client)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.clientSqlAdmin.UserAgent = userAgent
|
|
|
|
|
2016-11-23 07:55:40 +01:00
|
|
|
log.Printf("[INFO] Instantiating Google Pubsub Client...")
|
2015-10-28 18:55:50 +01:00
|
|
|
c.clientPubsub, err = pubsub.New(client)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.clientPubsub.UserAgent = userAgent
|
|
|
|
|
2016-11-23 07:55:40 +01:00
|
|
|
log.Printf("[INFO] Instantiating Google Cloud ResourceManager Client...")
|
2016-08-10 06:44:53 +02:00
|
|
|
c.clientResourceManager, err = cloudresourcemanager.New(client)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-08 08:27:32 +01:00
|
|
|
c.clientResourceManager.UserAgent = userAgent
|
|
|
|
|
2016-11-23 07:55:40 +01:00
|
|
|
log.Printf("[INFO] Instantiating Google Cloud IAM Client...")
|
2016-11-08 08:27:32 +01:00
|
|
|
c.clientIAM, err = iam.New(client)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.clientIAM.UserAgent = userAgent
|
2016-08-10 06:44:53 +02:00
|
|
|
|
2016-11-23 07:55:40 +01:00
|
|
|
log.Printf("[INFO] Instantiating Google Cloud Service Management Client...")
|
|
|
|
c.clientServiceMan, err = servicemanagement.New(client)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.clientServiceMan.UserAgent = userAgent
|
|
|
|
|
2017-02-20 18:32:24 +01:00
|
|
|
log.Printf("[INFO] Instantiating Google Cloud Billing Client...")
|
|
|
|
c.clientBilling, err = cloudbilling.New(client)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.clientBilling.UserAgent = userAgent
|
|
|
|
|
2014-08-25 20:48:20 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// accountFile represents the structure of the account file JSON file.
|
|
|
|
type accountFile struct {
|
|
|
|
PrivateKeyId string `json:"private_key_id"`
|
|
|
|
PrivateKey string `json:"private_key"`
|
|
|
|
ClientEmail string `json:"client_email"`
|
|
|
|
ClientId string `json:"client_id"`
|
|
|
|
}
|
|
|
|
|
2015-07-23 23:56:32 +02:00
|
|
|
func parseJSON(result interface{}, contents string) error {
|
|
|
|
r := strings.NewReader(contents)
|
|
|
|
dec := json.NewDecoder(r)
|
2014-08-25 20:48:20 +02:00
|
|
|
|
|
|
|
return dec.Decode(result)
|
|
|
|
}
|