Skip to content

Commit

Permalink
Merge pull request PagerDuty#826 from imjaroiswebdev/issue-796-tz-val…
Browse files Browse the repository at this point in the history
…idation

Address: Time zone validation is not aligned with the API
  • Loading branch information
imjaroiswebdev authored Feb 29, 2024
2 parents 7887dad + e254342 commit 966ed30
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 28 deletions.
13 changes: 4 additions & 9 deletions pagerduty/resource_pagerduty_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"time"

"github.com/PagerDuty/terraform-provider-pagerduty/util"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -53,15 +54,9 @@ func resourcePagerDutySchedule() *schema.Resource {
},

"time_zone": {
Type: schema.TypeString,
Required: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
_, err := time.LoadLocation(val.(string))
if err != nil {
errs = append(errs, err)
}
return
},
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: util.ValidateTZValueDiagFunc,
},

"overflow": {
Expand Down
13 changes: 4 additions & 9 deletions pagerduty/resource_pagerduty_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"time"

"github.com/PagerDuty/terraform-provider-pagerduty/util"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
Expand Down Expand Up @@ -238,15 +239,9 @@ func resourcePagerDutyService() *schema.Resource {
Optional: true,
},
"time_zone": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
_, err := time.LoadLocation(val.(string))
if err != nil {
errs = append(errs, err)
}
return
},
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: util.ValidateTZValueDiagFunc,
},
"start_time": {
Type: schema.TypeString,
Expand Down
15 changes: 5 additions & 10 deletions pagerduty/resource_pagerduty_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"time"

"github.com/PagerDuty/terraform-provider-pagerduty/util"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/heimweh/go-pagerduty/pagerduty"
Expand Down Expand Up @@ -79,16 +80,10 @@ func resourcePagerDutyUser() *schema.Resource {
},

"time_zone": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
_, err := time.LoadLocation(val.(string))
if err != nil {
errs = append(errs, err)
}
return
},
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateDiagFunc: util.ValidateTZValueDiagFunc,
},

"html_url": {
Expand Down
179 changes: 179 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math"
"reflect"
"regexp"
"sort"
"strings"
"time"
"unicode"
Expand Down Expand Up @@ -276,3 +277,181 @@ func ResourcePagerDutyParseColonCompoundID(id string) (string, string, error) {

return parts[0], parts[1], nil
}

func ValidateTZValueDiagFunc(v interface{}, p cty.Path) diag.Diagnostics {
var diags diag.Diagnostics

value := v.(string)
valid := false

foundAt := sort.SearchStrings(validTZ, value)
if foundAt < len(validTZ) && validTZ[foundAt] == value {
valid = true
}

if !valid {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("%q is a not valid input. Please refer to the list of allowed Time Zone values at https://developer.pagerduty.com/docs/1afe25e9c94cb-types#time-zone", value),
AttributePath: p,
})
}

return diags
}

