Skip to content

Commit

Permalink
CDPCP-9629 ID Broker resource shoudl detect if the environment is del…
Browse files Browse the repository at this point in the history
…eted
  • Loading branch information
daszabo committed Oct 31, 2023
1 parent cdf2374 commit f9d6ff8
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions resources/environments/resource_id_broker_mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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(),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit f9d6ff8

Please sign in to comment.