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