-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
328 lines (273 loc) · 9.38 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
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"
"github.com/opsgenie/opsgenie-go-sdk-v2/team"
"github.com/sirupsen/logrus"
)
var startHourWorkWeek uint32 = 17
var startMinuteWorkWeek uint32 = 0
var endHourWorkWeek uint32 = 9
var endMinWorkWeek uint32 = 0
const staticScheduleName string = "Test Schedule"
const staticScheduleID string = "XXXXXXXXXXXXXXX"
const staticScheduleTimezone string = "Europe/Warsaw"
const staticScheduleTeam string = "TestTeam"
const staticScheduleYear int = 2022
const staticScheduleEnabledFlag bool = true
const staticRotationStartEndTime int = 9
const staticTeamID string = "XXXXXXXXXXXXXXX"
const staticTeamName string = "Team Test"
const staticTeamDesc string = "None"
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 checkApiKey(apiKey string) string {
apiKeyEnv := os.Getenv("OPSGENIE_API_KEY")
if apiKeyEnv != "" {
apiKey = apiKeyEnv
} else if apiKey == "" {
fmt.Printf("Empty apiKey...\nPlease use -apiKey or export OPSGENIE_API_KEY=\"XXXXXXXXXXXXXXX\" \n")
os.Exit(1)
}
return apiKey
}
func createApi(apiKey string) *schedule.Client {
apiKey = checkApiKey(apiKey)
scheduleClient, err := schedule.NewClient(&client.Config{
ApiKey: apiKey,
LogLevel: logrus.ErrorLevel,
})
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)
return firstMonday
}
func getNumberOfWeeks(year int) int {
firstOfYear := time.Date(year, time.January, 1, 0, 0, 0, 0, time.UTC)
weekday := int(firstOfYear.Weekday())
daysUntilFirstThursday := (4 - weekday + 7) % 7
daysAfterFirstThursday := 365 - daysUntilFirstThursday
if firstOfYear.Year()%4 == 0 && (firstOfYear.Year()%100 != 0 || firstOfYear.Year()%400 == 0) {
daysAfterFirstThursday++
}
numberOfWeeks := (daysAfterFirstThursday / 7) + 1
return numberOfWeeks
}
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: &scheduleEnabledFlag,
OwnerTeam: &og.OwnerTeam{
Name: scheduleTeam,
},
})
if err != nil {
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, scheduleID string, year int, startEndTime int) {
month := time.Month(1)
firstMonday := getFirstMonday(year, month)
numberOfWeeks := getNumberOfWeeks(year)
// Set the next Monday at 9:00 AM
nextMonday := time.Date(year, month, int(firstMonday), startEndTime, 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())
_, err := 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: defaultSchedule[:],
},
},
ScheduleIdentifierType: schedule.Id,
ScheduleIdentifierValue: scheduleID,
})
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 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)
}
time.Sleep(10 * time.Second)
}
func getSchedule(scheduleClient schedule.Client, scheduleID string) *schedule.GetResult {
scheduleResult, err := scheduleClient.Get(nil, &schedule.GetRequest{
IdentifierType: schedule.Id,
IdentifierValue: scheduleID,
})
if err != nil {
fmt.Printf("Schedule %s can NOT be get.\n", scheduleID)
}
return scheduleResult
}
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 createTeamClient(apiKey string) *team.Client {
apiKey = checkApiKey(apiKey)
teamClient, err := team.NewClient(&client.Config{ApiKey: apiKey})
if err != nil {
fmt.Printf("TeamClient can NOT be created.\n")
}
return teamClient
}
func teamCreator(teamClient team.Client, teamName string, teamDesc string) *team.CreateTeamResult {
teamResult, err := teamClient.Create(nil, &team.CreateTeamRequest{
Name: teamName,
Description: teamDesc,
Members: []team.Member{},
})
if err != nil {
fmt.Printf("Team %s with id: %s has NOT been created.\n", teamResult.Name, teamResult.Id)
} else {
fmt.Printf("Team %s with id: %s has been created.\n", teamResult.Name, teamResult.Id)
}
return teamResult
}
func deleteTeam(teamClient team.Client, teamID string) {
_, err := teamClient.Delete(nil, &team.DeleteTeamRequest{
IdentifierType: team.Id,
IdentifierValue: teamID,
})
if err != nil {
fmt.Printf("Team %s can NOT be deleted.\n", teamID)
} else {
fmt.Printf("Team %s has been deleted.\n", teamID)
}
time.Sleep(10 * time.Second)
}
func main() {
// Api Key
apiKey := flag.String("apiKey", "", "# ApiKey for use in that script.\n# You can use the export OPSGENIE_API_KEY=\"XXXXXXXXXXXXXXX\"")
// Schedule Values
scheduleName := flag.String("scheduleName", staticScheduleName, "# Name of schedule")
scheduleID := flag.String("scheduleID", staticScheduleID, "# ID of schedule")
scheduleTimezone := flag.String("scheduleTimezone", staticScheduleTimezone, "# Timezone of the schedule")
scheduleTeam := flag.String("scheduleTeam", staticScheduleTeam, "# Name of the team in the schedule")
scheduleYear := flag.Int("scheduleYear", staticScheduleYear, "# Year of the schedule")
scheduleEnabledFlag := flag.Bool("scheduleEnabledFlag", staticScheduleEnabledFlag, "# Schedule is enabled")
scheduleRotationStartEndTime := flag.Int("scheduleRotationStartEndTime", staticRotationStartEndTime, "# Start Time of the rotation")
// Team Values
teamName := flag.String("teamName", staticTeamName, "# Name of team")
teamID := flag.String("teamID", staticTeamID, "# ID of team")
teamDesc := flag.String("teamDesc", staticTeamDesc, "# Description of team")
// Bool
delete := flag.Bool("delete", false, "# Delete schedule or team")
// Parsing a flags
flag.Parse()
// Initialization a Clients
scheduleClient := createApi(*apiKey)
teamClient := createTeamClient(*apiKey)
var createdTeam *team.CreateTeamResult
var createdSchedule *schedule.CreateResult
if *teamName != staticTeamName && *teamID == staticTeamID {
createdTeam = teamCreator(*teamClient, *teamName, *teamDesc)
teamID = &createdTeam.Id
}
if *scheduleName != staticScheduleName && *scheduleID == staticScheduleID {
createdSchedule := scheduleCreator(*scheduleClient, *scheduleName, *scheduleTimezone, *scheduleTeam, *scheduleEnabledFlag)
restrictionCreator(*scheduleClient, createdSchedule.Id, *scheduleYear, *scheduleRotationStartEndTime)
scheduleID = &createdSchedule.Id
}
if *delete {
if *scheduleID != staticScheduleID {
deleteSchedule(*scheduleClient, *scheduleID)
}
if *teamID != staticTeamID {
deleteTeam(*teamClient, *teamID)
}
if createdTeam != nil && createdSchedule != nil {
teamDeleteID := &createdTeam.Id
scheduleDeleteID := &createdSchedule.Id
deleteSchedule(*scheduleClient, *scheduleDeleteID)
deleteTeam(*teamClient, *teamDeleteID)
os.Exit(0)
}
}
}