Skip to content
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

Adding plan modifier for manager reset #134

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 14 additions & 50 deletions redfish/provider/resource_redfish_manager_reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
Expand Down Expand Up @@ -58,6 +60,9 @@ func ManagerResetSchema() map[string]schema.Attribute {
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
},
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"reset_type": schema.StringAttribute{
MarkdownDescription: "The type of the reset operation to be performed. Accepted value: GracefulRestart",
Expand All @@ -68,6 +73,9 @@ func ManagerResetSchema() map[string]schema.Attribute {
string(redfish.GracefulRestartResetType),
),
},
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
}
}
Expand Down Expand Up @@ -156,56 +164,12 @@ func (r *managerResetResource) Read(ctx context.Context, req resource.ReadReques
}

// Update updates the resource and sets the updated Terraform state on success.
func (r *managerResetResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Get state Data
tflog.Trace(ctx, "resource_manager_reset update: started")
var state, plan models.RedfishManagerReset
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

// Get plan Data
diags = req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

// Lock the mutex to avoid race conditions with other resources
redfishMutexKV.Lock(plan.RedfishServer[0].Endpoint.ValueString())
defer redfishMutexKV.Unlock(plan.RedfishServer[0].Endpoint.ValueString())

resetType := plan.ResetType.ValueString()
managerID := plan.Id.ValueString()

// Get manager
manager, err := getManager(r, plan, managerID)
if err != nil {
resp.Diagnostics.AddError("Error while retrieving manager from redfish API", err.Error())
return
}

// Perform manager reset
err = manager.Reset(redfish.ResetType(resetType))
if err != nil {
resp.Diagnostics.AddError("Error resetting manager", err.Error())
return
}

// Check iDRAC status
err = checkServerStatus(ctx, plan.RedfishServer[0].Endpoint.ValueString(), defaultCheckInterval, defaultCheckTimeout)
if err != nil {
resp.Diagnostics.AddError("Error while rebooting iDRAC. Operation may take longer duration to complete", err.Error())
return
}

tflog.Trace(ctx, "resource_manager_reset update: finished state update")
// Save into State
diags = resp.State.Set(ctx, &plan)
resp.Diagnostics.Append(diags...)
tflog.Trace(ctx, "resource_manager_reset update: finished")
func (*managerResetResource) Update(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) {
// Update should never happen, it will destroy and create in case of update
resp.Diagnostics.AddError(
"Error updating Manager reset.",
"An update plan of Manager Reset should never be invoked. This resource is supposed to be replaced on update.",
)
}

// Delete deletes the resource and removes the Terraform state on success.
Expand Down
Loading