chore: work save

This commit is contained in:
Marcel S. Henselin 2026-01-27 09:58:06 +01:00
parent d20cf15f40
commit a60b1db1f4
8 changed files with 364 additions and 63 deletions

View file

@ -283,7 +283,8 @@ func modelToCreateInstancePayload(netAcl []string, model postgresflexalpha.Insta
// Read refreshes the Terraform state with the latest data.
func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { // nolint:gocritic // function signature required by Terraform
functionError := "read instance failed"
functionErrorSummary := "read instance failed"
var model postgresflexalpha.InstanceModel
diags := req.State.Get(ctx, &model)
resp.Diagnostics.Append(diags...)
@ -300,9 +301,47 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
ctx = core.InitProviderContext(ctx)
projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString()
region := r.providerData.GetRegionWithOverride(model.Region)
// region := r.providerData.GetRegionWithOverride(model.Region)
// instanceId := model.InstanceId.ValueString()
var projectId string
if !model.ProjectId.IsNull() && !model.ProjectId.IsUnknown() {
projectId = model.ProjectId.ValueString()
} else {
if identityData.ProjectID.IsNull() || identityData.ProjectID.IsUnknown() {
core.LogAndAddError(ctx, &resp.Diagnostics, functionErrorSummary, "project_id not found in config")
return
}
projectId = identityData.ProjectID.ValueString()
}
var region string
if !model.Region.IsNull() && !model.Region.IsUnknown() {
region = r.providerData.GetRegionWithOverride(model.Region)
} else {
if identityData.Region.IsNull() || identityData.Region.IsUnknown() {
core.LogAndAddError(ctx, &resp.Diagnostics, functionErrorSummary, "region not found in config")
return
}
region = r.providerData.GetRegionWithOverride(identityData.Region)
}
var instanceId string
if !model.InstanceId.IsNull() && !model.InstanceId.IsUnknown() {
instanceId = model.InstanceId.ValueString()
} else {
if identityData.InstanceID.IsNull() || identityData.InstanceID.IsUnknown() {
core.LogAndAddError(ctx, &resp.Diagnostics, functionErrorSummary, "instance_id not found in config")
return
}
region = identityData.Region.ValueString()
}
tflog.Info(ctx, "Reading instance model", map[string]interface{}{
"projectId": projectId,
"region": region,
"instanceId": instanceId,
})
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "region", region)
@ -314,7 +353,7 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
resp.State.RemoveResource(ctx)
return
}
core.LogAndAddError(ctx, &resp.Diagnostics, functionError, err.Error())
core.LogAndAddError(ctx, &resp.Diagnostics, functionErrorSummary, err.Error())
return
}
@ -322,7 +361,7 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
respInstanceID, ok := instanceResp.GetIdOk()
if !ok {
core.LogAndAddError(ctx, &resp.Diagnostics, functionError, "response provided no ID")
core.LogAndAddError(ctx, &resp.Diagnostics, functionErrorSummary, "response provided no ID")
return
}
if !model.InstanceId.IsUnknown() && !model.InstanceId.IsNull() {
@ -330,7 +369,7 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
core.LogAndAddError(
ctx,
&resp.Diagnostics,
functionError,
functionErrorSummary,
"ID in response did not match ID in state",
)
return
@ -339,7 +378,7 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
err = mapGetInstanceResponseToModel(ctx, &model, instanceResp)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, functionError, fmt.Sprintf("Processing API payload: %v", err))
core.LogAndAddError(ctx, &resp.Diagnostics, functionErrorSummary, fmt.Sprintf("Processing API payload: %v", err))
return
}
@ -511,6 +550,9 @@ func (r *instanceResource) Delete(ctx context.Context, req resource.DeleteReques
// The expected format of the resource import identifier is: project_id,region,instance_id
func (r *instanceResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
tflog.Debug(ctx, "ImportState called with id:", map[string]interface{}{"id": req.ID})
ctx = core.InitProviderContext(ctx)
if req.ID != "" {
idParts := strings.Split(req.ID, core.Separator)
@ -534,6 +576,16 @@ func (r *instanceResource) ImportState(ctx context.Context, req resource.ImportS
return
}
resp.Diagnostics.Append(
resp.State.SetAttribute(
ctx,
path.Root("id"),
utils.BuildInternalTerraformId(
identityData.ProjectID.ValueString(),
identityData.Region.ValueString(),
identityData.InstanceID.ValueString(),
),
)...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), identityData.ProjectID.ValueString())...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), identityData.Region.ValueString())...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), identityData.InstanceID.ValueString())...)