opentofu/builtin/providers/softlayer/resource_softlayer_ssh_key.go
danielcbright 8921e10d71 Added softlayer virtual guest and ssh keys functionality:
Here is an example that will setup the following:
+ An SSH key resource.
+ A virtual server resource that uses an existing SSH key.
+ A virtual server resource using an existing SSH key and a Terraform managed SSH key (created as "test_key_1" in the example below).

(create this as sl.tf and run terraform commands from this directory):
```hcl
provider "softlayer" {
    username = ""
    api_key = ""
}

resource "softlayer_ssh_key" "test_key_1" {
    name = "test_key_1"
    public_key = "${file(\"~/.ssh/id_rsa_test_key_1.pub\")}"
    # Windows Example:
    # public_key = "${file(\"C:\ssh\keys\path\id_rsa_test_key_1.pub\")}"
}

resource "softlayer_virtual_guest" "my_server_1" {
    name = "my_server_1"
    domain = "example.com"
    ssh_keys = ["123456"]
    image = "DEBIAN_7_64"
    region = "ams01"
    public_network_speed = 10
    cpu = 1
    ram = 1024
}

resource "softlayer_virtual_guest" "my_server_2" {
    name = "my_server_2"
    domain = "example.com"
    ssh_keys = ["123456", "${softlayer_ssh_key.test_key_1.id}"]
    image = "CENTOS_6_64"
    region = "ams01"
    public_network_speed = 10
    cpu = 1
    ram = 1024
}
```

You'll need to provide your SoftLayer username and API key,
so that Terraform can connect. If you don't want to put
credentials in your configuration file, you can leave them
out:

```
provider "softlayer" {}
```

...and instead set these environment variables:

- **SOFTLAYER_USERNAME**: Your SoftLayer username
- **SOFTLAYER_API_KEY**: Your API key
2016-05-03 15:58:58 -05:00

160 lines
3.5 KiB
Go

package softlayer
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/hashicorp/terraform/helper/schema"
datatypes "github.com/maximilien/softlayer-go/data_types"
)
func resourceSoftLayerSSHKey() *schema.Resource {
return &schema.Resource{
Create: resourceSoftLayerSSHKeyCreate,
Read: resourceSoftLayerSSHKeyRead,
Update: resourceSoftLayerSSHKeyUpdate,
Delete: resourceSoftLayerSSHKeyDelete,
Exists: resourceSoftLayerSSHKeyExists,
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"public_key": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"fingerprint": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"notes": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: nil,
},
},
}
}
func resourceSoftLayerSSHKeyCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client).sshKeyService
// Build up our creation options
opts := datatypes.SoftLayer_Security_Ssh_Key{
Label: d.Get("name").(string),
Key: d.Get("public_key").(string),
}
if notes, ok := d.GetOk("notes"); ok {
opts.Notes = notes.(string)
}
res, err := client.CreateObject(opts)
if err != nil {
return fmt.Errorf("Error creating SSH Key: %s", err)
}
d.SetId(strconv.Itoa(res.Id))
log.Printf("[INFO] SSH Key: %d", res.Id)
return resourceSoftLayerSSHKeyRead(d, meta)
}
func resourceSoftLayerSSHKeyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client).sshKeyService
keyId, _ := strconv.Atoi(d.Id())
key, err := client.GetObject(keyId)
if err != nil {
// If the key is somehow already destroyed, mark as
// succesfully gone
if strings.Contains(err.Error(), "404 Not Found") {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving SSH key: %s", err)
}
d.Set("id", key.Id)
d.Set("name", key.Label)
d.Set("public_key", strings.TrimSpace(key.Key))
d.Set("fingerprint", key.Fingerprint)
d.Set("notes", key.Notes)
return nil
}
func resourceSoftLayerSSHKeyUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client).sshKeyService
keyId, _ := strconv.Atoi(d.Id())
key, err := client.GetObject(keyId)
if err != nil {
return fmt.Errorf("Error retrieving SSH key: %s", err)
}
if d.HasChange("name") {
key.Label = d.Get("name").(string)
}
if d.HasChange("notes") {
key.Notes = d.Get("notes").(string)
}
_, err = client.EditObject(keyId, key)
if err != nil {
return fmt.Errorf("Error editing SSH key: %s", err)
}
return nil
}
func resourceSoftLayerSSHKeyDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client).sshKeyService
id, err := strconv.Atoi(d.Id())
if err != nil {
return fmt.Errorf("Error deleting SSH Key: %s", err)
}
log.Printf("[INFO] Deleting SSH key: %d", id)
_, err = client.DeleteObject(id)
if err != nil {
return fmt.Errorf("Error deleting SSH key: %s", err)
}
d.SetId("")
return nil
}
func resourceSoftLayerSSHKeyExists(d *schema.ResourceData, meta interface{}) (bool, error) {
client := meta.(*Client).sshKeyService
if client == nil {
return false, fmt.Errorf("The client was nil.")
}
keyId, err := strconv.Atoi(d.Id())
if err != nil {
return false, fmt.Errorf("Not a valid ID, must be an integer: %s", err)
}
result, err := client.GetObject(keyId)
return result.Id == keyId && err == nil, nil
}