I’m running into an issue with a custom provider using he Terraform Provider Framework.
The service I’m integrating with exposes the following endpoints:
GET /webhooks
POST /webooks
GET /webooks/{:id}
PUT /webooks/{:id}
DELETE /webooks/{:id}
The API is not consistent IMHO - deleting a resource via DELETE /webhooks/{:id}
only removes the webhook from the collection (e.g. GET /webooks
) - the resource is still available at GET /webhooks/{:id}
.
Unfortunately I’m unable to change the API, so I want to work around this limitation in my custom provider. I need help determining a way of properly denoting a deleted resource so that Terraform knows the resource has been deleted and needs to be re-created. This is my current attempt, although it’s still not working.
Here is a snippet containing the relevant Read
method from the webhook resource:
// Read refreshes the Terraform state with the latest data.
func (r *webhookResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state WebhookResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// *** CHECKING FOR EXISTING WEBHOOK
tflog.Info(ctx, "Reading all webhooks")
exists := false
webhooks, err := r.client.GetWebhooks()
if err != nil {
resp.Diagnostics.AddError(
"Error reading webhooks",
"Could not read webhook, unexpected error: "+err.Error(),
)
return
}
for _, w := range webhooks {
tflog.Info(ctx, fmt.Sprintf("Found webhook[%s]: %+v", w.ID, w))
if state.ID.ValueString() == w.ID {
exists = true
}
}
// *** How can you set state to "removed"?
if !exists {
tflog.Info(ctx, "Webhook does not exist!")
state = WebhookResourceModel{}
// Set empty state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// *** Assuming webhook exists
tflog.Info(ctx, fmt.Sprintf("Reading webhook id: %s", state.ID.ValueString()))
// Continue with normal reading of state...
}
Here’s a link to the provider for reference: