From 9d10028d43dee499c6206ff1edaf2f7ab6400d67 Mon Sep 17 00:00:00 2001 From: Joe Topjian Date: Wed, 6 Apr 2016 19:17:51 +0000 Subject: [PATCH] provider/openstack: Fix Access Address Detection This commit fixes how access ip addresses are detected. The previous logic used was flawed and would detect the IPs in the wrong order. --- .../resource_openstack_compute_instance_v2.go | 34 +++++++----- ...urce_openstack_compute_instance_v2_test.go | 54 +++++++++++++++++++ 2 files changed, 75 insertions(+), 13 deletions(-) diff --git a/builtin/providers/openstack/resource_openstack_compute_instance_v2.go b/builtin/providers/openstack/resource_openstack_compute_instance_v2.go index 87cd4e5800c0..14233c6a26e6 100644 --- a/builtin/providers/openstack/resource_openstack_compute_instance_v2.go +++ b/builtin/providers/openstack/resource_openstack_compute_instance_v2.go @@ -1009,25 +1009,33 @@ func getInstanceAccessAddresses(d *schema.ResourceData, networks []map[string]in hostv4 = floatingIP } - // Loop through all networks and check for the following: - // * If the network is set as an access network. - // * If the network has a floating IP. - // * If the network has a v4/v6 fixed IP. + // Loop through all networks + // If the network has a valid floating, fixed v4, or fixed v6 address + // and hostv4 or hostv6 is not set, set hostv4/hostv6. + // If the network is an "access_network" overwrite hostv4/hostv6. for _, n := range networks { - if n["floating_ip"] != nil { - hostv4 = n["floating_ip"].(string) - } else { - if hostv4 == "" && n["fixed_ip_v4"] != nil { - hostv4 = n["fixed_ip_v4"].(string) + var accessNetwork bool + + if an, ok := n["access_network"].(bool); ok && an { + accessNetwork = true + } + + if fixedIPv4, ok := n["fixed_ip_v4"].(string); ok && fixedIPv4 != "" { + if hostv4 == "" || accessNetwork { + hostv4 = fixedIPv4 } } - if hostv6 == "" && n["fixed_ip_v6"] != nil { - hostv6 = n["fixed_ip_v6"].(string) + if floatingIP, ok := n["floating_ip"].(string); ok && floatingIP != "" { + if hostv4 == "" || accessNetwork { + hostv4 = floatingIP + } } - if an, ok := n["access_network"].(bool); ok && an { - break + if fixedIPv6, ok := n["fixed_ip_v6"].(string); ok && fixedIPv6 != "" { + if hostv6 == "" || accessNetwork { + hostv6 = fixedIPv6 + } } } diff --git a/builtin/providers/openstack/resource_openstack_compute_instance_v2_test.go b/builtin/providers/openstack/resource_openstack_compute_instance_v2_test.go index 1627693ba351..b87e807572bd 100644 --- a/builtin/providers/openstack/resource_openstack_compute_instance_v2_test.go +++ b/builtin/providers/openstack/resource_openstack_compute_instance_v2_test.go @@ -503,6 +503,60 @@ func TestAccComputeV2Instance_multiEphemeral(t *testing.T) { }) } +func TestAccComputeV2Instance_accessIPv4(t *testing.T) { + var instance servers.Server + var testAccComputeV2Instance_accessIPv4 = fmt.Sprintf(` + resource "openstack_compute_floatingip_v2" "myip" { + } + + resource "openstack_networking_network_v2" "network_1" { + name = "network_1" + } + + resource "openstack_networking_subnet_v2" "subnet_1" { + name = "subnet_1" + network_id = "${openstack_networking_network_v2.network_1.id}" + cidr = "192.168.1.0/24" + ip_version = 4 + enable_dhcp = true + no_gateway = true + } + + resource "openstack_compute_instance_v2" "instance_1" { + depends_on = ["openstack_networking_subnet_v2.subnet_1"] + + name = "instance_1" + security_groups = ["default"] + floating_ip = "${openstack_compute_floatingip_v2.myip.address}" + + network { + uuid = "%s" + } + + network { + uuid = "${openstack_networking_network_v2.network_1.id}" + fixed_ip_v4 = "192.168.1.100" + access_network = true + } + }`, os.Getenv("OS_NETWORK_ID")) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeV2InstanceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccComputeV2Instance_accessIPv4, + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance), + resource.TestCheckResourceAttr( + "openstack_compute_instance_v2.instance_1", "access_ip_v4", "192.168.1.100"), + ), + }, + }, + }) +} + func testAccCheckComputeV2InstanceDestroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) computeClient, err := config.computeV2Client(OS_REGION_NAME)