github.com/ggiamarchi/terraform@v0.3.7-0.20150607194748-ed2a66a46a71/builtin/providers/cloudstack/resource_cloudstack_ssh_keypair_test.go (about) 1 package cloudstack 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 "testing" 8 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 "github.com/xanzy/go-cloudstack/cloudstack" 12 ) 13 14 func TestAccCloudStackSSHKeyPair_create(t *testing.T) { 15 var sshkey cloudstack.SSHKeyPair 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckCloudStackSSHKeyPairDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccCloudStackSSHKeyPair_create, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckCloudStackSSHKeyPairExists("cloudstack_ssh_keypair.foo", &sshkey), 26 testAccCheckCloudStackSSHKeyPairAttributes(&sshkey), 27 testAccCheckCloudStackSSHKeyPairCreateAttributes("cloudstack_ssh_keypair.foo"), 28 ), 29 }, 30 }, 31 }) 32 } 33 34 func TestAccCloudStackSSHKeyPair_register(t *testing.T) { 35 var sshkey cloudstack.SSHKeyPair 36 37 resource.Test(t, resource.TestCase{ 38 PreCheck: func() { testAccPreCheck(t) }, 39 Providers: testAccProviders, 40 CheckDestroy: testAccCheckCloudStackSSHKeyPairDestroy, 41 Steps: []resource.TestStep{ 42 resource.TestStep{ 43 Config: testAccCloudStackSSHKeyPair_register, 44 Check: resource.ComposeTestCheckFunc( 45 testAccCheckCloudStackSSHKeyPairExists("cloudstack_ssh_keypair.foo", &sshkey), 46 testAccCheckCloudStackSSHKeyPairAttributes(&sshkey), 47 resource.TestCheckResourceAttr( 48 "cloudstack_ssh_keypair.foo", 49 "public_key", 50 CLOUDSTACK_SSH_PUBLIC_KEY), 51 ), 52 }, 53 }, 54 }) 55 } 56 57 func testAccCheckCloudStackSSHKeyPairExists(n string, sshkey *cloudstack.SSHKeyPair) resource.TestCheckFunc { 58 return func(s *terraform.State) error { 59 rs, ok := s.RootModule().Resources[n] 60 if !ok { 61 return fmt.Errorf("Not found: %s", n) 62 } 63 64 if rs.Primary.Attributes["name"] == "" { 65 return fmt.Errorf("No ssh key name is set") 66 } 67 68 cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) 69 p := cs.SSH.NewListSSHKeyPairsParams() 70 p.SetName(rs.Primary.Attributes["name"]) 71 list, err := cs.SSH.ListSSHKeyPairs(p) 72 73 if err != nil { 74 return err 75 } 76 77 if list.Count == 1 && list.SSHKeyPairs[0].Name == rs.Primary.Attributes["name"] { 78 //ssh key exists 79 *sshkey = *list.SSHKeyPairs[0] 80 return nil 81 } 82 83 return fmt.Errorf("SSH key not found") 84 } 85 } 86 87 func testAccCheckCloudStackSSHKeyPairAttributes( 88 sshkey *cloudstack.SSHKeyPair) resource.TestCheckFunc { 89 return func(s *terraform.State) error { 90 91 fingerprintLen := len(sshkey.Fingerprint) 92 if fingerprintLen != 47 { 93 return fmt.Errorf( 94 "SSH key: Attribute private_key expected length 47, got %d", 95 fingerprintLen) 96 } 97 98 return nil 99 } 100 } 101 102 func testAccCheckCloudStackSSHKeyPairCreateAttributes( 103 name string) resource.TestCheckFunc { 104 return func(s *terraform.State) error { 105 ms := s.RootModule() 106 rs, ok := ms.Resources[name] 107 if !ok { 108 return fmt.Errorf("Not found: %s", name) 109 } 110 111 is := rs.Primary 112 if is == nil { 113 return fmt.Errorf("No primary instance: %s", name) 114 } 115 116 log.Printf("Private key calculated: %s", is.Attributes["private_key"]) 117 if !strings.Contains(is.Attributes["private_key"], "PRIVATE KEY") { 118 return fmt.Errorf( 119 "SSH key: Attribute private_key expected 'PRIVATE KEY' to be present, got %s", 120 is.Attributes["private_key"]) 121 } 122 return nil 123 } 124 } 125 126 func testAccCheckCloudStackSSHKeyPairDestroy(s *terraform.State) error { 127 cs := testAccProvider.Meta().(*cloudstack.CloudStackClient) 128 129 for _, rs := range s.RootModule().Resources { 130 if rs.Type != "cloudstack_ssh_keypair" { 131 continue 132 } 133 134 if rs.Primary.Attributes["name"] == "" { 135 return fmt.Errorf("No ssh key name is set") 136 } 137 138 p := cs.SSH.NewDeleteSSHKeyPairParams(rs.Primary.Attributes["name"]) 139 _, err := cs.SSH.DeleteSSHKeyPair(p) 140 141 if err != nil { 142 return fmt.Errorf( 143 "Error deleting ssh key (%s): %s", 144 rs.Primary.Attributes["name"], err) 145 } 146 } 147 148 return nil 149 } 150 151 var testAccCloudStackSSHKeyPair_create = fmt.Sprintf(` 152 resource "cloudstack_ssh_keypair" "foo" { 153 name = "terraform-testacc" 154 }`) 155 156 var testAccCloudStackSSHKeyPair_register = fmt.Sprintf(` 157 resource "cloudstack_ssh_keypair" "foo" { 158 name = "terraform-testacc" 159 public_key = "%s" 160 }`, CLOUDSTACK_SSH_PUBLIC_KEY)