github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/config.go (about)

     1  package google
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"log"
     7  	"net/http"
     8  	"runtime"
     9  	"strings"
    10  
    11  	"github.com/hashicorp/terraform/helper/pathorcontents"
    12  	"github.com/hashicorp/terraform/terraform"
    13  	"golang.org/x/oauth2"
    14  	"golang.org/x/oauth2/google"
    15  	"golang.org/x/oauth2/jwt"
    16  	"google.golang.org/api/bigquery/v2"
    17  	"google.golang.org/api/cloudbilling/v1"
    18  	"google.golang.org/api/cloudresourcemanager/v1"
    19  	"google.golang.org/api/compute/v1"
    20  	"google.golang.org/api/container/v1"
    21  	"google.golang.org/api/dns/v1"
    22  	"google.golang.org/api/iam/v1"
    23  	"google.golang.org/api/pubsub/v1"
    24  	"google.golang.org/api/servicemanagement/v1"
    25  	"google.golang.org/api/sqladmin/v1beta4"
    26  	"google.golang.org/api/storage/v1"
    27  )
    28  
    29  // Config is the configuration structure used to instantiate the Google
    30  // provider.
    31  type Config struct {
    32  	Credentials string
    33  	Project     string
    34  	Region      string
    35  
    36  	clientBilling         *cloudbilling.Service
    37  	clientCompute         *compute.Service
    38  	clientContainer       *container.Service
    39  	clientDns             *dns.Service
    40  	clientPubsub          *pubsub.Service
    41  	clientResourceManager *cloudresourcemanager.Service
    42  	clientStorage         *storage.Service
    43  	clientSqlAdmin        *sqladmin.Service
    44  	clientIAM             *iam.Service
    45  	clientServiceMan      *servicemanagement.APIService
    46  	clientBigQuery        *bigquery.Service
    47  }
    48  
    49  func (c *Config) loadAndValidate() error {
    50  	var account accountFile
    51  	clientScopes := []string{
    52  		"https://www.googleapis.com/auth/compute",
    53  		"https://www.googleapis.com/auth/cloud-platform",
    54  		"https://www.googleapis.com/auth/ndev.clouddns.readwrite",
    55  		"https://www.googleapis.com/auth/devstorage.full_control",
    56  	}
    57  
    58  	var client *http.Client
    59  
    60  	if c.Credentials != "" {
    61  		contents, _, err := pathorcontents.Read(c.Credentials)
    62  		if err != nil {
    63  			return fmt.Errorf("Error loading credentials: %s", err)
    64  		}
    65  
    66  		// Assume account_file is a JSON string
    67  		if err := parseJSON(&account, contents); err != nil {
    68  			return fmt.Errorf("Error parsing credentials '%s': %s", contents, err)
    69  		}
    70  
    71  		// Get the token for use in our requests
    72  		log.Printf("[INFO] Requesting Google token...")
    73  		log.Printf("[INFO]   -- Email: %s", account.ClientEmail)
    74  		log.Printf("[INFO]   -- Scopes: %s", clientScopes)
    75  		log.Printf("[INFO]   -- Private Key Length: %d", len(account.PrivateKey))
    76  
    77  		conf := jwt.Config{
    78  			Email:      account.ClientEmail,
    79  			PrivateKey: []byte(account.PrivateKey),
    80  			Scopes:     clientScopes,
    81  			TokenURL:   "https://accounts.google.com/o/oauth2/token",
    82  		}
    83  
    84  		// Initiate an http.Client. The following GET request will be
    85  		// authorized and authenticated on the behalf of
    86  		// your service account.
    87  		client = conf.Client(oauth2.NoContext)
    88  
    89  	} else {
    90  		log.Printf("[INFO] Authenticating using DefaultClient")
    91  		err := error(nil)
    92  		client, err = google.DefaultClient(oauth2.NoContext, clientScopes...)
    93  		if err != nil {
    94  			return err
    95  		}
    96  	}
    97  
    98  	versionString := terraform.VersionString()
    99  	userAgent := fmt.Sprintf(
   100  		"(%s %s) Terraform/%s", runtime.GOOS, runtime.GOARCH, versionString)
   101  
   102  	var err error
   103  
   104  	log.Printf("[INFO] Instantiating GCE client...")
   105  	c.clientCompute, err = compute.New(client)
   106  	if err != nil {
   107  		return err
   108  	}
   109  	c.clientCompute.UserAgent = userAgent
   110  
   111  	log.Printf("[INFO] Instantiating GKE client...")
   112  	c.clientContainer, err = container.New(client)
   113  	if err != nil {
   114  		return err
   115  	}
   116  	c.clientContainer.UserAgent = userAgent
   117  
   118  	log.Printf("[INFO] Instantiating Google Cloud DNS client...")
   119  	c.clientDns, err = dns.New(client)
   120  	if err != nil {
   121  		return err
   122  	}
   123  	c.clientDns.UserAgent = userAgent
   124  
   125  	log.Printf("[INFO] Instantiating Google Storage Client...")
   126  	c.clientStorage, err = storage.New(client)
   127  	if err != nil {
   128  		return err
   129  	}
   130  	c.clientStorage.UserAgent = userAgent
   131  
   132  	log.Printf("[INFO] Instantiating Google SqlAdmin Client...")
   133  	c.clientSqlAdmin, err = sqladmin.New(client)
   134  	if err != nil {
   135  		return err
   136  	}
   137  	c.clientSqlAdmin.UserAgent = userAgent
   138  
   139  	log.Printf("[INFO] Instantiating Google Pubsub Client...")
   140  	c.clientPubsub, err = pubsub.New(client)
   141  	if err != nil {
   142  		return err
   143  	}
   144  	c.clientPubsub.UserAgent = userAgent
   145  
   146  	log.Printf("[INFO] Instantiating Google Cloud ResourceManager Client...")
   147  	c.clientResourceManager, err = cloudresourcemanager.New(client)
   148  	if err != nil {
   149  		return err
   150  	}
   151  	c.clientResourceManager.UserAgent = userAgent
   152  
   153  	log.Printf("[INFO] Instantiating Google Cloud IAM Client...")
   154  	c.clientIAM, err = iam.New(client)
   155  	if err != nil {
   156  		return err
   157  	}
   158  	c.clientIAM.UserAgent = userAgent
   159  
   160  	log.Printf("[INFO] Instantiating Google Cloud Service Management Client...")
   161  	c.clientServiceMan, err = servicemanagement.New(client)
   162  	if err != nil {
   163  		return err
   164  	}
   165  	c.clientServiceMan.UserAgent = userAgent
   166  
   167  	log.Printf("[INFO] Instantiating Google Cloud Billing Client...")
   168  	c.clientBilling, err = cloudbilling.New(client)
   169  	if err != nil {
   170  		return err
   171  	}
   172  	c.clientBilling.UserAgent = userAgent
   173  
   174  	log.Printf("[INFO] Instantiating Google Cloud BigQuery Client...")
   175  	c.clientBigQuery, err = bigquery.New(client)
   176  	if err != nil {
   177  		return err
   178  	}
   179  	c.clientBigQuery.UserAgent = userAgent
   180  
   181  	return nil
   182  }
   183  
   184  // accountFile represents the structure of the account file JSON file.
   185  type accountFile struct {
   186  	PrivateKeyId string `json:"private_key_id"`
   187  	PrivateKey   string `json:"private_key"`
   188  	ClientEmail  string `json:"client_email"`
   189  	ClientId     string `json:"client_id"`
   190  }
   191  
   192  func parseJSON(result interface{}, contents string) error {
   193  	r := strings.NewReader(contents)
   194  	dec := json.NewDecoder(r)
   195  
   196  	return dec.Decode(result)
   197  }