github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/openstack/resource_openstack_compute_keypair_v2_test.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 10 "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs" 11 ) 12 13 func TestAccComputeV2Keypair_basic(t *testing.T) { 14 var keypair keypairs.KeyPair 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckComputeV2KeypairDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccComputeV2Keypair_basic, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckComputeV2KeypairExists(t, "openstack_compute_keypair_v2.foo", &keypair), 25 ), 26 }, 27 }, 28 }) 29 } 30 31 func testAccCheckComputeV2KeypairDestroy(s *terraform.State) error { 32 config := testAccProvider.Meta().(*Config) 33 computeClient, err := config.computeV2Client(OS_REGION_NAME) 34 if err != nil { 35 return fmt.Errorf("(testAccCheckComputeV2KeypairDestroy) Error creating OpenStack compute client: %s", err) 36 } 37 38 for _, rs := range s.RootModule().Resources { 39 if rs.Type != "openstack_compute_keypair_v2" { 40 continue 41 } 42 43 _, err := keypairs.Get(computeClient, rs.Primary.ID).Extract() 44 if err == nil { 45 return fmt.Errorf("Keypair still exists") 46 } 47 } 48 49 return nil 50 } 51 52 func testAccCheckComputeV2KeypairExists(t *testing.T, n string, kp *keypairs.KeyPair) resource.TestCheckFunc { 53 return func(s *terraform.State) error { 54 rs, ok := s.RootModule().Resources[n] 55 if !ok { 56 return fmt.Errorf("Not found: %s", n) 57 } 58 59 if rs.Primary.ID == "" { 60 return fmt.Errorf("No ID is set") 61 } 62 63 config := testAccProvider.Meta().(*Config) 64 computeClient, err := config.computeV2Client(OS_REGION_NAME) 65 if err != nil { 66 return fmt.Errorf("(testAccCheckComputeV2KeypairExists) Error creating OpenStack compute client: %s", err) 67 } 68 69 found, err := keypairs.Get(computeClient, rs.Primary.ID).Extract() 70 if err != nil { 71 return err 72 } 73 74 if found.Name != rs.Primary.ID { 75 return fmt.Errorf("Keypair not found") 76 } 77 78 *kp = *found 79 80 return nil 81 } 82 } 83 84 var testAccComputeV2Keypair_basic = fmt.Sprintf(` 85 resource "openstack_compute_keypair_v2" "foo" { 86 region = "%s" 87 name = "test-keypair-tf" 88 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAjpC1hwiOCCmKEWxJ4qzTTsJbKzndLo1BCz5PcwtUnflmU+gHJtWMZKpuEGVi29h0A/+ydKek1O18k10Ff+4tyFjiHDQAT9+OfgWf7+b1yK+qDip3X1C0UPMbwHlTfSGWLGZquwhvEFx9k3h/M+VtMvwR1lJ9LUyTAImnNjWG7TAIPmui30HvM2UiFEmqkr4ijq45MyX2+fLIePLRIFuu1p4whjHAQYufqyno3BS48icQb4p6iVEZPo4AE2o9oIyQvj2mx4dk5Y8CgSETOZTYDOR3rU2fZTRDRgPJDH9FWvQjF5tA0p3d9CoWWd2s6GKKbfoUIi8R/Db1BSPJwkqB jrp-hp-pc" 89 }`, 90 OS_REGION_NAME)