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