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

89
main.go
View file

@ -102,26 +102,23 @@ func passInstanceTo(r io.Reader, d instanceReceiver) error {
return nil return nil
} }
/** /**
* Spawner utility * instance wrapper
*/ */
type spawner struct {
type instance struct {
api *instance.API api *instance.API
} }
func newSpawner() (spawner, error) { func (i *instance) init() error {
client, err := getClient() client, err := getClient()
if err != nil { if err != nil {
return spawner{}, err return err
} }
return spawner{ i.api = instance.NewAPI(client),
api: instance.NewAPI(client),
}, nil
} }
func (i *instance) getInstanceByName(zone scw.Zone, name string) (*instance.Server, error) {
func (sp *spawner) getInstanceByName(zone scw.Zone, name string) (*instance.Server, error) { lr, err := i.api.ListServers(&instance.ListServersRequest{
lr, err := sp.api.ListServers(&instance.ListServersRequest{
Zone: zone, Zone: zone,
Name: &name, Name: &name,
}) })
@ -136,8 +133,31 @@ func (sp *spawner) getInstanceByName(zone scw.Zone, name string) (*instance.Serv
return nil, instanceNotFound 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) z, err := scw.ParseZone(zone)
if err != nil { if err != nil {
return err return err
@ -158,7 +178,7 @@ func (sp *spawner) onInstance(zone, machine, image, name string) error {
Name: name, Name: name,
CommercialType: machine, CommercialType: machine,
Image: image, Image: image,
DynamicIPRequired: scw.BoolPtr(true), DynamicIPRequired: scw.BoolPtr(false),
}) })
if err != nil { if err != nil {
return err return err
@ -189,18 +209,28 @@ func (sp *spawner) onInstance(zone, machine, image, name string) error {
return nil return nil
} }
/**
* Destroyer
*/
type destroyer instance
func parseIP(s *instance.Server) string { func (dt *destroyer) onInstance(zone, machine, image, name string) error {
ip := "(no address)" z, err := scw.ParseZone(zone)
if s.PublicIP != nil { if err != nil {
ip = s.PublicIP.Address.String() return err
if s.PublicIP.Dynamic {
ip += " (dynamic)"
} }
} 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,27 +239,30 @@ func parseIP(s *instance.Server) string {
*/ */
func spawn() error { func spawn() error {
sp, err := newSpawner() sp := spawner{}
err := sp.init()
if err != nil { if err != nil {
return err return err
} }
err = passInstanceTo(os.Stdin, &sp) err = passInstanceTo(os.Stdin, &sp)
if err != nil {
return err return err
} }
return nil
}
func run() error { func run() error {
return nil return nil
} }
func destroy() 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) { func getClient() (*scw.Client, error) {