Skip to content

Commit

Permalink
Improve logging in X_from_id functions when multiple regex matches ar…
Browse files Browse the repository at this point in the history
…e found
  • Loading branch information
SarahFrench committed Mar 13, 2024
1 parent b8d019c commit 3dd88b4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
4 changes: 2 additions & 2 deletions mmv1/third_party/terraform/functions/element_from_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// ValidateElementFromIdArguments is reusable validation logic used in provider-defined functions that use the GetElementFromId function
func ValidateElementFromIdArguments(ctx context.Context, input string, regex *regexp.Regexp, pattern string) *function.FuncError {
func ValidateElementFromIdArguments(ctx context.Context, input string, regex *regexp.Regexp, pattern string, functionName string) *function.FuncError {
submatches := regex.FindAllStringSubmatchIndex(input, -1)

// Zero matches means unusable input; error returned
Expand All @@ -20,7 +20,7 @@ func ValidateElementFromIdArguments(ctx context.Context, input string, regex *re

// >1 matches means input usable but not ideal; debug log
if len(submatches) > 1 {
tflog.Debug(ctx, fmt.Sprintf("The input string \"%s\" contains more than one match for the pattern \"%s\" expected by the provider function. Terraform will use the first found match.", input, pattern))
tflog.Debug(ctx, fmt.Sprintf("Provider-defined function %s was called with input string: %s. This contains more than one match for the pattern %s. Terraform will use the first found match.", functionName, input, pattern))
}

return nil
Expand Down
12 changes: 8 additions & 4 deletions mmv1/third_party/terraform/functions/location_from_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import (
var _ function.Function = LocationFromIdFunction{}

func NewLocationFromIdFunction() function.Function {
return &LocationFromIdFunction{}
return &LocationFromIdFunction{
name: "location_from_id",
}
}

type LocationFromIdFunction struct{}
type LocationFromIdFunction struct {
name string // Makes function name available in Run logic for logging purposes
}

func (f LocationFromIdFunction) Metadata(ctx context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
resp.Name = "location_from_id"
resp.Name = f.name
}

func (f LocationFromIdFunction) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) {
Expand Down Expand Up @@ -47,7 +51,7 @@ 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
resp.Error = function.ConcatFuncErrors(ValidateElementFromIdArguments(ctx, arg0, regex, pattern))
resp.Error = function.ConcatFuncErrors(ValidateElementFromIdArguments(ctx, arg0, regex, pattern, f.name))
if resp.Error != nil {
return
}
Expand Down
12 changes: 8 additions & 4 deletions mmv1/third_party/terraform/functions/project_from_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import (
var _ function.Function = ProjectFromIdFunction{}

func NewProjectFromIdFunction() function.Function {
return &ProjectFromIdFunction{}
return &ProjectFromIdFunction{
name: "project_from_id",
}
}

type ProjectFromIdFunction struct{}
type ProjectFromIdFunction struct {
name string // Makes function name available in Run logic for logging purposes
}

func (f ProjectFromIdFunction) Metadata(ctx context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
resp.Name = "project_from_id"
resp.Name = f.name
}

func (f ProjectFromIdFunction) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) {
Expand Down Expand Up @@ -47,7 +51,7 @@ 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
resp.Error = function.ConcatFuncErrors(ValidateElementFromIdArguments(ctx, arg0, regex, pattern))
resp.Error = function.ConcatFuncErrors(ValidateElementFromIdArguments(ctx, arg0, regex, pattern, f.name))
if resp.Error != nil {
return
}
Expand Down
12 changes: 8 additions & 4 deletions mmv1/third_party/terraform/functions/region_from_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import (
var _ function.Function = RegionFromIdFunction{}

func NewRegionFromIdFunction() function.Function {
return &RegionFromIdFunction{}
return &RegionFromIdFunction{
name: "region_from_id",
}
}

type RegionFromIdFunction struct{}
type RegionFromIdFunction struct {
name string // Makes function name available in Run logic for logging purposes
}

func (f RegionFromIdFunction) Metadata(ctx context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
resp.Name = "region_from_id"
resp.Name = f.name
}

func (f RegionFromIdFunction) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) {
Expand Down Expand Up @@ -47,7 +51,7 @@ func (f RegionFromIdFunction) Run(ctx context.Context, req function.RunRequest,
pattern := "regions/{region}/" // Human-readable pseudo-regex pattern used in errors and warnings

// Validate input
resp.Error = function.ConcatFuncErrors(ValidateElementFromIdArguments(ctx, arg0, regex, pattern))
resp.Error = function.ConcatFuncErrors(ValidateElementFromIdArguments(ctx, arg0, regex, pattern, f.name))
if resp.Error != nil {
return
}
Expand Down
12 changes: 8 additions & 4 deletions mmv1/third_party/terraform/functions/zone_from_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import (
var _ function.Function = ZoneFromIdFunction{}

func NewZoneFromIdFunction() function.Function {
return &ZoneFromIdFunction{}
return &ZoneFromIdFunction{
name: "zone_from_id",
}
}

type ZoneFromIdFunction struct{}
type ZoneFromIdFunction struct {
name string // Makes function name available in Run logic for logging purposes
}

func (f ZoneFromIdFunction) Metadata(ctx context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
resp.Name = "zone_from_id"
resp.Name = f.name
}

func (f ZoneFromIdFunction) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) {
Expand Down Expand Up @@ -47,7 +51,7 @@ func (f ZoneFromIdFunction) Run(ctx context.Context, req function.RunRequest, re
pattern := "zones/{zone}/" // Human-readable pseudo-regex pattern used in errors and warnings

// Validate input
resp.Error = function.ConcatFuncErrors(ValidateElementFromIdArguments(ctx, arg0, regex, pattern))
resp.Error = function.ConcatFuncErrors(ValidateElementFromIdArguments(ctx, arg0, regex, pattern, f.name))
if resp.Error != nil {
return
}
Expand Down

0 comments on commit 3dd88b4

Please sign in to comment.