github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/gcloud/wait_operation.go (about) 1 package gcloud 2 3 import ( 4 "github.com/sirupsen/logrus" 5 "github.com/emc-advanced-dev/pkg/errors" 6 "google.golang.org/api/compute/v1" 7 "time" 8 ) 9 10 var defaultTimeout = time.Minute * 5 11 var defaultInterval = time.Millisecond * 250 12 13 func (p *GcloudProvider) waitOperation(operation string, global bool) error { 14 errc := make(chan error) 15 finished := make(chan struct{}) 16 17 backoff := int64(1) 18 go func() { 19 for { 20 done, err := p.waitCycle(operation, global) 21 if err != nil { 22 errc <- err 23 return 24 } 25 if done { 26 close(finished) 27 return 28 } 29 backoff *= 2 30 time.Sleep(time.Duration(backoff) * defaultInterval) 31 } 32 }() 33 34 select { 35 case err := <-errc: 36 return err 37 case <-finished: 38 return nil 39 case <-time.After(defaultTimeout): 40 return errors.New("timed out waiting more than "+defaultTimeout.String()+" for "+operation+" to complete", nil) 41 } 42 } 43 44 func (p *GcloudProvider) waitCycle(operation string, global bool) (bool, error) { 45 var status *compute.Operation 46 var err error 47 if global { 48 status, err = p.compute().GlobalOperations.Get(p.config.ProjectID, operation).Do() 49 } else { 50 status, err = p.compute().ZoneOperations.Get(p.config.ProjectID, p.config.Zone, operation).Do() 51 } 52 if err != nil { 53 return false, errors.New("getting status for operation "+operation, err) 54 } 55 logrus.Debugf("status for %v is %+v", operation, status) 56 if status.Status == "DONE" { 57 return true, nil 58 } 59 return false, nil 60 }