Skip to content

Commit

Permalink
Merge pull request #1 from y0rune/add-new-functions
Browse files Browse the repository at this point in the history
v.0.0.2
  • Loading branch information
y0rune authored Oct 29, 2022
2 parents 0aa9bb0 + fc27fc3 commit eb87c78
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 71 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ go get
```
-apiKey string
# ApiKey for use in that script
-scheduleEnabled
-delete
# Delete schedule
-scheduleEnabledFlag
# Schedule is enabled (default true)
-scheduleID string
# ID of schedule (default "XXXXXXXXXXXXXXX")
-scheduleName string
# Name of schedule (default "Test Schedule")
-scheduleTeam string
Expand All @@ -34,7 +38,7 @@ go get
### How to use it in the console?

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

### Output console
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ 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
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.1 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
golang.org/x/sys v0.1.0 // indirect
)
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,45 @@ 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-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-retryablehttp v0.5.1 h1:Vsx5XKPqPs3M6sM4U4GWyUqFS8aBiL9U5gkgvpkg4SE=
github.com/hashicorp/go-retryablehttp v0.5.1/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs=
github.com/hashicorp/go-retryablehttp v0.7.1 h1:sUiuQAnLlbvmExtFQs72iFW/HXeUn8Z1aJLQ4LJJbTQ=
github.com/hashicorp/go-retryablehttp v0.7.1/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
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/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/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/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.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/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
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=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
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.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
179 changes: 115 additions & 64 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,67 @@ var startMinuteWorkWeek uint32 = 0
var endHourWorkWeek uint32 = 9
var endMinWorkWeek uint32 = 0

var defaultSchedule = [...]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,
},
}

func createApi(apiKey string) *schedule.Client {
if apiKey == "" {
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)
os.Exit(1)
}

return scheduleClient
}

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)
Expand All @@ -30,24 +91,27 @@ func getNumberOfWeeks(year int, month time.Month) int {
return numberOfWeeks
}

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

if err != nil {
fmt.Printf("Error in scheduleCreator create: %d", err)
fmt.Printf("Schedule %s with id: %s has been NOT created. Error: %d \n", scheduleResult.Name, scheduleResult.Id, err)
os.Exit(1)
} else {
fmt.Printf("Schedule %s with id: %s has been created.\n", scheduleResult.Name, scheduleResult.Id)
}

return *scheduleResult
}

func restrictionCreator(scheduleClient schedule.Client, scheduleName string, year int) {
func restrictionCreator(scheduleClient schedule.Client, scheduleID string, year int) {
month := time.Month(1)
firstMonday := getFirstMonday(year, month)
numberOfWeeks := getNumberOfWeeks(year, month)
Expand All @@ -58,7 +122,7 @@ func restrictionCreator(scheduleClient schedule.Client, scheduleName string, yea
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{
_, err := scheduleClient.CreateRotation(nil, &schedule.CreateRotationRequest{
Rotation: &og.Rotation{
Name: weekName,
StartDate: &monday,
Expand All @@ -70,82 +134,69 @@ func restrictionCreator(scheduleClient schedule.Client, scheduleName string, yea
},
},
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,
},
},
Type: og.WeekdayAndTimeOfDay,
RestrictionList: defaultSchedule[:],
},
},
ScheduleIdentifierType: schedule.Name,
ScheduleIdentifierValue: scheduleName,
ScheduleIdentifierType: schedule.Id,
ScheduleIdentifierValue: scheduleID,
})

fmt.Printf("Rotation %s has been created for schedule %s.\n", weekName, scheduleName)
if err != nil {
fmt.Printf("Rotation %s has been NOT created for schedule %s.\n", weekName, scheduleID)
} else {
fmt.Printf("Rotation %s has been created for schedule %s.\n", weekName, scheduleID)
}
}
}

func main() {
func deleteSchedule(scheduleClient schedule.Client, scheduleID string) {
_, err := scheduleClient.Delete(nil, &schedule.DeleteRequest{
IdentifierType: schedule.Id,
IdentifierValue: scheduleID,
})

if err != nil {
fmt.Printf("Schedule %s has been NOT deleted.\n", scheduleID)
} else {
fmt.Printf("Schedule %s has been deleted.\n", scheduleID)
}
}

func getListRotation(scheduleClient schedule.Client, scheduleID string) *schedule.ListRotationsResult {
scheduleResult, err := scheduleClient.ListRotations(nil, &schedule.ListRotationsRequest{
ScheduleIdentifierType: schedule.Id,
ScheduleIdentifierValue: scheduleID,
})

if err != nil {
fmt.Printf("Schedule %s can not be get.\n", scheduleID)
}
return scheduleResult
}

func main() {
apiKey := flag.String("apiKey", "", "# ApiKey for use in that script")
scheduleName := flag.String("scheduleName", "Test Schedule", "# Name of schedule")
scheduleID := flag.String("scheduleID", "XXXXXXXXXXXXXXX", "# ID 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")
scheduleEnabledFlag := flag.Bool("scheduleEnabledFlag", true, "# Schedule is enabled")
delete := flag.Bool("delete", false, "# Delete schedule ")
flag.Parse()

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

if *delete && *scheduleName == "Test Schedule" && *scheduleID != "XXXXXXXXXXXXXXX" {
deleteSchedule(*scheduleClient, *scheduleID)
}

scheduleClient, err := schedule.NewClient(&client.Config{
ApiKey: *apiKey,
})
createdSchedule := scheduleCreator(*scheduleClient, *scheduleName, *scheduleTimezone, *scheduleTeam, *scheduleEnabledFlag)
restrictionCreator(*scheduleClient, createdSchedule.Id, *scheduleYear)

if err != nil {
fmt.Printf("Error in scheduleClient create: %d", err)
if *delete {
scheduleID = &createdSchedule.Id
deleteSchedule(*scheduleClient, *scheduleID)
}

createdSchedule := scheduleCreator(*scheduleClient, *scheduleName, *scheduleTimezone, *scheduleTeam, *scheduleEnabled)
restrictionCreator(*scheduleClient, createdSchedule.Name, *scheduleYear)
}

0 comments on commit eb87c78

Please sign in to comment.