github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/openstack/resource_openstack_compute_floatingip_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/floatingips" 11 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" 12 ) 13 14 func TestAccComputeV2FloatingIP_basic(t *testing.T) { 15 var fip floatingips.FloatingIP 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckComputeV2FloatingIPDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccComputeV2FloatingIP_basic, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckComputeV2FloatingIPExists("openstack_compute_floatingip_v2.fip_1", &fip), 26 ), 27 }, 28 }, 29 }) 30 } 31 32 func TestAccComputeV2FloatingIP_attach(t *testing.T) { 33 var instance servers.Server 34 var fip floatingips.FloatingIP 35 resource.Test(t, resource.TestCase{ 36 PreCheck: func() { testAccPreCheck(t) }, 37 Providers: testAccProviders, 38 CheckDestroy: testAccCheckComputeV2FloatingIPDestroy, 39 Steps: []resource.TestStep{ 40 resource.TestStep{ 41 Config: testAccComputeV2FloatingIP_attach, 42 Check: resource.ComposeTestCheckFunc( 43 testAccCheckComputeV2FloatingIPExists("openstack_compute_floatingip_v2.fip_1", &fip), 44 testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance), 45 testAccCheckComputeV2InstanceFloatingIPAttach(&instance, &fip), 46 ), 47 }, 48 }, 49 }) 50 } 51 52 func testAccCheckComputeV2FloatingIPDestroy(s *terraform.State) error { 53 config := testAccProvider.Meta().(*Config) 54 computeClient, err := config.computeV2Client(OS_REGION_NAME) 55 if err != nil { 56 return fmt.Errorf("Error creating OpenStack compute client: %s", err) 57 } 58 59 for _, rs := range s.RootModule().Resources { 60 if rs.Type != "openstack_compute_floatingip_v2" { 61 continue 62 } 63 64 _, err := floatingips.Get(computeClient, rs.Primary.ID).Extract() 65 if err == nil { 66 return fmt.Errorf("FloatingIP still exists") 67 } 68 } 69 70 return nil 71 } 72 73 func testAccCheckComputeV2FloatingIPExists(n string, kp *floatingips.FloatingIP) resource.TestCheckFunc { 74 return func(s *terraform.State) error { 75 rs, ok := s.RootModule().Resources[n] 76 if !ok { 77 return fmt.Errorf("Not found: %s", n) 78 } 79 80 if rs.Primary.ID == "" { 81 return fmt.Errorf("No ID is set") 82 } 83 84 config := testAccProvider.Meta().(*Config) 85 computeClient, err := config.computeV2Client(OS_REGION_NAME) 86 if err != nil { 87 return fmt.Errorf("Error creating OpenStack compute client: %s", err) 88 } 89 90 found, err := floatingips.Get(computeClient, rs.Primary.ID).Extract() 91 if err != nil { 92 return err 93 } 94 95 if found.ID != rs.Primary.ID { 96 return fmt.Errorf("FloatingIP not found") 97 } 98 99 *kp = *found 100 101 return nil 102 } 103 } 104 105 const testAccComputeV2FloatingIP_basic = ` 106 resource "openstack_compute_floatingip_v2" "fip_1" { 107 } 108 ` 109 110 var testAccComputeV2FloatingIP_attach = fmt.Sprintf(` 111 resource "openstack_compute_floatingip_v2" "fip_1" { 112 } 113 114 resource "openstack_compute_instance_v2" "instance_1" { 115 name = "instance_1" 116 security_groups = ["default"] 117 floating_ip = "${openstack_compute_floatingip_v2.fip_1.address}" 118 119 network { 120 uuid = "%s" 121 } 122 } 123 `, OS_NETWORK_ID)