Skip to content

Commit fd54e2b

Browse files
committed
fix: delete certificate on resource destruction
1 parent d44ee33 commit fd54e2b

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

fwprovider/nodes/resource_acme_certificate.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,7 @@ func (r *acmeCertificateResource) Update(
445445
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
446446
}
447447

448-
// Delete removes the certificate resource from Terraform state and cleans up ACME configuration from the node.
449-
// The certificate files are preserved on the Proxmox node, but the ACME configuration is removed.
448+
// Delete removes the certificate resource from Terraform state and cleans up ACME configuration and certificate from the node.
450449
func (r *acmeCertificateResource) Delete(
451450
ctx context.Context,
452451
req resource.DeleteRequest,
@@ -463,14 +462,26 @@ func (r *acmeCertificateResource) Delete(
463462
nodeName := state.NodeName.ValueString()
464463
nodeClient := r.client.Node(nodeName)
465464

466-
// Clean up the ACME configuration from the node. The certificate files will remain.
465+
// Delete the custom certificate
466+
restart := proxmoxtypes.CustomBool(true)
467+
deleteReq := &nodes.CertificateDeleteRequestBody{
468+
Restart: &restart,
469+
}
470+
471+
if err := nodeClient.DeleteCertificate(ctx, deleteReq); err != nil {
472+
resp.Diagnostics.AddWarning(
473+
"Failed to delete certificate",
474+
fmt.Sprintf("An error occurred while deleting the certificate for node %s: %s", nodeName, err.Error()),
475+
)
476+
}
477+
478+
// Clean up the ACME configuration from the node
467479
toDelete := "acme,acmedomain0,acmedomain1,acmedomain2,acmedomain3,acmedomain4"
468480
configUpdate := &nodes.ConfigUpdateRequestBody{
469481
Delete: &toDelete,
470482
}
471483

472484
if err := nodeClient.UpdateConfig(ctx, configUpdate); err != nil {
473-
// Log a warning as the resource is being deleted anyway, but the user should be notified.
474485
resp.Diagnostics.AddWarning(
475486
"Failed to clean up node ACME configuration",
476487
fmt.Sprintf("An error occurred while cleaning up ACME settings for node %s on delete: %s. Manual cleanup of /etc/pve/nodes/%s/config may be required.", nodeName, err.Error(), nodeName),

0 commit comments

Comments
 (0)