Skip to content

[ResponseOps] Maintenance Window Resource #1037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
formatting
  • Loading branch information
adcoelho committed Feb 26, 2025
commit 0d2f4636e52470b8fb6b337375165280b929896f
94 changes: 92 additions & 2 deletions generated/alerting/api_alerting_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 0 additions & 7 deletions generated/alerting/model_notify_when_action.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/clients/kibana/alerting.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func ruleActionsToActionsInner(ruleActions []models.AlertingRuleAction) []alerti
if !alerting.IsNil(action.Frequency) {
frequency := alerting.ActionsInnerFrequency{
Summary: action.Frequency.Summary,
NotifyWhen: (alerting.NotifyWhen)(action.Frequency.NotifyWhen),
NotifyWhen: (alerting.NotifyWhenAction)(action.Frequency.NotifyWhen),
}

if action.Frequency.Throttle != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/clients/kibana/maintenance_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func maintenanceWindowResponseToModel(res *alerting.MaintenanceWindowResponsePro
}

return &models.MaintenanceWindow{
Id: res.Id,
Title: res.Title,
Enabled: res.Enabled,
Start: res.Start,
Expand Down
43 changes: 33 additions & 10 deletions internal/kibana/maintenance_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func ResourceMaintenanceWindow() *schema.Resource {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rebuild this as a plugin-framework based resource (data view as an example). It automatically solves a bunch of TF related limitations, and should be the future for any new work in the provider.

apikeySchema := map[string]*schema.Schema{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like this wasn't generated but implemented by you. If so, couldn't this be generated based on the OAS files we create on the Kibana side?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generation is only for the API client, so the resource specification is usually done manually. Maybe there is a way but I don't think we use it in this project.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hashicorp have an openapi generator which builds a schema based on a set of openapi routes. It's not used in this project yet, but we're using it to help with Serverless project resources. It's... ok, but has some limitations to deal with.

"maintenance_window_id": {
"id": {
Description: "A UUID v1 or v4 to use instead of a randomly generated ID.",
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -48,7 +48,7 @@ func ResourceMaintenanceWindow() *schema.Resource {
Description: "Creates a Kibana Maintenance Window.",

CreateContext: resourceMaintenanceWindowCreate,
// UpdateContext: resourceMaintenanceWindowUpdate,
UpdateContext: resourceMaintenanceWindowUpdate,
ReadContext: resourceMaintenanceWindowRead,
DeleteContext: resourceMaintenanceWindowDelete,

Expand All @@ -63,16 +63,15 @@ func ResourceMaintenanceWindow() *schema.Resource {
func getMaintenanceWindowFromResourceData(d *schema.ResourceData, serverVersion *version.Version) (models.MaintenanceWindow, diag.Diagnostics) {
var diags diag.Diagnostics
maintenanceWindow := models.MaintenanceWindow{
MaintenanceWindowID: d.Get("maintenance_window_id").(string),
Title: d.Get("title").(string),
Enabled: d.Get("enabled").(bool),
Start: d.Get("start").(string),
Duration: d.Get("duration").(int),
Title: d.Get("title").(string),
Enabled: d.Get("enabled").(bool),
Start: d.Get("start").(string),
Duration: d.Get("duration").(int),
}

// Explicitly set maintenance window id if provided, otherwise we'll use the autogenerated ID from the Kibana API response
if maintenanceWindowID := getOrNilString("MaintenanceWindowID", d); maintenanceWindowID != nil && *maintenanceWindowID != "" {
maintenanceWindow.MaintenanceWindowID = *maintenanceWindowID
if maintenanceWindowID := getOrNilString("id", d); maintenanceWindowID != nil && *maintenanceWindowID != "" {
maintenanceWindow.Id = *maintenanceWindowID
}

return maintenanceWindow, diags
Expand Down Expand Up @@ -105,13 +104,37 @@ func resourceMaintenanceWindowCreate(ctx context.Context, d *schema.ResourceData
return resourceRuleRead(ctx, d, meta)
}

func resourceMaintenanceWindowUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, diags := clients.NewApiClientFromSDKResource(d, meta)
if diags.HasError() {
return diags
}

serverVersion, diags := client.ServerVersion(ctx)
if diags.HasError() {
return diags
}

maintenanceWindow, diags := getMaintenanceWindowFromResourceData(d, serverVersion)
if diags.HasError() {
return diags
}

// DO NOTHING
// res, diags := kibana.UpdateAlertingRule(ctx, client, maintenanceWindow)
d.SetId(maintenanceWindow.MaintenanceWindowID)

return resourceRuleRead(ctx, d, meta)
}

func resourceMaintenanceWindowRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, diags := clients.NewApiClientFromSDKResource(d, meta)
if diags.HasError() {
return diags
}

maintenanceWindow, diags := kibana.GetMaintenanceWindow(ctx, client, d.Id())

if maintenanceWindow == nil && diags == nil {
d.SetId("")
return diags
Expand All @@ -121,7 +144,7 @@ func resourceMaintenanceWindowRead(ctx context.Context, d *schema.ResourceData,
}

// set the fields
if err := d.Set("maintenance_window_id", maintenanceWindow.MaintenanceWindowID); err != nil {
if err := d.Set("id", maintenanceWindow.Id); err != nil {
return diag.FromErr(err)
}
if err := d.Set("title", maintenanceWindow.Title); err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/models/maintenance_window.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package models

type MaintenanceWindow struct {
Id string
MaintenanceWindowID string
Title string
Enabled bool
Expand Down
Loading