github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/gce/google/auth.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package google 5 6 import ( 7 "github.com/juju/errors" 8 "golang.org/x/oauth2" 9 goauth2 "golang.org/x/oauth2/google" 10 "google.golang.org/api/compute/v1" 11 ) 12 13 var ( 14 driverScopes = []string{ 15 "https://www.googleapis.com/auth/compute", 16 "https://www.googleapis.com/auth/devstorage.full_control", 17 } 18 ) 19 20 // newConnection opens a new low-level connection to the GCE API using 21 // the Auth's data and returns it. This includes building the 22 // OAuth-wrapping network transport. 23 func newConnection(creds *Credentials) (*compute.Service, error) { 24 jsonKey := creds.JSONKey 25 if jsonKey == nil { 26 built, err := creds.buildJSONKey() 27 if err != nil { 28 return nil, errors.Trace(err) 29 } 30 jsonKey = built 31 } 32 cfg, err := goauth2.JWTConfigFromJSON(jsonKey, driverScopes...) 33 if err != nil { 34 return nil, errors.Trace(err) 35 } 36 client := cfg.Client(oauth2.NoContext) 37 service, err := compute.New(client) 38 return service, errors.Trace(err) 39 }