From 7d30423a616d8ebda767e861fd2a4f99d01b7b03 Mon Sep 17 00:00:00 2001 From: Joakim Sernbrant Date: Fri, 26 Feb 2016 23:01:56 +0100 Subject: [PATCH] provider/cloudstack: Improve ssh keypair handling - adds support for projects - adds support for public_key strings as well as filenames --- .../resource_cloudstack_ssh_keypair.go | 28 +++++++++++++------ builtin/providers/cloudstack/resources.go | 16 +++++++++++ .../cloudstack/r/ssh_keypair.html.markdown | 12 ++++++-- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/builtin/providers/cloudstack/resource_cloudstack_ssh_keypair.go b/builtin/providers/cloudstack/resource_cloudstack_ssh_keypair.go index 8f6f0f9c5..5235985d7 100644 --- a/builtin/providers/cloudstack/resource_cloudstack_ssh_keypair.go +++ b/builtin/providers/cloudstack/resource_cloudstack_ssh_keypair.go @@ -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) diff --git a/builtin/providers/cloudstack/resources.go b/builtin/providers/cloudstack/resources.go index 8d7090e1f..3e781fd4a 100644 --- a/builtin/providers/cloudstack/resources.go +++ b/builtin/providers/cloudstack/resources.go @@ -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 +} diff --git a/website/source/docs/providers/cloudstack/r/ssh_keypair.html.markdown b/website/source/docs/providers/cloudstack/r/ssh_keypair.html.markdown index a7a57bb27..372d1f488 100644 --- a/website/source/docs/providers/cloudstack/r/ssh_keypair.html.markdown +++ b/website/source/docs/providers/cloudstack/r/ssh_keypair.html.markdown @@ -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