work on Connect()

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-12-31 07:45:37 -06:00
parent c5ff4637fb
commit e4a04f36f0
4 changed files with 90 additions and 34 deletions

View File

@ -45,12 +45,11 @@ func createDroplet(token, name, region, size, image string) (*godo.Droplet, erro
}
*/
func (d *DigitalOcean) Create(name string, region string) {
func (d *DigitalOcean) Create(name string, region string, size string) {
// region := "nyc1" // New York City region.
size := "s-1vcpu-1gb" // Size of the droplet.
// size := "s-1vcpu-1gb" // Size of the droplet.
image := "ubuntu-20-04-x64" // Image slug for Ubuntu 20.04 (LTS) x64.
return
// Create a new droplet.
droplet, err := d.createDropletNew(name, region, size, image)
if err != nil {
@ -115,7 +114,10 @@ var myCreate *windowCreate
// You can only have one of these
func InitCreateWindow() *windowCreate {
if ! myDo.Ready() {return nil}
if myCreate != nil {return myCreate}
if myCreate != nil {
myCreate.Show()
return myCreate
}
myCreate = new(windowCreate)
myCreate.ready = false
@ -127,8 +129,7 @@ func InitCreateWindow() *windowCreate {
myCreate.name = gadgets.NewBasicEntry(myCreate.grid, "Name").Set("test.wit.com")
myCreate.zone = gadgets.NewBasicDropdown(myCreate.grid, "Region")
myCreate.region = gadgets.NewBasicDropdown(myCreate.grid, "Region")
regions := myDo.listRegions()
@ -137,26 +138,47 @@ func InitCreateWindow() *windowCreate {
for i, region := range regions {
log.Infof("i: %d, Slug: %s, Name: %s, Available: %v\n", i, region.Slug, region.Name, region.Available)
log.Spew(i, region)
myCreate.zone.Add(region.Name)
if len(region.Sizes) == 0 {
log.Info("Skipping region. No available sizes region =", region.Name)
} else {
myCreate.region.Add(region.Name)
}
}
var zone godo.Region
myCreate.zone.Custom = func() {
s := myCreate.zone.Get()
myCreate.region.Custom = func() {
s := myCreate.region.Get()
log.Info("create droplet region changed to:", s)
for _, region := range regions {
if s == region.Name {
log.Info("Found region! slug =", region.Slug, region)
zone = region
log.Info("Found region! Now update all the sizes count =", len(region.Sizes))
for _, size := range region.Sizes {
log.Info("Size: ", size)
}
}
}
}
myCreate.grid.NewLabel("makes a new droplet")
myCreate.grid.NewButton("Create", func () {
myCreate.size = gadgets.NewBasicDropdown(myCreate.grid, "Size")
myCreate.size.Add("s-1vcpu-1gb")
myCreate.size.Add("s-1vcpu-1gb-amd")
myCreate.size.Add("s-1vcpu-1gb-intel")
myCreate.group.NewLabel("Create Droplet")
// box := myCreate.group.NewBox("vBox", false).Pad()
box := myCreate.group.NewBox("hBox", true).Pad()
box.NewButton("Cancel", func () {
myCreate.Hide()
})
box.NewButton("Create", func () {
name := myCreate.name.Get()
log.Info("create droplet name =", name, "zone =", zone.Slug)
myDo.Create(name, zone.Slug)
size := myCreate.size.Get()
log.Info("create droplet name =", name, "region =", zone.Slug, "size =", size)
myDo.Create(name, zone.Slug, size)
myCreate.Hide()
})
myCreate.ready = true

View File

@ -2,6 +2,8 @@ package digitalocean
import (
"errors"
"sort"
"strings"
"github.com/digitalocean/godo"
"go.wit.com/log"
@ -23,25 +25,30 @@ func (d *DigitalOcean) NewDroplet(dd *godo.Droplet) *Droplet {
droplet.ID = dd.ID
if (d.dGrid == nil) {
d.dGrid = d.group.NewGrid("grid", 9, 1).Pad()
d.dGrid = d.group.NewGrid("grid", 10, 1).Pad()
}
droplet.name = d.dGrid.NewLabel(dd.Name)
droplet.box4 = d.dGrid.NewBox("hBox", true)
droplet.grid4 = droplet.box4.NewGrid("grid", 2, 1).Pad()
var ipv4 []string
var ipv6 []string
for _, network := range dd.Networks.V4 {
if network.Type == "public" {
droplet.grid4.NewLabel(network.IPAddress)
ipv4 = append(ipv4, network.IPAddress)
}
}
droplet.box6 = d.dGrid.NewBox("hBox", true)
droplet.grid6 = droplet.box6.NewGrid("grid", 2, 1).Pad()
for _, network := range dd.Networks.V6 {
if network.Type == "public" {
droplet.grid6.NewLabel(network.IPAddress)
ipv6 = append(ipv6, network.IPAddress)
}
}
sort.Strings(ipv4)
sort.Strings(ipv6)
droplet.ipv4 = d.dGrid.NewLabel(strings.Join(ipv4, "\n"))
droplet.ipv6 = d.dGrid.NewLabel(strings.Join(ipv6, "\n"))
droplet.sizeSlug = d.dGrid.NewLabel(dd.SizeSlug)
droplet.status = d.dGrid.NewLabel(dd.Status)
@ -70,7 +77,7 @@ func (d *DigitalOcean) NewDroplet(dd *godo.Droplet) *Droplet {
}
func (d *Droplet) Active() bool {
if ! d.Exists() {return false}
if ! d.Ready() {return false}
log.Info("droplet.Active() status: ", d.poll.Status)
if (d.status.S == "active") {
return true
@ -78,9 +85,41 @@ func (d *Droplet) Active() bool {
return false
}
// Returns true if the droplet is finished installing
func (d *Droplet) Ready() bool {
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}
return true
}
func (d *Droplet) HasIPv4() bool {
if ! d.Ready() {return false}
return true
}
func (d *Droplet) GetIPv4() string {
if ! d.Ready() {return ""}
return d.ipv4.GetText()
}
func (d *Droplet) GetIPv6() string {
if ! d.Ready() {return ""}
log.Info("droplet GetIPv6 has: n.GetText()", d.ipv6.GetText())
return d.ipv6.GetText()
}
func (d *Droplet) Connect() {
if ! d.Exists() {return}
log.Info("droplet.Connect() here")
if ! d.Ready() {return}
if d.HasIPv4() {
ipv4 := d.GetIPv4()
log.Info("droplet has IPv4 =", ipv4)
}
log.Info("droplet.Connect() here", d.GetIPv4(), d.GetIPv6())
}
func (d *Droplet) Update(dpoll *godo.Droplet) {

View File

@ -24,7 +24,7 @@ func (d *DigitalOcean) ListSSHKeyID() error {
// Find the key by name.
for _, key := range keys {
log.Info("found ssh", key.Name)
log.Info("found ssh wierd", key.Name)
log.Verbose("found ssh key:", key)
}
/*

View File

@ -49,7 +49,8 @@ type windowCreate struct {
tag *gadgets.OneLiner
name *gadgets.BasicEntry
zone *gadgets.BasicDropdown
region *gadgets.BasicDropdown
size *gadgets.BasicDropdown
}
type ipButton struct {
@ -67,6 +68,7 @@ type Droplet struct {
poll *godo.Droplet // store what the digital ocean API returned
name *gui.Node
sizeSlug *gui.Node
status *gui.Node
destroy *gui.Node
connect *gui.Node
@ -74,13 +76,6 @@ type Droplet struct {
poweroff *gui.Node
edit *gui.Node
// a box and grid of the IPv4 addresses
box4 *gui.Node
grid4 *gui.Node
ipv4 []ipButton
// a box and grid of the IPv6 addresses
box6 *gui.Node
grid6 *gui.Node
ipv6 []ipButton
ipv4 *gui.Node
ipv6 *gui.Node
}