-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add caseinsensitive plan modifier for metro attribute
- Loading branch information
1 parent
7bf7d5c
commit 50a2923
Showing
12 changed files
with
177 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package planmodifiers | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func CaseInsensitiveString() planmodifier.String { | ||
return &caseInsensitivePlanModifier{} | ||
} | ||
|
||
type caseInsensitivePlanModifier struct{} | ||
|
||
func (d *caseInsensitivePlanModifier) PlanModifyString(ctx context.Context, request planmodifier.StringRequest, response *planmodifier.StringResponse) { | ||
oldValue := request.StateValue.ValueString() | ||
newValue := request.PlanValue.ValueString() | ||
|
||
result := oldValue | ||
if !strings.EqualFold(strings.ToLower(newValue), strings.ToLower(oldValue)) { | ||
result = newValue | ||
} | ||
|
||
response.PlanValue = types.StringValue(result) | ||
} | ||
|
||
func (d *caseInsensitivePlanModifier) Description(ctx context.Context) string { | ||
return "For same string but different cases, does not trigger diffs in the plan" | ||
} | ||
|
||
func (d *caseInsensitivePlanModifier) MarkdownDescription(ctx context.Context) string { | ||
return d.Description(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package planmodifiers | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func TestCaseInsensitiveSet(t *testing.T) { | ||
testCases := []struct { | ||
Old, New, Expected string | ||
}{ | ||
{ | ||
Old: "foo", | ||
New: "foo", | ||
Expected: "foo", | ||
}, | ||
{ | ||
Old: "Bar", | ||
New: "bar", | ||
Expected: "Bar", | ||
}, | ||
{ | ||
Old: "foo", | ||
New: "fOO", | ||
Expected: "foo", | ||
}, | ||
} | ||
|
||
testPlanModifier := CaseInsensitiveString() | ||
|
||
for i, testCase := range testCases { | ||
stateValue := types.StringValue(testCase.Old) | ||
planValue := types.StringValue(testCase.New) | ||
expectedValue := types.StringValue(testCase.Expected) | ||
|
||
req := planmodifier.StringRequest{ | ||
StateValue: stateValue, | ||
PlanValue: planValue, | ||
} | ||
|
||
var resp planmodifier.StringResponse | ||
|
||
testPlanModifier.PlanModifyString(context.Background(), req, &resp) | ||
|
||
if resp.Diagnostics.HasError() { | ||
t.Fatalf("%d: got error modifying plan: %v", i, resp.Diagnostics.Errors()) | ||
} | ||
|
||
if !resp.PlanValue.Equal(expectedValue) { | ||
t.Fatalf("%d: output plan value does not equal expected plan value", i) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...esources/metal/vlans/datasource_schema.go → ...resources/metal/vlan/datasource_schema.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package vlans | ||
package vlan | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters