Merge pull request #1009 from hairyhenderson/softlayer-remove-sshkey-on-delete-765

Deleting Machine-generated SSH key from SoftLayer when host is canceled.
This commit is contained in:
Evan Hazlett 2015-04-20 14:00:24 -07:00
commit 4047c97679
2 changed files with 29 additions and 1 deletions

View File

@ -31,6 +31,7 @@ type Driver struct {
MachineName string
CaCertPath string
PrivateKeyPath string
SSHKeyID int
SwarmMaster bool
SwarmHost string
SwarmDiscovery string
@ -384,6 +385,9 @@ func (d *Driver) Create() error {
return err
}
log.Infof("SSH key %s (%d) created in SoftLayer", key.Label, key.Id)
d.SSHKeyID = key.Id
spec := d.buildHostSpec()
spec.SshKeys = []*SshKey{key}
@ -453,7 +457,9 @@ func (d *Driver) publicSSHKeyPath() string {
func (d *Driver) Kill() error {
return d.getClient().VirtualGuest().PowerOff(d.Id)
}
func (d *Driver) Remove() error {
log.Infof("Canceling SoftLayer instance %d...", d.Id)
var err error
for i := 0; i < 5; i++ {
if err = d.getClient().VirtualGuest().Cancel(d.Id); err != nil {
@ -462,7 +468,16 @@ func (d *Driver) Remove() error {
}
break
}
return err
if err != nil {
return err
}
log.Infof("Removing SSH Key %d...", d.SSHKeyID)
if err = d.getClient().SshKey().Delete(d.SSHKeyID); err != nil {
return err
}
return nil
}
func (d *Driver) Restart() error {
return d.getClient().VirtualGuest().Reboot(d.Id)

View File

@ -150,6 +150,19 @@ func (c *sshKey) Create(label, key string) (*SshKey, error) {
return &k, nil
}
func (c *sshKey) Delete(id int) error {
var (
method = "DELETE"
uri = fmt.Sprintf("%s/%v", c.namespace(), id)
)
_, err := c.newRequest(method, uri, nil)
if err != nil {
return err
}
return nil
}
func (c *Client) VirtualGuest() *virtualGuest {
return &virtualGuest{c}
}