Skip to content

Commit

Permalink
Require replace on FAILED state
Browse files Browse the repository at this point in the history
When a DataSource is in the FAILED state, if there is any other change to the
resource to trigger an apply graph check, we will force a resoruce replacement.
DataSources anyways currently require replacement on all attributes at the moment
but when credential rotation or other updates are supported this will still be
needed.
  • Loading branch information
tjschutte committed Feb 9, 2024
1 parent 4a9ddf1 commit 4c0cbf1
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions internal/provider/data_source_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ func (r *dataSourceResource) Schema(ctx context.Context, req resource.SchemaRequ
MarkdownDescription: "The current state of the Ambar resource.",
Description: "The current state of the Ambar resource.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplaceIf(doesStateRequireReplace,
"If the state of the resource is not valid for further use, such as FAILED",
"If the state of the resource is not valid for further use, such as FAILED"),
},
},
"resource_id": schema.StringAttribute{
MarkdownDescription: "The unique Ambar resource id for this resource.",
Expand All @@ -97,6 +102,22 @@ func (r *dataSourceResource) Schema(ctx context.Context, req resource.SchemaRequ
}
}

func doesStateRequireReplace(ctx context.Context, request planmodifier.StringRequest, response *stringplanmodifier.RequiresReplaceIfFuncResponse) {
var data dataSourceResourceModel

// Read Terraform prior state data into the model
request.State.Get(ctx, &data)

tflog.Debug(ctx, "Got value for state: "+data.State.String())

if data.State.String() == "FAILED" {
response.RequiresReplace = true
return
}

return

Check failure on line 118 in internal/provider/data_source_resource.go

View workflow job for this annotation

GitHub Actions / Build

S1023: redundant `return` statement (gosimple)
}

func (r *dataSourceResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
Expand Down Expand Up @@ -153,7 +174,7 @@ func (r *dataSourceResource) Create(ctx context.Context, req resource.CreateRequ
plan.State = types.StringValue(createResourceResponse.State)

// Set state to fully populated data
diags = resp.State.Set(ctx, plan)
diags = resp.State.Set(ctx, &plan)
resp.Diagnostics.Append(diags...)

var describeDataSource Ambar.DescribeResourceRequest
Expand All @@ -165,22 +186,31 @@ func (r *dataSourceResource) Create(ctx context.Context, req resource.CreateRequ
time.Sleep(10 * time.Second)

describeResourceResponse, _, err = r.client.AmbarAPI.DescribeDataSource(ctx).DescribeResourceRequest(describeDataSource).Execute()
tflog.Debug(ctx, "Got state: "+describeResourceResponse.State)
if err != nil {
tflog.Error(ctx, "Got error!"+err.Error())
return
}

tflog.Debug(ctx, "Got state: "+describeResourceResponse.State)
if describeResourceResponse.State == "READY" {
break
}

if describeResourceResponse.State == "FAILED" {
tflog.Info(ctx, "Error creating the DataSource, failing creation.")
resp.Diagnostics.AddError(
"Error creating DataSource",
"Could not create DataSource, resource in FAILED state indicating errors while creating with passed values.",
)
return
}
}

// Map response body to schema and populate Computed attribute values
plan.State = types.StringValue(describeResourceResponse.State)

// Set state to fully populated data
diags = resp.State.Set(ctx, plan)
diags = resp.State.Set(ctx, &plan)
resp.Diagnostics.Append(diags...)
}

Expand Down

0 comments on commit 4c0cbf1

Please sign in to comment.