Skip to content

Commit 8f06fb7

Browse files
committed
review changes
1 parent c73de13 commit 8f06fb7

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

docs/resources/modelserving_token.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ resource "stackit_modelserving_token" "example" {
6969
- `description` (String) The description of the model serving auth token.
7070
- `region` (String) Region to which the model serving auth token is associated. If not defined, the provider region is used
7171
- `rotate_when_changed` (Map of String) A map of arbitrary key/value pairs that will force recreation of the token when they change, enabling token rotation based on external conditions such as a rotating timestamp. Changing this forces a new resource to be created.
72-
- `ttl_duration` (String) The TTL duration of the model serving auth token. E.g. 5h30m40s
72+
- `ttl_duration` (String) The TTL duration of the model serving auth token. E.g. 5h30m40s,5h,5h30m,30m,30s
7373

7474
### Read-Only
7575

stackit/internal/services/modelserving/token/resource.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,15 @@ func (r *tokenResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
221221
},
222222
},
223223
"ttl_duration": schema.StringAttribute{
224-
Description: "The TTL duration of the model serving auth token. E.g. 5h30m40s",
224+
Description: "The TTL duration of the model serving auth token. E.g. 5h30m40s,5h,5h30m,30m,30s",
225225
Required: false,
226226
Optional: true,
227227
PlanModifiers: []planmodifier.String{
228228
stringplanmodifier.RequiresReplace(),
229229
},
230+
Validators: []validator.String{
231+
validate.ValidDurationString(),
232+
},
230233
},
231234
"rotate_when_changed": schema.MapAttribute{
232235
Description: "A map of arbitrary key/value pairs that will force " +

stackit/internal/validate/validate.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,21 @@ func FileExists() *Validator {
295295
},
296296
}
297297
}
298+
299+
func ValidDurationString() *Validator {
300+
description := "value must be in a valid duration string. Such as \"300ms\", \"-1.5h\" or \"2h45m\".\nValid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\"."
301+
302+
return &Validator{
303+
description: description,
304+
validate: func(_ context.Context, req validator.StringRequest, resp *validator.StringResponse) {
305+
_, err := time.ParseDuration(req.ConfigValue.ValueString())
306+
if err != nil {
307+
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
308+
req.Path,
309+
description,
310+
req.ConfigValue.ValueString(),
311+
))
312+
}
313+
},
314+
}
315+
}

stackit/internal/validate/validate_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,3 +769,79 @@ func TestFileExists(t *testing.T) {
769769
})
770770
}
771771
}
772+
773+
func TestValidTtlDuration(t *testing.T) {
774+
tests := []struct {
775+
description string
776+
input string
777+
isValid bool
778+
}{
779+
{
780+
"valid duration with hours, minutes, and seconds",
781+
"5h30m40s",
782+
true,
783+
},
784+
{
785+
"valid duration with hours only",
786+
"5h",
787+
true,
788+
},
789+
{
790+
"valid duration with hours and minutes",
791+
"5h30m",
792+
true,
793+
},
794+
{
795+
"valid duration with minutes only",
796+
"30m",
797+
true,
798+
},
799+
{
800+
"valid duration with seconds only",
801+
"30s",
802+
true,
803+
},
804+
{
805+
"invalid duration with incorrect unit",
806+
"30o",
807+
false,
808+
},
809+
{
810+
"invalid duration without unit",
811+
"30",
812+
false,
813+
},
814+
{
815+
"invalid duration with invalid letters",
816+
"30e",
817+
false,
818+
},
819+
{
820+
"invalid duration with letters in middle",
821+
"1h30x",
822+
false,
823+
},
824+
{
825+
"empty string",
826+
"",
827+
false,
828+
},
829+
}
830+
831+
for _, tt := range tests {
832+
t.Run(tt.description, func(t *testing.T) {
833+
r := validator.StringResponse{}
834+
va := ValidDurationString()
835+
va.ValidateString(context.Background(), validator.StringRequest{
836+
ConfigValue: types.StringValue(tt.input),
837+
}, &r)
838+
839+
if !tt.isValid && !r.Diagnostics.HasError() {
840+
t.Fatalf("Expected validation to fail for input: %v", tt.input)
841+
}
842+
if tt.isValid && r.Diagnostics.HasError() {
843+
t.Fatalf("Expected validation to succeed for input: %v, but got errors: %v", tt.input, r.Diagnostics.Errors())
844+
}
845+
})
846+
}
847+
}

0 commit comments

Comments
 (0)