Skip to content

Commit

Permalink
ISSUE-143: Implement TimeZoneDiff func to Schedule as well (#245)
Browse files Browse the repository at this point in the history
fix format

update changelog.md
  • Loading branch information
trabab authored Jun 15, 2021
1 parent 4f9a3ad commit d98b6d4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
25 changes: 21 additions & 4 deletions opsgenie/resource_opsgenie_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions opsgenie/resource_opsgenie_schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit d98b6d4

Please sign in to comment.