Skip to content

Commit 7d30423

Browse files
committed
provider/cloudstack: Improve ssh keypair handling
- adds support for projects - adds support for public_key strings as well as filenames
1 parent 8650a3b commit 7d30423

3 files changed

Lines changed: 45 additions & 11 deletions

File tree

builtin/providers/cloudstack/resource_cloudstack_ssh_keypair.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package cloudstack
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"log"
76
"strings"
87

8+
"github.com/hashicorp/terraform/helper/pathorcontents"
99
"github.com/hashicorp/terraform/helper/schema"
10-
"github.com/mitchellh/go-homedir"
1110
"github.com/xanzy/go-cloudstack/cloudstack"
1211
)
1312

@@ -30,6 +29,12 @@ func resourceCloudStackSSHKeyPair() *schema.Resource {
3029
ForceNew: true,
3130
},
3231

32+
"project": &schema.Schema{
33+
Type: schema.TypeString,
34+
Optional: true,
35+
ForceNew: true,
36+
},
37+
3338
"private_key": &schema.Schema{
3439
Type: schema.TypeString,
3540
Computed: true,
@@ -51,24 +56,25 @@ func resourceCloudStackSSHKeyPairCreate(d *schema.ResourceData, meta interface{}
5156

5257
if publicKey != "" {
5358
// Register supplied key
54-
keyPath, err := homedir.Expand(publicKey)
55-
if err != nil {
56-
return fmt.Errorf("Error expanding the public key path: %v", err)
57-
}
58-
59-
key, err := ioutil.ReadFile(keyPath)
59+
key, _, err := pathorcontents.Read(publicKey)
6060
if err != nil {
6161
return fmt.Errorf("Error reading the public key: %v", err)
6262
}
6363

6464
p := cs.SSH.NewRegisterSSHKeyPairParams(name, string(key))
65+
if err := setProjectid(p, cs, d); err != nil {
66+
return err
67+
}
6568
_, err = cs.SSH.RegisterSSHKeyPair(p)
6669
if err != nil {
6770
return err
6871
}
6972
} else {
7073
// No key supplied, must create one and return the private key
7174
p := cs.SSH.NewCreateSSHKeyPairParams(name)
75+
if err := setProjectid(p, cs, d); err != nil {
76+
return err
77+
}
7278
r, err := cs.SSH.CreateSSHKeyPair(p)
7379
if err != nil {
7480
return err
@@ -89,6 +95,9 @@ func resourceCloudStackSSHKeyPairRead(d *schema.ResourceData, meta interface{})
8995

9096
p := cs.SSH.NewListSSHKeyPairsParams()
9197
p.SetName(d.Id())
98+
if err := setProjectid(p, cs, d); err != nil {
99+
return err
100+
}
92101

93102
r, err := cs.SSH.ListSSHKeyPairs(p)
94103
if err != nil {
@@ -112,6 +121,9 @@ func resourceCloudStackSSHKeyPairDelete(d *schema.ResourceData, meta interface{}
112121

113122
// Create a new parameter struct
114123
p := cs.SSH.NewDeleteSSHKeyPairParams(d.Id())
124+
if err := setProjectid(p, cs, d); err != nil {
125+
return err
126+
}
115127

116128
// Remove the SSH Keypair
117129
_, err := cs.SSH.DeleteSSHKeyPair(p)

builtin/providers/cloudstack/resources.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,19 @@ func setCidrList(rule map[string]interface{}, cidrList string) {
182182

183183
rule["cidr_list"] = cidrs
184184
}
185+
186+
type projectidSetter interface {
187+
SetProjectid(string)
188+
}
189+
190+
// If there is a project supplied, we retrieve and set the project id
191+
func setProjectid(p projectidSetter, cs *cloudstack.CloudStackClient, d *schema.ResourceData) error {
192+
if project, ok := d.GetOk("project"); ok {
193+
projectid, e := retrieveID(cs, "project", project.(string))
194+
if e != nil {
195+
return e.Error()
196+
}
197+
p.SetProjectid(projectid)
198+
}
199+
return nil
200+
}

website/source/docs/providers/cloudstack/r/ssh_keypair.html.markdown

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Creates or registers an SSH key pair.
1515
```
1616
resource "cloudstack_ssh_keypair" "default" {
1717
name = "myKey"
18+
public_key = "${file("~/.ssh/id_rsa.pub")}"
19+
project = "myProject"
1820
}
1921
```
2022

@@ -26,9 +28,13 @@ The following arguments are supported:
2628
within a CloudStack account. Changing this forces a new resource to be
2729
created.
2830

29-
* `public_key` - (Optional) The path to a public key that will be uploaded
30-
the remote machine. If this is omitted, CloudStack will generate a new
31-
key pair. Changing this forces a new resource to be created.
31+
* `public_key` - (Optional) The public key in OpenSSH
32+
`authorized_keys` format. If this is omitted, CloudStack will
33+
generate a new key pair. Changing this forces a new resource to be
34+
created.
35+
36+
* `project` - (Optional) The name or ID of the project to register this
37+
key to. Changing this forces a new resource to be created.
3238

3339
## Attributes Reference
3440

0 commit comments

Comments
 (0)