start moving towards gsclient-go continued

release/release-0.2.0
Felix Kronlage-Dammers 2019-09-10 22:12:56 +02:00
parent 1169b00074
commit 8ee76c728b
No known key found for this signature in database
GPG Key ID: 1AD3A9B87A0B612C
5 changed files with 38 additions and 37 deletions

View File

@ -4,7 +4,6 @@ package main
import (
"fmt"
"github.com/gridscale/gsclient-go"
)
type ListServersCommand struct {

View File

@ -4,8 +4,6 @@ package main
import (
"fmt"
"github.com/gridscale/gsclient-go"
)
type ServersStatusCommand struct {

View File

@ -4,8 +4,9 @@ package main
import (
"fmt"
"g.hazardous.org/fkr/libretto/virtualmachine/gridscale"
"time"
"github.com/gridscale/gsclient-go"
)
type SnapshotsAgeCommand struct {
@ -21,21 +22,25 @@ func (x *SnapshotsAgeCommand) Execute(args []string) error {
config(Checker.ConfigFile)
storage, err := gridscale.GetStorageByUUID(Config.UserId, Config.Token, x.StorageUUID)
if err != nil {
Unknown(fmt.Sprintf("%s: %s", "Error retrieving storage", err))
}
snapshot, error := storage.GetYoungestSnapshot(Config.UserId, Config.Token)
snapshotList, error := Config.Client.GetStorageSnapshotList(x.StorageUUID)
if error != nil {
Unknown("Error retrieving snapshots")
}
if snapshot == nil {
if snapshotList == nil {
Unknown("No snapshots found")
}
timestamp := snapshotList[0].Properties.CreateTime
storageSnapshot := gsclient.StorageSnapshot{}
for _,v := range snapshotList {
if v.Properties.CreateTime > timestamp {
timestamp = v.Properties.CreateTime
storageSnapshot = v
}
}
warningDuration := time.Duration(x.Warning) * time.Hour
criticalDuration := time.Duration(x.Critical) * time.Hour
@ -44,7 +49,7 @@ func (x *SnapshotsAgeCommand) Execute(args []string) error {
criticalDuration = criticalDuration * 24
}
checkAge(*snapshot, warningDuration, criticalDuration)
checkAge(storageSnapshot, warningDuration, criticalDuration)
return nil
}
@ -52,20 +57,26 @@ func (x *SnapshotsAgeCommand) Execute(args []string) error {
func init() {
}
func checkAge(snapshot gridscale.Snapshot, warning time.Duration, critical time.Duration) error {
func checkAge(snapshot gsclient.StorageSnapshot, warning time.Duration, critical time.Duration) error {
snapshotCreateTime, err := time.Parse("2019-09-02T12:44:48Z", snapshot.Properties.CreateTime)
if err != nil {
Unknown("Error converting CreateTime of snapshot")
}
nowAndW := time.Now().Local().Add(-warning)
nowAndC := time.Now().Local().Add(-critical)
if snapshot.CreateTime.Before(nowAndC) {
Critical(fmt.Sprintf("Snapshot is too old: %s", snapshot.CreateTime))
if snapshotCreateTime.Before(nowAndC) {
Critical(fmt.Sprintf("Snapshot is too old: %s", snapshotCreateTime))
}
if snapshot.CreateTime.Before(nowAndW) {
Warning(fmt.Sprintf("Snapshot is too old: %s", snapshot.CreateTime))
if snapshotCreateTime.Before(nowAndW) {
Warning(fmt.Sprintf("Snapshot is too old: %s", snapshotCreateTime))
}
Ok(fmt.Sprintf("Snapshot is new enough: %s", snapshot.CreateTime))
Ok(fmt.Sprintf("Snapshot is new enough: %s", snapshotCreateTime))
// never reached
return nil

View File

@ -4,7 +4,7 @@ package main
import (
"fmt"
"g.hazardous.org/fkr/libretto/virtualmachine/gridscale"
"github.com/gridscale/gsclient-go"
)
type SnapshotsCountCommand struct {
@ -19,13 +19,7 @@ func (x *SnapshotsCountCommand) Execute(args []string) error {
config(Checker.ConfigFile)
storage, err := gridscale.GetStorageByUUID(Config.UserId, Config.Token, x.StorageUUID)
if err != nil {
Unknown(fmt.Sprintf("%s: %s", "Error retrieving storage", err))
}
snapshots, error := storage.GetSnapshots(Config.UserId, Config.Token)
snapshots, error := Config.Client.GetStorageSnapshotList(x.StorageUUID)
if error != nil {
Unknown("Error retrieving snapshots")
@ -39,19 +33,19 @@ func (x *SnapshotsCountCommand) Execute(args []string) error {
func init() {
}
func count(snapshots map[string]*gridscale.Snapshot, warning int, critical int) error {
func count(snapshots []gsclient.StorageSnapshot, warning int, critical int) error {
snapshotCount := len(snapshots)
snapshotsCount := len(snapshots)
if snapshotCount < critical {
Critical(fmt.Sprintf("not enough snapshots - found %d snapshots", snapshotCount))
if snapshotsCount < critical {
Critical(fmt.Sprintf("not enough snapshots - found %d snapshots", snapshotsCount))
}
if snapshotCount < warning {
Warning(fmt.Sprintf("not enough snapshots - found %d snapshots", snapshotCount))
if snapshotsCount < warning {
Warning(fmt.Sprintf("not enough snapshots - found %d snapshots", snapshotsCount))
}
Ok(fmt.Sprintf("%d snapshots exist", len(snapshots)))
Ok(fmt.Sprintf("%d snapshots exist", snapshotsCount))
// never reached
return nil

View File

@ -4,7 +4,6 @@ package main
import (
"fmt"
"g.hazardous.org/fkr/libretto/virtualmachine/gridscale"
)
type StorageListCommand struct {
@ -16,14 +15,14 @@ func (x *StorageListCommand) Execute(args []string) error {
config(Checker.ConfigFile)
storages, error := gridscale.GetStorages(Config.UserId, Config.Token)
storages, error := Config.Client.GetStorageList()
if error != nil {
Unknown(fmt.Sprintf("Error while retrieving storages: %s", error))
}
for key, storage := range storages {
fmt.Printf("%s: %v\n\n", key, storage)
fmt.Printf("%s: %v\n\n", key, storage.Properties)
}