github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/orbiter/kinds/providers/gce/gceapi.go (about) 1 package gce 2 3 import ( 4 "fmt" 5 "strings" 6 "time" 7 8 "google.golang.org/api/compute/v1" 9 "google.golang.org/api/googleapi" 10 "google.golang.org/api/servicemanagement/v1" 11 ) 12 13 func toErrFunc(fn func()) func() error { 14 return func() error { 15 fn() 16 return nil 17 } 18 } 19 20 func computeOpCall(call func(...googleapi.CallOption) (*compute.Operation, error)) func() (*operation, error) { 21 return func() (*operation, error) { 22 op, err := call() 23 if err != nil { 24 return nil, err 25 } 26 if op != nil && op.Error != nil { 27 return nil, opErrs(op.Error) 28 } 29 return toOperation(op.Progress), err 30 } 31 } 32 33 type opErr struct { 34 msg string 35 } 36 37 func (o *opErr) Error() string { 38 return o.msg 39 } 40 41 func opErrs(errors *compute.OperationError) error { 42 opErr := opErr{} 43 for idx := range errors.Errors { 44 opErr.msg = fmt.Sprintf("%s%s,", opErr.msg, errors.Errors[idx].Message) 45 } 46 opErr.msg = strings.TrimSuffix(opErr.msg, ",") 47 return &opErr 48 } 49 50 func servicesOpCall(call func(...googleapi.CallOption) (*servicemanagement.Operation, error)) func() (*operation, error) { 51 return func() (*operation, error) { 52 _, err := call() 53 return toOperation(100), err 54 } 55 } 56 57 type operation struct { 58 progress int64 59 } 60 61 func toOperation(progress int64) *operation { 62 return &operation{progress: progress} 63 } 64 65 func operateFunc(before func(), call func() (*operation, error), after func() error) func() error { 66 return func() error { 67 if before != nil { 68 before() 69 } 70 op, err := call() 71 if err != nil { 72 return err 73 } 74 75 if op.progress < 100 { 76 time.Sleep(time.Second) 77 return operateFunc(before, call, after)() 78 } 79 80 if after != nil { 81 return after() 82 } 83 84 return nil 85 } 86 }