From afcc5821297a104274c5bc7f744814b346a98db1 Mon Sep 17 00:00:00 2001 From: Chris Marget Date: Thu, 29 Aug 2024 14:17:35 -0400 Subject: [PATCH] update `stringSliceOrNull()` helper function to support generic strings --- ...ity_template_interface_integration_test.go | 2 +- ...vity_template_loopback_integration_test.go | 2 +- ...r_connectivity_template_primitives_test.go | 2 +- ...nectivity_template_svi_integration_test.go | 2 +- ...tivity_template_system_integration_test.go | 2 +- ...ource_datacenter_device_allocation_test.go | 2 +- ...atacenter_routing_zone_integration_test.go | 6 +- ...eform_allocation_group_integration_test.go | 2 +- ...eeform_config_template_integration_test.go | 2 +- ...resource_freeform_link_integration_test.go | 2 +- ...ource_freeform_resource_integraion_test.go | 2 +- ...source_freeform_system_integration_test.go | 2 +- apstra/test_helpers_test.go | 64 ++++++++++++++++++- 13 files changed, 77 insertions(+), 15 deletions(-) diff --git a/apstra/resource_datacenter_connectivity_template_interface_integration_test.go b/apstra/resource_datacenter_connectivity_template_interface_integration_test.go index 3329a730..5291d157 100644 --- a/apstra/resource_datacenter_connectivity_template_interface_integration_test.go +++ b/apstra/resource_datacenter_connectivity_template_interface_integration_test.go @@ -88,7 +88,7 @@ func (o resourceDataCenterConnectivityTemplateInterface) render(rType, rName str o.blueprintId, o.name, stringOrNull(o.description), - stringSetOrNull(o.tags), + stringSliceOrNull(o.tags), ipLinks, routingZoneConstraints, virtualNetworkMultiples, diff --git a/apstra/resource_datacenter_connectivity_template_loopback_integration_test.go b/apstra/resource_datacenter_connectivity_template_loopback_integration_test.go index 30f0032a..fc245efa 100644 --- a/apstra/resource_datacenter_connectivity_template_loopback_integration_test.go +++ b/apstra/resource_datacenter_connectivity_template_loopback_integration_test.go @@ -51,7 +51,7 @@ func (o resourceDataCenterConnectivityTemplateLoopback) render(rType, rName stri o.blueprintId, o.name, stringOrNull(o.description), - stringSetOrNull(o.tags), + stringSliceOrNull(o.tags), bgpPeeringIpEndoints, ) } diff --git a/apstra/resource_datacenter_connectivity_template_primitives_test.go b/apstra/resource_datacenter_connectivity_template_primitives_test.go index 33c954f7..42106c6f 100644 --- a/apstra/resource_datacenter_connectivity_template_primitives_test.go +++ b/apstra/resource_datacenter_connectivity_template_primitives_test.go @@ -709,7 +709,7 @@ func (o resourceDataCenterConnectivityTemplatePrimitiveVirtualNetworkMultiple) r fmt.Sprintf(resourceDataCenterConnectivityTemplatePrimitiveVirtualNetworkMultipleHCL, o.name, stringOrNull(o.untaggedVnId), - stringSetOrNull(o.taggedVnIds), + stringSliceOrNull(o.taggedVnIds), ), ) } diff --git a/apstra/resource_datacenter_connectivity_template_svi_integration_test.go b/apstra/resource_datacenter_connectivity_template_svi_integration_test.go index b8a2ea7e..10b743b2 100644 --- a/apstra/resource_datacenter_connectivity_template_svi_integration_test.go +++ b/apstra/resource_datacenter_connectivity_template_svi_integration_test.go @@ -64,7 +64,7 @@ func (o resourceDataCenterConnectivityTemplateSvi) render(rType, rName string) s o.blueprintId, o.name, stringOrNull(o.description), - stringSetOrNull(o.tags), + stringSliceOrNull(o.tags), bgpPeeringIpEndoints, dynamicBgpPeerings, ) diff --git a/apstra/resource_datacenter_connectivity_template_system_integration_test.go b/apstra/resource_datacenter_connectivity_template_system_integration_test.go index b4c8a221..db3c520c 100644 --- a/apstra/resource_datacenter_connectivity_template_system_integration_test.go +++ b/apstra/resource_datacenter_connectivity_template_system_integration_test.go @@ -51,7 +51,7 @@ func (o resourceDataCenterConnectivityTemplateSystem) render(rType, rName string o.blueprintId, o.name, stringOrNull(o.description), - stringSetOrNull(o.tags), + stringSliceOrNull(o.tags), customStaticRoutes, ) } diff --git a/apstra/resource_datacenter_device_allocation_test.go b/apstra/resource_datacenter_device_allocation_test.go index 0d7797a0..1dabeced 100644 --- a/apstra/resource_datacenter_device_allocation_test.go +++ b/apstra/resource_datacenter_device_allocation_test.go @@ -88,7 +88,7 @@ func TestResourceDatacenterDeviceAllocation(t *testing.T) { cidrOrNull(in.loopbackIpv4), cidrOrNull(in.loopbackIpv6), stringOrNull(in.deployMode), - stringSetOrNull(in.tags), + stringSliceOrNull(in.tags), ) } diff --git a/apstra/resource_datacenter_routing_zone_integration_test.go b/apstra/resource_datacenter_routing_zone_integration_test.go index 22a6c302..f50918c5 100644 --- a/apstra/resource_datacenter_routing_zone_integration_test.go +++ b/apstra/resource_datacenter_routing_zone_integration_test.go @@ -53,10 +53,10 @@ func (o testRoutingZone) render(bpId apstra.ObjectId, rType, rName string) strin intPtrOrNull(o.vlan), intPtrOrNull(o.vni), - stringSetOrNull(o.dhcpServers), + stringSliceOrNull(o.dhcpServers), stringOrNull(o.routingPolicy), - stringSetOrNull(o.importRTs), - stringSetOrNull(o.exportRTs), + stringSliceOrNull(o.importRTs), + stringSliceOrNull(o.exportRTs), stringOrNull(o.irbMode), ) } diff --git a/apstra/resource_freeform_allocation_group_integration_test.go b/apstra/resource_freeform_allocation_group_integration_test.go index 07ca8265..91326a1b 100644 --- a/apstra/resource_freeform_allocation_group_integration_test.go +++ b/apstra/resource_freeform_allocation_group_integration_test.go @@ -42,7 +42,7 @@ func (o resourceAllocGroup) render(rType, rName string) string { o.blueprintId, o.name, utils.StringersToFriendlyString(o.groupType), - stringSetOrNull(o.poolIds), + stringSliceOrNull(o.poolIds), ) } diff --git a/apstra/resource_freeform_config_template_integration_test.go b/apstra/resource_freeform_config_template_integration_test.go index 220baeae..9ff209eb 100644 --- a/apstra/resource_freeform_config_template_integration_test.go +++ b/apstra/resource_freeform_config_template_integration_test.go @@ -40,7 +40,7 @@ func (o resourceFreeformConfigTemplate) render(rType, rName string) string { o.blueprintId, o.name, o.text, - stringSetOrNull(o.tags), + stringSliceOrNull(o.tags), ) } diff --git a/apstra/resource_freeform_link_integration_test.go b/apstra/resource_freeform_link_integration_test.go index eb7f5ba5..b48d7888 100644 --- a/apstra/resource_freeform_link_integration_test.go +++ b/apstra/resource_freeform_link_integration_test.go @@ -57,7 +57,7 @@ func (o resourceFreeformLink) render(rType, rName string) string { rType, rName, o.blueprintId, o.name, - stringSetOrNull(o.tags), + stringSliceOrNull(o.tags), o.endpoints[0].SystemId, stringPtrOrNull(o.endpoints[0].Interface.Data.IfName), intPtrOrNull(o.endpoints[0].Interface.Data.TransformationId), diff --git a/apstra/resource_freeform_resource_integraion_test.go b/apstra/resource_freeform_resource_integraion_test.go index 86641854..7d442e4a 100644 --- a/apstra/resource_freeform_resource_integraion_test.go +++ b/apstra/resource_freeform_resource_integraion_test.go @@ -59,7 +59,7 @@ func (o resourceFreeformResource) render(rType, rName string) string { ipNetOrNull(o.ipv4Value), ipNetOrNull(o.ipv6Value), stringOrNull(o.allocatedFrom.String()), - stringSetOrNull(o.assignedTo), + stringSliceOrNull(o.assignedTo), ) } diff --git a/apstra/resource_freeform_system_integration_test.go b/apstra/resource_freeform_system_integration_test.go index 2f15e9a4..3fe9d5cc 100644 --- a/apstra/resource_freeform_system_integration_test.go +++ b/apstra/resource_freeform_system_integration_test.go @@ -50,7 +50,7 @@ func (o resourceFreeformSystem) render(rType, rName string) string { o.hostname, o.systemType, stringOrNull(o.deployMode), - stringSetOrNull(o.tags), + stringSliceOrNull(o.tags), ) } diff --git a/apstra/test_helpers_test.go b/apstra/test_helpers_test.go index dd3ea2de..a66baaa6 100644 --- a/apstra/test_helpers_test.go +++ b/apstra/test_helpers_test.go @@ -138,7 +138,7 @@ func intPtrOrNull[A constraints.Integer](in *A) string { return fmt.Sprintf("%d", *in) } -func stringSetOrNull(in []string) string { +func stringSliceOrNull[S ~string](in []S) string { if len(in) == 0 { return "null" } @@ -407,6 +407,68 @@ func padFormatStr(n, base int) (string, error) { return fmt.Sprintf("%%%d%s", c, baseChar), nil } +func TestStringSliceOrNull(t *testing.T) { + type stringTestCase struct { + s []string + e string + } + + stringTestCases := map[string]stringTestCase{ + "with_strings": { + s: []string{"a", "b", "c"}, + e: `[ "a", "b", "c" ]`, + }, + "empty": { + s: []string{}, + e: "null", + }, + "nil": { + s: nil, + e: "null", + }, + } + + for tName, tCase := range stringTestCases { + t.Run("string_"+tName, func(t *testing.T) { + t.Parallel() + r := stringSliceOrNull(tCase.s) + if tCase.e != r { + t.Fatalf("expected %q, got %q", tCase.e, r) + } + }) + } + + type idTestCase struct { + s []apstra.ObjectId + e string + } + + idTestCases := map[string]idTestCase{ + "with_strings": { + s: []apstra.ObjectId{"a", "b", "c"}, + e: `[ "a", "b", "c" ]`, + }, + "empty": { + s: []apstra.ObjectId{}, + e: "null", + }, + "nil": { + s: nil, + e: "null", + }, + } + + for tName, tCase := range idTestCases { + t.Run("ObjectId_"+tName, func(t *testing.T) { + t.Parallel() + r := stringSliceOrNull(tCase.s) + if tCase.e != r { + t.Fatalf("expected %q, got %q", tCase.e, r) + } + }) + } +} + func TestPadFormatStr(t *testing.T) { type testCase struct { n int