diff --git a/sample/postgres/postresql.tf b/sample/postgres/postresql.tf index 6771ede4..cbe34a39 100644 --- a/sample/postgres/postresql.tf +++ b/sample/postgres/postresql.tf @@ -30,11 +30,48 @@ resource "stackitprivatepreview_postgresflexalpha_instance" "msh-sna-pe-example" } network = { acl = ["0.0.0.0/0", "193.148.160.0/19", "170.85.2.177/32"] - access_scope = "SNA" + access_scope = "PUBLIC" } version = 14 } +resource "stackitprivatepreview_postgresflexalpha_instance" "import_for_deletion" { + project_id = var.project_id + name = "mshpetest2" + backup_schedule = "0 0 * * *" + retention_days = 45 + flavor_id = data.stackitprivatepreview_postgresflexalpha_flavor.pgsql_flavor.flavor_id + replicas = 1 + storage = { + # class = "premium-perf2-stackit" + performance_class = "premium-perf2-stackit" + size = 10 + } + encryption = { + # key_id = stackit_kms_key.key.key_id + # keyring_id = stackit_kms_keyring.keyring.keyring_id + kek_key_id = var.key_id + kek_key_ring_id = var.keyring_id + kek_key_version = var.key_version + service_account = var.sa_email + } + network = { + acl = ["0.0.0.0/0", "193.148.160.0/19", "170.85.2.177/32"] + access_scope = "PUBLIC" + } + version = 14 +} + +import { + to = stackitprivatepreview_postgresflexalpha_instance.import_for_deletion + identity = { + project_id = "9a6b8cca-7d45-41b1-b5e5-68123db347af" + region = "eu01" + instance_id = "d52b5d4c-be3f-4c14-a107-330dab99fd2e" + } +} + + resource "stackitprivatepreview_postgresflexalpha_instance" "msh-sna-pe-example2" { project_id = var.project_id name = "mshpetest2-1" @@ -59,7 +96,7 @@ resource "stackitprivatepreview_postgresflexalpha_instance" "msh-sna-pe-example2 acl = ["0.0.0.0/0", "193.148.160.0/19", "170.85.2.177/32"] access_scope = "SNA" } - version = 14 + version = 16 } resource "stackitprivatepreview_postgresflexalpha_user" "ptlsdbadminuser" { @@ -70,6 +107,14 @@ resource "stackitprivatepreview_postgresflexalpha_user" "ptlsdbadminuser" { # roles = ["createdb", "login", "createrole"] } +resource "stackitprivatepreview_postgresflexalpha_user" "ptlsdbadminuser2" { + project_id = var.project_id + instance_id = stackitprivatepreview_postgresflexalpha_instance.msh-sna-pe-example2.instance_id + username = var.db_admin_username + roles = ["createdb", "login"] + # roles = ["createdb", "login", "createrole"] +} + resource "stackitprivatepreview_postgresflexalpha_user" "ptlsdbuser" { project_id = var.project_id instance_id = stackitprivatepreview_postgresflexalpha_instance.msh-sna-pe-example.instance_id diff --git a/stackit/internal/services/postgresflexalpha/instance/planModifiers.yaml b/stackit/internal/services/postgresflexalpha/instance/planModifiers.yaml index 537c8e2d..3149c39f 100644 --- a/stackit/internal/services/postgresflexalpha/instance/planModifiers.yaml +++ b/stackit/internal/services/postgresflexalpha/instance/planModifiers.yaml @@ -8,4 +8,8 @@ fields: - 'RequiresReplace' - name: 'network.acl' modifiers: + - 'UseStateForUnknown' + - name: 'network.access_scope' + modifiers: + - 'UseStateForUnknown' - 'RequiresReplace' diff --git a/stackit/internal/services/postgresflexalpha/instance/resource.go b/stackit/internal/services/postgresflexalpha/instance/resource.go index d4d0ef3b..951d5cf1 100644 --- a/stackit/internal/services/postgresflexalpha/instance/resource.go +++ b/stackit/internal/services/postgresflexalpha/instance/resource.go @@ -206,7 +206,11 @@ func (r *instanceResource) Create( } ctx = core.LogResponse(ctx) - instanceId := *createResp.Id + instanceId, ok := createResp.GetIdOk() + if !ok { + core.LogAndAddError(ctx, &resp.Diagnostics, "error creating instance", "could not find instance id in response") + return + } model.InstanceId = types.StringValue(instanceId) model.Id = utils.BuildInternalTerraformId(projectId, region, instanceId) @@ -371,8 +375,27 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques ctx = core.InitProviderContext(ctx) - projectId := model.ProjectId.ValueString() - instanceId := model.InstanceId.ValueString() + // Read identity data + var identityData InstanceResourceIdentityModel + resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...) + if resp.Diagnostics.HasError() { + return + } + + //if model.InstanceId.IsNull() || model.InstanceId.IsUnknown() { + // core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", "instanceId is null or unknown") + // return + //} + // + //if model.ProjectId.IsNull() || model.ProjectId.IsUnknown() { + // core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", "projectId is null or unknown") + // return + //} + + //projectId := model.ProjectId.ValueString() + //instanceId := model.InstanceId.ValueString() + projectId := identityData.ProjectID.ValueString() + instanceId := identityData.InstanceID.ValueString() region := model.Region.ValueString() ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "instance_id", instanceId) @@ -485,20 +508,35 @@ func (r *instanceResource) Delete(ctx context.Context, req resource.DeleteReques } // ImportState imports a resource into the Terraform state on success. -// The expected format of the resource import identifier is: project_id,instance_id +// 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) { - idParts := strings.Split(req.ID, core.Separator) + tflog.Debug(ctx, "ImportState called with id:", map[string]interface{}{"id": req.ID}) + if req.ID != "" { + idParts := strings.Split(req.ID, core.Separator) - if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" { - core.LogAndAddError(ctx, &resp.Diagnostics, - "Error importing instance", - fmt.Sprintf("Expected import identifier with format: [project_id],[region],[instance_id] Got: %q", req.ID), - ) + if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" { + core.LogAndAddError(ctx, &resp.Diagnostics, + "Error importing instance", + fmt.Sprintf("Expected import identifier with format: [project_id],[region],[instance_id] Got: %q", req.ID), + ) + return + } + + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), idParts[1])...) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[2])...) return } - resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...) - resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), idParts[1])...) - resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[2])...) + var identityData InstanceResourceIdentityModel + resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...) + if resp.Diagnostics.HasError() { + return + } + + 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())...) + tflog.Info(ctx, "Postgres Flex instance state imported") } diff --git a/stackit/internal/wait/postgresflexalpha/wait.go b/stackit/internal/wait/postgresflexalpha/wait.go index 8ea93c38..b7ec7bbf 100644 --- a/stackit/internal/wait/postgresflexalpha/wait.go +++ b/stackit/internal/wait/postgresflexalpha/wait.go @@ -175,16 +175,16 @@ func PartialUpdateInstanceWaitHandler( return true, s, fmt.Errorf("instance with id %s has unexpected status %s", instanceId, *s.Status) case InstanceStateEmpty: return false, nil, nil - case InstanceStateUnknown: + case InstanceStatePending: return false, nil, nil case InstanceStateProgressing: return false, nil, nil - case InstanceStatePending: - return false, nil, nil - case InstanceStateTerminating: - return false, nil, nil case InstanceStateSuccess: return true, s, nil + case InstanceStateTerminating: + return false, nil, nil + case InstanceStateUnknown: + return false, nil, nil case InstanceStateFailed: return true, s, fmt.Errorf("update failed for instance with id %s", instanceId) }