From 2c2e34a8fc255e7f5e270084ec011e63e4264738 Mon Sep 17 00:00:00 2001 From: Thomas Stinner Date: Mon, 5 Sep 2022 14:54:46 +0200 Subject: [PATCH 1/5] Allow settings tags for lb_listener_v2 --- openstack/lb_v2_shared.go | 16 ++++ .../resource_openstack_lb_listener_v2.go | 8 ++ .../resource_openstack_lb_listener_v2_test.go | 74 +++++++++++++++++++ 3 files changed, 98 insertions(+) diff --git a/openstack/lb_v2_shared.go b/openstack/lb_v2_shared.go index 19e116e2f..5e0d863df 100644 --- a/openstack/lb_v2_shared.go +++ b/openstack/lb_v2_shared.go @@ -189,6 +189,11 @@ func chooseLBV2ListenerCreateOpts(d *schema.ResourceData, config *Config) (neutr opts.TimeoutTCPInspect = &timeoutTCPInspect } + if v, ok := d.GetOk("tags"); ok { + tags := v.(*schema.Set).List() + opts.Tags = expandToStringSlice(tags) + } + // Get and check insert headers map. rawHeaders := d.Get("insert_headers").(map[string]interface{}) headers, err := expandLBV2ListenerHeadersMap(rawHeaders) @@ -338,6 +343,17 @@ func chooseLBV2ListenerUpdateOpts(d *schema.ResourceData, config *Config) (neutr opts.AllowedCIDRs = &allowedCidrs } + if d.HasChange("tags") { + hasChange = true + if v, ok := d.GetOk("tags"); ok { + tags := v.(*schema.Set).List() + tagsToUpdate := expandToStringSlice(tags) + opts.Tags = &tagsToUpdate + } else { + opts.Tags = &[]string{} + } + } + if hasChange { return opts, nil } diff --git a/openstack/resource_openstack_lb_listener_v2.go b/openstack/resource_openstack_lb_listener_v2.go index 203bd2086..cd1dd0eaa 100644 --- a/openstack/resource_openstack_lb_listener_v2.go +++ b/openstack/resource_openstack_lb_listener_v2.go @@ -141,6 +141,13 @@ func resourceListenerV2() *schema.Resource { Optional: true, Elem: &schema.Schema{Type: schema.TypeString}, }, + + "tags": { + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, }, } } @@ -223,6 +230,7 @@ func resourceListenerV2Read(ctx context.Context, d *schema.ResourceData, meta in d.Set("default_tls_container_ref", listener.DefaultTlsContainerRef) d.Set("allowed_cidrs", listener.AllowedCIDRs) d.Set("region", GetRegion(d, config)) + d.Set("tags", listener.Tags) // Required by import. if len(listener.Loadbalancers) > 0 { diff --git a/openstack/resource_openstack_lb_listener_v2_test.go b/openstack/resource_openstack_lb_listener_v2_test.go index c10774a40..818bfc4f4 100644 --- a/openstack/resource_openstack_lb_listener_v2_test.go +++ b/openstack/resource_openstack_lb_listener_v2_test.go @@ -2,6 +2,7 @@ package openstack import ( "fmt" + octavialoadbalancers "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/loadbalancers" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -28,6 +29,8 @@ func TestAccLBV2Listener_basic(t *testing.T) { testAccCheckLBV2ListenerExists("openstack_lb_listener_v2.listener_1", &listener), resource.TestCheckResourceAttr( "openstack_lb_listener_v2.listener_1", "connection_limit", "-1"), + testAccCheckLBV2ListenerHasTag("openstack_lb_loadbalancer_v2.loadbalancer_1", "tag1"), + testAccCheckLBV2ListenerTagCount("openstack_lb_loadbalancer_v2.loadbalancer_1", 1), ), }, { @@ -218,6 +221,76 @@ func testAccCheckLBV2ListenerExists(n string, listener *listeners.Listener) reso } } +func testAccCheckLBV2ListenerHasTag(n, tag string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + + config := testAccProvider.Meta().(*Config) + lbClient, err := chooseLBV2AccTestClient(config, osRegionName) + if err != nil { + return fmt.Errorf("Error creating OpenStack load balancing client: %s", err) + } + + found, err := octavialoadbalancers.Get(lbClient, rs.Primary.ID).Extract() + if err != nil { + return err + } + + if found.ID != rs.Primary.ID { + return fmt.Errorf("Loadbalancer not found") + } + + for _, v := range found.Tags { + if tag == v { + return nil + } + } + + return fmt.Errorf("Tag not found: %s", tag) + } +} + +func testAccCheckLBV2ListenerTagCount(n string, expected int) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + + config := testAccProvider.Meta().(*Config) + lbClient, err := chooseLBV2AccTestClient(config, osRegionName) + if err != nil { + return fmt.Errorf("Error creating OpenStack load balancing client: %s", err) + } + + found, err := octavialoadbalancers.Get(lbClient, rs.Primary.ID).Extract() + if err != nil { + return err + } + + if found.ID != rs.Primary.ID { + return fmt.Errorf("Loadbalancer not found") + } + + if len(found.Tags) != expected { + return fmt.Errorf("Expecting %d tags, found %d", expected, len(found.Tags)) + } + + return nil + } +} + const testAccLbV2ListenerConfigBasic = ` resource "openstack_networking_network_v2" "network_1" { name = "network_1" @@ -255,6 +328,7 @@ resource "openstack_lb_listener_v2" "listener_1" { protocol_port = 8080 default_pool_id = "${openstack_lb_pool_v2.pool_1.id}" loadbalancer_id = "${openstack_lb_loadbalancer_v2.loadbalancer_1.id}" + tags = ["tag1"] timeouts { create = "5m" From 0a4cb6bcc50879a3bcd7a91b96b6edc28bb73e60 Mon Sep 17 00:00:00 2001 From: Thomas Stinner Date: Tue, 6 Sep 2022 13:47:19 +0200 Subject: [PATCH 2/5] gophercloud doesn't return tags in structure --- .../resource_openstack_lb_listener_v2_test.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/openstack/resource_openstack_lb_listener_v2_test.go b/openstack/resource_openstack_lb_listener_v2_test.go index 818bfc4f4..fc24938d9 100644 --- a/openstack/resource_openstack_lb_listener_v2_test.go +++ b/openstack/resource_openstack_lb_listener_v2_test.go @@ -2,7 +2,6 @@ package openstack import ( "fmt" - octavialoadbalancers "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/loadbalancers" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -29,8 +28,9 @@ func TestAccLBV2Listener_basic(t *testing.T) { testAccCheckLBV2ListenerExists("openstack_lb_listener_v2.listener_1", &listener), resource.TestCheckResourceAttr( "openstack_lb_listener_v2.listener_1", "connection_limit", "-1"), - testAccCheckLBV2ListenerHasTag("openstack_lb_loadbalancer_v2.loadbalancer_1", "tag1"), + /*testAccCheckLBV2ListenerHasTag("openstack_lb_loadbalancer_v2.loadbalancer_1", "tag1"), testAccCheckLBV2ListenerTagCount("openstack_lb_loadbalancer_v2.loadbalancer_1", 1), + */ ), }, { @@ -221,7 +221,10 @@ func testAccCheckLBV2ListenerExists(n string, listener *listeners.Listener) reso } } -func testAccCheckLBV2ListenerHasTag(n, tag string) resource.TestCheckFunc { +/** + * gophercloud doesn't return the tags in the structure. Need to fix tghis first. + */ +/*func testAccCheckLBV2ListenerHasTag(n, tag string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -238,7 +241,7 @@ func testAccCheckLBV2ListenerHasTag(n, tag string) resource.TestCheckFunc { return fmt.Errorf("Error creating OpenStack load balancing client: %s", err) } - found, err := octavialoadbalancers.Get(lbClient, rs.Primary.ID).Extract() + found, err := listeners.Get(lbClient, rs.Primary.ID).Extract() if err != nil { return err } @@ -256,7 +259,12 @@ func testAccCheckLBV2ListenerHasTag(n, tag string) resource.TestCheckFunc { return fmt.Errorf("Tag not found: %s", tag) } } +*/ +/** + * gophercloud doesn't return the tags in the structure. Need to fix tghis first. + */ +/* func testAccCheckLBV2ListenerTagCount(n string, expected int) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -274,7 +282,7 @@ func testAccCheckLBV2ListenerTagCount(n string, expected int) resource.TestCheck return fmt.Errorf("Error creating OpenStack load balancing client: %s", err) } - found, err := octavialoadbalancers.Get(lbClient, rs.Primary.ID).Extract() + found, err := listeners.Get(lbClient, rs.Primary.ID).Extract() if err != nil { return err } @@ -290,6 +298,7 @@ func testAccCheckLBV2ListenerTagCount(n string, expected int) resource.TestCheck return nil } } +*/ const testAccLbV2ListenerConfigBasic = ` resource "openstack_networking_network_v2" "network_1" { From 0ad5b2ec4fd3e402d8f93f3da6f1ae4eb11e25f5 Mon Sep 17 00:00:00 2001 From: Thomas Stinner Date: Tue, 6 Sep 2022 14:50:30 +0200 Subject: [PATCH 3/5] Add test for tags again --- .../resource_openstack_lb_listener_v2_test.go | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/openstack/resource_openstack_lb_listener_v2_test.go b/openstack/resource_openstack_lb_listener_v2_test.go index fc24938d9..ee5e7b50a 100644 --- a/openstack/resource_openstack_lb_listener_v2_test.go +++ b/openstack/resource_openstack_lb_listener_v2_test.go @@ -28,9 +28,8 @@ func TestAccLBV2Listener_basic(t *testing.T) { testAccCheckLBV2ListenerExists("openstack_lb_listener_v2.listener_1", &listener), resource.TestCheckResourceAttr( "openstack_lb_listener_v2.listener_1", "connection_limit", "-1"), - /*testAccCheckLBV2ListenerHasTag("openstack_lb_loadbalancer_v2.loadbalancer_1", "tag1"), + testAccCheckLBV2ListenerHasTag("openstack_lb_loadbalancer_v2.loadbalancer_1", "tag1"), testAccCheckLBV2ListenerTagCount("openstack_lb_loadbalancer_v2.loadbalancer_1", 1), - */ ), }, { @@ -221,10 +220,7 @@ func testAccCheckLBV2ListenerExists(n string, listener *listeners.Listener) reso } } -/** - * gophercloud doesn't return the tags in the structure. Need to fix tghis first. - */ -/*func testAccCheckLBV2ListenerHasTag(n, tag string) resource.TestCheckFunc { +func testAccCheckLBV2ListenerHasTag(n, tag string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -259,12 +255,7 @@ func testAccCheckLBV2ListenerExists(n string, listener *listeners.Listener) reso return fmt.Errorf("Tag not found: %s", tag) } } -*/ -/** - * gophercloud doesn't return the tags in the structure. Need to fix tghis first. - */ -/* func testAccCheckLBV2ListenerTagCount(n string, expected int) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -298,7 +289,6 @@ func testAccCheckLBV2ListenerTagCount(n string, expected int) resource.TestCheck return nil } } -*/ const testAccLbV2ListenerConfigBasic = ` resource "openstack_networking_network_v2" "network_1" { From e5fbccc415a1378839d7d4b95c9a768928f9d3c9 Mon Sep 17 00:00:00 2001 From: Thomas Stinner Date: Tue, 6 Sep 2022 15:18:29 +0200 Subject: [PATCH 4/5] Use testcheckresourceattr --- .../resource_openstack_lb_listener_v2_test.go | 74 +------------------ 1 file changed, 2 insertions(+), 72 deletions(-) diff --git a/openstack/resource_openstack_lb_listener_v2_test.go b/openstack/resource_openstack_lb_listener_v2_test.go index ee5e7b50a..431aa7e01 100644 --- a/openstack/resource_openstack_lb_listener_v2_test.go +++ b/openstack/resource_openstack_lb_listener_v2_test.go @@ -28,8 +28,6 @@ func TestAccLBV2Listener_basic(t *testing.T) { testAccCheckLBV2ListenerExists("openstack_lb_listener_v2.listener_1", &listener), resource.TestCheckResourceAttr( "openstack_lb_listener_v2.listener_1", "connection_limit", "-1"), - testAccCheckLBV2ListenerHasTag("openstack_lb_loadbalancer_v2.loadbalancer_1", "tag1"), - testAccCheckLBV2ListenerTagCount("openstack_lb_loadbalancer_v2.loadbalancer_1", 1), ), }, { @@ -62,6 +60,8 @@ func TestAccLBV2Listener_octavia(t *testing.T) { Config: testAccLbV2ListenerConfigOctavia, Check: resource.ComposeTestCheckFunc( testAccCheckLBV2ListenerExists("openstack_lb_listener_v2.listener_1", &listener), + resource.TestCheckResourceAttr( + "openstack_lb_listener_v2.listener_1", "tags", "[\"tag1\"]"), resource.TestCheckResourceAttr( "openstack_lb_listener_v2.listener_1", "connection_limit", "5"), resource.TestCheckResourceAttr( @@ -220,76 +220,6 @@ func testAccCheckLBV2ListenerExists(n string, listener *listeners.Listener) reso } } -func testAccCheckLBV2ListenerHasTag(n, tag string) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } - - config := testAccProvider.Meta().(*Config) - lbClient, err := chooseLBV2AccTestClient(config, osRegionName) - if err != nil { - return fmt.Errorf("Error creating OpenStack load balancing client: %s", err) - } - - found, err := listeners.Get(lbClient, rs.Primary.ID).Extract() - if err != nil { - return err - } - - if found.ID != rs.Primary.ID { - return fmt.Errorf("Loadbalancer not found") - } - - for _, v := range found.Tags { - if tag == v { - return nil - } - } - - return fmt.Errorf("Tag not found: %s", tag) - } -} - -func testAccCheckLBV2ListenerTagCount(n string, expected int) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } - - config := testAccProvider.Meta().(*Config) - lbClient, err := chooseLBV2AccTestClient(config, osRegionName) - if err != nil { - return fmt.Errorf("Error creating OpenStack load balancing client: %s", err) - } - - found, err := listeners.Get(lbClient, rs.Primary.ID).Extract() - if err != nil { - return err - } - - if found.ID != rs.Primary.ID { - return fmt.Errorf("Loadbalancer not found") - } - - if len(found.Tags) != expected { - return fmt.Errorf("Expecting %d tags, found %d", expected, len(found.Tags)) - } - - return nil - } -} - const testAccLbV2ListenerConfigBasic = ` resource "openstack_networking_network_v2" "network_1" { name = "network_1" From fc75eb4580eb727b4f577a14489db974ae4464a5 Mon Sep 17 00:00:00 2001 From: kayrus Date: Sat, 11 Mar 2023 18:53:46 +0100 Subject: [PATCH 5/5] Fix tags definition --- openstack/resource_openstack_lb_listener_v2_test.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/openstack/resource_openstack_lb_listener_v2_test.go b/openstack/resource_openstack_lb_listener_v2_test.go index 431aa7e01..48f51e9a3 100644 --- a/openstack/resource_openstack_lb_listener_v2_test.go +++ b/openstack/resource_openstack_lb_listener_v2_test.go @@ -61,7 +61,7 @@ func TestAccLBV2Listener_octavia(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckLBV2ListenerExists("openstack_lb_listener_v2.listener_1", &listener), resource.TestCheckResourceAttr( - "openstack_lb_listener_v2.listener_1", "tags", "[\"tag1\"]"), + "openstack_lb_listener_v2.listener_1", "tags.#", "1"), resource.TestCheckResourceAttr( "openstack_lb_listener_v2.listener_1", "connection_limit", "5"), resource.TestCheckResourceAttr( @@ -79,6 +79,8 @@ func TestAccLBV2Listener_octavia(t *testing.T) { Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( "openstack_lb_listener_v2.listener_1", "name", "listener_1_updated"), + resource.TestCheckResourceAttr( + "openstack_lb_listener_v2.listener_1", "tags.#", "2"), resource.TestCheckResourceAttr( "openstack_lb_listener_v2.listener_1", "connection_limit", "100"), resource.TestCheckResourceAttr( @@ -257,7 +259,6 @@ resource "openstack_lb_listener_v2" "listener_1" { protocol_port = 8080 default_pool_id = "${openstack_lb_pool_v2.pool_1.id}" loadbalancer_id = "${openstack_lb_loadbalancer_v2.loadbalancer_1.id}" - tags = ["tag1"] timeouts { create = "5m" @@ -349,6 +350,7 @@ resource "openstack_lb_listener_v2" "listener_1" { timeout_tcp_inspect = 4000 default_pool_id = "${openstack_lb_pool_v2.pool_1.id}" loadbalancer_id = "${openstack_lb_loadbalancer_v2.loadbalancer_1.id}" + tags = ["tag1"] timeouts { create = "5m" @@ -401,6 +403,7 @@ resource "openstack_lb_listener_v2" "listener_1" { admin_state_up = "true" default_pool_id = "${openstack_lb_pool_v2.pool_1.id}" loadbalancer_id = "${openstack_lb_loadbalancer_v2.loadbalancer_1.id}" + tags = ["tag1", "tag2"] timeouts { create = "5m"