diff --git a/internal/lang/funcs/cidr.go b/internal/lang/funcs/cidr.go index 604cd43cbc0f..28b636a24afd 100644 --- a/internal/lang/funcs/cidr.go +++ b/internal/lang/funcs/cidr.go @@ -4,7 +4,6 @@ package funcs import ( - "encoding/json" "fmt" "math/big" "sort" @@ -24,7 +23,7 @@ var CidrCollapseFunc = function.New(&function.Spec{ Type: cty.List(cty.String), }, }, - Type: function.StaticReturnType(cty.String), + Type: function.StaticReturnType(cty.List(cty.String)), RefineResult: refineNotNull, Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) { var cidrsInput []string @@ -55,18 +54,15 @@ var CidrCollapseFunc = function.New(&function.Spec{ } } - var collapsedCidrs []string + retVals := []cty.Value{} for _, net := range cidrs { - collapsedCidrs = append(collapsedCidrs, net.String()) + retVals = append(retVals, cty.StringVal(net.String())) } - sort.Strings(collapsedCidrs) + sort.Slice(retVals, func(i, j int) bool { + return i > j + }) - val, err := json.Marshal(collapsedCidrs) - if err != nil { - return cty.UnknownVal(cty.String), err - } - - return cty.StringVal(string(val)), nil + return cty.ListVal(retVals), nil }, }) diff --git a/internal/lang/funcs/cidr_test.go b/internal/lang/funcs/cidr_test.go index 3d8274fee9c5..7bbb737b31f9 100644 --- a/internal/lang/funcs/cidr_test.go +++ b/internal/lang/funcs/cidr_test.go @@ -409,8 +409,10 @@ func TestCidrCollapse(t *testing.T) { cty.StringVal("192.168.0.56/32"), cty.StringVal("192.0.0.0/8"), }), - Want: cty.StringVal("[\"192.0.0.0/8\"]"), - Err: "", + Want: cty.ListVal([]cty.Value{ + cty.StringVal("192.0.0.0/8"), + }), + Err: "", }, { Cidrs: cty.ListVal([]cty.Value{ @@ -418,8 +420,11 @@ func TestCidrCollapse(t *testing.T) { cty.StringVal("167.123.0.42/32"), cty.StringVal("167.123.0.0/16"), }), - Want: cty.StringVal("[\"167.123.0.0/16\",\"192.168.0.0/16\"]"), - Err: "", + Want: cty.ListVal([]cty.Value{ + cty.StringVal("167.123.0.0/16"), + cty.StringVal("192.168.0.0/16"), + }), + Err: "", }, } diff --git a/website/data/language-nav-data.json b/website/data/language-nav-data.json index d96dad4d5bcf..1503a7c5d70b 100644 --- a/website/data/language-nav-data.json +++ b/website/data/language-nav-data.json @@ -690,6 +690,10 @@ { "title": "cidrsubnets", "href": "/language/functions/cidrsubnets" + }, + { + "title": "cidrcollapse", + "href": "/language/functions/cidrcollapse" } ] }, diff --git a/website/docs/language/functions/cidrcollapse.mdx b/website/docs/language/functions/cidrcollapse.mdx index 7353fe41c458..7df4aa819822 100644 --- a/website/docs/language/functions/cidrcollapse.mdx +++ b/website/docs/language/functions/cidrcollapse.mdx @@ -2,7 +2,7 @@ page_title: cidrcollapse - Functions - Configuration Language description: |- The cidrcollapse function takes a list of CIDR ranges and collapses them into a - json encoded string of superset ranges + list of strings of superset ranges --- # `cidrcollapse` Function @@ -16,11 +16,11 @@ cidrcollapse(cidrs) `cidrs` must be given as a list of valid CIDR ranges. -The result is a JSON encoded string value of superset ranges. +The result is a string list value of superset ranges. ## Examples ``` > cidrcollapse(["192.168.0.0/16", "167.123.0.42/32", "167.123.0.0/16"]) -"[\"167.123.0.0/16\",\"192.168.0.0/16\"]" +["167.123.0.0/16","192.168.0.0/16"] ``` \ No newline at end of file