Commit da31dc35 authored by Erick Hitter's avatar Erick Hitter

Successful API retrieval

parent 91935a4f
Pipeline #172 failed with stages
in 3 minutes and 5 seconds
...@@ -3,5 +3,5 @@ ...@@ -3,5 +3,5 @@
"debug": false, "debug": false,
"api-key": "", "api-key": "",
"threshold": 3600, "threshold": 3600,
"schedule": "0 10 * * * *" "schedule": "* */10 * * * *"
} }
package main package main
import ( import (
"context"
"encoding/json" "encoding/json"
"flag" "flag"
"io/ioutil" "io/ioutil"
...@@ -10,7 +11,9 @@ import ( ...@@ -10,7 +11,9 @@ import (
"path/filepath" "path/filepath"
"syscall" "syscall"
"github.com/digitalocean/godo"
"github.com/robfig/cron" "github.com/robfig/cron"
"golang.org/x/oauth2"
) )
type config struct { type config struct {
...@@ -21,6 +24,10 @@ type config struct { ...@@ -21,6 +24,10 @@ type config struct {
Schedule string `json:"schedule"` Schedule string `json:"schedule"`
} }
type TokenSource struct {
AccessToken string
}
var ( var (
configPath string configPath string
...@@ -28,8 +35,12 @@ var ( ...@@ -28,8 +35,12 @@ var (
debugDest string debugDest string
debug bool debug bool
apiKey string
threshold int threshold int
schedule string schedule string
client *godo.Client
) )
func init() { func init() {
...@@ -54,6 +65,8 @@ func init() { ...@@ -54,6 +65,8 @@ func init() {
debugDest = cfg.DebugDest debugDest = cfg.DebugDest
debug = cfg.Debug debug = cfg.Debug
apiKey = cfg.ApiKey
threshold = cfg.Threshold threshold = cfg.Threshold
schedule = cfg.Schedule schedule = cfg.Schedule
...@@ -70,29 +83,80 @@ func main() { ...@@ -70,29 +83,80 @@ func main() {
logger.Println("Test") logger.Println("Test")
} }
if authenticate() { authenticate()
startCron() startCron()
} else {
sig <- syscall.SIGTERM
}
caughtSig := <-sig caughtSig := <-sig
logger.Printf("Stopping, got signal %s", caughtSig) logger.Printf("Stopping, got signal %s", caughtSig)
} }
func authenticate() bool { func authenticate() {
return true tokenSource := &TokenSource{
AccessToken: apiKey,
}
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
client = godo.NewClient(oauthClient)
}
func (t *TokenSource) Token() (*oauth2.Token, error) {
token := &oauth2.Token{
AccessToken: t.AccessToken,
}
return token, nil
} }
func startCron() { func startCron() {
c := cron.New() c := cron.New()
c.AddFunc(schedule, check) c.AddFunc(schedule, checkApi)
c.Start() c.Start()
} }
func check() { func checkApi() {
logger.Println("Check!") context := context.TODO()
droplets, err := listDroplets(context, client)
if err != nil {
logger.Fatal("Failed to retrieve droplet list")
}
for _, droplet := range droplets {
go checkDropletAge(droplet)
}
}
func listDroplets(ctx context.Context, client *godo.Client) ([]godo.Droplet, error) {
list := []godo.Droplet{}
opt := &godo.ListOptions{}
for {
droplets, resp, err := client.Droplets.List(ctx, opt)
if err != nil {
return nil, err
}
for _, d := range droplets {
list = append(list, d)
}
if resp.Links == nil || resp.Links.IsLastPage() {
break
}
page, err := resp.Links.CurrentPage()
if err != nil {
return nil, err
}
opt.Page = page + 1
}
return list, nil
}
func checkDropletAge(droplet godo.Droplet) {
logger.Print(droplet.ID)
logger.Print(droplet.Created)
} }
func setUpLogger() { func setUpLogger() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment