From 3daa082c23d17a7ea314375d8db6e7e60260bdeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20H=C3=BCmbeli?= Date: Fri, 12 Jul 2024 14:53:00 +0200 Subject: [PATCH] PSA-30969 Use Set instead of List --- docs/resources/endpoint.md | 2 +- .../provider/endpoint/endpoint_schema_v1.go | 2 +- internal/provider/provider_test.go | 143 ++++++++++++++++++ 3 files changed, 145 insertions(+), 2 deletions(-) diff --git a/docs/resources/endpoint.md b/docs/resources/endpoint.md index afa4251..49bc64a 100644 --- a/docs/resources/endpoint.md +++ b/docs/resources/endpoint.md @@ -36,7 +36,7 @@ resource "dgservicebus_endpoint" "example" { - `endpoint_name` (String) The name of the endpoint to create. - `queue_options` (Attributes) The options for the queue, which is created for the endpoint. (see [below for nested schema](#nestedatt--queue_options)) -- `subscriptions` (Attributes List) (see [below for nested schema](#nestedatt--subscriptions)) +- `subscriptions` (Attributes Set) (see [below for nested schema](#nestedatt--subscriptions)) - `topic_name` (String) The name of the topic to create the endpoint on. ### Optional diff --git a/internal/provider/endpoint/endpoint_schema_v1.go b/internal/provider/endpoint/endpoint_schema_v1.go index 52bb57b..3c45430 100644 --- a/internal/provider/endpoint/endpoint_schema_v1.go +++ b/internal/provider/endpoint/endpoint_schema_v1.go @@ -35,7 +35,7 @@ func NewSchemaV1() schema.Schema { stringplanmodifier.RequiresReplace(), }, }, - "subscriptions": schema.ListNestedAttribute{ + "subscriptions": schema.SetNestedAttribute{ Required: true, NestedObject: schema.NestedAttributeObject{ Attributes: map[string]schema.Attribute{ diff --git a/internal/provider/provider_test.go b/internal/provider/provider_test.go index 39b99f2..8c527c4 100644 --- a/internal/provider/provider_test.go +++ b/internal/provider/provider_test.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "regexp" + "strings" "testing" "terraform-provider-dg-servicebus/internal/provider/asb" @@ -322,6 +323,148 @@ func TestAcc_EndpointDataSourceTest(t *testing.T) { ensure_enpoint_deleted(client, endpoint_correlation_with_subcription_name) } +func TestAcc_EndpointSubscriptionOrderChanges(t *testing.T) { + client := createClient(t) + + endpoint_name := acctest.RandString(10) + "-test-ordes-endpoint" + subscriptionFilterValue1 := "Dg.Test.Subscription.V1" + subscriptionFilterValue2 := "Dg.Test.Subscription.V2" + subscriptionFilterValue3 := "Dg.Test.Subscription.V3" + subscriptionFilterValue4 := "Dg.Test.Subscription.V4" + + resource.ParallelTest(t, resource.TestCase{ + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + //Create resource with 3 subscriptions + { + Config: providerConfig + fmt.Sprintf(` + resource "dgservicebus_endpoint" "test" { + endpoint_name = "%v" + topic_name = "bundle-1" + subscriptions = [ + {filter = "%v", filter_type = "sql"}, + {filter = "%v", filter_type = "sql"}, + {filter = "%v", filter_type = "sql"} + ] + + queue_options = { + enable_partitioning = true, + max_size_in_megabytes = 5120, + max_message_size_in_kilobytes = 256 + } + }`, endpoint_name, subscriptionFilterValue1, subscriptionFilterValue2, subscriptionFilterValue3), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("dgservicebus_endpoint.test", "subscriptions.#", "3"), + resource.TestCheckResourceAttr("dgservicebus_endpoint.test", "subscriptions.0.filter", subscriptionFilterValue1), + resource.TestCheckResourceAttr("dgservicebus_endpoint.test", "subscriptions.1.filter", subscriptionFilterValue2), + resource.TestCheckResourceAttr("dgservicebus_endpoint.test", "subscriptions.2.filter", subscriptionFilterValue3), + resource.TestCheckResourceAttr("dgservicebus_endpoint.test", "endpoint_name", endpoint_name), + ), + }, + // Change order of subscriptions + { + Config: providerConfig + fmt.Sprintf(` + resource "dgservicebus_endpoint" "test" { + endpoint_name = "%v" + topic_name = "bundle-1" + subscriptions = [ + {filter = "%v", filter_type = "sql"}, + {filter = "%v", filter_type = "sql"}, + {filter = "%v", filter_type = "sql"} + ] + + queue_options = { + enable_partitioning = true, + max_size_in_megabytes = 5120, + max_message_size_in_kilobytes = 256 + } + }`, endpoint_name, subscriptionFilterValue3, subscriptionFilterValue1, subscriptionFilterValue2), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("dgservicebus_endpoint.test", "subscriptions.#", "3"), + resource.TestCheckResourceAttr("dgservicebus_endpoint.test", "subscriptions.0.filter", subscriptionFilterValue1), + resource.TestCheckResourceAttr("dgservicebus_endpoint.test", "subscriptions.1.filter", subscriptionFilterValue2), + resource.TestCheckResourceAttr("dgservicebus_endpoint.test", "subscriptions.2.filter", subscriptionFilterValue3), + resource.TestCheckResourceAttr("dgservicebus_endpoint.test", "endpoint_name", endpoint_name), + func(s *terraform.State) error { + subscriptions, _ := client.GetAsbSubscriptionsRules(context.Background(), asb.AsbEndpointModel{ + EndpointName: endpoint_name, + TopicName: "bundle-1", + }) + if len(subscriptions) != 3 { + return fmt.Errorf("Expected 3 subscriptions") + } + contains := func(subscriptions []asb.AsbSubscriptionRule, filter string) bool { + for _, subscription := range subscriptions { + if strings.Contains(subscription.Filter, filter) { + return true + } + } + return false + } + + if !contains(subscriptions, subscriptionFilterValue1) || !contains(subscriptions, subscriptionFilterValue2) || !contains(subscriptions, subscriptionFilterValue3) { + return fmt.Errorf("Expected subscriptions to contain %v, %v, %v", subscriptionFilterValue1, subscriptionFilterValue2, subscriptionFilterValue3) + } + + return nil + }, + ), + }, + // Add new subscription, remove one + { + Config: providerConfig + fmt.Sprintf(` + resource "dgservicebus_endpoint" "test" { + endpoint_name = "%v" + topic_name = "bundle-1" + subscriptions = [ + {filter = "%v", filter_type = "sql"}, + {filter = "%v", filter_type = "sql"}, + {filter = "%v", filter_type = "sql"} + ] + + queue_options = { + enable_partitioning = true, + max_size_in_megabytes = 5120, + max_message_size_in_kilobytes = 256 + } + }`, endpoint_name, subscriptionFilterValue3, subscriptionFilterValue2, subscriptionFilterValue4), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("dgservicebus_endpoint.test", "subscriptions.#", "3"), + resource.TestCheckResourceAttr("dgservicebus_endpoint.test", "subscriptions.0.filter", subscriptionFilterValue2), + resource.TestCheckResourceAttr("dgservicebus_endpoint.test", "subscriptions.1.filter", subscriptionFilterValue3), + resource.TestCheckResourceAttr("dgservicebus_endpoint.test", "subscriptions.2.filter", subscriptionFilterValue4), + resource.TestCheckResourceAttr("dgservicebus_endpoint.test", "endpoint_name", endpoint_name), + func(s *terraform.State) error { + subscriptions, _ := client.GetAsbSubscriptionsRules(context.Background(), asb.AsbEndpointModel{ + EndpointName: endpoint_name, + TopicName: "bundle-1", + }) + if len(subscriptions) != 3 { + return fmt.Errorf("Expected 3 subscriptions") + } + contains := func(subscriptions []asb.AsbSubscriptionRule, filter string) bool { + for _, subscription := range subscriptions { + if strings.Contains(subscription.Filter, filter) { + return true + } + } + return false + } + + if !contains(subscriptions, subscriptionFilterValue2) || !contains(subscriptions, subscriptionFilterValue3) || !contains(subscriptions, subscriptionFilterValue4) { + return fmt.Errorf("Expected subscriptions to contain %v, %v, %v", subscriptionFilterValue2, subscriptionFilterValue3, subscriptionFilterValue4) + } + + return nil + }, + ), + }, + }, + }) + + ensure_enpoint_deleted(client, endpoint_name) +} + func TestAcc_EndpointSqlCorrelationUpdate(t *testing.T) { // Init test test resources var client = createClient(t)