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