provider/cloudstack: Improve ssh keypair handling
- adds support for projects - adds support for public_key strings as well as filenames
This commit is contained in:
parent
8650a3bccd
commit
7d30423a61
|
@ -2,12 +2,11 @@ package cloudstack
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/helper/pathorcontents"
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
"github.com/mitchellh/go-homedir"
|
|
||||||
"github.com/xanzy/go-cloudstack/cloudstack"
|
"github.com/xanzy/go-cloudstack/cloudstack"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -30,6 +29,12 @@ func resourceCloudStackSSHKeyPair() *schema.Resource {
|
||||||
ForceNew: true,
|
ForceNew: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"project": &schema.Schema{
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Optional: true,
|
||||||
|
ForceNew: true,
|
||||||
|
},
|
||||||
|
|
||||||
"private_key": &schema.Schema{
|
"private_key": &schema.Schema{
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Computed: true,
|
Computed: true,
|
||||||
|
@ -51,17 +56,15 @@ func resourceCloudStackSSHKeyPairCreate(d *schema.ResourceData, meta interface{}
|
||||||
|
|
||||||
if publicKey != "" {
|
if publicKey != "" {
|
||||||
// Register supplied key
|
// Register supplied key
|
||||||
keyPath, err := homedir.Expand(publicKey)
|
key, _, err := pathorcontents.Read(publicKey)
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Error expanding the public key path: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
key, err := ioutil.ReadFile(keyPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error reading the public key: %v", err)
|
return fmt.Errorf("Error reading the public key: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
p := cs.SSH.NewRegisterSSHKeyPairParams(name, string(key))
|
p := cs.SSH.NewRegisterSSHKeyPairParams(name, string(key))
|
||||||
|
if err := setProjectid(p, cs, d); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
_, err = cs.SSH.RegisterSSHKeyPair(p)
|
_, err = cs.SSH.RegisterSSHKeyPair(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -69,6 +72,9 @@ func resourceCloudStackSSHKeyPairCreate(d *schema.ResourceData, meta interface{}
|
||||||
} else {
|
} else {
|
||||||
// No key supplied, must create one and return the private key
|
// No key supplied, must create one and return the private key
|
||||||
p := cs.SSH.NewCreateSSHKeyPairParams(name)
|
p := cs.SSH.NewCreateSSHKeyPairParams(name)
|
||||||
|
if err := setProjectid(p, cs, d); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
r, err := cs.SSH.CreateSSHKeyPair(p)
|
r, err := cs.SSH.CreateSSHKeyPair(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -89,6 +95,9 @@ func resourceCloudStackSSHKeyPairRead(d *schema.ResourceData, meta interface{})
|
||||||
|
|
||||||
p := cs.SSH.NewListSSHKeyPairsParams()
|
p := cs.SSH.NewListSSHKeyPairsParams()
|
||||||
p.SetName(d.Id())
|
p.SetName(d.Id())
|
||||||
|
if err := setProjectid(p, cs, d); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
r, err := cs.SSH.ListSSHKeyPairs(p)
|
r, err := cs.SSH.ListSSHKeyPairs(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -112,6 +121,9 @@ func resourceCloudStackSSHKeyPairDelete(d *schema.ResourceData, meta interface{}
|
||||||
|
|
||||||
// Create a new parameter struct
|
// Create a new parameter struct
|
||||||
p := cs.SSH.NewDeleteSSHKeyPairParams(d.Id())
|
p := cs.SSH.NewDeleteSSHKeyPairParams(d.Id())
|
||||||
|
if err := setProjectid(p, cs, d); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Remove the SSH Keypair
|
// Remove the SSH Keypair
|
||||||
_, err := cs.SSH.DeleteSSHKeyPair(p)
|
_, err := cs.SSH.DeleteSSHKeyPair(p)
|
||||||
|
|
|
@ -182,3 +182,19 @@ func setCidrList(rule map[string]interface{}, cidrList string) {
|
||||||
|
|
||||||
rule["cidr_list"] = cidrs
|
rule["cidr_list"] = cidrs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type projectidSetter interface {
|
||||||
|
SetProjectid(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there is a project supplied, we retrieve and set the project id
|
||||||
|
func setProjectid(p projectidSetter, cs *cloudstack.CloudStackClient, d *schema.ResourceData) error {
|
||||||
|
if project, ok := d.GetOk("project"); ok {
|
||||||
|
projectid, e := retrieveID(cs, "project", project.(string))
|
||||||
|
if e != nil {
|
||||||
|
return e.Error()
|
||||||
|
}
|
||||||
|
p.SetProjectid(projectid)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -15,6 +15,8 @@ Creates or registers an SSH key pair.
|
||||||
```
|
```
|
||||||
resource "cloudstack_ssh_keypair" "default" {
|
resource "cloudstack_ssh_keypair" "default" {
|
||||||
name = "myKey"
|
name = "myKey"
|
||||||
|
public_key = "${file("~/.ssh/id_rsa.pub")}"
|
||||||
|
project = "myProject"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -26,9 +28,13 @@ The following arguments are supported:
|
||||||
within a CloudStack account. Changing this forces a new resource to be
|
within a CloudStack account. Changing this forces a new resource to be
|
||||||
created.
|
created.
|
||||||
|
|
||||||
* `public_key` - (Optional) The path to a public key that will be uploaded
|
* `public_key` - (Optional) The public key in OpenSSH
|
||||||
the remote machine. If this is omitted, CloudStack will generate a new
|
`authorized_keys` format. If this is omitted, CloudStack will
|
||||||
key pair. Changing this forces a new resource to be created.
|
generate a new key pair. Changing this forces a new resource to be
|
||||||
|
created.
|
||||||
|
|
||||||
|
* `project` - (Optional) The name or ID of the project to register this
|
||||||
|
key to. Changing this forces a new resource to be created.
|
||||||
|
|
||||||
## Attributes Reference
|
## Attributes Reference
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue