Merge pull request #5112 from serbaut/master
provider/cloudstack: Improve ssh keypair handling
This commit is contained in:
commit
7cbc9e3c07
|
@ -2,12 +2,11 @@ package cloudstack
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/pathorcontents"
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
"github.com/mitchellh/go-homedir"
|
||||
"github.com/xanzy/go-cloudstack/cloudstack"
|
||||
)
|
||||
|
||||
|
@ -30,6 +29,12 @@ func resourceCloudStackSSHKeyPair() *schema.Resource {
|
|||
ForceNew: true,
|
||||
},
|
||||
|
||||
"project": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
|
||||
"private_key": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
|
@ -51,17 +56,15 @@ func resourceCloudStackSSHKeyPairCreate(d *schema.ResourceData, meta interface{}
|
|||
|
||||
if publicKey != "" {
|
||||
// Register supplied key
|
||||
keyPath, err := homedir.Expand(publicKey)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error expanding the public key path: %v", err)
|
||||
}
|
||||
|
||||
key, err := ioutil.ReadFile(keyPath)
|
||||
key, _, err := pathorcontents.Read(publicKey)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error reading the public key: %v", err)
|
||||
}
|
||||
|
||||
p := cs.SSH.NewRegisterSSHKeyPairParams(name, string(key))
|
||||
if err := setProjectid(p, cs, d); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = cs.SSH.RegisterSSHKeyPair(p)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -69,6 +72,9 @@ func resourceCloudStackSSHKeyPairCreate(d *schema.ResourceData, meta interface{}
|
|||
} else {
|
||||
// No key supplied, must create one and return the private key
|
||||
p := cs.SSH.NewCreateSSHKeyPairParams(name)
|
||||
if err := setProjectid(p, cs, d); err != nil {
|
||||
return err
|
||||
}
|
||||
r, err := cs.SSH.CreateSSHKeyPair(p)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -89,6 +95,9 @@ func resourceCloudStackSSHKeyPairRead(d *schema.ResourceData, meta interface{})
|
|||
|
||||
p := cs.SSH.NewListSSHKeyPairsParams()
|
||||
p.SetName(d.Id())
|
||||
if err := setProjectid(p, cs, d); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r, err := cs.SSH.ListSSHKeyPairs(p)
|
||||
if err != nil {
|
||||
|
@ -112,6 +121,9 @@ func resourceCloudStackSSHKeyPairDelete(d *schema.ResourceData, meta interface{}
|
|||
|
||||
// Create a new parameter struct
|
||||
p := cs.SSH.NewDeleteSSHKeyPairParams(d.Id())
|
||||
if err := setProjectid(p, cs, d); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Remove the SSH Keypair
|
||||
_, err := cs.SSH.DeleteSSHKeyPair(p)
|
||||
|
|
|
@ -182,3 +182,19 @@ func setCidrList(rule map[string]interface{}, cidrList string) {
|
|||
|
||||
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" {
|
||||
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
|
||||
created.
|
||||
|
||||
* `public_key` - (Optional) The path to a public key that will be uploaded
|
||||
the remote machine. If this is omitted, CloudStack will generate a new
|
||||
key pair. Changing this forces a new resource to be created.
|
||||
* `public_key` - (Optional) The public key in OpenSSH
|
||||
`authorized_keys` format. If this is omitted, CloudStack will
|
||||
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
|
||||
|
||||
|
|
Loading…
Reference in New Issue