diff --git a/lang/functions.go b/lang/functions.go index 1f5ec62ef2..dabffb6693 100644 --- a/lang/functions.go +++ b/lang/functions.go @@ -100,6 +100,7 @@ func (s *Scope) Functions() map[string]function.Function { "rsadecrypt": funcs.RsaDecryptFunc, "setintersection": stdlib.SetIntersectionFunc, "setproduct": funcs.SetProductFunc, + "setsubtract": stdlib.SetSubtractFunc, "setunion": stdlib.SetUnionFunc, "sha1": funcs.Sha1Func, "sha256": funcs.Sha256Func, diff --git a/lang/functions_test.go b/lang/functions_test.go index 09af2aec0b..029cc0c968 100644 --- a/lang/functions_test.go +++ b/lang/functions_test.go @@ -687,6 +687,15 @@ func TestFunctions(t *testing.T) { }, }, + "setsubtract": { + { + `setsubtract(["a", "b", "c"], ["a", "c"])`, + cty.SetVal([]cty.Value{ + cty.StringVal("b"), + }), + }, + }, + "setunion": { { `setunion(["a", "b"], ["b", "c"], ["d"])`, diff --git a/website/docs/configuration/functions/setintersection.html.md b/website/docs/configuration/functions/setintersection.html.md index ef48dc0362..6444aeb8b3 100644 --- a/website/docs/configuration/functions/setintersection.html.md +++ b/website/docs/configuration/functions/setintersection.html.md @@ -40,5 +40,6 @@ the ordering of the given elements is not preserved. a given element value. * [`setproduct`](./setproduct.html) computes the _Cartesian product_ of multiple sets. +* [`setsubtract`](./setsubtract.html) computes the _relative complement_ of two sets * [`setunion`](./setunion.html) computes the _union_ of multiple sets. diff --git a/website/docs/configuration/functions/setproduct.html.md b/website/docs/configuration/functions/setproduct.html.md index 708764813b..6b9f77e5ba 100644 --- a/website/docs/configuration/functions/setproduct.html.md +++ b/website/docs/configuration/functions/setproduct.html.md @@ -224,5 +224,6 @@ elements in the input variables. object types are defined explicitly. * [`setintersection`](./setintersection.html) computes the _intersection_ of multiple sets. +* [`setsubtract`](./setsubtract.html) computes the _relative complement_ of two sets * [`setunion`](./setunion.html) computes the _union_ of multiple sets. diff --git a/website/docs/configuration/functions/setsubtract.html.md b/website/docs/configuration/functions/setsubtract.html.md new file mode 100644 index 0000000000..0bf3b7acc4 --- /dev/null +++ b/website/docs/configuration/functions/setsubtract.html.md @@ -0,0 +1,49 @@ +--- +layout: "functions" +page_title: "setsubtract - Functions - Configuration Language" +sidebar_current: "docs-funcs-collection-setsubtract" +description: |- + The setsubtract function returns a new set containing the elements + from the first set that are not present in the second set +--- + +# `setsubtract` Function + +-> **Note:** This page is about Terraform 0.12 and later. For Terraform 0.11 and +earlier, see +[0.11 Configuration Language: Interpolation Syntax](../../configuration-0-11/interpolation.html). + +The `setsubtract` function returns a new set containing the elements from the first set that are not present in the second set. In other words, it computes the +[relative complement](https://en.wikipedia.org/wiki/Complement_(set_theory)#Relative_complement) of the first set in the second set. + +```hcl +setsubtract(a, b) +``` + +## Examples + +``` +> setsubtract(["a", "b", "c"], ["a", "c"]) +[ + "b", +] +``` + +### Set Difference (Symmetric Difference) + +``` +> setunion(setsubtract(["a", "b", "c"], ["a", "c", "d"]), setsubtract(["a", "c", "d"], ["a", "b", "c"])) +[ + "b", + "d", +] +``` + + +## Related Functions + +* [`setintersection`](./setintersection.html) computes the _intersection_ of multiple sets +* [`setproduct`](./setproduct.html) computes the _Cartesian product_ of multiple + sets. +* [`setunion`](./setunion.html) computes the _union_ of + multiple sets. diff --git a/website/docs/configuration/functions/setunion.html.md b/website/docs/configuration/functions/setunion.html.md index 77e3d92d9d..41103e588c 100644 --- a/website/docs/configuration/functions/setunion.html.md +++ b/website/docs/configuration/functions/setunion.html.md @@ -45,3 +45,4 @@ the ordering of the given elements is not preserved. multiple sets. * [`setproduct`](./setproduct.html) computes the _Cartesian product_ of multiple sets. +* [`setsubtract`](./setsubtract.html) computes the _relative complement_ of two sets diff --git a/website/layouts/functions.erb b/website/layouts/functions.erb index 1c98bd9d63..063347f511 100644 --- a/website/layouts/functions.erb +++ b/website/layouts/functions.erb @@ -222,6 +222,10 @@ setproduct +
  • + setsubtract +
  • +
  • setunion