1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/modules/digitalocean/droplet.go
Chris Cummer f84142553c
WTF-986 User-definable DigitalOcean columns (#1001)
* WTF-986 Wrap the DigitalOcean droplet in our own droplet

This gives us something to build off while still providing the
underlying functionality of the original droplet instance.

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-986 Dynamically display droplet attributes based on defined column names

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-986 Read DigitalOcean column configuration from settings

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-986 Extract the reflection bits into a Reflective package

Signed-off-by: Chris Cummer <chriscummer@me.com>
2020-10-14 09:29:58 -07:00

81 lines
1.6 KiB
Go

package digitalocean
import (
"fmt"
"reflect"
"strings"
"github.com/digitalocean/godo"
"github.com/wtfutil/wtf/utils"
)
// Droplet represents WTF's view of a DigitalOcean droplet
type Droplet struct {
godo.Droplet
Image Image
Region Region
}
// Image represents WTF's view of a DigitalOcean droplet image
type Image struct {
godo.Image
utils.Reflective
}
// Region represents WTF's view of a DigitalOcean region
type Region struct {
godo.Region
utils.Reflective
}
// NewDroplet creates and returns an instance of Droplet
func NewDroplet(doDroplet godo.Droplet) *Droplet {
droplet := &Droplet{
doDroplet,
Image{
*doDroplet.Image,
utils.Reflective{},
},
Region{
*doDroplet.Region,
utils.Reflective{},
},
}
return droplet
}
/* -------------------- Exported Functions -------------------- */
// StringValueForProperty returns a string value for the given column
func (drop *Droplet) StringValueForProperty(propName string) (string, error) {
var strVal string
var err error
// Figure out if we should forward this property to a sub-object
// Lets us support "Region.Name" column definitions
split := strings.Split(propName, ".")
switch split[0] {
case "Image":
strVal, err = drop.Image.StringValueForProperty(split[1])
case "Region":
strVal, err = drop.Region.StringValueForProperty(split[1])
default:
v := reflect.ValueOf(drop)
refVal := reflect.Indirect(v).FieldByName(propName)
if !refVal.IsValid() {
err = fmt.Errorf("invalid property name: %s", propName)
} else {
strVal = fmt.Sprintf("%v", refVal)
}
}
return strVal, err
}