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