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