2015-02-25 12:38:23 +01:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-04-02 16:04:59 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-02-25 12:38:23 +01:00
|
|
|
"testing"
|
|
|
|
|
2015-09-27 06:06:51 +02:00
|
|
|
"github.com/digitalocean/godo"
|
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
|
2015-02-25 12:38:23 +01:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckDigitalOceanSSHKeyDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckDigitalOceanSSHKeyConfig_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckDigitalOceanSSHKeyExists("digitalocean_ssh_key.foobar", &key),
|
|
|
|
testAccCheckDigitalOceanSSHKeyAttributes(&key),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"digitalocean_ssh_key.foobar", "name", "foobar"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2015-04-02 16:04:59 +02:00
|
|
|
"digitalocean_ssh_key.foobar", "public_key", testAccValidPublicKey),
|
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
|
2015-09-27 06:06:51 +02:00
|
|
|
_, _, err = client.Keys.GetByID(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 testAccCheckDigitalOceanSSHKeyAttributes(key *godo.Key) resource.TestCheckFunc {
|
2015-02-25 12:38:23 +01:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
|
|
|
if key.Name != "foobar" {
|
|
|
|
return fmt.Errorf("Bad name: %s", key.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
foundKey, _, err := client.Keys.GetByID(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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 16:04:59 +02:00
|
|
|
var testAccCheckDigitalOceanSSHKeyConfig_basic = fmt.Sprintf(`
|
2015-02-25 12:38:23 +01:00
|
|
|
resource "digitalocean_ssh_key" "foobar" {
|
|
|
|
name = "foobar"
|
2015-04-02 16:04:59 +02:00
|
|
|
public_key = "%s"
|
|
|
|
}`, testAccValidPublicKey)
|
|
|
|
|
|
|
|
var testAccValidPublicKey = strings.TrimSpace(`
|
|
|
|
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVERRN7/9484SOBJ3HSKxxNG5JN8owAjy5f9yYwcUg+JaUVuytn5Pv3aeYROHGGg+5G346xaq3DAwX6Y5ykr2fvjObgncQBnuU5KHWCECO/4h8uWuwh/kfniXPVjFToc+gnkqA+3RKpAecZhFXwfalQ9mMuYGFxn+fwn8cYEApsJbsEmb0iJwPiZ5hjFC8wREuiTlhPHDgkBLOiycd20op2nXzDbHfCHInquEe/gYxEitALONxm0swBOwJZwlTDOB7C6y2dzlrtxr1L59m7pCkWI4EtTRLvleehBoj3u7jB4usR
|
|
|
|
`)
|