Skip to content

Commit f62cdab

Browse files
committed
Simplify model code.
1 parent 60c005b commit f62cdab

File tree

1 file changed

+61
-133
lines changed

1 file changed

+61
-133
lines changed

internal/kibana/maintenance_window/models.go

Lines changed: 61 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package maintenance_window
22

33
import (
44
"context"
5+
"encoding/json"
56

67
"github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
78
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
@@ -128,64 +129,9 @@ func (model *MaintenanceWindowModel) fromAPICreateResponse(ctx context.Context,
128129
return nil
129130
}
130131

131-
response := data.JSON200
132-
133-
var diags diag.Diagnostics
134-
135-
resourceID := clients.CompositeId{
136-
ClusterId: model.SpaceID.ValueString(),
137-
ResourceId: response.Id,
138-
}
139-
140-
model.ID = types.StringValue(resourceID.String())
141-
model.Title = types.StringValue(response.Title)
142-
model.Enabled = types.BoolValue(response.Enabled)
143-
144-
model.CustomSchedule = MaintenanceWindowSchedule{
145-
Start: types.StringValue(response.Schedule.Custom.Start),
146-
Duration: types.StringValue(response.Schedule.Custom.Duration),
147-
Timezone: types.StringPointerValue(response.Schedule.Custom.Timezone),
148-
}
149-
150-
if response.Schedule.Custom.Recurring != nil {
151-
model.CustomSchedule.Recurring = &MaintenanceWindowScheduleRecurring{
152-
End: types.StringPointerValue(response.Schedule.Custom.Recurring.End),
153-
Every: types.StringPointerValue(response.Schedule.Custom.Recurring.Every),
154-
OnWeekDay: types.ListNull(types.StringType),
155-
OnMonth: types.ListNull(types.Int32Type),
156-
OnMonthDay: types.ListNull(types.Int32Type),
157-
}
158-
159-
if response.Schedule.Custom.Recurring.Occurrences != nil {
160-
occurrences := int32(*response.Schedule.Custom.Recurring.Occurrences)
161-
model.CustomSchedule.Recurring.Occurrences = types.Int32PointerValue(&occurrences)
162-
}
163-
164-
if response.Schedule.Custom.Recurring.OnWeekDay != nil {
165-
onWeekDay, _ := types.ListValueFrom(ctx, types.StringType, response.Schedule.Custom.Recurring.OnWeekDay)
166-
model.CustomSchedule.Recurring.OnWeekDay = onWeekDay
167-
}
168-
169-
if response.Schedule.Custom.Recurring.OnMonth != nil {
170-
onMonth, _ := types.ListValueFrom(ctx, types.Float32Type, response.Schedule.Custom.Recurring.OnMonth)
171-
model.CustomSchedule.Recurring.OnMonth = onMonth
172-
}
173-
174-
if response.Schedule.Custom.Recurring.OnMonthDay != nil {
175-
onMonthDay, _ := types.ListValueFrom(ctx, types.Float32Type, response.Schedule.Custom.Recurring.OnMonthDay)
176-
model.CustomSchedule.Recurring.OnMonthDay = onMonthDay
177-
}
178-
}
179-
180-
if response.Scope != nil {
181-
model.Scope = &MaintenanceWindowScope{
182-
Alerting: MaintenanceWindowAlertingScope{
183-
Kql: types.StringValue(response.Scope.Alerting.Query.Kql),
184-
},
185-
}
186-
}
187-
188-
return diags
132+
var response = &ResponseJson{}
133+
json.Unmarshal(data.Body, response)
134+
return model._fromAPIResponse(ctx, *response)
189135
}
190136

191137
/* READ */
@@ -195,64 +141,9 @@ func (model *MaintenanceWindowModel) fromAPIReadResponse(ctx context.Context, da
195141
return nil
196142
}
197143

