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