Open
Description
Describe the bug
When resources are manually removed in the Avi Controller while existing within Terraform state, the provider attempts to read it and fails with a 404 error, causing the Terraform run to stop. Instead, the provider should recognize that the resource no longer exists and remove it from state or recreate it if specified in the configuration.
I have tested a handful of resources, and they have the same problem, as they all utilize the same APIRead function for retrieving and erroring:
https://github.com/vmware/terraform-provider-avi/blob/eng/avi/utils.go#L424-L425
╷
│ Error: Encountered an error on GET request to URL https://avicontroller-nonprod.contoso.com/api/alertscriptconfig/alertscriptconfig-0f1e3696-099a-464c-9309-75f5401b9578: HTTP code: 404; error from Controller: map[error:AlertScriptConfig object not found!]
│
│ with avi_alertscriptconfig.test,
│ on main.tf line 20, in resource "avi_alertscriptconfig" "test":
│ 20: resource "avi_alertscriptconfig" "test" {
│
╵
Reproduction steps
- Create an
avi_alertscriptconfig
resource using Terraform. - Delete the alertscriptconfig object directly in the Avi Controller interface or via API.
- Run a terraform plan.
terraform {
required_providers {
avi = {
source = "vmware/avi"
version = "30.2.2"
}
}
}
provider "avi" {
avi_username = "USER"
avi_password = "PASSWORD"
avi_controller = "avicontroller.contoso.com"
avi_tenant = "admin"
avi_version = "30.2.2"
}
resource "avi_alertscriptconfig" "test" {
action_script = "foo"
name = "test-controlscript"
}
Terraform Debug output:
avi_alertscriptconfig.test: Refreshing state... [id=https://avicontroller-nonprod.contoso.com/api/alertscriptconfig/alertscriptconfig-0f1e3696-099a-464c-9309-75f5401b9578]
2025-01-25T01:37:07.079-0600 [INFO] provider.terraform-provider-avi_v30.2.2.exe: 2025/01/25 01:37:07 [INFO] APIRead Found Tenant Ref admin: timestamp=2025-01-25T01:37:07.078-0600
2025-01-25T01:37:07.079-0600 [INFO] provider.terraform-provider-avi_v30.2.2.exe: 2025/01/25 01:37:07 [DEBUG] APIRead reading object with objType alertscriptconfig id https://avicontroller-nonprod.contoso.com/api/alertscriptconfig/alertscriptconfig-0f1e3696-099a-464c-9309-75f5401b9578: timestamp=2025-01-25T01:37:07.079-0600
2025-01-25T01:37:07.080-0600 [INFO] provider.terraform-provider-avi_v30.2.2.exe: 2025/01/25 01:37:07 [DEBUG] APIRead reading object with objType alertscriptconfig id https://avicontroller-nonprod.contoso.com/api/alertscriptconfig/alertscriptconfig-0f1e3696-099a-464c-9309-75f5401b9578: timestamp=2025-01-25T01:37:07.079-0600
2025-01-25T01:37:07.080-0600 [INFO] provider.terraform-provider-avi_v30.2.2.exe: 2025/01/25 01:37:07 [DEBUG] APIRead reading object with id alertscriptconfig-0f1e3696-099a-464c-9309-75f5401b9578 path api/alertscriptconfig/alertscriptconfig-0f1e3696-099a-464c-9309-75f5401b9578: timestamp=2025-01-25T01:37:07.079-0600
2025-01-25T01:37:07.127-0600 [INFO] provider.terraform-provider-avi_v30.2.2.exe: 2025/01/25 01:37:07 [ERROR] APIRead object with uuid alertscriptconfig-0f1e3696-099a-464c-9309-75f5401b9578 not found err Encountered an error on GET request to URL https://avicontroller-nonprod.contoso.com/api/alertscriptconfig/alertscriptconfig-0f1e3696-099a-464c-9309-75f5401b9578: HTTP code: 404; error from Controller: map[error:AlertScriptConfig object not found!]: timestamp=2025-01-25T01:37:07.127-0600
2025-01-25T01:37:07.128-0600 [INFO] provider.terraform-provider-avi_v30.2.2.exe: 2025/01/25 01:37:07 [ERROR] in reading object Encountered an error on GET request to URL https://avicontroller-nonprod.contoso.com/api/alertscriptconfig/alertscriptconfig-0f1e3696-099a-464c-9309-75f5401b9578: HTTP code: 404; error from Controller: map[error:AlertScriptConfig object not found!]: timestamp=2025-01-25T01:37:07.127-0600
2025-01-25T01:37:07.128-0600 [ERROR] vertex "avi_alertscriptconfig.test" error: Encountered an error on GET request to URL https://avicontroller-nonprod.contoso.com/api/alertscriptconfig/alertscriptconfig-0f1e3696-099a-464c-9309-75f5401b9578: HTTP code: 404; error from Controller: map[error:AlertScriptConfig object not found!]
2025-01-25T01:37:07.128-0600 [ERROR] provider.terraform-provider-avi_v30.2.2.exe: Response contains error diagnostic: @caller=github.com/hashicorp/[email protected]/tfprotov5/internal/diag/diagnostics.go:56 diagnostic_detail="" tf_provider_addr=provider tf_rpc=ReadResource tf_resource_type=avi_alertscriptconfig @module=sdk.proto diagnostic_severity=ERROR diagnostic_summary="Encountered an error on GET request to URL https://avicontroller-nonprod.contoso.com/api/alertscriptconfig/alertscriptconfig-0f1e3696-099a-464c-9309-75f5401b9578: HTTP code: 404; error from Controller: map[error:AlertScriptConfig object not found!]" tf_proto_version=5.3 tf_req_id=40a083bb-3aad-427a-0380-e413b6c7effe timestamp=2025-01-25T01:37:07.127-0600
2025-01-25T01:37:07.128-0600 [ERROR] vertex "avi_alertscriptconfig.test (expand)" error: Encountered an error on GET request to URL https://avicontroller-nonprod.contoso.com/api/alertscriptconfig/alertscriptconfig-0f1e3696-099a-464c-9309-75f5401b9578: HTTP code: 404; error from Controller: map[error:AlertScriptConfig object not found!]
2025-01-25T01:37:07.129-0600 [WARN] Planning encountered errors, so plan is not applyable
╷
│ Error: Encountered an error on GET request to URL https://avicontroller-nonprod.contoso.com/api/alertscriptconfig/alertscriptconfig-0f1e3696-099a-464c-9309-75f5401b9578: HTTP code: 404; error from Controller: map[error:AlertScriptConfig object not found!]
│
│ with avi_alertscriptconfig.test,
│ on main.tf line 20, in resource "avi_alertscriptconfig" "test":
│ 20: resource "avi_alertscriptconfig" "test" {
│
╵
### Expected behavior
The provider should gracefully handle the missing resource by setting the resource ID to empty or removing it from Terraform’s state. Terraform would then decide whether to recreate it (if it’s still in the configuration) or leave it removed (if it’s no longer in the configuration).
### Additional context
Environment:
- Terraform v1.8.0
- terraform-provider-avi v30.2.2
- AVI 30.2.2