Created
November 18, 2016 17:38
-
-
Save andrewsomething/d43ccaf333531001612647405188d4ac to your computer and use it in GitHub Desktop.
List DigitalOcean snapshots using godo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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