2015-02-25 12:38:23 +01:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2017-05-15 15:54:16 +02:00
|
|
|
"context"
|
2015-02-25 12:38:23 +01:00
|
|
|
"fmt"
|
2015-04-02 16:04:59 +02:00
|
|
|
"strconv"
|
2015-02-25 12:38:23 +01:00
|
|
|
"testing"
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
"github.com/digitalocean/godo"
|
2017-03-27 18:59:09 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-02-25 12:38:23 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccDigitalOceanSSHKey_Basic(t *testing.T) {
|
2015-09-27 06:06:51 +02:00
|
|
|
var key godo.Key
|
2017-03-27 18:59:09 +02:00
|
|
|
rInt := acctest.RandInt()
|
|
|
|
publicKeyMaterial, _, err := acctest.RandSSHKeyPair("digitalocean@ssh-acceptance-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot generate test SSH key pair: %s", err)
|
|
|
|
}
|
2015-02-25 12:38:23 +01:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckDigitalOceanSSHKeyDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-01-20 18:47:54 +01:00
|
|
|
{
|
2017-03-27 18:59:09 +02:00
|
|
|
Config: testAccCheckDigitalOceanSSHKeyConfig_basic(rInt, publicKeyMaterial),
|
2015-02-25 12:38:23 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckDigitalOceanSSHKeyExists("digitalocean_ssh_key.foobar", &key),
|
|
|
|
resource.TestCheckResourceAttr(
|
2017-03-27 18:59:09 +02:00
|
|
|
"digitalocean_ssh_key.foobar", "name", fmt.Sprintf("foobar-%d", rInt)),
|
2015-02-25 12:38:23 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2017-03-27 18:59:09 +02:00
|
|
|
"digitalocean_ssh_key.foobar", "public_key", publicKeyMaterial),
|
2015-02-25 12:38:23 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckDigitalOceanSSHKeyDestroy(s *terraform.State) error {
|
2015-09-27 06:06:51 +02:00
|
|
|
client := testAccProvider.Meta().(*godo.Client)
|
2015-02-25 12:38:23 +01:00
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "digitalocean_ssh_key" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
id, err := strconv.Atoi(rs.Primary.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-25 12:38:23 +01:00
|
|
|
// Try to find the key
|
2017-05-15 15:54:16 +02:00
|
|
|
_, _, err = client.Keys.GetByID(context.Background(), id)
|
2015-02-25 12:38:23 +01:00
|
|
|
|
|
|
|
if err == nil {
|
2016-02-13 07:39:23 +01:00
|
|
|
return fmt.Errorf("SSH key still exists")
|
2015-02-25 12:38:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
func testAccCheckDigitalOceanSSHKeyExists(n string, key *godo.Key) resource.TestCheckFunc {
|
2015-02-25 12:38:23 +01:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
client := testAccProvider.Meta().(*godo.Client)
|
2015-02-25 12:38:23 +01:00
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
id, err := strconv.Atoi(rs.Primary.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the key
|
2017-05-15 15:54:16 +02:00
|
|
|
foundKey, _, err := client.Keys.GetByID(context.Background(), id)
|
2015-02-25 12:38:23 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
if strconv.Itoa(foundKey.ID) != rs.Primary.ID {
|
2015-02-25 12:38:23 +01:00
|
|
|
return fmt.Errorf("Record not found")
|
|
|
|
}
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
*key = *foundKey
|
2015-02-25 12:38:23 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 18:59:09 +02:00
|
|
|
func testAccCheckDigitalOceanSSHKeyConfig_basic(rInt int, key string) string {
|
2017-02-10 22:41:35 +01:00
|
|
|
return fmt.Sprintf(`
|
2015-02-25 12:38:23 +01:00
|
|
|
resource "digitalocean_ssh_key" "foobar" {
|
2017-03-27 18:59:09 +02:00
|
|
|
name = "foobar-%d"
|
2015-04-02 16:04:59 +02:00
|
|
|
public_key = "%s"
|
2017-03-27 18:59:09 +02:00
|
|
|
}`, rInt, key)
|
2017-02-10 22:41:35 +01:00
|
|
|
}
|