github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/openstack/resource_openstack_compute_floatingip_associate_v2_test.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
     9  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
    10  
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccComputeV2FloatingIPAssociate_basic(t *testing.T) {
    16  	var instance servers.Server
    17  	var fip floatingips.FloatingIP
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckComputeV2FloatingIPAssociateDestroy,
    23  		Steps: []resource.TestStep{
    24  			resource.TestStep{
    25  				Config: testAccComputeV2FloatingIPAssociate_basic,
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
    28  					testAccCheckNetworkingV2FloatingIPExists("openstack_networking_floatingip_v2.fip_1", &fip),
    29  					testAccCheckComputeV2FloatingIPAssociateAssociated(&fip, &instance, 1),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func TestAccComputeV2FloatingIPAssociate_fixedIP(t *testing.T) {
    37  	var instance servers.Server
    38  	var fip floatingips.FloatingIP
    39  
    40  	resource.Test(t, resource.TestCase{
    41  		PreCheck:     func() { testAccPreCheck(t) },
    42  		Providers:    testAccProviders,
    43  		CheckDestroy: testAccCheckComputeV2FloatingIPAssociateDestroy,
    44  		Steps: []resource.TestStep{
    45  			resource.TestStep{
    46  				Config: testAccComputeV2FloatingIPAssociate_fixedIP,
    47  				Check: resource.ComposeTestCheckFunc(
    48  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
    49  					testAccCheckNetworkingV2FloatingIPExists("openstack_networking_floatingip_v2.fip_1", &fip),
    50  					testAccCheckComputeV2FloatingIPAssociateAssociated(&fip, &instance, 1),
    51  				),
    52  			},
    53  		},
    54  	})
    55  }
    56  
    57  func TestAccComputeV2FloatingIPAssociate_attachToFirstNetwork(t *testing.T) {
    58  	var instance servers.Server
    59  	var fip floatingips.FloatingIP
    60  
    61  	resource.Test(t, resource.TestCase{
    62  		PreCheck:     func() { testAccPreCheck(t) },
    63  		Providers:    testAccProviders,
    64  		CheckDestroy: testAccCheckComputeV2FloatingIPAssociateDestroy,
    65  		Steps: []resource.TestStep{
    66  			resource.TestStep{
    67  				Config: testAccComputeV2FloatingIPAssociate_attachToFirstNetwork,
    68  				Check: resource.ComposeTestCheckFunc(
    69  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
    70  					testAccCheckNetworkingV2FloatingIPExists("openstack_networking_floatingip_v2.fip_1", &fip),
    71  					testAccCheckComputeV2FloatingIPAssociateAssociated(&fip, &instance, 1),
    72  				),
    73  			},
    74  		},
    75  	})
    76  }
    77  
    78  func TestAccComputeV2FloatingIPAssociate_attachToSecondNetwork(t *testing.T) {
    79  	var instance servers.Server
    80  	var fip floatingips.FloatingIP
    81  
    82  	resource.Test(t, resource.TestCase{
    83  		PreCheck:     func() { testAccPreCheck(t) },
    84  		Providers:    testAccProviders,
    85  		CheckDestroy: testAccCheckComputeV2FloatingIPAssociateDestroy,
    86  		Steps: []resource.TestStep{
    87  			resource.TestStep{
    88  				Config: testAccComputeV2FloatingIPAssociate_attachToSecondNetwork,
    89  				Check: resource.ComposeTestCheckFunc(
    90  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
    91  					testAccCheckNetworkingV2FloatingIPExists("openstack_networking_floatingip_v2.fip_1", &fip),
    92  					testAccCheckComputeV2FloatingIPAssociateAssociated(&fip, &instance, 2),
    93  				),
    94  			},
    95  		},
    96  	})
    97  }
    98  
    99  func TestAccComputeV2FloatingIPAssociate_attachNew(t *testing.T) {
   100  	var instance servers.Server
   101  	var fip_1 floatingips.FloatingIP
   102  	var fip_2 floatingips.FloatingIP
   103  
   104  	resource.Test(t, resource.TestCase{
   105  		PreCheck:     func() { testAccPreCheck(t) },
   106  		Providers:    testAccProviders,
   107  		CheckDestroy: testAccCheckComputeV2FloatingIPAssociateDestroy,
   108  		Steps: []resource.TestStep{
   109  			resource.TestStep{
   110  				Config: testAccComputeV2FloatingIPAssociate_attachNew_1,
   111  				Check: resource.ComposeTestCheckFunc(
   112  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
   113  					testAccCheckNetworkingV2FloatingIPExists("openstack_networking_floatingip_v2.fip_1", &fip_1),
   114  					testAccCheckNetworkingV2FloatingIPExists("openstack_networking_floatingip_v2.fip_2", &fip_2),
   115  					testAccCheckComputeV2FloatingIPAssociateAssociated(&fip_1, &instance, 1),
   116  				),
   117  			},
   118  			resource.TestStep{
   119  				Config: testAccComputeV2FloatingIPAssociate_attachNew_2,
   120  				Check: resource.ComposeTestCheckFunc(
   121  					testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
   122  					testAccCheckNetworkingV2FloatingIPExists("openstack_networking_floatingip_v2.fip_1", &fip_1),
   123  					testAccCheckNetworkingV2FloatingIPExists("openstack_networking_floatingip_v2.fip_2", &fip_2),
   124  					testAccCheckComputeV2FloatingIPAssociateAssociated(&fip_2, &instance, 1),
   125  				),
   126  			},
   127  		},
   128  	})
   129  }
   130  
   131  func testAccCheckComputeV2FloatingIPAssociateDestroy(s *terraform.State) error {
   132  	config := testAccProvider.Meta().(*Config)
   133  	computeClient, err := config.computeV2Client(OS_REGION_NAME)
   134  	if err != nil {
   135  		return fmt.Errorf("Error creating OpenStack compute client: %s", err)
   136  	}
   137  
   138  	for _, rs := range s.RootModule().Resources {
   139  		if rs.Type != "openstack_compute_floatingip_associate_v2" {
   140  			continue
   141  		}
   142  
   143  		floatingIP, instanceId, _, err := parseComputeFloatingIPAssociateId(rs.Primary.ID)
   144  		if err != nil {
   145  			return err
   146  		}
   147  
   148  		instance, err := servers.Get(computeClient, instanceId).Extract()
   149  		if err != nil {
   150  			// If the error is a 404, then the instance does not exist,
   151  			// and therefore the floating IP cannot be associated to it.
   152  			if _, ok := err.(gophercloud.ErrDefault404); ok {
   153  				return nil
   154  			}
   155  			return err
   156  		}
   157  
   158  		// But if the instance still exists, then walk through its known addresses
   159  		// and see if there's a floating IP.
   160  		for _, networkAddresses := range instance.Addresses {
   161  			for _, element := range networkAddresses.([]interface{}) {
   162  				address := element.(map[string]interface{})
   163  				if address["OS-EXT-IPS:type"] == "floating" {
   164  					return fmt.Errorf("Floating IP %s is still attached to instance %s", floatingIP, instanceId)
   165  				}
   166  			}
   167  		}
   168  	}
   169  
   170  	return nil
   171  }
   172  
   173  func testAccCheckComputeV2FloatingIPAssociateAssociated(
   174  	fip *floatingips.FloatingIP, instance *servers.Server, n int) resource.TestCheckFunc {
   175  	return func(s *terraform.State) error {
   176  		config := testAccProvider.Meta().(*Config)
   177  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   178  
   179  		newInstance, err := servers.Get(computeClient, instance.ID).Extract()
   180  		if err != nil {
   181  			return err
   182  		}
   183  
   184  		// Walk through the instance's addresses and find the match
   185  		i := 0
   186  		for _, networkAddresses := range newInstance.Addresses {
   187  			i += 1
   188  			if i != n {
   189  				continue
   190  			}
   191  			for _, element := range networkAddresses.([]interface{}) {
   192  				address := element.(map[string]interface{})
   193  				if address["OS-EXT-IPS:type"] == "floating" && address["addr"] == fip.FloatingIP {
   194  					return nil
   195  				}
   196  			}
   197  		}
   198  		return fmt.Errorf("Floating IP %s was not attached to instance %s", fip.FloatingIP, instance.ID)
   199  	}
   200  }
   201  
   202  const testAccComputeV2FloatingIPAssociate_basic = `
   203  resource "openstack_compute_instance_v2" "instance_1" {
   204    name = "instance_1"
   205    security_groups = ["default"]
   206  }
   207  
   208  resource "openstack_networking_floatingip_v2" "fip_1" {
   209  }
   210  
   211  resource "openstack_compute_floatingip_associate_v2" "fip_1" {
   212    floating_ip = "${openstack_networking_floatingip_v2.fip_1.address}"
   213    instance_id = "${openstack_compute_instance_v2.instance_1.id}"
   214  }
   215  `
   216  
   217  const testAccComputeV2FloatingIPAssociate_fixedIP = `
   218  resource "openstack_compute_instance_v2" "instance_1" {
   219    name = "instance_1"
   220    security_groups = ["default"]
   221  }
   222  
   223  resource "openstack_networking_floatingip_v2" "fip_1" {
   224  }
   225  
   226  resource "openstack_compute_floatingip_associate_v2" "fip_1" {
   227    floating_ip = "${openstack_networking_floatingip_v2.fip_1.address}"
   228    instance_id = "${openstack_compute_instance_v2.instance_1.id}"
   229    fixed_ip = "${openstack_compute_instance_v2.instance_1.access_ip_v4}"
   230  }
   231  `
   232  
   233  var testAccComputeV2FloatingIPAssociate_attachToFirstNetwork = fmt.Sprintf(`
   234  resource "openstack_compute_instance_v2" "instance_1" {
   235    name = "instance_1"
   236    security_groups = ["default"]
   237  
   238    network {
   239      uuid = "%s"
   240    }
   241  }
   242  
   243  resource "openstack_networking_floatingip_v2" "fip_1" {
   244  }
   245  
   246  resource "openstack_compute_floatingip_associate_v2" "fip_1" {
   247    floating_ip = "${openstack_networking_floatingip_v2.fip_1.address}"
   248    instance_id = "${openstack_compute_instance_v2.instance_1.id}"
   249    fixed_ip = "${openstack_compute_instance_v2.instance_1.network.0.fixed_ip_v4}"
   250  }
   251  `, OS_NETWORK_ID)
   252  
   253  var testAccComputeV2FloatingIPAssociate_attachToSecondNetwork = fmt.Sprintf(`
   254  resource "openstack_networking_network_v2" "network_1" {
   255    name = "network_1"
   256  }
   257  
   258  resource "openstack_networking_subnet_v2" "subnet_1" {
   259    name = "subnet_1"
   260    network_id = "${openstack_networking_network_v2.network_1.id}"
   261    cidr = "192.168.1.0/24"
   262    ip_version = 4
   263    enable_dhcp = true
   264    no_gateway = true
   265  }
   266  
   267  resource "openstack_compute_instance_v2" "instance_1" {
   268    name = "instance_1"
   269    security_groups = ["default"]
   270  
   271    network {
   272      uuid = "${openstack_networking_network_v2.network_1.id}"
   273    }
   274  
   275    network {
   276      uuid = "%s"
   277    }
   278  }
   279  
   280  resource "openstack_networking_floatingip_v2" "fip_1" {
   281  }
   282  
   283  resource "openstack_compute_floatingip_associate_v2" "fip_1" {
   284    floating_ip = "${openstack_networking_floatingip_v2.fip_1.address}"
   285    instance_id = "${openstack_compute_instance_v2.instance_1.id}"
   286    fixed_ip = "${openstack_compute_instance_v2.instance_1.network.1.fixed_ip_v4}"
   287  }
   288  `, OS_NETWORK_ID)
   289  
   290  const testAccComputeV2FloatingIPAssociate_attachNew_1 = `
   291  resource "openstack_compute_instance_v2" "instance_1" {
   292    name = "instance_1"
   293    security_groups = ["default"]
   294  }
   295  
   296  resource "openstack_networking_floatingip_v2" "fip_1" {
   297  }
   298  
   299  resource "openstack_networking_floatingip_v2" "fip_2" {
   300  }
   301  
   302  resource "openstack_compute_floatingip_associate_v2" "fip_1" {
   303    floating_ip = "${openstack_networking_floatingip_v2.fip_1.address}"
   304    instance_id = "${openstack_compute_instance_v2.instance_1.id}"
   305  }
   306  `
   307  
   308  const testAccComputeV2FloatingIPAssociate_attachNew_2 = `
   309  resource "openstack_compute_instance_v2" "instance_1" {
   310    name = "instance_1"
   311    security_groups = ["default"]
   312  }
   313  
   314  resource "openstack_networking_floatingip_v2" "fip_1" {
   315  }
   316  
   317  resource "openstack_networking_floatingip_v2" "fip_2" {
   318  }
   319  
   320  resource "openstack_compute_floatingip_associate_v2" "fip_1" {
   321    floating_ip = "${openstack_networking_floatingip_v2.fip_2.address}"
   322    instance_id = "${openstack_compute_instance_v2.instance_1.id}"
   323  }
   324  `