Skip to content

Commit

Permalink
Add issensitive function
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed Apr 30, 2024
1 parent 2e4997c commit 2f69e5a
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 2 deletions.
22 changes: 22 additions & 0 deletions terraform/lang/funcs/sensitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,32 @@ var NonsensitiveFunc = function.New(&function.Spec{
},
})

var IssensitiveFunc = function.New(&function.Spec{
Params: []function.Parameter{{
Name: "value",
Type: cty.DynamicPseudoType,
AllowUnknown: true,
AllowNull: true,
AllowMarked: true,
AllowDynamicType: true,
}},
Type: func(args []cty.Value) (cty.Type, error) {
return cty.Bool, nil
},
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
s := args[0].HasMark(marks.Sensitive)
return cty.BoolVal(s), nil
},
})

func Sensitive(v cty.Value) (cty.Value, error) {
return SensitiveFunc.Call([]cty.Value{v})
}

func Nonsensitive(v cty.Value) (cty.Value, error) {
return NonsensitiveFunc.Call([]cty.Value{v})
}

func Issensitive(v cty.Value) (cty.Value, error) {
return IssensitiveFunc.Call([]cty.Value{v})
}
72 changes: 72 additions & 0 deletions terraform/lang/funcs/sensitive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,75 @@ func TestNonsensitive(t *testing.T) {
})
}
}

func TestIssensitive(t *testing.T) {
tests := []struct {
Input cty.Value
Sensitive bool
WantErr string
}{
{
cty.NumberIntVal(1).Mark(marks.Sensitive),
true,
``,
},
{
cty.NumberIntVal(1),
false,
``,
},
{
cty.DynamicVal.Mark(marks.Sensitive),
true,
``,
},
{
cty.UnknownVal(cty.String).Mark(marks.Sensitive),
true,
``,
},
{
cty.NullVal(cty.EmptyObject).Mark(marks.Sensitive),
true,
``,
},
{
cty.NullVal(cty.String),
false,
``,
},
{
cty.DynamicVal,
false,
``,
},
{
cty.UnknownVal(cty.String),
false,
``,
},
}

for _, test := range tests {
t.Run(fmt.Sprintf("issensitive(%#v)", test.Input), func(t *testing.T) {
got, err := Issensitive(test.Input)

if test.WantErr != "" {
if err == nil {
t.Fatal("succeeded; want error")
}
if got, want := err.Error(), test.WantErr; got != want {
t.Fatalf("wrong error\ngot: %s\nwant: %s", got, want)
}
return
} else if err != nil {
t.Fatalf("unexpected error: %s", err)
}

if (got.True() && !test.Sensitive) || (got.False() && test.Sensitive) {
t.Errorf("wrong result \ngot: %#v\nwant: %#v", got, test.Sensitive)
}
})
}

}
3 changes: 2 additions & 1 deletion terraform/lang/functions.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
// SPDX-License-Identifier: BUSL-1.1

package lang

Expand Down Expand Up @@ -106,6 +106,7 @@ func (s *Scope) Functions() map[string]function.Function {
"rsadecrypt": funcs.RsaDecryptFunc,
"sensitive": funcs.SensitiveFunc,
"nonsensitive": funcs.NonsensitiveFunc,
"issensitive": funcs.IssensitiveFunc,
"setintersection": stdlib.SetIntersectionFunc,
"setproduct": stdlib.SetProductFunc,
"setsubtract": stdlib.SetSubtractFunc,
Expand Down
9 changes: 8 additions & 1 deletion terraform/lang/functions_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
// SPDX-License-Identifier: BUSL-1.1

package lang

Expand Down Expand Up @@ -480,6 +480,13 @@ func TestFunctions(t *testing.T) {
},
},

"issensitive": {
{
`issensitive(1)`,
cty.False,
},
},

"join": {
{
`join(" ", ["Hello", "World"])`,
Expand Down

0 comments on commit 2f69e5a

Please sign in to comment.