github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/test/gcloud/test.go (about) 1 package main 2 3 import ( 4 "github.com/sirupsen/logrus" 5 "github.com/emc-advanced-dev/pkg/errors" 6 "golang.org/x/net/context" 7 "golang.org/x/oauth2/google" 8 "google.golang.org/api/compute/v1" 9 "os" 10 ) 11 12 func main() { 13 if err := run(); err != nil { 14 logrus.Fatal(err) 15 } 16 } 17 18 func run() error { 19 logrus.SetLevel(logrus.DebugLevel) 20 args := os.Args[1:] 21 if len(args) != 4 { 22 logrus.Warnf("bad args: %v", args) 23 return errors.New("usage: go run main.go PROJECT IMAGE BUCKET OBJECT", nil) 24 } 25 project := args[0] 26 name := args[1] 27 bucket := args[2] 28 object := args[3] 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, compute.CloudPlatformScope, compute.DevstorageReadWriteScope) 34 if err != nil { 35 return errors.New("failed to start default client", err) 36 } 37 computeService, err := compute.New(client) 38 if err != nil { 39 return errors.New("failed to start compute client", err) 40 } 41 42 imageSpec := &compute.Image{ 43 Name: name, 44 RawDisk: &compute.ImageRawDisk{ 45 ContainerType: "TAR", 46 Source: "http://storage.googleapis.com/" + bucket + "/" + object, 47 }, 48 SourceType: "RAW", 49 } 50 51 logrus.Debugf("creating image from " + imageSpec.RawDisk.Source) 52 53 req := computeService.Images.Insert(project, imageSpec) 54 55 logrus.Debugf("sending request:\n%+v", req) 56 57 gImage, err := req.Do() 58 if err != nil { 59 return errors.New("creating gcloud image from storage", err) 60 } 61 logrus.Infof("success: %v", gImage) 62 return nil 63 }