From 750745854858675fe6b927000baaaf312182c002 Mon Sep 17 00:00:00 2001 From: Tohfa Rabab Date: Thu, 27 May 2021 16:42:41 +0530 Subject: [PATCH] ISSUE-143: Implement TimeZoneDiff func to Schedule as well fix format update changelog.md --- CHANGELOG.md | 4 ++++ opsgenie/resource_opsgenie_schedule.go | 25 +++++++++++++++++---- opsgenie/resource_opsgenie_schedule_test.go | 16 +++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 22f37196..bcba2402 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.6.5 (June 15, 2021) +BUGFIX: +* **Schedule:** Timezone diff problem fixed. + ## 0.6.4 (April 14, 2021) BUGFIX: * **Notification Policy:** De-duplication Action problem fixed. diff --git a/opsgenie/resource_opsgenie_schedule.go b/opsgenie/resource_opsgenie_schedule.go index 1f4d99c5..0ce2c58d 100644 --- a/opsgenie/resource_opsgenie_schedule.go +++ b/opsgenie/resource_opsgenie_schedule.go @@ -2,10 +2,10 @@ package opsgenie import ( "context" - "log" - "github.com/opsgenie/opsgenie-go-sdk-v2/og" "github.com/opsgenie/opsgenie-go-sdk-v2/schedule" + "log" + "time" "fmt" @@ -35,8 +35,10 @@ func resourceOpsgenieSchedule() *schema.Resource { ValidateFunc: validateOpsgenieScheduleDescription, }, "timezone": { - Type: schema.TypeString, - Optional: true, + Type: schema.TypeString, + Optional: true, + Default: "America/New_York", + DiffSuppressFunc: checkTimeZoneDifference, }, "enabled": { Type: schema.TypeBool, @@ -50,6 +52,21 @@ func resourceOpsgenieSchedule() *schema.Resource { } } +func checkTimeZoneDifference(k, old, new string, d *schema.ResourceData) bool { + locationOld, errOld := time.LoadLocation(old) + if errOld != nil { + return false + } + locationNew, errNew := time.LoadLocation(new) + if errNew != nil { + return false + } + now := time.Now() + timeOld := now.In(locationOld) + timeNew := now.In(locationNew) + return timeOld.Format(time.ANSIC) == timeNew.Format(time.ANSIC) +} + func resourceOpsgenieScheduleCreate(d *schema.ResourceData, meta interface{}) error { client, err := schedule.NewClient(meta.(*OpsgenieClient).client.Config) if err != nil { diff --git a/opsgenie/resource_opsgenie_schedule_test.go b/opsgenie/resource_opsgenie_schedule_test.go index 85c4e29a..8f38aa39 100644 --- a/opsgenie/resource_opsgenie_schedule_test.go +++ b/opsgenie/resource_opsgenie_schedule_test.go @@ -174,3 +174,19 @@ resource "opsgenie_schedule" "test" { `, rString) } + +func TestCheckTimeZoneDifference(t *testing.T) { + oldTimeZone := "America/Los_Angeles" + newTimeZone := "Canada/Pacific" + if !checkTimeZoneDiff("", oldTimeZone, newTimeZone, nil) { + t.Errorf("Timezones should be equal") + } +} + +func TestCheckTimeZoneDifference_notEqual(t *testing.T) { + oldTimeZone := "America/Los_Angeles" + newTimeZone := "Europe/Istanbul" + if checkTimeZoneDiff("", oldTimeZone, newTimeZone, nil) { + t.Errorf("Timezones should be equal") + } +}