github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/digitalocean/resource_digitalocean_ssh_key_test.go (about) 1 package digitalocean 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 "testing" 8 9 "github.com/digitalocean/godo" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccDigitalOceanSSHKey_Basic(t *testing.T) { 15 var key godo.Key 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckDigitalOceanSSHKeyDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccCheckDigitalOceanSSHKeyConfig_basic, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckDigitalOceanSSHKeyExists("digitalocean_ssh_key.foobar", &key), 26 testAccCheckDigitalOceanSSHKeyAttributes(&key), 27 resource.TestCheckResourceAttr( 28 "digitalocean_ssh_key.foobar", "name", "foobar"), 29 resource.TestCheckResourceAttr( 30 "digitalocean_ssh_key.foobar", "public_key", strings.TrimSpace(testAccValidPublicKey)), 31 ), 32 }, 33 }, 34 }) 35 } 36 37 func testAccCheckDigitalOceanSSHKeyDestroy(s *terraform.State) error { 38 client := testAccProvider.Meta().(*godo.Client) 39 40 for _, rs := range s.RootModule().Resources { 41 if rs.Type != "digitalocean_ssh_key" { 42 continue 43 } 44 45 id, err := strconv.Atoi(rs.Primary.ID) 46 if err != nil { 47 return err 48 } 49 50 // Try to find the key 51 _, _, err = client.Keys.GetByID(id) 52 53 if err == nil { 54 return fmt.Errorf("SSH key still exists") 55 } 56 } 57 58 return nil 59 } 60 61 func testAccCheckDigitalOceanSSHKeyAttributes(key *godo.Key) resource.TestCheckFunc { 62 return func(s *terraform.State) error { 63 64 if key.Name != "foobar" { 65 return fmt.Errorf("Bad name: %s", key.Name) 66 } 67 68 return nil 69 } 70 } 71 72 func testAccCheckDigitalOceanSSHKeyExists(n string, key *godo.Key) resource.TestCheckFunc { 73 return func(s *terraform.State) error { 74 rs, ok := s.RootModule().Resources[n] 75 76 if !ok { 77 return fmt.Errorf("Not found: %s", n) 78 } 79 80 if rs.Primary.ID == "" { 81 return fmt.Errorf("No Record ID is set") 82 } 83 84 client := testAccProvider.Meta().(*godo.Client) 85 86 id, err := strconv.Atoi(rs.Primary.ID) 87 if err != nil { 88 return err 89 } 90 91 // Try to find the key 92 foundKey, _, err := client.Keys.GetByID(id) 93 94 if err != nil { 95 return err 96 } 97 98 if strconv.Itoa(foundKey.ID) != rs.Primary.ID { 99 return fmt.Errorf("Record not found") 100 } 101 102 *key = *foundKey 103 104 return nil 105 } 106 } 107 108 var testAccCheckDigitalOceanSSHKeyConfig_basic = fmt.Sprintf(` 109 resource "digitalocean_ssh_key" "foobar" { 110 name = "foobar" 111 public_key = "%s" 112 }`, testAccValidPublicKey) 113 114 var testAccValidPublicKey = `ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVERRN7/9484SOBJ3HSKxxNG5JN8owAjy5f9yYwcUg+JaUVuytn5Pv3aeYROHGGg+5G346xaq3DAwX6Y5ykr2fvjObgncQBnuU5KHWCECO/4h8uWuwh/kfniXPVjFToc+gnkqA+3RKpAecZhFXwfalQ9mMuYGFxn+fwn8cYEApsJbsEmb0iJwPiZ5hjFC8wREuiTlhPHDgkBLOiycd20op2nXzDbHfCHInquEe/gYxEitALONxm0swBOwJZwlTDOB7C6y2dzlrtxr1L59m7pCkWI4EtTRLvleehBoj3u7jB4usR 115 `