diff options
Diffstat (limited to 'droplet.go')
| -rw-r--r-- | droplet.go | 165 |
1 files changed, 107 insertions, 58 deletions
@@ -1,10 +1,11 @@ package digitalocean -import ( +import ( "errors" "sort" - "strings" "strconv" + "strings" + "github.com/digitalocean/godo" "go.wit.com/log" @@ -12,10 +13,12 @@ import ( ) func (d *DigitalOcean) NewDroplet(dd *godo.Droplet) *Droplet { - if ! myDo.Ready() {return nil} + if !myDo.Ready() { + return nil + } // check if the droplet ID already exists - if (d.dropMap[dd.ID] != nil) { + if d.dropMap[dd.ID] != nil { log.Error(errors.New("droplet.NewDroplet() already exists")) return d.dropMap[dd.ID] } @@ -26,7 +29,7 @@ func (d *DigitalOcean) NewDroplet(dd *godo.Droplet) *Droplet { droplet.ID = dd.ID droplet.image = dd.Image.Name + " (" + dd.Image.Slug + ")" - if (d.dGrid == nil) { + if d.dGrid == nil { d.dGrid = d.group.NewGrid("grid", 12, 1).Pad() } @@ -56,23 +59,23 @@ func (d *DigitalOcean) NewDroplet(dd *godo.Droplet) *Droplet { droplet.imageN = d.dGrid.NewLabel(dd.Image.Slug) droplet.statusN = d.dGrid.NewLabel(dd.Status) - droplet.connect = d.dGrid.NewButton("Connect", func () { + droplet.connect = d.dGrid.NewButton("Connect", func() { droplet.Connect() }) - droplet.edit = d.dGrid.NewButton("Edit", func () { + droplet.edit = d.dGrid.NewButton("Edit", func() { droplet.Show() }) - droplet.poweroff = d.dGrid.NewButton("Power Off", func () { + droplet.poweroff = d.dGrid.NewButton("Power Off", func() { droplet.PowerOff() }) - droplet.poweron = d.dGrid.NewButton("Power On", func () { + droplet.poweron = d.dGrid.NewButton("Power On", func() { droplet.PowerOn() }) - droplet.destroy = d.dGrid.NewButton("Destroy", func () { + droplet.destroy = d.dGrid.NewButton("Destroy", func() { droplet.Destroy() }) @@ -81,9 +84,11 @@ func (d *DigitalOcean) NewDroplet(dd *godo.Droplet) *Droplet { } func (d *Droplet) Active() bool { - if ! d.Ready() {return false} + if !d.Ready() { + return false + } log.Log(POLL, "droplet.Active() status: ", d.poll.Status, "d.statusN.GetText() =", d.statusN.GetText()) - if (d.statusN.GetText() == "active") { + if d.statusN.GetText() == "active" { return true } return false @@ -91,25 +96,33 @@ func (d *Droplet) Active() bool { // Returns true if the droplet is finished installing func (d *Droplet) Ready() bool { - if d == nil {return false} + if d == nil { + return false + } return d.ready } // Returns true if the droplet is running func (d *Droplet) On() bool { - if ! d.Ready() {return false} + if !d.Ready() { + return false + } return true } func (d *Droplet) HasIPv4() bool { - if ! d.Ready() {return false} + if !d.Ready() { + return false + } if d.ipv4.GetText() == "" { return false } return true } func (d *Droplet) HasIPv6() bool { - if ! d.Ready() {return false} + if !d.Ready() { + return false + } if d.ipv6.GetText() == "" { return false } @@ -117,18 +130,24 @@ func (d *Droplet) HasIPv6() bool { } func (d *Droplet) GetIPv4() string { - if ! d.Ready() {return ""} + if !d.Ready() { + return "" + } return d.ipv4.GetText() } func (d *Droplet) GetIPv6() string { - if ! d.Ready() {return ""} + if !d.Ready() { + return "" + } log.Info("droplet GetIPv6 has: n.GetText()", d.ipv6.GetText()) return d.ipv6.GetText() } func (d *Droplet) Connect() { - if ! d.Ready() {return} + if !d.Ready() { + return + } if d.HasIPv4() { ipv4 := d.GetIPv4() log.Info("droplet has IPv4 =", ipv4) @@ -145,7 +164,9 @@ func (d *Droplet) Connect() { } func (d *Droplet) Update(dpoll *godo.Droplet) { - if ! d.Exists() {return} + if !d.Exists() { + return + } d.poll = dpoll log.Log(POLL, "droplet", dpoll.Name, "dpoll.Status =", dpoll.Status) log.Spew(dpoll) @@ -164,50 +185,58 @@ func (d *Droplet) Update(dpoll *godo.Droplet) { } func (d *Droplet) PowerOn() { - if ! d.Exists() {return} + if !d.Exists() { + return + } log.Info("droplet.PowerOn() should do it here") myDo.PowerOn(d.ID) } func (d *Droplet) PowerOff() { - if ! d.Exists() {return} + if !d.Exists() { + return + } log.Info("droplet.PowerOff() here") myDo.PowerOff(d.ID) } func (d *Droplet) Destroy() { - if ! d.Exists() {return} + if !d.Exists() { + return + } log.Info("droplet.Destroy() ID =", d.ID, "Name =", d.nameN.GetText()) myDo.deleteDroplet(d) } /* -type Droplet struct { - ID int `json:"id,float64,omitempty"` - Name string `json:"name,omitempty"` - Memory int `json:"memory,omitempty"` - Vcpus int `json:"vcpus,omitempty"` - Disk int `json:"disk,omitempty"` - Region *Region `json:"region,omitempty"` - Image *Image `json:"image,omitempty"` - Size *Size `json:"size,omitempty"` - SizeSlug string `json:"size_slug,omitempty"` - BackupIDs []int `json:"backup_ids,omitempty"` - NextBackupWindow *BackupWindow `json:"next_backup_window,omitempty"` - SnapshotIDs []int `json:"snapshot_ids,omitempty"` - Features []string `json:"features,omitempty"` - Locked bool `json:"locked,bool,omitempty"` - Status string `json:"status,omitempty"` - Networks *Networks `json:"networks,omitempty"` - Created string `json:"created_at,omitempty"` - Kernel *Kernel `json:"kernel,omitempty"` - Tags []string `json:"tags,omitempty"` - VolumeIDs []string `json:"volume_ids"` - VPCUUID string `json:"vpc_uuid,omitempty"` -} + type Droplet struct { + ID int `json:"id,float64,omitempty"` + Name string `json:"name,omitempty"` + Memory int `json:"memory,omitempty"` + Vcpus int `json:"vcpus,omitempty"` + Disk int `json:"disk,omitempty"` + Region *Region `json:"region,omitempty"` + Image *Image `json:"image,omitempty"` + Size *Size `json:"size,omitempty"` + SizeSlug string `json:"size_slug,omitempty"` + BackupIDs []int `json:"backup_ids,omitempty"` + NextBackupWindow *BackupWindow `json:"next_backup_window,omitempty"` + SnapshotIDs []int `json:"snapshot_ids,omitempty"` + Features []string `json:"features,omitempty"` + Locked bool `json:"locked,bool,omitempty"` + Status string `json:"status,omitempty"` + Networks *Networks `json:"networks,omitempty"` + Created string `json:"created_at,omitempty"` + Kernel *Kernel `json:"kernel,omitempty"` + Tags []string `json:"tags,omitempty"` + VolumeIDs []string `json:"volume_ids"` + VPCUUID string `json:"vpc_uuid,omitempty"` + } */ func (d *Droplet) Show() { - if ! d.Exists() {return} + if !d.Exists() { + return + } log.Info("droplet: ID =", d.ID) log.Info("droplet: Name =", d.GetName()) log.Info("droplet: Size =", d.GetSize()) @@ -220,47 +249,67 @@ func (d *Droplet) Show() { } func (d *Droplet) Hide() { - if ! d.Exists() {return} + if !d.Exists() { + return + } log.Info("droplet.Hide() window") - if ! d.hidden { + if !d.hidden { // d.window.Hide() } d.hidden = true } func (d *Droplet) Exists() bool { - if ! myDo.Ready() {return false} - if d == nil {return false} - if d.poll == nil {return false} + if !myDo.Ready() { + return false + } + if d == nil { + return false + } + if d.poll == nil { + return false + } return d.ready } func (d *Droplet) GetName() string { - if ! d.Ready() {return ""} + if !d.Ready() { + return "" + } return d.nameN.GetText() } func (d *Droplet) GetSize() string { - if ! d.Ready() {return ""} + if !d.Ready() { + return "" + } return d.sizeSlugN.GetText() } func (d *Droplet) GetMemory() string { - if ! d.Ready() {return ""} + if !d.Ready() { + return "" + } return strconv.Itoa(d.memory) } func (d *Droplet) GetDisk() string { - if ! d.Ready() {return ""} + if !d.Ready() { + return "" + } return strconv.Itoa(d.disk) } func (d *Droplet) GetImage() string { - if ! d.Ready() {return ""} + if !d.Ready() { + return "" + } return d.imageN.GetText() } func (d *Droplet) GetStatus() string { - if ! d.Ready() {return ""} + if !d.Ready() { + return "" + } return d.statusN.GetText() } |
