Skip to content

Commit d623300

Browse files
committed
restructure maintenance window model code
1 parent 199d5fa commit d623300

File tree

1 file changed

+97
-130
lines changed

1 file changed

+97
-130
lines changed

internal/kibana/maintenance_window/models.go

Lines changed: 97 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ type MaintenanceWindowScheduleRecurring struct {
4747
/* CREATE */
4848

4949
func (model MaintenanceWindowModel) toAPICreateRequest(ctx context.Context) (kbapi.PostMaintenanceWindowJSONRequestBody, diag.Diagnostics) {
50-
var diags = diag.Diagnostics{}
51-
5250
body := kbapi.PostMaintenanceWindowJSONRequestBody{
5351
Enabled: model.Enabled.ValueBoolPointer(),
5452
Title: model.Title.ValueString(),
@@ -61,65 +59,9 @@ func (model MaintenanceWindowModel) toAPICreateRequest(ctx context.Context) (kba
6159
body.Schedule.Custom.Timezone = model.CustomSchedule.Timezone.ValueStringPointer()
6260
}
6361

64-
if model.CustomSchedule.Recurring != nil {
65-
66-
body.Schedule.Custom.Recurring = &struct {
67-
End *string `json:"end,omitempty"`
68-
Every *string `json:"every,omitempty"`
69-
Occurrences *float32 `json:"occurrences,omitempty"`
70-
OnMonth *[]float32 `json:"onMonth,omitempty"`
71-
OnMonthDay *[]float32 `json:"onMonthDay,omitempty"`
72-
OnWeekDay *[]string `json:"onWeekDay,omitempty"`
73-
}{
74-
End: model.CustomSchedule.Recurring.End.ValueStringPointer(),
75-
Every: model.CustomSchedule.Recurring.Every.ValueStringPointer(),
76-
}
77-
78-
if utils.IsKnown(model.CustomSchedule.Recurring.Occurrences) {
79-
occurrences := float32(model.CustomSchedule.Recurring.Occurrences.ValueInt32())
80-
body.Schedule.Custom.Recurring.Occurrences = &occurrences
81-
}
82-
83-
if utils.IsKnown(model.CustomSchedule.Recurring.OnWeekDay) {
84-
var onWeekDay []string
85-
diags.Append(model.CustomSchedule.Recurring.OnWeekDay.ElementsAs(ctx, &onWeekDay, true)...)
86-
body.Schedule.Custom.Recurring.OnWeekDay = &onWeekDay
87-
}
88-
89-
if utils.IsKnown(model.CustomSchedule.Recurring.OnMonth) {
90-
var onMonth []float32
91-
diags.Append(model.CustomSchedule.Recurring.OnMonth.ElementsAs(ctx, &onMonth, true)...)
92-
body.Schedule.Custom.Recurring.OnMonth = &onMonth
93-
}
94-
95-
if utils.IsKnown(model.CustomSchedule.Recurring.OnMonthDay) {
96-
var onMonthDay []float32
97-
diags.Append(model.CustomSchedule.Recurring.OnMonthDay.ElementsAs(ctx, &onMonthDay, true)...)
98-
body.Schedule.Custom.Recurring.OnMonthDay = &onMonthDay
99-
}
100-
}
101-
102-
if model.Scope != nil {
103-
body.Scope = &struct {
104-
Alerting struct {
105-
Query struct {
106-
Kql string `json:"kql"`
107-
} `json:"query"`
108-
} `json:"alerting"`
109-
}{
110-
Alerting: struct {
111-
Query struct {
112-
Kql string `json:"kql"`
113-
} `json:"query"`
114-
}{
115-
Query: struct {
116-
Kql string `json:"kql"`
117-
}{
118-
Kql: model.Scope.Alerting.Kql.ValueString(),
119-
},
120-
},
121-
}
122-
}
62+
customRecurring, diags := model.CustomSchedule.Recurring.toAPIRequest(ctx)
63+
body.Schedule.Custom.Recurring = customRecurring
64+
body.Scope = model.Scope.toAPIRequest()
12365

12466
return body, diags
12567
}
@@ -161,14 +103,12 @@ func (model *MaintenanceWindowModel) fromAPIReadResponse(ctx context.Context, da
161103
/* UPDATE */
162104

163105
func (model MaintenanceWindowModel) toAPIUpdateRequest(ctx context.Context) (kbapi.PatchMaintenanceWindowIdJSONRequestBody, diag.Diagnostics) {
164-
var diags = diag.Diagnostics{}
165-
166106
body := kbapi.PatchMaintenanceWindowIdJSONRequestBody{
167107
Enabled: model.Enabled.ValueBoolPointer(),
168108
Title: model.Title.ValueStringPointer(),
169109
}
170110

171-
schedule := struct {
111+
body.Schedule = &struct {
172112
Custom struct {
173113
Duration string `json:"duration"`
174114
Recurring *struct {
@@ -201,76 +141,13 @@ func (model MaintenanceWindowModel) toAPIUpdateRequest(ctx context.Context) (kba
201141
},
202142
}
203143

204-
body.Schedule = &schedule
205-
206144
if utils.IsKnown(model.CustomSchedule.Timezone) {
207145
body.Schedule.Custom.Timezone = model.CustomSchedule.Timezone.ValueStringPointer()
208146
}
209147

210-
if model.CustomSchedule.Recurring != nil {
211-
212-
body.Schedule.Custom.Recurring = &struct {
213-
End *string `json:"end,omitempty"`
214-
Every *string `json:"every,omitempty"`
215-
Occurrences *float32 `json:"occurrences,omitempty"`
216-
OnMonth *[]float32 `json:"onMonth,omitempty"`
217-
OnMonthDay *[]float32 `json:"onMonthDay,omitempty"`
218-
OnWeekDay *[]string `json:"onWeekDay,omitempty"`
219-
}{}
220-
221-
if !model.CustomSchedule.Recurring.End.IsNull() {
222-
body.Schedule.Custom.Recurring.End = model.CustomSchedule.Recurring.End.ValueStringPointer()
223-
}
224-
225-
if !model.CustomSchedule.Recurring.Every.IsNull() {
226-
body.Schedule.Custom.Recurring.Every = model.CustomSchedule.Recurring.Every.ValueStringPointer()
227-
}
228-
229-
if !model.CustomSchedule.Recurring.Occurrences.IsNull() && !model.CustomSchedule.Recurring.Occurrences.IsUnknown() && model.CustomSchedule.Recurring.Occurrences.ValueInt32() > 0 {
230-
occurrences := float32(model.CustomSchedule.Recurring.Occurrences.ValueInt32())
231-
body.Schedule.Custom.Recurring.Occurrences = &occurrences
232-
}
233-
234-
if !model.CustomSchedule.Recurring.OnWeekDay.IsNull() && !model.CustomSchedule.Recurring.OnWeekDay.IsUnknown() {
235-
var onWeekDay []string
236-
diags.Append(model.CustomSchedule.Recurring.OnWeekDay.ElementsAs(ctx, &onWeekDay, true)...)
237-
body.Schedule.Custom.Recurring.OnWeekDay = &onWeekDay
238-
}
239-
240-
if !model.CustomSchedule.Recurring.OnMonth.IsNull() && !model.CustomSchedule.Recurring.OnMonth.IsUnknown() {
241-
var onMonth []float32
242-
diags.Append(model.CustomSchedule.Recurring.OnMonth.ElementsAs(ctx, &onMonth, true)...)
243-
body.Schedule.Custom.Recurring.OnMonth = &onMonth
244-
}
245-
246-
if !model.CustomSchedule.Recurring.OnMonthDay.IsNull() && !model.CustomSchedule.Recurring.OnMonthDay.IsUnknown() {
247-
var onMonthDay []float32
248-
diags.Append(model.CustomSchedule.Recurring.OnMonthDay.ElementsAs(ctx, &onMonthDay, true)...)
249-
body.Schedule.Custom.Recurring.OnMonthDay = &onMonthDay
250-
}
251-
}
252-
253-
if model.Scope != nil {
254-
body.Scope = &struct {
255-
Alerting struct {
256-
Query struct {
257-
Kql string `json:"kql"`
258-
} `json:"query"`
259-
} `json:"alerting"`
260-
}{
261-
Alerting: struct {
262-
Query struct {
263-
Kql string `json:"kql"`
264-
} `json:"query"`
265-
}{
266-
Query: struct {
267-
Kql string `json:"kql"`
268-
}{
269-
Kql: model.Scope.Alerting.Kql.ValueString(),
270-
},
271-
},
272-
}
273-
}
148+
customRecurring, diags := model.CustomSchedule.Recurring.toAPIRequest(ctx)
149+
body.Schedule.Custom.Recurring = customRecurring
150+
body.Scope = model.Scope.toAPIRequest()
274151

275152
return body, diags
276153
}
@@ -382,3 +259,93 @@ func (model *MaintenanceWindowModel) _fromAPIResponse(ctx context.Context, respo
382259

383260
return diags
384261
}
262+
263+
/* HELPERS */
264+
265+
func (model *MaintenanceWindowScope) toAPIRequest() *struct {
266+
Alerting struct {
267+
Query struct {
268+
Kql string `json:"kql"`
269+
} `json:"query"`
270+
} `json:"alerting"`
271+
} {
272+
if model == nil {
273+
return nil
274+
}
275+
276+
return &struct {
277+
Alerting struct {
278+
Query struct {
279+
Kql string `json:"kql"`
280+
} `json:"query"`
281+
} `json:"alerting"`
282+
}{
283+
Alerting: struct {
284+
Query struct {
285+
Kql string `json:"kql"`
286+
} `json:"query"`
287+
}{
288+
Query: struct {
289+
Kql string `json:"kql"`
290+
}{
291+
Kql: model.Alerting.Kql.ValueString(),
292+
},
293+
},
294+
}
295+
}
296+
297+
func (model *MaintenanceWindowScheduleRecurring) toAPIRequest(ctx context.Context) (*struct {
298+
End *string `json:"end,omitempty"`
299+
Every *string `json:"every,omitempty"`
300+
Occurrences *float32 `json:"occurrences,omitempty"`
301+
OnMonth *[]float32 `json:"onMonth,omitempty"`
302+
OnMonthDay *[]float32 `json:"onMonthDay,omitempty"`
303+
OnWeekDay *[]string `json:"onWeekDay,omitempty"`
304+
}, diag.Diagnostics) {
305+
if model == nil {
306+
return nil, nil
307+
}
308+
309+
var diags diag.Diagnostics
310+
result := &struct {
311+
End *string `json:"end,omitempty"`
312+
Every *string `json:"every,omitempty"`
313+
Occurrences *float32 `json:"occurrences,omitempty"`
314+
OnMonth *[]float32 `json:"onMonth,omitempty"`
315+
OnMonthDay *[]float32 `json:"onMonthDay,omitempty"`
316+
OnWeekDay *[]string `json:"onWeekDay,omitempty"`
317+
}{}
318+
319+
if utils.IsKnown(model.End) {
320+
result.End = model.End.ValueStringPointer()
321+
}
322+
323+
if utils.IsKnown(model.Every) {
324+
result.Every = model.Every.ValueStringPointer()
325+
}
326+
327+
if utils.IsKnown(model.Occurrences) {
328+
occurrences := float32(model.Occurrences.ValueInt32())
329+
result.Occurrences = &occurrences
330+
}
331+
332+
if utils.IsKnown(model.OnWeekDay) {
333+
var onWeekDay []string
334+
diags.Append(model.OnWeekDay.ElementsAs(ctx, &onWeekDay, true)...)
335+
result.OnWeekDay = &onWeekDay
336+
}
337+
338+
if utils.IsKnown(model.OnMonth) {
339+
var onMonth []float32
340+
diags.Append(model.OnMonth.ElementsAs(ctx, &onMonth, true)...)
341+
result.OnMonth = &onMonth
342+
}
343+
344+
if utils.IsKnown(model.OnMonthDay) {
345+
var onMonthDay []float32
346+
diags.Append(model.OnMonthDay.ElementsAs(ctx, &onMonthDay, true)...)
347+
result.OnMonthDay = &onMonthDay
348+
}
349+
350+
return result, diags
351+
}

0 commit comments

Comments
 (0)