2015-05-07 16:10:57 +02:00
|
|
|
package cloudstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/xanzy/go-cloudstack/cloudstack"
|
|
|
|
)
|
|
|
|
|
2015-06-08 01:18:14 +02:00
|
|
|
func TestAccCloudStackSSHKeyPair_basic(t *testing.T) {
|
2015-05-07 16:10:57 +02:00
|
|
|
var sshkey cloudstack.SSHKeyPair
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckCloudStackSSHKeyPairDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCloudStackSSHKeyPair_create,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudStackSSHKeyPairExists("cloudstack_ssh_keypair.foo", &sshkey),
|
|
|
|
testAccCheckCloudStackSSHKeyPairAttributes(&sshkey),
|
2015-06-09 12:38:05 +02:00
|
|
|
testAccCheckCloudStackSSHKeyPairCreateAttributes("terraform-test-keypair"),
|
2015-05-07 16:10:57 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccCloudStackSSHKeyPair_register(t *testing.T) {
|
|
|
|
var sshkey cloudstack.SSHKeyPair
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckCloudStackSSHKeyPairDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCloudStackSSHKeyPair_register,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudStackSSHKeyPairExists("cloudstack_ssh_keypair.foo", &sshkey),
|
|
|
|
testAccCheckCloudStackSSHKeyPairAttributes(&sshkey),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"cloudstack_ssh_keypair.foo",
|
|
|
|
"public_key",
|
|
|
|
CLOUDSTACK_SSH_PUBLIC_KEY),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckCloudStackSSHKeyPairExists(n string, sshkey *cloudstack.SSHKeyPair) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2015-06-09 12:38:05 +02:00
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No key pair ID is set")
|
2015-05-07 16:10:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
|
|
|
|
p := cs.SSH.NewListSSHKeyPairsParams()
|
2015-06-09 12:38:05 +02:00
|
|
|
p.SetName(rs.Primary.ID)
|
2015-05-07 16:10:57 +02:00
|
|
|
|
2015-06-09 12:38:05 +02:00
|
|
|
list, err := cs.SSH.ListSSHKeyPairs(p)
|
2015-05-07 16:10:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-09 12:38:05 +02:00
|
|
|
if list.Count != 1 || list.SSHKeyPairs[0].Name != rs.Primary.ID {
|
|
|
|
return fmt.Errorf("Key pair not found")
|
2015-05-07 16:10:57 +02:00
|
|
|
}
|
|
|
|
|
2015-06-09 12:38:05 +02:00
|
|
|
*sshkey = *list.SSHKeyPairs[0]
|
|
|
|
|
|
|
|
return nil
|
2015-05-07 16:10:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckCloudStackSSHKeyPairAttributes(
|
2015-06-09 12:38:05 +02:00
|
|
|
keypair *cloudstack.SSHKeyPair) resource.TestCheckFunc {
|
2015-05-07 16:10:57 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
2015-06-09 12:38:05 +02:00
|
|
|
fpLen := len(keypair.Fingerprint)
|
|
|
|
if fpLen != 47 {
|
|
|
|
return fmt.Errorf("SSH key: Attribute private_key expected length 47, got %d", fpLen)
|
2015-05-07 16:10:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-09 12:38:05 +02:00
|
|
|
func testAccCheckCloudStackSSHKeyPairCreateAttributes(name string) resource.TestCheckFunc {
|
2015-05-07 16:10:57 +02:00
|
|
|
return func(s *terraform.State) error {
|
2015-06-09 12:38:05 +02:00
|
|
|
found := false
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "cloudstack_ssh_keypair" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID != name {
|
|
|
|
continue
|
|
|
|
}
|
2015-05-07 16:10:57 +02:00
|
|
|
|
2015-06-09 12:38:05 +02:00
|
|
|
if !strings.Contains(rs.Primary.Attributes["private_key"], "PRIVATE KEY") {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"SSH key: Attribute private_key expected 'PRIVATE KEY' to be present, got %s",
|
|
|
|
rs.Primary.Attributes["private_key"])
|
|
|
|
}
|
|
|
|
|
|
|
|
found = true
|
|
|
|
break
|
2015-05-07 16:10:57 +02:00
|
|
|
}
|
|
|
|
|
2015-06-09 12:38:05 +02:00
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("Could not find key pair %s", name)
|
2015-05-07 16:10:57 +02:00
|
|
|
}
|
2015-06-09 12:38:05 +02:00
|
|
|
|
2015-05-07 16:10:57 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckCloudStackSSHKeyPairDestroy(s *terraform.State) error {
|
|
|
|
cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "cloudstack_ssh_keypair" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-06-09 12:38:05 +02:00
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No key pair ID is set")
|
2015-05-07 16:10:57 +02:00
|
|
|
}
|
|
|
|
|
2015-06-09 12:38:05 +02:00
|
|
|
p := cs.SSH.NewListSSHKeyPairsParams()
|
|
|
|
p.SetName(rs.Primary.ID)
|
2015-05-07 16:10:57 +02:00
|
|
|
|
2015-06-09 12:38:05 +02:00
|
|
|
list, err := cs.SSH.ListSSHKeyPairs(p)
|
2015-05-07 16:10:57 +02:00
|
|
|
if err != nil {
|
2015-06-09 12:38:05 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-11 17:14:19 +02:00
|
|
|
for _, keyPair := range list.SSHKeyPairs {
|
|
|
|
if keyPair.Name == rs.Primary.ID {
|
|
|
|
return fmt.Errorf("Key pair %s still exists", rs.Primary.ID)
|
|
|
|
}
|
2015-05-07 16:10:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAccCloudStackSSHKeyPair_create = fmt.Sprintf(`
|
|
|
|
resource "cloudstack_ssh_keypair" "foo" {
|
2015-06-09 12:38:05 +02:00
|
|
|
name = "terraform-test-keypair"
|
2015-05-07 16:10:57 +02:00
|
|
|
}`)
|
|
|
|
|
|
|
|
var testAccCloudStackSSHKeyPair_register = fmt.Sprintf(`
|
|
|
|
resource "cloudstack_ssh_keypair" "foo" {
|
2015-06-09 12:38:05 +02:00
|
|
|
name = "terraform-test-keypair"
|
2015-05-07 16:10:57 +02:00
|
|
|
public_key = "%s"
|
|
|
|
}`, CLOUDSTACK_SSH_PUBLIC_KEY)
|