WIP destroy

This commit is contained in:
Quentin 2021-09-22 23:21:05 +02:00
parent 248346d980
commit 5476ba20d1
Signed by: quentin
GPG key ID: A98E9B769E4FF428

91
main.go
View file

@ -102,26 +102,23 @@ func passInstanceTo(r io.Reader, d instanceReceiver) error {
return nil
}
/**
* Spawner utility
* instance wrapper
*/
type spawner struct {
type instance struct {
api *instance.API
}
func newSpawner() (spawner, error) {
func (i *instance) init() error {
client, err := getClient()
if err != nil {
return spawner{}, err
return err
}
return spawner{
api: instance.NewAPI(client),
}, nil
i.api = instance.NewAPI(client),
}
func (sp *spawner) getInstanceByName(zone scw.Zone, name string) (*instance.Server, error) {
lr, err := sp.api.ListServers(&instance.ListServersRequest{
func (i *instance) getInstanceByName(zone scw.Zone, name string) (*instance.Server, error) {
lr, err := i.api.ListServers(&instance.ListServersRequest{
Zone: zone,
Name: &name,
})
@ -136,8 +133,31 @@ func (sp *spawner) getInstanceByName(zone scw.Zone, name string) (*instance.Serv
return nil, instanceNotFound
}
func (sp *spawner) onInstance(zone, machine, image, name string) error {
func parseIP(s *instance.Server) string {
ip := "(no address)"
if s.PublicIP != nil {
ip = s.PublicIP.Address.String()
if s.PublicIP.Dynamic {
ip += " (dynamic)"
}
} else if s.PrivateIP != nil {
ip = fmt.Sprintf("%v %v", s.PrivateIP, "(private)")
}
return ip
}
/**
* Spawner
*/
type spawner instance
func newSpawner() (spawner, error) {
return newInstance()
}
func (sp *spawner) onInstance(zone, machine, image, name string) error {
z, err := scw.ParseZone(zone)
if err != nil {
return err
@ -158,7 +178,7 @@ func (sp *spawner) onInstance(zone, machine, image, name string) error {
Name: name,
CommercialType: machine,
Image: image,
DynamicIPRequired: scw.BoolPtr(true),
DynamicIPRequired: scw.BoolPtr(false),
})
if err != nil {
return err
@ -189,18 +209,28 @@ func (sp *spawner) onInstance(zone, machine, image, name string) error {
return nil
}
/**
* Destroyer
*/
type destroyer instance
func parseIP(s *instance.Server) string {
ip := "(no address)"
if s.PublicIP != nil {
ip = s.PublicIP.Address.String()
if s.PublicIP.Dynamic {
ip += " (dynamic)"
func (dt *destroyer) onInstance(zone, machine, image, name string) error {
z, err := scw.ParseZone(zone)
if err != nil {
return err
}
} else if s.PrivateIP != nil {
ip = fmt.Sprintf("%v %v", s.PrivateIP, "(private)")
targetServer, err := dt.inst.getInstanceByName(z, name)
if err != nil {
return err
}
return ip
err = dt.inst.api.DeleteServer(&instance.DeleteServerRequest{
Zone: z,
ServerID: targetServer.ID
})
return err
}
@ -209,17 +239,14 @@ func parseIP(s *instance.Server) string {
*/
func spawn() error {
sp, err := newSpawner()
sp := spawner{}
err := sp.init()
if err != nil {
return err
}
err = passInstanceTo(os.Stdin, &sp)
if err != nil {
return err
}
return nil
}
func run() error {
@ -227,9 +254,15 @@ func run() error {
}
func destroy() error {
return nil
}
dt := destroyer{}
err := dt.init()
if err != nil {
return err
}
err = passInstanceTo(os.Stdin, &dt)
return err
}
func getClient() (*scw.Client, error) {