Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: update droplet create example #538

Merged
merged 2 commits into from Jun 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 26 additions & 16 deletions util/droplet_test.go
Expand Up @@ -2,28 +2,38 @@ package util

import (
"context"

"golang.org/x/oauth2"
"fmt"
"log"

"github.com/digitalocean/godo"
)

func ExampleWaitForActive() {
// build client
pat := "mytoken"
token := &oauth2.Token{AccessToken: pat}
t := oauth2.StaticTokenSource(token)

ctx := context.TODO()
oauthClient := oauth2.NewClient(ctx, t)
client := godo.NewClient(oauthClient)

// create your droplet and retrieve the create action uri
uri := "https://api.digitalocean.com/v2/actions/xxxxxxxx"
// Create a godo client.
client := godo.NewFromToken("dop_v1_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")

// block until the action is complete
err := WaitForActive(ctx, client, uri)
// Create a Droplet.
droplet, resp, err := client.Droplets.Create(context.Background(), &godo.DropletCreateRequest{
Name: "test-droplet",
Region: "nyc3",
Size: "s-1vcpu-1gb",
Image: godo.DropletCreateImage{
Slug: "ubuntu-20-04-x64",
},
})
if err != nil {
panic(err)
log.Fatalf("failed to create droplet: %v\n", err)
}

// Find the Droplet create action, then wait for it to complete.
for _, action := range resp.Links.Actions {
if action.Rel == "create" {
// Block until the action is complete.
if err := WaitForActive(context.Background(), client, action.HREF); err != nil {
log.Fatalf("error waiting for droplet to become active: %v\n", err)
}
}
}

fmt.Println(droplet.Name)
}