Skip to content

Commit eaef718

Browse files
committed
Update name_from_id function to use errors & use new logging approach
1 parent 725f362 commit eaef718

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

mmv1/third_party/terraform/functions/name_from_id.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ import (
1010
var _ function.Function = NameFromIdFunction{}
1111

1212
func NewNameFromIdFunction() function.Function {
13-
return &NameFromIdFunction{}
13+
return &NameFromIdFunction{
14+
name: "name_from_id",
15+
}
1416
}
1517

16-
type NameFromIdFunction struct{}
18+
type NameFromIdFunction struct {
19+
name string
20+
}
1721

1822
func (f NameFromIdFunction) Metadata(ctx context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
19-
resp.Name = "name_from_id"
23+
resp.Name = f.name
2024
}
2125

2226
func (f NameFromIdFunction) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) {
@@ -36,9 +40,8 @@ func (f NameFromIdFunction) Definition(ctx context.Context, req function.Definit
3640
func (f NameFromIdFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
3741
// Load arguments from function call
3842
var arg0 string
39-
resp.Diagnostics.Append(req.Arguments.GetArgument(ctx, 0, &arg0)...)
40-
41-
if resp.Diagnostics.HasError() {
43+
resp.Error = function.ConcatFuncErrors(req.Arguments.GetArgument(ctx, 0, &arg0))
44+
if resp.Error != nil {
4245
return
4346
}
4447

@@ -48,12 +51,12 @@ func (f NameFromIdFunction) Run(ctx context.Context, req function.RunRequest, re
4851
pattern := "resourceType/{name}$" // Human-readable pseudo-regex pattern used in errors and warnings
4952

5053
// Validate input
51-
ValidateElementFromIdArguments(arg0, regex, pattern, resp)
52-
if resp.Diagnostics.HasError() {
54+
resp.Error = function.ConcatFuncErrors(ValidateElementFromIdArguments(ctx, arg0, regex, pattern, f.name))
55+
if resp.Error != nil {
5356
return
5457
}
5558

5659
// Get and return element from input string
5760
name := GetElementFromId(arg0, regex, template)
58-
resp.Diagnostics.Append(resp.Result.Set(ctx, name)...)
61+
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, name))
5962
}

mmv1/third_party/terraform/functions/name_from_id_internal_test.go

+3-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/google/go-cmp/cmp"
99
"github.com/hashicorp/terraform-plugin-framework/attr"
10-
"github.com/hashicorp/terraform-plugin-framework/diag"
1110
"github.com/hashicorp/terraform-plugin-framework/function"
1211
"github.com/hashicorp/terraform-plugin-framework/types"
1312
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
@@ -60,13 +59,7 @@ func TestFunctionRun_name_from_id(t *testing.T) {
6059
},
6160
expected: function.RunResponse{
6261
Result: function.NewResultData(types.StringNull()),
63-
Diagnostics: diag.Diagnostics{
64-
diag.NewArgumentErrorDiagnostic(
65-
0,
66-
noMatchesErrorSummary,
67-
fmt.Sprintf("The input string \"%s\" doesn't contain the expected pattern \"resourceType/{name}$\".", invalidInput),
68-
),
69-
},
62+
Error: function.NewArgumentFuncError(0, fmt.Sprintf("The input string \"%s\" doesn't contain the expected pattern \"resourceType/{name}$\".", invalidInput)),
7063
},
7164
},
7265
}
@@ -89,8 +82,8 @@ func TestFunctionRun_name_from_id(t *testing.T) {
8982
if diff := cmp.Diff(got.Result, tc.expected.Result); diff != "" {
9083
t.Errorf("unexpected diff between expected and received result: %s", diff)
9184
}
92-
if diff := cmp.Diff(got.Diagnostics, tc.expected.Diagnostics); diff != "" {
93-
t.Errorf("unexpected diff between expected and received diagnostics: %s", diff)
85+
if diff := cmp.Diff(got.Error, tc.expected.Error); diff != "" {
86+
t.Errorf("unexpected diff between expected and received errors: %s", diff)
9487
}
9588
})
9689
}

0 commit comments

Comments
 (0)