diff --git a/resources/environments/resource_id_broker_mappings.go b/resources/environments/resource_id_broker_mappings.go index 3ae903dc..12f80e80 100644 --- a/resources/environments/resource_id_broker_mappings.go +++ b/resources/environments/resource_id_broker_mappings.go @@ -14,6 +14,7 @@ import ( "context" "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/cdp" + "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/environments/client" "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/environments/client/operations" environmentsmodels "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/environments/models" "github.com/cloudera/terraform-provider-cdp/utils" @@ -200,6 +201,21 @@ func isSetIDBEnvNotFoundError(err error) bool { return false } +func isEnvironmentStillExists(ctx context.Context, client *client.Environments, envName string, state *idBrokerMappingsResourceModel) bool { + envParams := operations.NewDescribeEnvironmentParamsWithContext(ctx) + envParams.WithInput(&environmentsmodels.DescribeEnvironmentRequest{ + EnvironmentName: &envName, + }) + _, err := client.Operations.DescribeEnvironment(envParams) + if err != nil { + tflog.Warn(ctx, "Something happened during environment fetch: "+err.Error()) + if isEnvNotFoundError(err) { + return false + } + } + return true +} + func (r *idBrokerMappingsResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { var state idBrokerMappingsResourceModel diags := req.State.Get(ctx, &state) @@ -209,6 +225,16 @@ func (r *idBrokerMappingsResource) Read(ctx context.Context, req resource.ReadRe } client := r.client.Environments + + if !isEnvironmentStillExists(ctx, client, state.EnvironmentName.ValueString(), &state) { + resp.Diagnostics.AddWarning("Resource not found on provider", "Environment not found, removing ID Broker mapping from state.") + tflog.Warn(ctx, "Environment not found, removing ID Broker mapping from state", map[string]interface{}{ + "id": state.ID.ValueString(), + }) + resp.State.RemoveResource(ctx) + return + } + params := operations.NewGetIDBrokerMappingsParamsWithContext(ctx) params.WithInput(&environmentsmodels.GetIDBrokerMappingsRequest{ EnvironmentName: state.EnvironmentName.ValueStringPointer(), @@ -281,6 +307,15 @@ func (r *idBrokerMappingsResource) Update(ctx context.Context, req resource.Upda client := r.client.Environments + if !isEnvironmentStillExists(ctx, client, state.EnvironmentName.ValueString(), &state) { + resp.Diagnostics.AddWarning("Resource not found on provider", "Environment not found, removing ID Broker mapping from state.") + tflog.Warn(ctx, "Environment not found, removing ID Broker mapping from state", map[string]interface{}{ + "id": state.ID.ValueString(), + }) + resp.State.RemoveResource(ctx) + return + } + params := operations.NewSetIDBrokerMappingsParamsWithContext(ctx) params.WithInput(toSetIDBrokerMappingsRequest(ctx, &state, &resp.Diagnostics)) responseOk, err := client.Operations.SetIDBrokerMappings(params) @@ -319,6 +354,15 @@ func (r *idBrokerMappingsResource) Delete(ctx context.Context, req resource.Dele client := r.client.Environments + if !isEnvironmentStillExists(ctx, client, state.EnvironmentName.ValueString(), &state) { + resp.Diagnostics.AddWarning("Resource not found on provider", "Environment not found, removing ID Broker mapping from state.") + tflog.Warn(ctx, "Environment not found, removing ID Broker mapping from state", map[string]interface{}{ + "id": state.ID.ValueString(), + }) + resp.State.RemoveResource(ctx) + return + } + params := operations.NewSetIDBrokerMappingsParamsWithContext(ctx) input := &environmentsmodels.SetIDBrokerMappingsRequest{} input.EnvironmentName = state.EnvironmentName.ValueStringPointer()