198-
response := data.JSON200
199-
200-
var diags diag.Diagnostics
201-
202-
resourceID := clients.CompositeId{
203-
ClusterId: model.SpaceID.ValueString(),
204-
ResourceId: response.Id,
205-
}
206-
207-
model.ID = types.StringValue(resourceID.String())
208-
model.Title = types.StringValue(response.Title)
209-
model.Enabled = types.BoolValue(response.Enabled)
210-
211-
model.CustomSchedule = MaintenanceWindowSchedule{
212-
Start: types.StringValue(response.Schedule.Custom.Start),
213-
Duration: types.StringValue(response.Schedule.Custom.Duration),
214-
Timezone: types.StringPointerValue(response.Schedule.Custom.Timezone),
215-
}
216-
217-
if response.Schedule.Custom.Recurring != nil {
218-
model.CustomSchedule.Recurring = &MaintenanceWindowScheduleRecurring{
219-
End: types.StringPointerValue(response.Schedule.Custom.Recurring.End),
220-
Every: types.StringPointerValue(response.Schedule.Custom.Recurring.Every),
221-
OnWeekDay: types.ListNull(types.StringType),
222-
OnMonth: types.ListNull(types.Float32Type),
223-
OnMonthDay: types.ListNull(types.Float32Type),
224-
}
225-
226-
if response.Schedule.Custom.Recurring.Occurrences != nil {
227-
occurrences := int32(*response.Schedule.Custom.Recurring.Occurrences)
228-
model.CustomSchedule.Recurring.Occurrences = types.Int32PointerValue(&occurrences)
229-
}
230-
231-
if response.Schedule.Custom.Recurring.OnWeekDay != nil {
232-
onWeekDay, _ := types.ListValueFrom(ctx, types.StringType, response.Schedule.Custom.Recurring.OnWeekDay)
233-
model.CustomSchedule.Recurring.OnWeekDay = onWeekDay
234-
}
235-
236-
if response.Schedule.Custom.Recurring.OnMonth != nil {
237-
onMonth, _ := types.ListValueFrom(ctx, types.Float32Type, response.Schedule.Custom.Recurring.OnMonth)
238-
model.CustomSchedule.Recurring.OnMonth = onMonth
239-
}
240-
241-
if response.Schedule.Custom.Recurring.OnMonthDay != nil {
242-
onMonthDay, _ := types.ListValueFrom(ctx, types.Float32Type, response.Schedule.Custom.Recurring.OnMonthDay)
243-
model.CustomSchedule.Recurring.OnMonthDay = onMonthDay
244-
}
245-
}
246-
247-
if response.Scope != nil {
248-
model.Scope = &MaintenanceWindowScope{
249-
Alerting: MaintenanceWindowAlertingScope{
250-
Kql: types.StringValue(response.Scope.Alerting.Query.Kql),
251-
},
252-
}
253-
}
254-
255-
return diags
144+
var response = &ResponseJson{}
145+
json.Unmarshal(data.Body, response)
146+
return model._fromAPIResponse(ctx, *response)
256147
}
257148

258149
/* UPDATE */
@@ -383,7 +274,60 @@ func (model *MaintenanceWindowModel) fromAPIUpdateResponse(ctx context.Context,
383274
return nil
384275
}
385276

386-
response := data.JSON200
277+
var response = &ResponseJson{}
278+
json.Unmarshal(data.Body, response)
279+
return model._fromAPIResponse(ctx, *response)
280+
}
281+
282+
/* DELETE */
283+
284+
func (model MaintenanceWindowModel) getMaintenanceWindowIDAndSpaceID() (maintenanceWindowID string, spaceID string) {
285+
maintenanceWindowID = model.ID.ValueString()
286+
spaceID = model.SpaceID.ValueString()
287+
288+
resourceID := model.ID.ValueString()
289+
maybeCompositeID, _ := clients.CompositeIdFromStr(resourceID)
290+
if maybeCompositeID != nil {
291+
maintenanceWindowID = maybeCompositeID.ResourceId
292+
spaceID = maybeCompositeID.ClusterId
293+
}
294+
295+
return
296+
}
297+
298+
/* UTILS */
299+
300+
type ResponseJson struct {
301+
CreatedAt string `json:"created_at"`
302+
CreatedBy *string `json:"created_by"`
303+
Enabled bool `json:"enabled"`
304+
Id string `json:"id"`
305+
Schedule struct {
306+
Custom struct {
307+
Duration string `json:"duration"`
308+
Recurring *struct {
309+
End *string `json:"end,omitempty"`
310+
Every *string `json:"every,omitempty"`
311+
Occurrences *float32 `json:"occurrences,omitempty"`
312+
OnMonth *[]float32 `json:"onMonth,omitempty"`
313+
OnMonthDay *[]float32 `json:"onMonthDay,omitempty"`
314+
OnWeekDay *[]string `json:"onWeekDay,omitempty"`
315+
} `json:"recurring,omitempty"`
316+
Start string `json:"start"`
317+
Timezone *string `json:"timezone,omitempty"`
318+
} `json:"custom"`
319+
} `json:"schedule"`
320+
Scope *struct {
321+
Alerting struct {
322+
Query struct {
323+
Kql string `json:"kql"`
324+
} `json:"query"`
325+
} `json:"alerting"`
326+
} `json:"scope,omitempty"`
327+
Title string `json:"title"`
328+
}
329+
330+
func (model *MaintenanceWindowModel) _fromAPIResponse(ctx context.Context, response ResponseJson) diag.Diagnostics {
387331

388332
var diags diag.Diagnostics
389333

@@ -442,19 +386,3 @@ func (model *MaintenanceWindowModel) fromAPIUpdateResponse(ctx context.Context,
442386

443387
return diags
444388
}
445-
446-
/* DELETE */
447-
448-
func (model MaintenanceWindowModel) getMaintenanceWindowIDAndSpaceID() (maintenanceWindowID string, spaceID string) {
449-
maintenanceWindowID = model.ID.ValueString()
450-
spaceID = model.SpaceID.ValueString()
451-
452-
resourceID := model.ID.ValueString()
453-
maybeCompositeID, _ := clients.CompositeIdFromStr(resourceID)
454-
if maybeCompositeID != nil {
455-
maintenanceWindowID = maybeCompositeID.ResourceId
456-
spaceID = maybeCompositeID.ClusterId
457-
}
458-
459-
return
460-
}

0 commit comments

Comments
 (0)