Skip to content

Commit

Permalink
New Resource: `azurerm_data_protection_backup_policy_mysql_flexible_s…
Browse files Browse the repository at this point in the history
…erver` (hashicorp#26955)

* New Resource: azurerm_data_protection_backup_policy_mysql_flexible_server

* update code

* add validate for timezone

* add validation for backup_repeating_time_intervals
  • Loading branch information
neil-yechenwei authored Sep 20, 2024
1 parent 09f2097 commit 596a61e
Show file tree
Hide file tree
Showing 9 changed files with 1,403 additions and 0 deletions.
37 changes: 37 additions & 0 deletions helpers/validate/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package validate

import (
"fmt"
"strings"
"time"

iso8601 "github.com/btubbs/datetime"
Expand Down Expand Up @@ -65,6 +66,42 @@ func ISO8601DateTime(i interface{}, k string) (warnings []string, errors []error
return warnings, errors
}

func ISO8601RepeatingTime(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %s to be string", k))
return
}

if !strings.HasPrefix(v, "R/") {
errors = append(errors, fmt.Errorf("%s must start with 'R/'", k))
return
}

partsWithoutPrefix := strings.TrimPrefix(v, "R/")

pIndex := strings.Index(partsWithoutPrefix, "/P")
if pIndex == -1 {
errors = append(errors, fmt.Errorf("%s must end with duration", k))
return
}

dateTime := partsWithoutPrefix[:pIndex]
duration := partsWithoutPrefix[pIndex+1:]

if _, err := iso8601.Parse(dateTime, time.UTC); err != nil {
errors = append(errors, fmt.Errorf("%q has the invalid ISO8601 date format %q: %+v", k, i, err))
return
}

if _, err := period.Parse(duration); err != nil {
errors = append(errors, err)
return
}

return warnings, errors
}

func AzureTimeZoneString() func(interface{}, string) ([]string, []error) {
// List collected from https://support.microsoft.com/en-gb/help/973627/microsoft-time-zone-index-values
// TODO look into programatic retrieval https://docs.microsoft.com/en-us/rest/api/maps/timezone/gettimezoneenumwindows
Expand Down
60 changes: 60 additions & 0 deletions helpers/validate/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,63 @@ func TestISO8601Duration(t *testing.T) {
}
}
}

func TestISO8601RepeatingTime(t *testing.T) {
cases := []struct {
Value string
Errors int
}{
{
Value: "R/2021-05-23T02:30:00+00:00/P1W",
Errors: 0,
},
{
Value: "R/2021-05-23T02:30:00+00:00/PT20S",
Errors: 0,
},
{
Value: "R/2021-05-23T02:30:00+00:00/PT0.5S",
Errors: 0,
},
{
Value: "R",
Errors: 1,
},
{
Value: "R/",
Errors: 1,
},
{
Value: "2021-05-23T02:30:03+01:02",
Errors: 1,
},
{
Value: "P1W",
Errors: 1,
},
{
Value: "R/2021-05-23T02:30:00+00:00",
Errors: 1,
},
{
Value: "C/2021-05-23T02:30:00+00:00",
Errors: 1,
},
{
Value: "2021-05-23T02:30:00+00:00/D1W",
Errors: 1,
},
{
Value: "2021-05-23T02:30:00+00:00/P1W",
Errors: 1,
},
}

for _, tc := range cases {
_, errors := ISO8601RepeatingTime(tc.Value, "example")

if len(errors) != tc.Errors {
t.Fatalf("Expected ISO8601RepeatingTime to trigger '%d' errors for '%s' - got '%d'", tc.Errors, tc.Value, len(errors))
}
}
}
Loading

0 comments on commit 596a61e

Please sign in to comment.