// validTZ at the moment there not an API to fetch this values, so hardcoding
// them here
var validTZ []string = []string{
"Africa/Algiers",
"Africa/Cairo",
"Africa/Casablanca",
"Africa/Harare",
"Africa/Johannesburg",
"Africa/Monrovia",
"Africa/Nairobi",
"America/Argentina/Buenos_Aires",
"America/Bogota",
"America/Caracas",
"America/Chicago",
"America/Chihuahua",
"America/Denver",
"America/Godthab",
"America/Guatemala",
"America/Guyana",
"America/Halifax",
"America/Indiana/Indianapolis",
"America/Juneau",
"America/La_Paz",
"America/Lima",
"America/Lima",
"America/Los_Angeles",
"America/Mazatlan",
"America/Mexico_City",
"America/Mexico_City",
"America/Monterrey",
"America/Montevideo",
"America/New_York",
"America/Phoenix",
"America/Puerto_Rico",
"America/Regina",
"America/Santiago",
"America/Sao_Paulo",
"America/St_Johns",
"America/Tijuana",
"Asia/Almaty",
"Asia/Baghdad",
"Asia/Baku",
"Asia/Bangkok",
"Asia/Bangkok",
"Asia/Chongqing",
"Asia/Colombo",
"Asia/Dhaka",
"Asia/Dhaka",
"Asia/Hong_Kong",
"Asia/Irkutsk",
"Asia/Jakarta",
"Asia/Jerusalem",
"Asia/Kabul",
"Asia/Kamchatka",
"Asia/Karachi",
"Asia/Karachi",
"Asia/Kathmandu",
"Asia/Kolkata",
"Asia/Kolkata",
"Asia/Kolkata",
"Asia/Kolkata",
"Asia/Krasnoyarsk",
"Asia/Kuala_Lumpur",
"Asia/Kuwait",
"Asia/Magadan",
"Asia/Muscat",
"Asia/Muscat",
"Asia/Novosibirsk",
"Asia/Rangoon",
"Asia/Riyadh",
"Asia/Seoul",
"Asia/Shanghai",
"Asia/Singapore",
"Asia/Srednekolymsk",
"Asia/Taipei",
"Asia/Tashkent",
"Asia/Tbilisi",
"Asia/Tehran",
"Asia/Tokyo",
"Asia/Tokyo",
"Asia/Tokyo",
"Asia/Ulaanbaatar",
"Asia/Urumqi",
"Asia/Vladivostok",
"Asia/Yakutsk",
"Asia/Yekaterinburg",
"Asia/Yerevan",
"Atlantic/Azores",
"Atlantic/Cape_Verde",
"Atlantic/South_Georgia",
"Australia/Adelaide",
"Australia/Brisbane",
"Australia/Darwin",
"Australia/Hobart",
"Australia/Melbourne",
"Australia/Melbourne",
"Australia/Perth",
"Australia/Sydney",
"Etc/GMT+12",
"Etc/UTC",
"Europe/Amsterdam",
"Europe/Athens",
"Europe/Belgrade",
"Europe/Berlin",
"Europe/Bratislava",
"Europe/Brussels",
"Europe/Bucharest",
"Europe/Budapest",
"Europe/Copenhagen",
"Europe/Dublin",
"Europe/Helsinki",
"Europe/Istanbul",
"Europe/Kaliningrad",
"Europe/Kiev",
"Europe/Lisbon",
"Europe/Ljubljana",
"Europe/London",
"Europe/London",
"Europe/Madrid",
"Europe/Minsk",
"Europe/Moscow",
"Europe/Moscow",
"Europe/Paris",
"Europe/Prague",
"Europe/Riga",
"Europe/Rome",
"Europe/Samara",
"Europe/Sarajevo",
"Europe/Skopje",
"Europe/Sofia",
"Europe/Stockholm",
"Europe/Tallinn",
"Europe/Vienna",
"Europe/Vilnius",
"Europe/Volgograd",
"Europe/Warsaw",
"Europe/Zagreb",
"Europe/Zurich",
"Europe/Zurich",
"Pacific/Apia",
"Pacific/Auckland",
"Pacific/Auckland",
"Pacific/Chatham",
"Pacific/Fakaofo",
"Pacific/Fiji",
"Pacific/Guadalcanal",
"Pacific/Guam",
"Pacific/Honolulu",
"Pacific/Majuro",
"Pacific/Midway",
"Pacific/Noumea",
"Pacific/Pago_Pago",
"Pacific/Port_Moresby",
"Pacific/Tongatapu",
}
43 changes: 43 additions & 0 deletions util/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package util

import (
"fmt"
"reflect"
"testing"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
)

func TestValidateTZValueDiagFunc(t *testing.T) {
notValidTZ1 := "not a valid TZ"

cases := []struct {
given string
want diag.Diagnostics
path cty.Path
}{
{
given: "America/Montevideo",
want: nil,
path: cty.Path{},
},
{
given: "America/Indiana/Indianapolis",
want: nil,
path: cty.Path{},
},
{
given: notValidTZ1,
want: diag.Diagnostics{diag.Diagnostic{Severity: 0, Summary: fmt.Sprintf("\"%s\" is a not valid input. Please refer to the list of allowed Time Zone values at https://developer.pagerduty.com/docs/1afe25e9c94cb-types#time-zone", notValidTZ1), Detail: "", AttributePath: cty.Path{cty.GetAttrStep{Name: "time_zone"}}}},
path: cty.Path{cty.GetAttrStep{Name: "time_zone"}},
},
}

for _, c := range cases {
got := ValidateTZValueDiagFunc(c.given, c.path)
if !reflect.DeepEqual(got, c.want) {
t.Errorf("want %v; got %v", c.want, got)
}
}
}

0 comments on commit 966ed30

Please sign in to comment.