Skip to content

Instantly share code, notes, and snippets.

@andrewsomething
Created November 18, 2016 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewsomething/d43ccaf333531001612647405188d4ac to your computer and use it in GitHub Desktop.
Save andrewsomething/d43ccaf333531001612647405188d4ac to your computer and use it in GitHub Desktop.
List DigitalOcean snapshots using godo
package main
import "os"
import "fmt"
import "golang.org/x/oauth2"
import "github.com/digitalocean/godo"
type TokenSource struct {
AccessToken string
}
func (t *TokenSource) Token() (*oauth2.Token, error) {
token := &oauth2.Token{
AccessToken: t.AccessToken,
}
return token, nil
}
func main() {
pat := os.Getenv("DO_TOKEN")
tokenSource := &TokenSource{
AccessToken: pat,
}
oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
client := godo.NewClient(oauthClient)
opt := &godo.ListOptions{
Page: 1,
PerPage: 200,
}
vol, _, err := client.Snapshots.ListVolume(opt)
if err != nil {
fmt.Printf("Something bad happened: %s\n\n", err)
} else {
fmt.Printf("Volumes:\n%v\n\n", godo.Stringify(vol))
}
drop, _, err := client.Snapshots.ListDroplet(opt)
if err != nil {
fmt.Printf("Something bad happened: %s\n\n", err)
} else {
fmt.Printf("Droplets:\n%v\n\n", godo.Stringify(drop))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment