-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
153 additions
and
0 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,45 @@ | ||
package modifiers | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
type setTrimSuffix struct { | ||
} | ||
|
||
func SetTrimSuffixValue() planmodifier.Set { | ||
return setTrimSuffix{} | ||
} | ||
|
||
func (m setTrimSuffix) Description(context.Context) string { | ||
return "Trim suffix from configuration value when comparing with state" | ||
} | ||
|
||
func (m setTrimSuffix) MarkdownDescription(ctx context.Context) string { | ||
return m.Description(ctx) | ||
} | ||
|
||
func (m setTrimSuffix) PlanModifySet(ctx context.Context, req planmodifier.SetRequest, resp *planmodifier.SetResponse) { | ||
if req.ConfigValue.IsNull() || req.PlanValue.IsUnknown() || req.PlanValue.IsNull() { | ||
return | ||
} | ||
|
||
var planValue []string | ||
resp.Diagnostics.Append(req.PlanValue.ElementsAs(ctx, &planValue, false)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
for i, element := range planValue { | ||
planValue[i] = strings.TrimSuffix(element, ".") | ||
} | ||
|
||
serializedPlanValue, diags := types.SetValueFrom(ctx, types.StringType, planValue) | ||
resp.Diagnostics.Append(diags...) | ||
|
||
resp.PlanValue = serializedPlanValue | ||
} |
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,84 @@ | ||
package modifiers_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/terraform-providers/terraform-provider-dnsimple/internal/framework/modifiers" | ||
) | ||
|
||
func TestSetTrimSuffixValue(t *testing.T) { | ||
t.Parallel() | ||
|
||
serializeList := func(elements []string) types.Set { | ||
listValue, _ := types.SetValueFrom(context.Background(), types.StringType, elements) | ||
return listValue | ||
} | ||
|
||
type testCase struct { | ||
plannedValue types.Set | ||
stateValue types.Set | ||
configValue types.Set | ||
expectedValue types.Set | ||
expectError bool | ||
} | ||
tests := map[string]testCase{ | ||
"trim `.` in string value ": { | ||
plannedValue: serializeList([]string{"beta.alpha.", "gamma.omega"}), | ||
stateValue: types.SetNull(types.StringType), | ||
configValue: serializeList([]string{"beta.alpha.", "gamma.omega"}), | ||
expectedValue: serializeList([]string{"beta.alpha", "gamma.omega"}), | ||
}, | ||
"when state is Null": { | ||
plannedValue: serializeList([]string{"beta.alpha.", "gamma.omega."}), | ||
stateValue: types.SetNull(types.StringType), | ||
configValue: serializeList([]string{"beta.alpha.", "gamma.omega."}), | ||
expectedValue: serializeList([]string{"beta.alpha", "gamma.omega"}), | ||
}, | ||
"when plan is Null": { | ||
plannedValue: types.SetNull(types.StringType), | ||
stateValue: types.SetNull(types.StringType), | ||
configValue: serializeList([]string{"beta.alpha.", "gamma.omega."}), | ||
expectedValue: types.SetNull(types.StringType), | ||
}, | ||
"when plan is Unknown": { | ||
plannedValue: types.SetUnknown(types.StringType), | ||
stateValue: types.SetNull(types.StringType), | ||
configValue: serializeList([]string{"beta.alpha.", "gamma.omega."}), | ||
expectedValue: types.SetUnknown(types.StringType), | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
name, test := name, test | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx := context.Background() | ||
request := planmodifier.SetRequest{ | ||
Path: path.Root("test"), | ||
PlanValue: test.plannedValue, | ||
StateValue: test.stateValue, | ||
ConfigValue: test.configValue, | ||
} | ||
response := planmodifier.SetResponse{ | ||
PlanValue: request.PlanValue, | ||
} | ||
modifiers.SetTrimSuffixValue().PlanModifySet(ctx, request, &response) | ||
|
||
if !response.Diagnostics.HasError() && test.expectError { | ||
t.Fatal("expected error, got no error") | ||
} | ||
|
||
if response.Diagnostics.HasError() && !test.expectError { | ||
t.Fatalf("got unexpected error: %s", response.Diagnostics) | ||
} | ||
|
||
assert.Equal(t, test.expectedValue, response.PlanValue) | ||
}) | ||
} | ||
} |
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