Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
y0rune committed Oct 22, 2022
0 parents commit 0aa9bb0
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 0 deletions.
101 changes: 101 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Opsgenie-scheduler-creator

The script `Opsgenie-scheduler-creator` is a automatically creator a schedule
with rotation in OpsGenie. The rotation settings are:
- Workweek (from Monday to Friday): daily - 17:00 - 9:00 (5:00PM - 9:00AM)
- Weekend (from Friday to Monday): all weekend - 17:00 - 9:00 (5:00PM - 9:00AM)

## Instalation

```bash
git clone https://github.com/y0rune/opsgenie-scheduler-creator.git
go get
```

## Arguments

```
-apiKey string
# ApiKey for use in that script
-scheduleEnabled
# Schedule is enabled (default true)
-scheduleName string
# Name of schedule (default "Test Schedule")
-scheduleTeam string
# Name of the team in the schedule (default "TestTeam")
-scheduleTimezone string
# Timezone of the schedule (default "Europe/Warsaw")
-scheduleYear int
# Year of the schedule (default 2022)
```

## Example of usage

### How to use it in the console?

```bash
go run main.go --apiKey XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX --scheduleName "YEAR_2023" --scheduleYear 2023
```

### Output console

```
INFO[2022-10-23T00:36:15.270221+02:00] Client is configured with ApiUrl: api.opsgenie.com, RetryMaxCount: 4
Rotation w1-2.1-9.1 has been created for schedule YEAR_2023.
Rotation w2-9.1-16.1 has been created for schedule YEAR_2023.
Rotation w3-16.1-23.1 has been created for schedule YEAR_2023.
Rotation w4-23.1-30.1 has been created for schedule YEAR_2023.
Rotation w5-30.1-6.2 has been created for schedule YEAR_2023.
Rotation w6-6.2-13.2 has been created for schedule YEAR_2023.
Rotation w7-13.2-20.2 has been created for schedule YEAR_2023.
Rotation w8-20.2-27.2 has been created for schedule YEAR_2023.
Rotation w9-27.2-6.3 has been created for schedule YEAR_2023.
Rotation w10-6.3-13.3 has been created for schedule YEAR_2023.
Rotation w11-13.3-20.3 has been created for schedule YEAR_2023.
Rotation w11-13.3-20.3 has been created for schedule YEAR_2023.
Rotation w12-20.3-27.3 has been created for schedule YEAR_2023.
Rotation w13-27.3-3.4 has been created for schedule YEAR_2023.
Rotation w14-3.4-10.4 has been created for schedule YEAR_2023.
Rotation w15-10.4-17.4 has been created for schedule YEAR_2023.
Rotation w16-17.4-24.4 has been created for schedule YEAR_2023.
Rotation w17-24.4-1.5 has been created for schedule YEAR_2023.
Rotation w18-1.5-8.5 has been created for schedule YEAR_2023.
Rotation w19-8.5-15.5 has been created for schedule YEAR_2023.
Rotation w20-15.5-22.5 has been created for schedule YEAR_2023.
Rotation w21-22.5-29.5 has been created for schedule YEAR_2023.
Rotation w22-29.5-5.6 has been created for schedule YEAR_2023.
Rotation w23-5.6-12.6 has been created for schedule YEAR_2023.
Rotation w24-12.6-19.6 has been created for schedule YEAR_2023.
Rotation w25-19.6-26.6 has been created for schedule YEAR_2023.
Rotation w26-26.6-3.7 has been created for schedule YEAR_2023.
Rotation w27-3.7-10.7 has been created for schedule YEAR_2023.
Rotation w28-10.7-17.7 has been created for schedule YEAR_2023.
Rotation w29-17.7-24.7 has been created for schedule YEAR_2023.
Rotation w30-24.7-31.7 has been created for schedule YEAR_2023.
Rotation w31-31.7-7.8 has been created for schedule YEAR_2023.
Rotation w32-7.8-14.8 has been created for schedule YEAR_2023.
Rotation w33-14.8-21.8 has been created for schedule YEAR_2023.
Rotation w34-21.8-28.8 has been created for schedule YEAR_2023.
Rotation w35-28.8-4.9 has been created for schedule YEAR_2023.
Rotation w36-4.9-11.9 has been created for schedule YEAR_2023.
Rotation w37-11.9-18.9 has been created for schedule YEAR_2023.
Rotation w38-18.9-25.9 has been created for schedule YEAR_2023.
Rotation w39-25.9-2.10 has been created for schedule YEAR_2023.
Rotation w40-2.10-9.10 has been created for schedule YEAR_2023.
Rotation w41-9.10-16.10 has been created for schedule YEAR_2023.
Rotation w42-16.10-23.10 has been created for schedule YEAR_2023.
Rotation w43-23.10-30.10 has been created for schedule YEAR_2023.
Rotation w44-30.10-6.11 has been created for schedule YEAR_2023.
Rotation w45-6.11-13.11 has been created for schedule YEAR_2023.
Rotation w46-13.11-20.11 has been created for schedule YEAR_2023.
Rotation w47-20.11-27.11 has been created for schedule YEAR_2023.
Rotation w48-27.11-4.12 has been created for schedule YEAR_2023.
Rotation w49-4.12-11.12 has been created for schedule YEAR_2023.
Rotation w50-11.12-18.12 has been created for schedule YEAR_2023.
Rotation w51-18.12-25.12 has been created for schedule YEAR_2023.
Rotation w52-25.12-1.1 has been created for schedule YEAR_2023.
```

