chore: work save

This commit is contained in:
Marcel_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())...)

View file

@ -0,0 +1,120 @@
fields:
- name: 'id'
modifiers:
- 'UseStateForUnknown'
- name: 'instance_id'
validators:
- validate.NoSeparator
- validate.UUID
modifiers:
- 'UseStateForUnknown'
- name: 'project_id'
validators:
- validate.NoSeparator
- validate.UUID
modifiers:
- 'UseStateForUnknown'
- 'RequiresReplace'
- name: 'name'
modifiers:
- 'UseStateForUnknown'
- 'RequiresReplace'
- name: 'backup_schedule'
modifiers:
- 'UseStateForUnknown'
- name: 'encryption.kek_key_id'
validators:
- validate.NoSeparator
modifiers:
- 'RequiresReplace'
- name: 'encryption.kek_key_version'
validators:
- validate.NoSeparator
modifiers:
- 'RequiresReplace'
- name: 'encryption.kek_key_ring_id'
validators:
- validate.NoSeparator
modifiers:
- 'RequiresReplace'
- name: 'encryption.service_account'
validators:
- validate.NoSeparator
modifiers:
- 'RequiresReplace'
- name: 'network.access_scope'
validators:
- validate.NoSeparator
modifiers:
- 'UseStateForUnknown'
- 'RequiresReplace'
- name: 'network.acl'
modifiers:
- 'UseStateForUnknown'
- name: 'network.instance_address'
modifiers:
- 'UseStateForUnknown'
- name: 'network.router_address'
modifiers:
- 'UseStateForUnknown'
- name: 'status'
modifiers:
- 'UseStateForUnknown'
- name: 'region'
modifiers:
- 'RequiresReplace'
- name: 'retention_days'
modifiers:
- 'UseStateForUnknown'
- name: 'edition'
modifiers:
- 'UseStateForUnknown'
- 'RequiresReplace'
- name: 'version'
modifiers:
- 'UseStateForUnknown'
- 'RequiresReplace'
- name: 'replicas'
modifiers:
- 'UseStateForUnknown'
- 'RequiresReplace'
- name: 'storage'
modifiers:
- 'UseStateForUnknown'
- name: 'storage.class'
modifiers:
- 'UseStateForUnknown'
- 'RequiresReplace'
- name: 'storage.size'
modifiers:
- 'UseStateForUnknown'
- name: 'flavor_id'
modifiers:
- 'UseStateForUnknown'
- 'RequiresReplace'
- name: 'is_deletable'
modifiers:
- 'UseStateForUnknown'

View file

@ -339,7 +339,7 @@ func (r *instanceResource) Schema(_ context.Context, _ resource.SchemaRequest, r
Description: descriptions["status"],
},
"encryption": schema.SingleNestedAttribute{
Required: true,
Optional: true,
PlanModifiers: []planmodifier.Object{
objectplanmodifier.RequiresReplace(),
objectplanmodifier.UseStateForUnknown(),

View file

@ -29,22 +29,22 @@ func InstanceResourceSchema(ctx context.Context) schema.Schema {
"encryption": schema.SingleNestedAttribute{
Attributes: map[string]schema.Attribute{
"kek_key_id": schema.StringAttribute{
Required: true,
Optional: true,
Description: "The key identifier",
MarkdownDescription: "The key identifier",
},
"kek_key_ring_id": schema.StringAttribute{
Required: true,
Optional: true,
Description: "The keyring identifier",
MarkdownDescription: "The keyring identifier",
},
"kek_key_version": schema.StringAttribute{
Required: true,
Optional: true,
Description: "The key version",
MarkdownDescription: "The key version",
},
"service_account": schema.StringAttribute{
Required: true,
Optional: true,
},
},
CustomType: EncryptionType{

View file

@ -16,6 +16,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/mhenselin/terraform-provider-stackitprivatepreview/stackit/internal/core"
"github.com/mhenselin/terraform-provider-stackitprivatepreview/stackit/internal/features"
postgresFlexAlphaDatabase "github.com/mhenselin/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/database"
@ -320,6 +321,7 @@ func (p *Provider) Schema(_ context.Context, _ provider.SchemaRequest, resp *pro
// Configure prepares a stackit API client for data sources and resources.
func (p *Provider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
tflog.Info(ctx, "Configuring provider client")
// Retrieve provider data and configuration
var providerConfig providerModel
diags := req.Config.Get(ctx, &providerConfig)