How to use waitForCompletion method of gce Package

Best Syzkaller code snippet using gce.waitForCompletion

gce.go

Source:gce.go Github

copy

Full Screen

...136 })137 if err != nil {138 return "", fmt.Errorf("failed to create instance: %v", err)139 }140 if err := ctx.waitForCompletion("zone", "create image", op.Name, false); err != nil {141 if _, ok := err.(resourcePoolExhaustedError); ok && instance.Scheduling.Preemptible {142 instance.Scheduling.Preemptible = false143 goto retry144 }145 return "", err146 }147 var inst *compute.Instance148 err = ctx.apiCall(func() (err error) {149 inst, err = ctx.computeService.Instances.Get(ctx.ProjectID, ctx.ZoneID, name).Do()150 return151 })152 if err != nil {153 return "", fmt.Errorf("error getting instance %s details after creation: %v", name, err)154 }155 // Finds its internal IP.156 ip := ""157 for _, iface := range inst.NetworkInterfaces {158 if strings.HasPrefix(iface.NetworkIP, "10.") {159 ip = iface.NetworkIP160 break161 }162 }163 if ip == "" {164 return "", fmt.Errorf("didn't find instance internal IP address")165 }166 return ip, nil167}168func (ctx *Context) DeleteInstance(name string, wait bool) error {169 var op *compute.Operation170 err := ctx.apiCall(func() (err error) {171 op, err = ctx.computeService.Instances.Delete(ctx.ProjectID, ctx.ZoneID, name).Do()172 return173 })174 if apiErr, ok := err.(*googleapi.Error); ok && apiErr.Code == 404 {175 return nil176 }177 if err != nil {178 return fmt.Errorf("failed to delete instance: %v", err)179 }180 if wait {181 if err := ctx.waitForCompletion("zone", "delete image", op.Name, true); err != nil {182 return err183 }184 }185 return nil186}187func (ctx *Context) IsInstanceRunning(name string) bool {188 var inst *compute.Instance189 err := ctx.apiCall(func() (err error) {190 inst, err = ctx.computeService.Instances.Get(ctx.ProjectID, ctx.ZoneID, name).Do()191 return192 })193 if err != nil {194 return false195 }196 return inst.Status == "RUNNING"197}198func (ctx *Context) CreateImage(imageName, gcsFile string) error {199 image := &compute.Image{200 Name: imageName,201 RawDisk: &compute.ImageRawDisk{202 Source: "https://storage.googleapis.com/" + gcsFile,203 },204 Licenses: []string{205 "https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx",206 },207 }208 var op *compute.Operation209 err := ctx.apiCall(func() (err error) {210 op, err = ctx.computeService.Images.Insert(ctx.ProjectID, image).Do()211 return212 })213 if err != nil {214 // Try again without the vmx license in case it is not supported.215 image.Licenses = nil216 err := ctx.apiCall(func() (err error) {217 op, err = ctx.computeService.Images.Insert(ctx.ProjectID, image).Do()218 return219 })220 if err != nil {221 return fmt.Errorf("failed to create image: %v", err)222 }223 }224 if err := ctx.waitForCompletion("global", "create image", op.Name, false); err != nil {225 return err226 }227 return nil228}229func (ctx *Context) DeleteImage(imageName string) error {230 var op *compute.Operation231 err := ctx.apiCall(func() (err error) {232 op, err = ctx.computeService.Images.Delete(ctx.ProjectID, imageName).Do()233 return234 })235 if apiErr, ok := err.(*googleapi.Error); ok && apiErr.Code == 404 {236 return nil237 }238 if err != nil {239 return fmt.Errorf("failed to delete image: %v", err)240 }241 if err := ctx.waitForCompletion("global", "delete image", op.Name, true); err != nil {242 return err243 }244 return nil245}246type resourcePoolExhaustedError string247func (err resourcePoolExhaustedError) Error() string {248 return string(err)249}250func (ctx *Context) waitForCompletion(typ, desc, opName string, ignoreNotFound bool) error {251 time.Sleep(3 * time.Second)252 for {253 time.Sleep(3 * time.Second)254 var op *compute.Operation255 err := ctx.apiCall(func() (err error) {256 switch typ {257 case "global":258 op, err = ctx.computeService.GlobalOperations.Get(ctx.ProjectID, opName).Do()259 case "zone":260 op, err = ctx.computeService.ZoneOperations.Get(ctx.ProjectID, ctx.ZoneID, opName).Do()261 default:262 panic("unknown operation type: " + typ)263 }264 return...

Full Screen

Full Screen

waitForCompletion

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 instanceName, err := metadata.InstanceName()4 if err != nil {5 fmt.Println("Could not get instance name: %v", err)6 }7 projectID, err := metadata.ProjectID()8 if err != nil {9 fmt.Println("Could not get project ID: %v", err)10 }11 zone, err := metadata.Zone()12 if err != nil {13 fmt.Println("Could not get zone: %v", err)14 }15 gceObject := gce.NewService(&gce.Options{16 })17 err = gceObject.WaitForCompletion(instanceName, zone, timeout)18 if err != nil {19 fmt.Println("Could not wait for completion: %v", err)20 }21 fmt.Println("Done")22}

Full Screen

Full Screen

waitForCompletion

Using AI Code Generation

copy

Full Screen

1func main(){2}3func waitForCompletion {4}5func createInstance {6}

Full Screen

Full Screen

waitForCompletion

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 gce := gce.NewGCE("project-id", "zone", "instance-name")4 gce.waitForCompletion()5 fmt.Println(gce.Status)6}

Full Screen

Full Screen

waitForCompletion

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 gce := gcp.GCE{}4 gce.StartInstance("instance-1", "us-central1-a")5 gce.WaitForCompletion()6}7import (8func main() {9 gce := gcp.GCE{}10 gce.StartInstance("instance-1", "us-central1-a")11 gce.WaitForCompletion()12}13import (14func main() {15 gce := gcp.GCE{}16 gce.StartInstance("instance-1", "us-central1-a")17 gce.WaitForCompletion()18}19import (20func main() {21 gce := gcp.GCE{}22 gce.StartInstance("instance-1", "us-central1-a")23 gce.WaitForCompletion()24}25import (26func main() {27 gce := gcp.GCE{}28 gce.StartInstance("instance-1", "us-central1-a")29 gce.WaitForCompletion()30}31import (32func main() {33 gce := gcp.GCE{}34 gce.StartInstance("instance-1", "us-central1-a")35 gce.WaitForCompletion()36}37import (38func main() {39 gce := gcp.GCE{}40 gce.StartInstance("instance-1", "us-central1-a")41 gce.WaitForCompletion()42}43import (44func main() {45 gce := gcp.GCE{}46 gce.StartInstance("instance-1", "us-central1-a")

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful