github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/gcloud/gcloud_provider.go (about) 1 package gcloud 2 3 import ( 4 "path/filepath" 5 6 "github.com/sirupsen/logrus" 7 "github.com/emc-advanced-dev/pkg/errors" 8 "github.com/solo-io/unik/pkg/config" 9 "github.com/solo-io/unik/pkg/state" 10 "golang.org/x/net/context" 11 "golang.org/x/oauth2/google" 12 "google.golang.org/api/compute/v1" 13 "google.golang.org/api/storage/v1" 14 ) 15 16 func GcloudStateFile() string { 17 return filepath.Join(config.Internal.UnikHome, "gcloud", "state.json") 18 } 19 20 type GcloudProvider struct { 21 config config.Gcloud 22 state state.State 23 computeSvc *compute.Service 24 storageSvc *storage.Service 25 } 26 27 func NewGcloudProvier(config config.Gcloud) (*GcloudProvider, error) { 28 logrus.Infof("state file: %s", GcloudStateFile()) 29 30 // Use oauth2.NoContext if there isn't a good context to pass in. 31 ctx := context.Background() 32 33 client, err := google.DefaultClient(ctx, compute.ComputeScope) 34 if err != nil { 35 return nil, errors.New("failed to start default client", err) 36 } 37 computeService, err := compute.New(client) 38 if err != nil { 39 return nil, errors.New("failed to start compute client", err) 40 } 41 42 storageSevice, err := storage.New(client) 43 if err != nil { 44 return nil, errors.New("failed to start storage client", err) 45 } 46 47 return &GcloudProvider{ 48 config: config, 49 state: state.NewBasicState(GcloudStateFile()), 50 computeSvc: computeService, 51 storageSvc: storageSevice, 52 }, nil 53 } 54 55 func (p *GcloudProvider) WithState(state state.State) *GcloudProvider { 56 p.state = state 57 return p 58 } 59 60 func (p *GcloudProvider) compute() *compute.Service { 61 return p.computeSvc 62 } 63 64 func (p *GcloudProvider) storage() *storage.Service { 65 return p.storageSvc 66 }