### Schedule settings in OpsGenie
![alt text](https://github.com/y0rune/opsgenie-scheduler-creator/blob/main/screenshots/OpsGenieSchedule.png)
![alt text](https://github.com/y0rune/opsgenie-scheduler-creator/blob/main/screenshots/OpsGenieUpdateRotation.png)
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module opsgenie-scheduler-creator

go 1.19

require github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.13

require (
github.com/hashicorp/go-cleanhttp v0.5.0 // indirect
github.com/hashicorp/go-retryablehttp v0.5.1 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/sirupsen/logrus v1.4.2 // indirect
golang.org/x/sys v0.1.0 // indirect
)
31 changes: 31 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/hashicorp/go-cleanhttp v0.5.0 h1:wvCrVc9TjDls6+YGAF2hAifE1E5U1+b4tH6KdvN3Gig=
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-retryablehttp v0.5.1 h1:Vsx5XKPqPs3M6sM4U4GWyUqFS8aBiL9U5gkgvpkg4SE=
github.com/hashicorp/go-retryablehttp v0.5.1/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.13 h1:nV98dkBpqaYbDnhefmOQ+Rn4hE+jD6AtjYHXaU5WyJI=
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.13/go.mod h1:4OjcxgwdXzezqytxN534MooNmrxRD50geWZxTD7845s=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
151 changes: 151 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package main

import (
"flag"
"fmt"
"os"
"time"

"github.com/opsgenie/opsgenie-go-sdk-v2/client"
"github.com/opsgenie/opsgenie-go-sdk-v2/og"
"github.com/opsgenie/opsgenie-go-sdk-v2/schedule"
)

var startHourWorkWeek uint32 = 17
var startMinuteWorkWeek uint32 = 0
var endHourWorkWeek uint32 = 9
var endMinWorkWeek uint32 = 0

func getFirstMonday(year int, month time.Month) int {
t := time.Date(year, month, 1, 0, 0, 0, 0, time.UTC)
firstMonday := ((8-int(t.Weekday()))%7 + 1)

return firstMonday
}

func getNumberOfWeeks(year int, month time.Month) int {
t := time.Date(year, month, 1, 0, 0, 0, 0, time.UTC)
_, numberOfWeeks := t.ISOWeek()

return numberOfWeeks
}

func scheduleCreator(scheduleClient schedule.Client, scheduleName string, scheduleTimezone string, scheduleTeam string, scheduleEnabled bool) schedule.CreateResult {
scheduleResult, err := scheduleClient.Create(nil, &schedule.CreateRequest{
Name: scheduleName,
Timezone: scheduleTimezone,
Enabled: &scheduleEnabled,
OwnerTeam: &og.OwnerTeam{
Name: scheduleTeam,
},
})

if err != nil {
fmt.Printf("Error in scheduleCreator create: %d", err)
}

return *scheduleResult
}

func restrictionCreator(scheduleClient schedule.Client, scheduleName string, year int) {
month := time.Month(1)
firstMonday := getFirstMonday(year, month)
numberOfWeeks := getNumberOfWeeks(year, month)

nextMonday := time.Date(year, month, int(firstMonday), 1, 0, 0, 0, time.UTC)
for week := 1; week <= numberOfWeeks; week++ {
monday := nextMonday
nextMonday = nextMonday.AddDate(0, 0, 7)
weekName := fmt.Sprintf("w%d-%d.%d-%d.%d", week, monday.Day(), monday.Month(), nextMonday.Day(), nextMonday.Month())

scheduleClient.CreateRotation(nil, &schedule.CreateRotationRequest{
Rotation: &og.Rotation{
Name: weekName,
StartDate: &monday,
EndDate: &nextMonday,
Type: og.Weekly,
Participants: []og.Participant{
{
Type: og.None,
},
},
TimeRestriction: &og.TimeRestriction{
Type: og.WeekdayAndTimeOfDay,
RestrictionList: []og.Restriction{
{
StartDay: "monday",
EndDay: "tuesday",
EndHour: &endHourWorkWeek,
EndMin: &endMinWorkWeek,
StartHour: &startHourWorkWeek,
StartMin: &startMinuteWorkWeek,
},
{
StartDay: "tuesday",
EndDay: "wednesday",
EndHour: &endHourWorkWeek,
EndMin: &endMinWorkWeek,
StartHour: &startHourWorkWeek,
StartMin: &startMinuteWorkWeek,
},
{
StartDay: "wednesday",
EndDay: "thursday",
EndHour: &endHourWorkWeek,
EndMin: &endMinWorkWeek,
StartHour: &startHourWorkWeek,
StartMin: &startMinuteWorkWeek,
},
{
StartDay: "thursday",
EndDay: "friday",
EndHour: &endHourWorkWeek,
EndMin: &endMinWorkWeek,
StartHour: &startHourWorkWeek,
StartMin: &startMinuteWorkWeek,
},
{
StartDay: "friday",
EndDay: "monday",
EndHour: &endHourWorkWeek,
EndMin: &endMinWorkWeek,
StartHour: &startHourWorkWeek,
StartMin: &startMinuteWorkWeek,
},
},
},
},
ScheduleIdentifierType: schedule.Name,
ScheduleIdentifierValue: scheduleName,
})

fmt.Printf("Rotation %s has been created for schedule %s.\n", weekName, scheduleName)
}
}

func main() {

apiKey := flag.String("apiKey", "", "# ApiKey for use in that script")
scheduleName := flag.String("scheduleName", "Test Schedule", "# Name of schedule")
scheduleTimezone := flag.String("scheduleTimezone", "Europe/Warsaw", "# Timezone of the schedule")
scheduleTeam := flag.String("scheduleTeam", "TestTeam", "# Name of the team in the schedule")
scheduleYear := flag.Int("scheduleYear", 2022, "# Year of the schedule")
scheduleEnabled := flag.Bool("scheduleEnabled", true, "# Schedule is enabled")
flag.Parse()

if (*apiKey == "") || (apiKey == nil) {
fmt.Printf("Empty apiKey... Please use -apiKey \n")
os.Exit(1)
}

scheduleClient, err := schedule.NewClient(&client.Config{
ApiKey: *apiKey,
})

if err != nil {
fmt.Printf("Error in scheduleClient create: %d", err)
}

createdSchedule := scheduleCreator(*scheduleClient, *scheduleName, *scheduleTimezone, *scheduleTeam, *scheduleEnabled)
restrictionCreator(*scheduleClient, createdSchedule.Name, *scheduleYear)
}
Binary file added screenshots/OpsGenieSchedule.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/OpsGenieUpdateRotation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0aa9bb0

Please sign in to comment.