mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-07 14:44:11 -06:00
8921e10d71
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
132 lines
3.8 KiB
Go
132 lines
3.8 KiB
Go
package softlayer
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
datatypes "github.com/maximilien/softlayer-go/data_types"
|
|
)
|
|
|
|
func TestAccSoftLayerSSHKey_Basic(t *testing.T) {
|
|
var key datatypes.SoftLayer_Security_Ssh_Key
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckSoftLayerSSHKeyDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccCheckSoftLayerSSHKeyConfig_basic,
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckSoftLayerSSHKeyExists("softlayer_ssh_key.testacc_foobar", &key),
|
|
testAccCheckSoftLayerSSHKeyAttributes(&key),
|
|
resource.TestCheckResourceAttr(
|
|
"softlayer_ssh_key.testacc_foobar", "name", "testacc_foobar"),
|
|
resource.TestCheckResourceAttr(
|
|
"softlayer_ssh_key.testacc_foobar", "public_key", testAccValidPublicKey),
|
|
resource.TestCheckResourceAttr(
|
|
"softlayer_ssh_key.testacc_foobar", "notes", "first_note"),
|
|
),
|
|
},
|
|
|
|
resource.TestStep{
|
|
Config: testAccCheckSoftLayerSSHKeyConfig_updated,
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckSoftLayerSSHKeyExists("softlayer_ssh_key.testacc_foobar", &key),
|
|
resource.TestCheckResourceAttr(
|
|
"softlayer_ssh_key.testacc_foobar", "name", "changed_name"),
|
|
resource.TestCheckResourceAttr(
|
|
"softlayer_ssh_key.testacc_foobar", "public_key", testAccValidPublicKey),
|
|
resource.TestCheckResourceAttr(
|
|
"softlayer_ssh_key.testacc_foobar", "notes", "changed_note"),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func testAccCheckSoftLayerSSHKeyDestroy(s *terraform.State) error {
|
|
client := testAccProvider.Meta().(*Client).sshKeyService
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
if rs.Type != "softlayer_ssh_key" {
|
|
continue
|
|
}
|
|
|
|
keyId, _ := strconv.Atoi(rs.Primary.ID)
|
|
|
|
// Try to find the key
|
|
_, err := client.GetObject(keyId)
|
|
|
|
if err == nil {
|
|
return fmt.Errorf("SSH key still exists")
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func testAccCheckSoftLayerSSHKeyAttributes(key *datatypes.SoftLayer_Security_Ssh_Key) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
|
|
if key.Label != "testacc_foobar" {
|
|
return fmt.Errorf("Bad name: %s", key.Label)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func testAccCheckSoftLayerSSHKeyExists(n string, key *datatypes.SoftLayer_Security_Ssh_Key) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
return fmt.Errorf("Not found: %s", n)
|
|
}
|
|
|
|
if rs.Primary.ID == "" {
|
|
return fmt.Errorf("No Record ID is set")
|
|
}
|
|
|
|
keyId, _ := strconv.Atoi(rs.Primary.ID)
|
|
|
|
client := testAccProvider.Meta().(*Client).sshKeyService
|
|
foundKey, err := client.GetObject(keyId)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if strconv.Itoa(int(foundKey.Id)) != rs.Primary.ID {
|
|
return fmt.Errorf("Record not found")
|
|
}
|
|
|
|
*key = foundKey
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
var testAccCheckSoftLayerSSHKeyConfig_basic = fmt.Sprintf(`
|
|
resource "softlayer_ssh_key" "testacc_foobar" {
|
|
name = "testacc_foobar"
|
|
notes = "first_note"
|
|
public_key = "%s"
|
|
}`, testAccValidPublicKey)
|
|
|
|
var testAccCheckSoftLayerSSHKeyConfig_updated = fmt.Sprintf(`
|
|
resource "softlayer_ssh_key" "testacc_foobar" {
|
|
name = "changed_name"
|
|
notes = "changed_note"
|
|
public_key = "%s"
|
|
}`, testAccValidPublicKey)
|
|
|
|
var testAccValidPublicKey = strings.TrimSpace(`
|
|
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVERRN7/9484SOBJ3HSKxxNG5JN8owAjy5f9yYwcUg+JaUVuytn5Pv3aeYROHGGg+5G346xaq3DAwX6Y5ykr2fvjObgncQBnuU5KHWCECO/4h8uWuwh/kfniXPVjFToc+gnkqA+3RKpAecZhFXwfalQ9mMuYGFxn+fwn8cYEApsJbsEmb0iJwPiZ5hjFC8wREuiTlhPHDgkBLOiycd20op2nXzDbHfCHInquEe/gYxEitALONxm0swBOwJZwlTDOB7C6y2dzlrtxr1L59m7pCkWI4EtTRLvleehBoj3u7jB4usR
|
|
`)
|