github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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/sqladmin/v1beta4" 23 "google.golang.org/api/storage/v1" 24 ) 25 26 // Config is the configuration structure used to instantiate the Google 27 // provider. 28 type Config struct { 29 Credentials string 30 Project string 31 Region string 32 33 clientCompute *compute.Service 34 clientContainer *container.Service 35 clientDns *dns.Service 36 clientPubsub *pubsub.Service 37 clientResourceManager *cloudresourcemanager.Service 38 clientStorage *storage.Service 39 clientSqlAdmin *sqladmin.Service 40 clientIAM *iam.Service 41 } 42 43 func (c *Config) loadAndValidate() error { 44 var account accountFile 45 clientScopes := []string{ 46 "https://www.googleapis.com/auth/compute", 47 "https://www.googleapis.com/auth/cloud-platform", 48 "https://www.googleapis.com/auth/ndev.clouddns.readwrite", 49 "https://www.googleapis.com/auth/devstorage.full_control", 50 } 51 52 var client *http.Client 53 54 if c.Credentials != "" { 55 contents, _, err := pathorcontents.Read(c.Credentials) 56 if err != nil { 57 return fmt.Errorf("Error loading credentials: %s", err) 58 } 59 60 // Assume account_file is a JSON string 61 if err := parseJSON(&account, contents); err != nil { 62 return fmt.Errorf("Error parsing credentials '%s': %s", contents, err) 63 } 64 65 // Get the token for use in our requests 66 log.Printf("[INFO] Requesting Google token...") 67 log.Printf("[INFO] -- Email: %s", account.ClientEmail) 68 log.Printf("[INFO] -- Scopes: %s", clientScopes) 69 log.Printf("[INFO] -- Private Key Length: %d", len(account.PrivateKey)) 70 71 conf := jwt.Config{ 72 Email: account.ClientEmail, 73 PrivateKey: []byte(account.PrivateKey), 74 Scopes: clientScopes, 75 TokenURL: "https://accounts.google.com/o/oauth2/token", 76 } 77 78 // Initiate an http.Client. The following GET request will be 79 // authorized and authenticated on the behalf of 80 // your service account. 81 client = conf.Client(oauth2.NoContext) 82 83 } else { 84 log.Printf("[INFO] Authenticating using DefaultClient") 85 err := error(nil) 86 client, err = google.DefaultClient(oauth2.NoContext, clientScopes...) 87 if err != nil { 88 return err 89 } 90 } 91 92 versionString := terraform.VersionString() 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 log.Printf("[INFO] Instatiating Google Cloud ResourceManager Client...") 141 c.clientResourceManager, err = cloudresourcemanager.New(client) 142 if err != nil { 143 return err 144 } 145 c.clientResourceManager.UserAgent = userAgent 146 147 log.Printf("[INFO] Instatiating Google Cloud IAM Client...") 148 c.clientIAM, err = iam.New(client) 149 if err != nil { 150 return err 151 } 152 c.clientIAM.UserAgent = userAgent 153 154 return nil 155 } 156 157 // accountFile represents the structure of the account file JSON file. 158 type accountFile struct { 159 PrivateKeyId string `json:"private_key_id"` 160 PrivateKey string `json:"private_key"` 161 ClientEmail string `json:"client_email"` 162 ClientId string `json:"client_id"` 163 } 164 165 func parseJSON(result interface{}, contents string) error { 166 r := strings.NewReader(contents) 167 dec := json.NewDecoder(r) 168 169 return dec.Decode(result) 170 }