terraform-provider-google/config.go

176 lines
4.4 KiB
Go
Raw Normal View History

2014-08-25 18:48:20 +00:00
package google
import (
"encoding/json"
"fmt"
"io/ioutil"
2014-08-25 18:48:20 +00:00
"log"
"net/http"
"os"
"runtime"
"strings"
2014-08-25 18:48:20 +00:00
2015-01-30 19:53:09 +00:00
// TODO(dcunnin): Use version code from version.go
// "github.com/hashicorp/terraform"
2015-02-12 02:21:24 +00:00
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
"google.golang.org/api/compute/v1"
"google.golang.org/api/container/v1"
2015-04-30 05:32:34 +00:00
"google.golang.org/api/dns/v1"
"google.golang.org/api/storage/v1"
2014-08-25 18:48:20 +00:00
)
// Config is the configuration structure used to instantiate the Google
// provider.
type Config struct {
AccountFile string
AccountFileContents string
Project string
Region string
2014-08-25 18:48:20 +00:00
clientCompute *compute.Service
clientContainer *container.Service
2015-04-30 05:32:34 +00:00
clientDns *dns.Service
clientStorage *storage.Service
2014-08-25 18:48:20 +00:00
}
func (c *Config) loadAndValidate() error {
var account accountFile
// TODO: validation that it isn't blank
if c.AccountFile == "" {
c.AccountFile = os.Getenv("GOOGLE_ACCOUNT_FILE")
}
if c.AccountFileContents == "" {
c.AccountFileContents = os.Getenv("GOOGLE_ACCOUNT_FILE_CONTENTS")
}
if c.Project == "" {
c.Project = os.Getenv("GOOGLE_PROJECT")
}
if c.Region == "" {
c.Region = os.Getenv("GOOGLE_REGION")
}
2014-08-25 18:48:20 +00:00
2015-02-12 02:21:24 +00:00
var client *http.Client
2015-01-30 19:53:09 +00:00
if c.AccountFile != "" {
if c.AccountFileContents != "" {
2015-01-30 19:53:09 +00:00
return fmt.Errorf(
"Cannot provide both account_file and account_file_contents",
)
}
b, err := ioutil.ReadFile(c.AccountFile)
if err != nil {
return err
}
c.AccountFileContents = string(b)
}
if c.AccountFileContents != "" {
if err := parseJSON(&account, c.AccountFileContents); err != nil {
return fmt.Errorf(
"Error parsing account file contents '%s': %s",
c.AccountFileContents,
2015-01-30 19:53:09 +00:00
err)
}
2015-04-30 05:32:34 +00:00
clientScopes := []string{
"https://www.googleapis.com/auth/compute",
"https://www.googleapis.com/auth/cloud-platform",
2015-04-30 05:32:34 +00:00
"https://www.googleapis.com/auth/ndev.clouddns.readwrite",
"https://www.googleapis.com/auth/devstorage.full_control",
2015-04-30 05:32:34 +00:00
}
2015-02-12 02:21:24 +00:00
2015-01-30 19:53:09 +00: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 02:21:24 +00: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 19:53:09 +00:00
} else {
log.Printf("[INFO] Requesting Google token via GCE Service Role...")
2015-02-12 02:21:24 +00:00
client = &http.Client{
Transport: &oauth2.Transport{
// Fetch from Google Compute Engine's metadata server to retrieve
// an access token for the provided account.
// If no account is specified, "default" is used.
Source: google.ComputeTokenSource(""),
},
}
2014-08-25 18:48:20 +00:00
}
2015-04-30 05:32:34 +00:00
// Build UserAgent
versionString := "0.0.0"
// TODO(dcunnin): Use Terraform's version code from version.go
// versionString := main.Version
// if main.VersionPrerelease != "" {
// versionString = fmt.Sprintf("%s-%s", versionString, main.VersionPrerelease)
// }
2015-04-30 05:32:34 +00:00
userAgent := fmt.Sprintf(
"(%s %s) Terraform/%s", runtime.GOOS, runtime.GOARCH, versionString)
2015-04-30 05:32:34 +00: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
log.Printf("[INFO] Instantiating GKE client...")
c.clientContainer, err = container.New(client)
if err != nil {
return err
}
c.clientContainer.UserAgent = userAgent
2015-04-30 05:32:34 +00:00
log.Printf("[INFO] Instantiating Google Cloud DNS client...")
c.clientDns, err = dns.New(client)
2014-08-25 18:48:20 +00:00
if err != nil {
return err
}
2015-04-30 05:32:34 +00:00
c.clientDns.UserAgent = userAgent
2014-08-25 18:48:20 +00:00
log.Printf("[INFO] Instantiating Google Storage Client...")
c.clientStorage, err = storage.New(client)
if err != nil {
return err
}
c.clientStorage.UserAgent = userAgent
2014-08-25 18:48:20 +00: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"`
}
func parseJSON(result interface{}, contents string) error {
r := strings.NewReader(contents)
dec := json.NewDecoder(r)
2014-08-25 18:48:20 +00:00
return dec.Decode(result)
}