2016-02-10 21:30:32 +01:00
|
|
|
package triton
|
|
|
|
|
|
|
|
import (
|
2017-05-10 23:54:57 +02:00
|
|
|
"context"
|
2016-02-10 21:30:32 +01:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2017-03-31 00:25:27 +02:00
|
|
|
"github.com/joyent/triton-go"
|
2016-02-10 21:30:32 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccTritonKey_basic(t *testing.T) {
|
|
|
|
keyName := fmt.Sprintf("acctest-%d", acctest.RandInt())
|
2017-03-31 00:25:27 +02:00
|
|
|
publicKeyMaterial, _, err := acctest.RandSSHKeyPair("TestAccTritonKey_basic@terraform")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot generate test SSH key pair: %s", err)
|
|
|
|
}
|
|
|
|
config := testAccTritonKey_basic(keyName, publicKeyMaterial)
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckTritonKeyDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: config,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckTritonKeyExists("triton_key.test"),
|
|
|
|
resource.TestCheckResourceAttr("triton_key.test", "name", keyName),
|
|
|
|
resource.TestCheckResourceAttr("triton_key.test", "key", publicKeyMaterial),
|
|
|
|
func(*terraform.State) error {
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccTritonKey_noKeyName(t *testing.T) {
|
|
|
|
keyComment := fmt.Sprintf("acctest_%d@terraform", acctest.RandInt())
|
|
|
|
keyMaterial, _, err := acctest.RandSSHKeyPair(keyComment)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot generate test SSH key pair: %s", err)
|
|
|
|
}
|
|
|
|
config := testAccTritonKey_noKeyName(keyMaterial)
|
2016-02-10 21:30:32 +01:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testCheckTritonKeyDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-03-31 00:25:27 +02:00
|
|
|
{
|
2016-02-10 21:30:32 +01:00
|
|
|
Config: config,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testCheckTritonKeyExists("triton_key.test"),
|
2017-03-31 00:25:27 +02:00
|
|
|
resource.TestCheckResourceAttr("triton_key.test", "name", keyComment),
|
|
|
|
resource.TestCheckResourceAttr("triton_key.test", "key", keyMaterial),
|
2016-02-10 21:30:32 +01:00
|
|
|
func(*terraform.State) error {
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCheckTritonKeyExists(name string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
// Ensure we have enough information in state to look up in API
|
|
|
|
rs, ok := s.RootModule().Resources[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", name)
|
|
|
|
}
|
2017-03-31 00:25:27 +02:00
|
|
|
conn := testAccProvider.Meta().(*triton.Client)
|
2016-02-10 21:30:32 +01:00
|
|
|
|
2017-05-10 23:54:57 +02:00
|
|
|
key, err := conn.Keys().GetKey(context.Background(), &triton.GetKeyInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
KeyName: rs.Primary.ID,
|
|
|
|
})
|
2016-02-10 21:30:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Bad: Check Key Exists: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
if key == nil {
|
2016-02-10 21:30:32 +01:00
|
|
|
return fmt.Errorf("Bad: Key %q does not exist", rs.Primary.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCheckTritonKeyDestroy(s *terraform.State) error {
|
2017-03-31 00:25:27 +02:00
|
|
|
conn := testAccProvider.Meta().(*triton.Client)
|
2016-02-10 21:30:32 +01:00
|
|
|
|
|
|
|
return resource.Retry(1*time.Minute, func() *resource.RetryError {
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "triton_key" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-05-10 23:54:57 +02:00
|
|
|
key, err := conn.Keys().GetKey(context.Background(), &triton.GetKeyInput{
|
2017-03-31 00:25:27 +02:00
|
|
|
KeyName: rs.Primary.ID,
|
|
|
|
})
|
2016-02-10 21:30:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
if key != nil {
|
2016-02-10 21:30:32 +01:00
|
|
|
return resource.RetryableError(fmt.Errorf("Bad: Key %q still exists", rs.Primary.ID))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
var testAccTritonKey_basic = func(keyName string, keyMaterial string) string {
|
|
|
|
return fmt.Sprintf(`resource "triton_key" "test" {
|
|
|
|
name = "%s"
|
|
|
|
key = "%s"
|
|
|
|
}
|
|
|
|
`, keyName, keyMaterial)
|
2016-02-10 21:30:32 +01:00
|
|
|
}
|
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
var testAccTritonKey_noKeyName = func(keyMaterial string) string {
|
|
|
|
return fmt.Sprintf(`resource "triton_key" "test" {
|
|
|
|
key = "%s"
|
|
|
|
}
|
|
|
|
`, keyMaterial)
|
|
|
|
}
|