ref 624723: server backup schedules (#416)

Signed-off-by: Adrian Nackov <adrian.nackov@mail.schwarz>
This commit is contained in:
a_nackov 2024-06-26 13:51:06 +03:00 committed by GitHub
parent 7fbb13c0b6
commit b5eb8bd379
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 1852 additions and 0 deletions

View file

@ -14,6 +14,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
"github.com/teambition/rrule-go"
)
const (
@ -225,3 +226,30 @@ func CIDR() *Validator {
},
}
}
func Rrule() *Validator {
description := "value must be in a valid RRULE format"
return &Validator{
description: description,
validate: func(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
// The go library rrule-go expects \n before RRULE (to be a newline and not a space)
// for example: "DTSTART;TZID=America/New_York:19970902T090000\nRRULE:FREQ=DAILY;COUNT=10"
// whereas a valid rrule according to the API docs is:
// for example: "DTSTART;TZID=America/New_York:19970902T090000 RRULE:FREQ=DAILY;COUNT=10"
//
// So we will accept a ' ' (which is valid per API docs),
// but replace it with a '\n' for the rrule-go validations
value := req.ConfigValue.ValueString()
value = strings.ReplaceAll(value, " ", "\n")
if _, err := rrule.StrToRRuleSet(value); err != nil {
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
req.Path,
description,
req.ConfigValue.ValueString(),
))
}
},
}
}

View file

@ -574,3 +574,57 @@ func TestCIDR(t *testing.T) {
})
}
}
func TestRrule(t *testing.T) {
tests := []struct {
description string
input string
isValid bool
}{
{
"ok",
"DTSTART;TZID=Europe/Sofia:20200803T023000 RRULE:FREQ=DAILY;INTERVAL=1",
true,
},
{
"ok-2",
"DTSTART;TZID=Europe/Sofia:20200803T023000\nRULE:FREQ=DAILY;INTERVAL=1",
true,
},
{
"Empty",
"",
false,
},
{
"not ok",
"afssfdfs",
false,
},
{
"not ok-missing-space-before-rrule",
"DTSTART;TZID=Europe/Sofia:20200803T023000RRULE:FREQ=DAILY;INTERVAL=1",
false,
},
{
"not ok-missing-interval",
"DTSTART;TZID=Europe/Sofia:20200803T023000 RRULE:FREQ=DAILY;INTERVAL=",
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
r := validator.StringResponse{}
Rrule().ValidateString(context.Background(), validator.StringRequest{
ConfigValue: types.StringValue(tt.input),
}, &r)
if !tt.isValid && !r.Diagnostics.HasError() {
t.Fatalf("Should have failed")
}
if tt.isValid && r.Diagnostics.HasError() {
t.Fatalf("Should not have failed: %v", r.Diagnostics.Errors())
}
})
}
}