-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add a
wait_for
attribute to the mailjet_sender_validate
res…
…ource The validation does not always succeed on the first try even when all the resources exists. This attribute adds the possibility to let the provider retry multiple times before failing.
- Loading branch information
Showing
6 changed files
with
180 additions
and
2 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package mailjet | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
) | ||
|
||
var _ validator.String = timeDurationValidator{} | ||
|
||
type timeDurationValidator struct { | ||
} | ||
|
||
func (validator timeDurationValidator) Description(_ context.Context) string { | ||
return `must be a string representing a duration of at least 1 second` | ||
} | ||
|
||
func (validator timeDurationValidator) MarkdownDescription(ctx context.Context) string { | ||
return validator.Description(ctx) | ||
} | ||
|
||
func (validator timeDurationValidator) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) { | ||
s := req.ConfigValue | ||
|
||
if s.IsUnknown() || s.IsNull() { | ||
return | ||
} | ||
|
||
duration, err := time.ParseDuration(s.ValueString()) | ||
|
||
if err != nil { | ||
resp.Diagnostics.Append(diag.NewAttributeErrorDiagnostic( | ||
req.Path, | ||
"failed to parse the time duration", | ||
fmt.Sprintf("%q %s", s.ValueString(), validator.Description(ctx))), | ||
) | ||
return | ||
} | ||
|
||
if duration < time.Second { | ||
resp.Diagnostics.Append(diag.NewAttributeErrorDiagnostic( | ||
req.Path, | ||
"the time duration must be at least 1 second", | ||
fmt.Sprintf("%q %s", s.ValueString(), validator.Description(ctx))), | ||
) | ||
return | ||
} | ||
} | ||
|
||
func TimeDurationAtLeast1Sec() validator.String { | ||
return timeDurationValidator{} | ||
} |
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,73 @@ | ||
package mailjet | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func TestTimeDuration(t *testing.T) { | ||
t.Parallel() | ||
|
||
type testCase struct { | ||
val types.String | ||
expectedDiagnostics diag.Diagnostics | ||
} | ||
|
||
tests := map[string]testCase{ | ||
"unknown": { | ||
val: types.StringUnknown(), | ||
}, | ||
"null": { | ||
val: types.StringNull(), | ||
}, | ||
"valid": { | ||
val: types.StringValue("30s"), | ||
}, | ||
"invalid": { | ||
val: types.StringValue("30wrong"), | ||
expectedDiagnostics: diag.Diagnostics{ | ||
diag.NewAttributeErrorDiagnostic( | ||
path.Root("test"), | ||
"failed to parse the time duration", | ||
`"30wrong" must be a string representing a duration of at least 1 second`, | ||
), | ||
}, | ||
}, | ||
"invalid_too_small": { | ||
val: types.StringValue("0s"), | ||
expectedDiagnostics: diag.Diagnostics{ | ||
diag.NewAttributeErrorDiagnostic( | ||
path.Root("test"), | ||
"the time duration must be at least 1 second", | ||
`"0s" must be a string representing a duration of at least 1 second`, | ||
), | ||
}, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
name, test := name, test | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
request := validator.StringRequest{ | ||
Path: path.Root("test"), | ||
PathExpression: path.MatchRoot("test"), | ||
ConfigValue: test.val, | ||
} | ||
|
||
response := validator.StringResponse{} | ||
|
||
TimeDurationAtLeast1Sec().ValidateString(context.Background(), request, &response) | ||
|
||
if diff := cmp.Diff(response.Diagnostics, test.expectedDiagnostics); diff != "" { | ||
t.Errorf("unexpected diagnostics difference: %s", diff) | ||
} | ||
}) | ||
} | ||
} |