github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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/gophercloud/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("openstack_compute_keypair_v2.kp_1", &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("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(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("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 const testAccComputeV2Keypair_basic = ` 85 resource "openstack_compute_keypair_v2" "kp_1" { 86 name = "kp_1" 87 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAjpC1hwiOCCmKEWxJ4qzTTsJbKzndLo1BCz5PcwtUnflmU+gHJtWMZKpuEGVi29h0A/+ydKek1O18k10Ff+4tyFjiHDQAT9+OfgWf7+b1yK+qDip3X1C0UPMbwHlTfSGWLGZquwhvEFx9k3h/M+VtMvwR1lJ9LUyTAImnNjWG7TAIPmui30HvM2UiFEmqkr4ijq45MyX2+fLIePLRIFuu1p4whjHAQYufqyno3BS48icQb4p6iVEZPo4AE2o9oIyQvj2mx4dk5Y8CgSETOZTYDOR3rU2fZTRDRgPJDH9FWvQjF5tA0p3d9CoWWd2s6GKKbfoUIi8R/Db1BSPJwkqB jrp-hp-pc" 88 } 89 `