github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/packet/resource_packet_ssh_key_test.go (about) 1 package packet 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 "github.com/packethost/packngo" 11 ) 12 13 func TestAccPacketSSHKey_Basic(t *testing.T) { 14 var key packngo.SSHKey 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckPacketSSHKeyDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccCheckPacketSSHKeyConfig_basic, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckPacketSSHKeyExists("packet_ssh_key.foobar", &key), 25 testAccCheckPacketSSHKeyAttributes(&key), 26 resource.TestCheckResourceAttr( 27 "packet_ssh_key.foobar", "name", "foobar"), 28 resource.TestCheckResourceAttr( 29 "packet_ssh_key.foobar", "public_key", testAccValidPublicKey), 30 ), 31 }, 32 }, 33 }) 34 } 35 36 func testAccCheckPacketSSHKeyDestroy(s *terraform.State) error { 37 client := testAccProvider.Meta().(*packngo.Client) 38 39 for _, rs := range s.RootModule().Resources { 40 if rs.Type != "packet_ssh_key" { 41 continue 42 } 43 44 _, _, err := client.SSHKeys.Get(rs.Primary.ID) 45 46 if err == nil { 47 fmt.Errorf("SSH key still exists") 48 } 49 } 50 51 return nil 52 } 53 54 func testAccCheckPacketSSHKeyAttributes(key *packngo.SSHKey) resource.TestCheckFunc { 55 return func(s *terraform.State) error { 56 57 if key.Label != "foobar" { 58 return fmt.Errorf("Bad name: %s", key.Label) 59 } 60 61 return nil 62 } 63 } 64 65 func testAccCheckPacketSSHKeyExists(n string, key *packngo.SSHKey) resource.TestCheckFunc { 66 return func(s *terraform.State) error { 67 rs, ok := s.RootModule().Resources[n] 68 69 if !ok { 70 return fmt.Errorf("Not found: %s", n) 71 } 72 73 if rs.Primary.ID == "" { 74 return fmt.Errorf("No Record ID is set") 75 } 76 77 client := testAccProvider.Meta().(*packngo.Client) 78 79 foundKey, _, err := client.SSHKeys.Get(rs.Primary.ID) 80 81 if err != nil { 82 return err 83 } 84 85 if foundKey.ID != rs.Primary.ID { 86 return fmt.Errorf("SSh Key not found: %v - %v", rs.Primary.ID, foundKey) 87 } 88 89 *key = *foundKey 90 91 fmt.Printf("key: %v", key) 92 return nil 93 } 94 } 95 96 var testAccCheckPacketSSHKeyConfig_basic = fmt.Sprintf(` 97 resource "packet_ssh_key" "foobar" { 98 name = "foobar" 99 public_key = "%s" 100 }`, testAccValidPublicKey) 101 102 var testAccValidPublicKey = strings.TrimSpace(` 103 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVERRN7/9484SOBJ3HSKxxNG5JN8owAjy5f9yYwcUg+JaUVuytn5Pv3aeYROHGGg+5G346xaq3DAwX6Y5ykr2fvjObgncQBnuU5KHWCECO/4h8uWuwh/kfniXPVjFToc+gnkqA+3RKpAecZhFXwfalQ9mMuYGFxn+fwn8cYEApsJbsEmb0iJwPiZ5hjFC8wREuiTlhPHDgkBLOiycd20op2nXzDbHfCHInquEe/gYxEitALONxm0swBOwJZwlTDOB7C6y2dzlrtxr1L59m7pCkWI4EtTRLvleehBoj3u7jB4usR 104 `)