From 6fe04710088959b0db7d91a3ab38b3036ca6e0de Mon Sep 17 00:00:00 2001 From: Joe Topjian Date: Sun, 21 May 2017 02:18:35 +0000 Subject: [PATCH] provider/openstack: Catch error during instance network parsing This commit catches an error when the instance is parsing and building its network list. This can happen when a cloud provider responds with a non-JSON response for the list of networks. --- .../resource_openstack_compute_instance_v2.go | 19 ++++--- ...urce_openstack_compute_instance_v2_test.go | 53 +++++++++++++++++++ 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/builtin/providers/openstack/resource_openstack_compute_instance_v2.go b/builtin/providers/openstack/resource_openstack_compute_instance_v2.go index 028b6fb57..4484e7c89 100644 --- a/builtin/providers/openstack/resource_openstack_compute_instance_v2.go +++ b/builtin/providers/openstack/resource_openstack_compute_instance_v2.go @@ -1056,19 +1056,26 @@ func getInstanceNetworks(computeClient *gophercloud.ServiceClient, d *schema.Res log.Printf("[DEBUG] os-tenant-networks disabled.") tenantNetworkExt = false } else { - return nil, err + log.Printf("[DEBUG] unexpected os-tenant-networks error: %s", err) + tenantNetworkExt = false } } } + // In some cases, a call to os-tenant-networks might work, + // but the response is invalid. Catch this during extraction. + networkList := []tenantnetworks.Network{} + if tenantNetworkExt { + networkList, err = tenantnetworks.ExtractNetworks(allPages) + if err != nil { + log.Printf("[DEBUG] error extracting os-tenant-networks results: %s", err) + tenantNetworkExt = false + } + } + networkID := "" networkName := "" if tenantNetworkExt { - networkList, err := tenantnetworks.ExtractNetworks(allPages) - if err != nil { - return nil, err - } - for _, network := range networkList { if network.Name == rawMap["name"] { tenantnet = network 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 affc17d6f..c8d4b9046 100644 --- a/builtin/providers/openstack/resource_openstack_compute_instance_v2_test.go +++ b/builtin/providers/openstack/resource_openstack_compute_instance_v2_test.go @@ -13,6 +13,7 @@ import ( "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/secgroups" "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/volumeattach" "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" + "github.com/gophercloud/gophercloud/openstack/networking/v2/networks" "github.com/gophercloud/gophercloud/pagination" ) @@ -666,6 +667,27 @@ func TestAccComputeV2Instance_timeout(t *testing.T) { }) } +func TestAccComputeV2Instance_networkNameToID(t *testing.T) { + var instance servers.Server + var network networks.Network + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeV2InstanceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccComputeV2Instance_networkNameToID, + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance), + testAccCheckNetworkingV2NetworkExists("openstack_networking_network_v2.network_1", &network), + resource.TestCheckResourceAttrPtr( + "openstack_compute_instance_v2.instance_1", "network.1.uuid", &network.ID), + ), + }, + }, + }) +} + func testAccCheckComputeV2InstanceDestroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) computeClient, err := config.computeV2Client(OS_REGION_NAME) @@ -1580,3 +1602,34 @@ resource "openstack_compute_instance_v2" "instance_1" { } } ` + +var testAccComputeV2Instance_networkNameToID = fmt.Sprintf(` +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"] + + network { + uuid = "%s" + } + + network { + name = "${openstack_networking_network_v2.network_1.name}" + } + +} +`, OS_NETWORK_ID)