Skip to content

Commit

Permalink
Update project and location from id functions to use errors
Browse files Browse the repository at this point in the history
  • Loading branch information
SarahFrench committed Mar 12, 2024
1 parent b56708e commit d17f21d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 48 deletions.
11 changes: 5 additions & 6 deletions mmv1/third_party/terraform/functions/location_from_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ func (f LocationFromIdFunction) Definition(ctx context.Context, req function.Def
func (f LocationFromIdFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
// Load arguments from function call
var arg0 string
resp.Diagnostics.Append(req.Arguments.GetArgument(ctx, 0, &arg0)...)

if resp.Diagnostics.HasError() {
resp.Error = function.ConcatFuncErrors(req.Arguments.GetArgument(ctx, 0, &arg0))
if resp.Error != nil {
return
}

Expand All @@ -48,12 +47,12 @@ func (f LocationFromIdFunction) Run(ctx context.Context, req function.RunRequest
pattern := "locations/{location}/" // Human-readable pseudo-regex pattern used in errors and warnings

// Validate input
ValidateElementFromIdArguments(arg0, regex, pattern, resp)
if resp.Diagnostics.HasError() {
resp.Error = function.ConcatFuncErrors(ValidateElementFromIdArguments(ctx, arg0, regex, pattern))
if resp.Error != nil {
return
}

// Get and return element from input string
location := GetElementFromId(arg0, regex, template)
resp.Diagnostics.Append(resp.Result.Set(ctx, location)...)
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, location))
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/function"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
Expand Down Expand Up @@ -55,19 +54,12 @@ func TestFunctionRun_location_from_id(t *testing.T) {
Result: function.NewResultData(types.StringValue(location)),
},
},
"it returns a warning and the first submatch when given repetitive input": {
"it returns the first submatch (with no error) when given repetitive input": {
request: function.RunRequest{
Arguments: function.NewArgumentsData([]attr.Value{types.StringValue(repetitiveInput)}),
},
expected: function.RunResponse{
Result: function.NewResultData(types.StringValue(location)),
Diagnostics: diag.Diagnostics{
diag.NewArgumentWarningDiagnostic(
0,
ambiguousMatchesWarningSummary,
fmt.Sprintf("The input string \"%s\" contains more than one match for the pattern \"locations/{location}/\". Terraform will use the first found match.", repetitiveInput),
),
},
},
},
"it returns an error when given input with no submatches": {
Expand All @@ -76,13 +68,7 @@ func TestFunctionRun_location_from_id(t *testing.T) {
},
expected: function.RunResponse{
Result: function.NewResultData(types.StringNull()),
Diagnostics: diag.Diagnostics{
diag.NewArgumentErrorDiagnostic(
0,
noMatchesErrorSummary,
fmt.Sprintf("The input string \"%s\" doesn't contain the expected pattern \"locations/{location}/\".", invalidInput),
),
},
Error: function.NewArgumentFuncError(0, fmt.Sprintf("The input string \"%s\" doesn't contain the expected pattern \"locations/{location}/\".", invalidInput)),
},
},
}
Expand All @@ -105,8 +91,8 @@ func TestFunctionRun_location_from_id(t *testing.T) {
if diff := cmp.Diff(got.Result, tc.expected.Result); diff != "" {
t.Errorf("unexpected diff between expected and received result: %s", diff)
}
if diff := cmp.Diff(got.Diagnostics, tc.expected.Diagnostics); diff != "" {
t.Errorf("unexpected diff between expected and received diagnostics: %s", diff)
if diff := cmp.Diff(got.Error, tc.expected.Error); diff != "" {
t.Errorf("unexpected diff between expected and received errors: %s", diff)
}
})
}
Expand Down
11 changes: 5 additions & 6 deletions mmv1/third_party/terraform/functions/project_from_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ func (f ProjectFromIdFunction) Definition(ctx context.Context, req function.Defi
func (f ProjectFromIdFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
// Load arguments from function call
var arg0 string
resp.Diagnostics.Append(req.Arguments.GetArgument(ctx, 0, &arg0)...)

if resp.Diagnostics.HasError() {
resp.Error = function.ConcatFuncErrors(req.Arguments.GetArgument(ctx, 0, &arg0))
if resp.Error != nil {
return
}

Expand All @@ -48,12 +47,12 @@ func (f ProjectFromIdFunction) Run(ctx context.Context, req function.RunRequest,
pattern := "projects/{project}/" // Human-readable pseudo-regex pattern used in errors and warnings

// Validate input
ValidateElementFromIdArguments(arg0, regex, pattern, resp)
if resp.Diagnostics.HasError() {
resp.Error = function.ConcatFuncErrors(ValidateElementFromIdArguments(ctx, arg0, regex, pattern))
if resp.Error != nil {
return
}

// Get and return element from input string
projectId := GetElementFromId(arg0, regex, template)
resp.Diagnostics.Append(resp.Result.Set(ctx, projectId)...)
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, projectId))
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/function"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
Expand Down Expand Up @@ -55,19 +54,12 @@ func TestFunctionRun_project_from_id(t *testing.T) {
Result: function.NewResultData(types.StringValue(projectId)),
},
},
"it returns a warning and the first submatch when given repetitive input": {
"it returns the first submatch (with no error) when given repetitive input": {
request: function.RunRequest{
Arguments: function.NewArgumentsData([]attr.Value{types.StringValue(repetitiveInput)}),
},
expected: function.RunResponse{
Result: function.NewResultData(types.StringValue(projectId)),
Diagnostics: diag.Diagnostics{
diag.NewArgumentWarningDiagnostic(
0,
ambiguousMatchesWarningSummary,
fmt.Sprintf("The input string \"%s\" contains more than one match for the pattern \"projects/{project}/\". Terraform will use the first found match.", repetitiveInput),
),
},
},
},
"it returns an error when given input with no submatches": {
Expand All @@ -76,13 +68,9 @@ func TestFunctionRun_project_from_id(t *testing.T) {
},
expected: function.RunResponse{
Result: function.NewResultData(types.StringNull()),
Diagnostics: diag.Diagnostics{
diag.NewArgumentErrorDiagnostic(
0,
noMatchesErrorSummary,
fmt.Sprintf("The input string \"%s\" doesn't contain the expected pattern \"projects/{project}/\".", invalidInput),
),
},
Error: function.NewArgumentFuncError(
0,
fmt.Sprintf("The input string \"%s\" doesn't contain the expected pattern \"projects/{project}/\".", invalidInput)),
},
},
}
Expand All @@ -105,8 +93,8 @@ func TestFunctionRun_project_from_id(t *testing.T) {
if diff := cmp.Diff(got.Result, tc.expected.Result); diff != "" {
t.Errorf("unexpected diff between expected and received result: %s", diff)
}
if diff := cmp.Diff(got.Diagnostics, tc.expected.Diagnostics); diff != "" {
t.Errorf("unexpected diff between expected and received diagnostics: %s", diff)
if diff := cmp.Diff(got.Error, tc.expected.Error); diff != "" {
t.Errorf("unexpected diff between expected and received errors: %s", diff)
}
})
}
Expand Down

0 comments on commit d17f21d

Please sign in to comment.