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