From 6cc1dffc229f981d0981fb65098859acefb8a4e1 Mon Sep 17 00:00:00 2001 From: Alexander Dahmen Date: Fri, 21 Mar 2025 13:52:10 +0100 Subject: [PATCH] feat(postgresql): Region adjustment (#713) Signed-off-by: Alexander Dahmen --- docs/data-sources/postgresflex_database.md | 6 +- docs/data-sources/postgresflex_instance.md | 6 +- docs/data-sources/postgresflex_user.md | 6 +- docs/resources/postgresflex_database.md | 6 +- docs/resources/postgresflex_instance.md | 6 +- docs/resources/postgresflex_user.md | 6 +- go.mod | 2 +- go.sum | 6 +- .../postgresflex/database/datasource.go | 36 ++++-- .../postgresflex/database/resource.go | 99 ++++++++++++---- .../postgresflex/database/resource_test.go | 19 +++- .../postgresflex/instance/datasource.go | 36 ++++-- .../postgresflex/instance/resource.go | 107 ++++++++++++++---- .../postgresflex/instance/resource_test.go | 24 +++- .../postgresflex/postgresflex_acc_test.go | 36 +++--- .../services/postgresflex/user/datasource.go | 41 +++++-- .../postgresflex/user/datasource_test.go | 23 +++- .../services/postgresflex/user/resource.go | 98 ++++++++++++---- .../postgresflex/user/resource_test.go | 43 +++++-- 19 files changed, 458 insertions(+), 148 deletions(-) diff --git a/docs/data-sources/postgresflex_database.md b/docs/data-sources/postgresflex_database.md index 7d89ad63..d69ca39c 100644 --- a/docs/data-sources/postgresflex_database.md +++ b/docs/data-sources/postgresflex_database.md @@ -29,8 +29,12 @@ data "stackit_postgresflex_database" "example" { - `instance_id` (String) ID of the Postgres Flex instance. - `project_id` (String) STACKIT project ID to which the instance is associated. +### Optional + +- `region` (String) The resource region. If not defined, the provider region is used. + ### Read-Only -- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`instance_id`,`database_id`". +- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`region`,`instance_id`,`database_id`". - `name` (String) Database name. - `owner` (String) Username of the database owner. diff --git a/docs/data-sources/postgresflex_instance.md b/docs/data-sources/postgresflex_instance.md index 45688a49..91f4136a 100644 --- a/docs/data-sources/postgresflex_instance.md +++ b/docs/data-sources/postgresflex_instance.md @@ -27,12 +27,16 @@ data "stackit_postgresflex_instance" "example" { - `instance_id` (String) ID of the PostgresFlex instance. - `project_id` (String) STACKIT project ID to which the instance is associated. +### Optional + +- `region` (String) The resource region. If not defined, the provider region is used. + ### Read-Only - `acl` (List of String) The Access Control List (ACL) for the PostgresFlex instance. - `backup_schedule` (String) - `flavor` (Attributes) (see [below for nested schema](#nestedatt--flavor)) -- `id` (String) Terraform's internal data source. ID. It is structured as "`project_id`,`instance_id`". +- `id` (String) Terraform's internal data source. ID. It is structured as "`project_id`,`region`,`instance_id`". - `name` (String) Instance name. - `replicas` (Number) - `storage` (Attributes) (see [below for nested schema](#nestedatt--storage)) diff --git a/docs/data-sources/postgresflex_user.md b/docs/data-sources/postgresflex_user.md index afd7dbd4..5e91aeba 100644 --- a/docs/data-sources/postgresflex_user.md +++ b/docs/data-sources/postgresflex_user.md @@ -29,10 +29,14 @@ data "stackit_postgresflex_user" "example" { - `project_id` (String) STACKIT project ID to which the instance is associated. - `user_id` (String) User ID. +### Optional + +- `region` (String) The resource region. If not defined, the provider region is used. + ### Read-Only - `host` (String) -- `id` (String) Terraform's internal data source. ID. It is structured as "`project_id`,`instance_id`,`user_id`". +- `id` (String) Terraform's internal data source. ID. It is structured as "`project_id`,`region`,`instance_id`,`user_id`". - `port` (Number) - `roles` (Set of String) - `username` (String) diff --git a/docs/resources/postgresflex_database.md b/docs/resources/postgresflex_database.md index 71cc2e06..e55f60e5 100644 --- a/docs/resources/postgresflex_database.md +++ b/docs/resources/postgresflex_database.md @@ -31,7 +31,11 @@ resource "stackit_postgresflex_database" "example" { - `owner` (String) Username of the database owner. - `project_id` (String) STACKIT project ID to which the instance is associated. +### Optional + +- `region` (String) The resource region. If not defined, the provider region is used. + ### Read-Only - `database_id` (String) Database ID. -- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`instance_id`,`database_id`". +- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`region`,`instance_id`,`database_id`". diff --git a/docs/resources/postgresflex_instance.md b/docs/resources/postgresflex_instance.md index 649980bd..e3e38dc6 100644 --- a/docs/resources/postgresflex_instance.md +++ b/docs/resources/postgresflex_instance.md @@ -45,9 +45,13 @@ resource "stackit_postgresflex_instance" "example" { - `storage` (Attributes) (see [below for nested schema](#nestedatt--storage)) - `version` (String) +### Optional + +- `region` (String) The resource region. If not defined, the provider region is used. + ### Read-Only -- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`instance_id`". +- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`region`,`instance_id`". - `instance_id` (String) ID of the PostgresFlex instance. diff --git a/docs/resources/postgresflex_user.md b/docs/resources/postgresflex_user.md index 577901c4..aa380d9a 100644 --- a/docs/resources/postgresflex_user.md +++ b/docs/resources/postgresflex_user.md @@ -31,10 +31,14 @@ resource "stackit_postgresflex_user" "example" { - `roles` (Set of String) Database access levels for the user. Supported values are: `login`, `createdb`. - `username` (String) +### Optional + +- `region` (String) The resource region. If not defined, the provider region is used. + ### Read-Only - `host` (String) -- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`instance_id`,`user_id`". +- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`region`,`instance_id`,`user_id`". - `password` (String, Sensitive) - `port` (Number) - `uri` (String, Sensitive) diff --git a/go.mod b/go.mod index 975bccf0..4d83e0bc 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,7 @@ require ( github.com/stackitcloud/stackit-sdk-go/services/objectstorage v1.1.0 github.com/stackitcloud/stackit-sdk-go/services/observability v0.3.0 github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.20.0 - github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.18.0 + github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.0.1 github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v0.21.0 github.com/stackitcloud/stackit-sdk-go/services/redis v0.21.0 github.com/stackitcloud/stackit-sdk-go/services/resourcemanager v0.13.0 diff --git a/go.sum b/go.sum index 905f7612..bb5a32c8 100644 --- a/go.sum +++ b/go.sum @@ -175,8 +175,8 @@ github.com/stackitcloud/stackit-sdk-go/services/observability v0.3.0 h1:Hn4BwKCz github.com/stackitcloud/stackit-sdk-go/services/observability v0.3.0/go.mod h1:PxfwA6YFtxwOajB4iTp1Eq7G02qUC3HdQPJvHGjQ1hk= github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.20.0 h1:y83IhdbQv8EHovWPTqeulGgyZKJ39AQW1klo0g7a7og= github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.20.0/go.mod h1:Gk3hWaQDCJGgaixjGkUmoIr74VNWwdAakiUrvizpOWQ= -github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.18.0 h1:cwdmiwbSml70kE9xV9C25t9WggDT98NdSfWD9w/r4wU= -github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.18.0/go.mod h1:wNPezvzJUgUj+C50EqyMAj5PSkhawT+2Zsdh01WQpAM= +github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.0.1 h1:vUi9//CyfS6UMv0hftYMamimjJLco5lxT/KW9y4QPqM= +github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.0.1/go.mod h1:7TqfCUZRW7sjv8qOrLV5IvS6jqvY9Uxka165zdjYwD4= github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v0.21.0 h1:zEJXwsuasmYH8dONZrCsZzcann/+6HZDKUPhN3mOmY0= github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v0.21.0/go.mod h1:SaL9BCTeWcEmU9JiKgNihEXKnFKDTn91L9ehgvauWuM= github.com/stackitcloud/stackit-sdk-go/services/redis v0.21.0 h1:UDIRWwiZ2/2ukmn60wPo83PUSuWPaXqbuRzkRTjRQNQ= @@ -195,8 +195,6 @@ github.com/stackitcloud/stackit-sdk-go/services/serviceenablement v0.5.0 h1:QG+r github.com/stackitcloud/stackit-sdk-go/services/serviceenablement v0.5.0/go.mod h1:16dOVT052cMuHhUJ3NIcPuY7TrpCr9QlxmvvfjLZubA= github.com/stackitcloud/stackit-sdk-go/services/ske v0.22.0 h1:3KUVls8zXsbT2tOYRSHyp3/l0Kpjl4f3INmQKYTe65Y= github.com/stackitcloud/stackit-sdk-go/services/ske v0.22.0/go.mod h1:63IvXpBJTIVONAnGPSDo0sRJ+6n6tzO918OLqfYBxto= -github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v0.10.0 h1:STq6VaVUeHLeXzl1r5E4+MK5lcNVtdKjjP7N0XOowY4= -github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v0.10.0/go.mod h1:hdeLDwSCOmGIYtY4DGN15kjL44DQvo/txHXtTEvZidA= github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v1.0.0 h1:RYJO0rZea9+sxVfaJDWRo2zgfKNgiUcA5c0nbvZURiU= github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v1.0.0/go.mod h1:d2ICXCS2h3IMsZW0OanWkEH2XdLiY/XRKx2TcR940nw= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/stackit/internal/services/postgresflex/database/datasource.go b/stackit/internal/services/postgresflex/database/datasource.go index 1c4f046f..07f3fde9 100644 --- a/stackit/internal/services/postgresflex/database/datasource.go +++ b/stackit/internal/services/postgresflex/database/datasource.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" @@ -29,7 +30,8 @@ func NewDatabaseDataSource() datasource.DataSource { // databaseDataSource is the data source implementation. type databaseDataSource struct { - client *postgresflex.APIClient + client *postgresflex.APIClient + providerData core.ProviderData } // Metadata returns the data source type name. @@ -44,7 +46,8 @@ func (r *databaseDataSource) Configure(ctx context.Context, req datasource.Confi return } - providerData, ok := req.ProviderData.(core.ProviderData) + var ok bool + r.providerData, ok = req.ProviderData.(core.ProviderData) if !ok { core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData)) return @@ -52,15 +55,15 @@ func (r *databaseDataSource) Configure(ctx context.Context, req datasource.Confi var apiClient *postgresflex.APIClient var err error - if providerData.PostgresFlexCustomEndpoint != "" { + if r.providerData.PostgresFlexCustomEndpoint != "" { apiClient, err = postgresflex.NewAPIClient( - config.WithCustomAuth(providerData.RoundTripper), - config.WithEndpoint(providerData.PostgresFlexCustomEndpoint), + config.WithCustomAuth(r.providerData.RoundTripper), + config.WithEndpoint(r.providerData.PostgresFlexCustomEndpoint), ) } else { apiClient, err = postgresflex.NewAPIClient( - config.WithCustomAuth(providerData.RoundTripper), - config.WithRegion(providerData.GetRegion()), + config.WithCustomAuth(r.providerData.RoundTripper), + config.WithRegion(r.providerData.GetRegion()), ) } @@ -77,12 +80,13 @@ func (r *databaseDataSource) Configure(ctx context.Context, req datasource.Confi func (r *databaseDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { descriptions := map[string]string{ "main": "Postgres Flex database resource schema. Must have a `region` specified in the provider configuration.", - "id": "Terraform's internal resource ID. It is structured as \"`project_id`,`instance_id`,`database_id`\".", + "id": "Terraform's internal resource ID. It is structured as \"`project_id`,`region`,`instance_id`,`database_id`\".", "database_id": "Database ID.", "instance_id": "ID of the Postgres Flex instance.", "project_id": "STACKIT project ID to which the instance is associated.", "name": "Database name.", "owner": "Username of the database owner.", + "region": "The resource region. If not defined, the provider region is used.", } resp.Schema = schema.Schema{ @@ -123,6 +127,11 @@ func (r *databaseDataSource) Schema(_ context.Context, _ datasource.SchemaReques Description: descriptions["owner"], Computed: true, }, + "region": schema.StringAttribute{ + // the region cannot be found, so it has to be passed + Optional: true, + Description: descriptions["region"], + }, }, } } @@ -138,11 +147,18 @@ func (r *databaseDataSource) Read(ctx context.Context, req datasource.ReadReques projectId := model.ProjectId.ValueString() instanceId := model.InstanceId.ValueString() databaseId := model.DatabaseId.ValueString() + var region string + if utils.IsUndefined(model.Region) { + region = r.providerData.GetRegion() + } else { + region = model.Region.ValueString() + } ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "database_id", databaseId) + ctx = tflog.SetField(ctx, "region", region) - databaseResp, err := getDatabase(ctx, r.client, projectId, instanceId, databaseId) + databaseResp, err := getDatabase(ctx, r.client, projectId, region, instanceId, databaseId) if err != nil { oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped if ok && oapiErr.StatusCode == http.StatusNotFound { @@ -153,7 +169,7 @@ func (r *databaseDataSource) Read(ctx context.Context, req datasource.ReadReques } // Map response body to schema and populate Computed attribute values - err = mapFields(databaseResp, &model) + err = mapFields(databaseResp, &model, region) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading database", fmt.Sprintf("Processing API payload: %v", err)) return diff --git a/stackit/internal/services/postgresflex/database/resource.go b/stackit/internal/services/postgresflex/database/resource.go index 36beb071..60453fbd 100644 --- a/stackit/internal/services/postgresflex/database/resource.go +++ b/stackit/internal/services/postgresflex/database/resource.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" "github.com/hashicorp/terraform-plugin-framework/path" @@ -28,6 +29,7 @@ var ( _ resource.Resource = &databaseResource{} _ resource.ResourceWithConfigure = &databaseResource{} _ resource.ResourceWithImportState = &databaseResource{} + _ resource.ResourceWithModifyPlan = &databaseResource{} ) type Model struct { @@ -37,6 +39,7 @@ type Model struct { ProjectId types.String `tfsdk:"project_id"` Name types.String `tfsdk:"name"` Owner types.String `tfsdk:"owner"` + Region types.String `tfsdk:"region"` } // NewDatabaseResource is a helper function to simplify the provider implementation. @@ -46,7 +49,38 @@ func NewDatabaseResource() resource.Resource { // databaseResource is the resource implementation. type databaseResource struct { - client *postgresflex.APIClient + client *postgresflex.APIClient + providerData core.ProviderData +} + +// ModifyPlan implements resource.ResourceWithModifyPlan. +// Use the modifier to set the effective region in the current plan. +func (r *databaseResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // nolint:gocritic // function signature required by Terraform + var configModel Model + // skip initial empty configuration to avoid follow-up errors + if req.Config.Raw.IsNull() { + return + } + resp.Diagnostics.Append(req.Config.Get(ctx, &configModel)...) + if resp.Diagnostics.HasError() { + return + } + + var planModel Model + resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...) + if resp.Diagnostics.HasError() { + return + } + + utils.AdaptRegion(ctx, configModel.Region, &planModel.Region, r.providerData.GetRegion(), resp) + if resp.Diagnostics.HasError() { + return + } + + resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...) + if resp.Diagnostics.HasError() { + return + } } // Metadata returns the resource type name. @@ -61,7 +95,8 @@ func (r *databaseResource) Configure(ctx context.Context, req resource.Configure return } - providerData, ok := req.ProviderData.(core.ProviderData) + var ok bool + r.providerData, ok = req.ProviderData.(core.ProviderData) if !ok { core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData)) return @@ -69,15 +104,15 @@ func (r *databaseResource) Configure(ctx context.Context, req resource.Configure var apiClient *postgresflex.APIClient var err error - if providerData.PostgresFlexCustomEndpoint != "" { + if r.providerData.PostgresFlexCustomEndpoint != "" { apiClient, err = postgresflex.NewAPIClient( - config.WithCustomAuth(providerData.RoundTripper), - config.WithEndpoint(providerData.PostgresFlexCustomEndpoint), + config.WithCustomAuth(r.providerData.RoundTripper), + config.WithEndpoint(r.providerData.PostgresFlexCustomEndpoint), ) } else { apiClient, err = postgresflex.NewAPIClient( - config.WithCustomAuth(providerData.RoundTripper), - config.WithRegion(providerData.GetRegion()), + config.WithCustomAuth(r.providerData.RoundTripper), + config.WithRegion(r.providerData.GetRegion()), ) } @@ -94,12 +129,13 @@ func (r *databaseResource) Configure(ctx context.Context, req resource.Configure func (r *databaseResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { descriptions := map[string]string{ "main": "Postgres Flex database resource schema. Must have a `region` specified in the provider configuration.", - "id": "Terraform's internal resource ID. It is structured as \"`project_id`,`instance_id`,`database_id`\".", + "id": "Terraform's internal resource ID. It is structured as \"`project_id`,`region`,`instance_id`,`database_id`\".", "database_id": "Database ID.", "instance_id": "ID of the Postgres Flex instance.", "project_id": "STACKIT project ID to which the instance is associated.", "name": "Database name.", "owner": "Username of the database owner.", + "region": "The resource region. If not defined, the provider region is used.", } resp.Schema = schema.Schema{ @@ -160,6 +196,15 @@ func (r *databaseResource) Schema(_ context.Context, _ resource.SchemaRequest, r stringplanmodifier.RequiresReplace(), }, }, + "region": schema.StringAttribute{ + Optional: true, + // must be computed to allow for storing the override value from the provider + Computed: true, + Description: descriptions["region"], + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, }, } } @@ -173,9 +218,11 @@ func (r *databaseResource) Create(ctx context.Context, req resource.CreateReques return } projectId := model.ProjectId.ValueString() + region := model.Region.ValueString() instanceId := model.InstanceId.ValueString() ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "instance_id", instanceId) + ctx = tflog.SetField(ctx, "region", region) // Generate API request body from model payload, err := toCreatePayload(&model) @@ -184,7 +231,7 @@ func (r *databaseResource) Create(ctx context.Context, req resource.CreateReques return } // Create new database - databaseResp, err := r.client.CreateDatabase(ctx, projectId, instanceId).CreateDatabasePayload(*payload).Execute() + databaseResp, err := r.client.CreateDatabase(ctx, projectId, region, instanceId).CreateDatabasePayload(*payload).Execute() if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating database", fmt.Sprintf("Calling API: %v", err)) return @@ -196,14 +243,14 @@ func (r *databaseResource) Create(ctx context.Context, req resource.CreateReques databaseId := *databaseResp.Id ctx = tflog.SetField(ctx, "database_id", databaseId) - database, err := getDatabase(ctx, r.client, projectId, instanceId, databaseId) + database, err := getDatabase(ctx, r.client, projectId, region, instanceId, databaseId) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating database", fmt.Sprintf("Getting database details after creation: %v", err)) return } // Map response body to schema - err = mapFields(database, &model) + err = mapFields(database, &model, region) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating database", fmt.Sprintf("Processing API payload: %v", err)) return @@ -228,11 +275,16 @@ func (r *databaseResource) Read(ctx context.Context, req resource.ReadRequest, r projectId := model.ProjectId.ValueString() instanceId := model.InstanceId.ValueString() databaseId := model.DatabaseId.ValueString() + region := model.Region.ValueString() + if region == "" { + region = r.providerData.GetRegion() + } ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "database_id", databaseId) + ctx = tflog.SetField(ctx, "region", region) - databaseResp, err := getDatabase(ctx, r.client, projectId, instanceId, databaseId) + databaseResp, err := getDatabase(ctx, r.client, projectId, region, instanceId, databaseId) if err != nil { oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped if (ok && oapiErr.StatusCode == http.StatusNotFound) || errors.Is(err, databaseNotFoundErr) { @@ -244,7 +296,7 @@ func (r *databaseResource) Read(ctx context.Context, req resource.ReadRequest, r } // Map response body to schema - err = mapFields(databaseResp, &model) + err = mapFields(databaseResp, &model, region) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading database", fmt.Sprintf("Processing API payload: %v", err)) return @@ -278,12 +330,14 @@ func (r *databaseResource) Delete(ctx context.Context, req resource.DeleteReques projectId := model.ProjectId.ValueString() instanceId := model.InstanceId.ValueString() databaseId := model.DatabaseId.ValueString() + region := model.Region.ValueString() ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "database_id", databaseId) + ctx = tflog.SetField(ctx, "region", region) // Delete existing record set - err := r.client.DeleteDatabase(ctx, projectId, instanceId, databaseId).Execute() + err := r.client.DeleteDatabase(ctx, projectId, region, instanceId, databaseId).Execute() if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting database", fmt.Sprintf("Calling API: %v", err)) } @@ -294,17 +348,18 @@ func (r *databaseResource) Delete(ctx context.Context, req resource.DeleteReques // The expected format of the resource import identifier is: project_id,zone_id,record_set_id func (r *databaseResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { idParts := strings.Split(req.ID, core.Separator) - if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" { + if len(idParts) != 4 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" || idParts[3] == "" { core.LogAndAddError(ctx, &resp.Diagnostics, "Error importing database", - fmt.Sprintf("Expected import identifier with format [project_id],[instance_id],[database_id], got %q", req.ID), + fmt.Sprintf("Expected import identifier with format [project_id],[region],[instance_id],[database_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("instance_id"), idParts[1])...) - resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("database_id"), idParts[2])...) + 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])...) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("database_id"), idParts[3])...) core.LogAndAddWarning(ctx, &resp.Diagnostics, "Postgresflex database imported with empty password", "The database password is not imported as it is only available upon creation of a new database. The password field will be empty.", @@ -312,7 +367,7 @@ func (r *databaseResource) ImportState(ctx context.Context, req resource.ImportS tflog.Info(ctx, "Postgres Flex database state imported") } -func mapFields(databaseResp *postgresflex.InstanceDatabase, model *Model) error { +func mapFields(databaseResp *postgresflex.InstanceDatabase, model *Model, region string) error { if databaseResp == nil { return fmt.Errorf("response is nil") } @@ -333,6 +388,7 @@ func mapFields(databaseResp *postgresflex.InstanceDatabase, model *Model) error } idParts := []string{ model.ProjectId.ValueString(), + region, model.InstanceId.ValueString(), databaseId, } @@ -341,6 +397,7 @@ func mapFields(databaseResp *postgresflex.InstanceDatabase, model *Model) error ) model.DatabaseId = types.StringValue(databaseId) model.Name = types.StringPointerValue(databaseResp.Name) + model.Region = types.StringValue(region) if databaseResp.Options != nil { owner, ok := (*databaseResp.Options)["owner"] @@ -375,8 +432,8 @@ func toCreatePayload(model *Model) (*postgresflex.CreateDatabasePayload, error) var databaseNotFoundErr = errors.New("database not found") // The API does not have a GetDatabase endpoint, only ListDatabases -func getDatabase(ctx context.Context, client *postgresflex.APIClient, projectId, instanceId, databaseId string) (*postgresflex.InstanceDatabase, error) { - resp, err := client.ListDatabases(ctx, projectId, instanceId).Execute() +func getDatabase(ctx context.Context, client *postgresflex.APIClient, projectId, region, instanceId, databaseId string) (*postgresflex.InstanceDatabase, error) { + resp, err := client.ListDatabases(ctx, projectId, region, instanceId).Execute() if err != nil { return nil, err } diff --git a/stackit/internal/services/postgresflex/database/resource_test.go b/stackit/internal/services/postgresflex/database/resource_test.go index 1a5b8965..1770801b 100644 --- a/stackit/internal/services/postgresflex/database/resource_test.go +++ b/stackit/internal/services/postgresflex/database/resource_test.go @@ -10,9 +10,11 @@ import ( ) func TestMapFields(t *testing.T) { + const testRegion = "region" tests := []struct { description string input *postgresflex.InstanceDatabase + region string expected Model isValid bool }{ @@ -21,13 +23,15 @@ func TestMapFields(t *testing.T) { &postgresflex.InstanceDatabase{ Id: utils.Ptr("uid"), }, + testRegion, Model{ - Id: types.StringValue("pid,iid,uid"), + Id: types.StringValue("pid,region,iid,uid"), DatabaseId: types.StringValue("uid"), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), Name: types.StringNull(), Owner: types.StringNull(), + Region: types.StringValue(testRegion), }, true, }, @@ -40,13 +44,15 @@ func TestMapFields(t *testing.T) { "owner": "username", }, }, + testRegion, Model{ - Id: types.StringValue("pid,iid,uid"), + Id: types.StringValue("pid,region,iid,uid"), DatabaseId: types.StringValue("uid"), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), Name: types.StringValue("dbname"), Owner: types.StringValue("username"), + Region: types.StringValue(testRegion), }, true, }, @@ -59,25 +65,29 @@ func TestMapFields(t *testing.T) { "owner": "", }, }, + testRegion, Model{ - Id: types.StringValue("pid,iid,uid"), + Id: types.StringValue("pid,region,iid,uid"), DatabaseId: types.StringValue("uid"), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), Name: types.StringValue(""), Owner: types.StringValue(""), + Region: types.StringValue(testRegion), }, true, }, { "nil_response", nil, + testRegion, Model{}, false, }, { "empty_response", &postgresflex.InstanceDatabase{}, + testRegion, Model{}, false, }, @@ -90,6 +100,7 @@ func TestMapFields(t *testing.T) { "owner": "username", }, }, + testRegion, Model{}, false, }, @@ -100,7 +111,7 @@ func TestMapFields(t *testing.T) { ProjectId: tt.expected.ProjectId, InstanceId: tt.expected.InstanceId, } - err := mapFields(tt.input, state) + err := mapFields(tt.input, state, tt.region) if !tt.isValid && err == nil { t.Fatalf("Should have failed") } diff --git a/stackit/internal/services/postgresflex/instance/datasource.go b/stackit/internal/services/postgresflex/instance/datasource.go index 72ab6cf2..7ae3233c 100644 --- a/stackit/internal/services/postgresflex/instance/datasource.go +++ b/stackit/internal/services/postgresflex/instance/datasource.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/types/basetypes" "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" @@ -32,7 +33,8 @@ func NewInstanceDataSource() datasource.DataSource { // instanceDataSource is the data source implementation. type instanceDataSource struct { - client *postgresflex.APIClient + client *postgresflex.APIClient + providerData core.ProviderData } // Metadata returns the data source type name. @@ -47,7 +49,8 @@ func (r *instanceDataSource) Configure(ctx context.Context, req datasource.Confi return } - providerData, ok := req.ProviderData.(core.ProviderData) + var ok bool + r.providerData, ok = req.ProviderData.(core.ProviderData) if !ok { core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData)) return @@ -55,15 +58,15 @@ func (r *instanceDataSource) Configure(ctx context.Context, req datasource.Confi var apiClient *postgresflex.APIClient var err error - if providerData.PostgresFlexCustomEndpoint != "" { + if r.providerData.PostgresFlexCustomEndpoint != "" { apiClient, err = postgresflex.NewAPIClient( - config.WithCustomAuth(providerData.RoundTripper), - config.WithEndpoint(providerData.PostgresFlexCustomEndpoint), + config.WithCustomAuth(r.providerData.RoundTripper), + config.WithEndpoint(r.providerData.PostgresFlexCustomEndpoint), ) } else { apiClient, err = postgresflex.NewAPIClient( - config.WithCustomAuth(providerData.RoundTripper), - config.WithRegion(providerData.GetRegion()), + config.WithCustomAuth(r.providerData.RoundTripper), + config.WithRegion(r.providerData.GetRegion()), ) } @@ -80,11 +83,12 @@ func (r *instanceDataSource) Configure(ctx context.Context, req datasource.Confi func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { descriptions := map[string]string{ "main": "Postgres Flex instance data source schema. Must have a `region` specified in the provider configuration.", - "id": "Terraform's internal data source. ID. It is structured as \"`project_id`,`instance_id`\".", + "id": "Terraform's internal data source. ID. It is structured as \"`project_id`,`region`,`instance_id`\".", "instance_id": "ID of the PostgresFlex instance.", "project_id": "STACKIT project ID to which the instance is associated.", "name": "Instance name.", "acl": "The Access Control List (ACL) for the PostgresFlex instance.", + "region": "The resource region. If not defined, the provider region is used.", } resp.Schema = schema.Schema{ @@ -156,6 +160,11 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques "version": schema.StringAttribute{ Computed: true, }, + "region": schema.StringAttribute{ + // the region cannot be found, so it has to be passed + Optional: true, + Description: descriptions["region"], + }, }, } } @@ -171,9 +180,16 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques projectId := model.ProjectId.ValueString() instanceId := model.InstanceId.ValueString() + var region string + if utils.IsUndefined(model.Region) { + region = r.providerData.GetRegion() + } else { + region = model.Region.ValueString() + } ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "instance_id", instanceId) - instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).Execute() + ctx = tflog.SetField(ctx, "region", region) + instanceResp, err := r.client.GetInstance(ctx, projectId, region, instanceId).Execute() if err != nil { oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped if ok && oapiErr.StatusCode == http.StatusNotFound { @@ -205,7 +221,7 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques } } - err = mapFields(ctx, instanceResp, &model, flavor, storage) + err = mapFields(ctx, instanceResp, &model, flavor, storage, region) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Processing API payload: %v", err)) return diff --git a/stackit/internal/services/postgresflex/instance/resource.go b/stackit/internal/services/postgresflex/instance/resource.go index 3c4c632d..0f67a40a 100644 --- a/stackit/internal/services/postgresflex/instance/resource.go +++ b/stackit/internal/services/postgresflex/instance/resource.go @@ -36,6 +36,7 @@ var ( _ resource.Resource = &instanceResource{} _ resource.ResourceWithConfigure = &instanceResource{} _ resource.ResourceWithImportState = &instanceResource{} + _ resource.ResourceWithModifyPlan = &instanceResource{} ) type Model struct { @@ -49,6 +50,7 @@ type Model struct { Replicas types.Int64 `tfsdk:"replicas"` Storage types.Object `tfsdk:"storage"` Version types.String `tfsdk:"version"` + Region types.String `tfsdk:"region"` } // Struct corresponding to Model.Flavor @@ -86,7 +88,38 @@ func NewInstanceResource() resource.Resource { // instanceResource is the resource implementation. type instanceResource struct { - client *postgresflex.APIClient + client *postgresflex.APIClient + providerData core.ProviderData +} + +// ModifyPlan implements resource.ResourceWithModifyPlan. +// Use the modifier to set the effective region in the current plan. +func (r *instanceResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // nolint:gocritic // function signature required by Terraform + var configModel Model + // skip initial empty configuration to avoid follow-up errors + if req.Config.Raw.IsNull() { + return + } + resp.Diagnostics.Append(req.Config.Get(ctx, &configModel)...) + if resp.Diagnostics.HasError() { + return + } + + var planModel Model + resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...) + if resp.Diagnostics.HasError() { + return + } + + utils.AdaptRegion(ctx, configModel.Region, &planModel.Region, r.providerData.GetRegion(), resp) + if resp.Diagnostics.HasError() { + return + } + + resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...) + if resp.Diagnostics.HasError() { + return + } } // Metadata returns the resource type name. @@ -101,7 +134,8 @@ func (r *instanceResource) Configure(ctx context.Context, req resource.Configure return } - providerData, ok := req.ProviderData.(core.ProviderData) + var ok bool + r.providerData, ok = req.ProviderData.(core.ProviderData) if !ok { core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData)) return @@ -109,15 +143,15 @@ func (r *instanceResource) Configure(ctx context.Context, req resource.Configure var apiClient *postgresflex.APIClient var err error - if providerData.PostgresFlexCustomEndpoint != "" { + if r.providerData.PostgresFlexCustomEndpoint != "" { apiClient, err = postgresflex.NewAPIClient( - config.WithCustomAuth(providerData.RoundTripper), - config.WithEndpoint(providerData.PostgresFlexCustomEndpoint), + config.WithCustomAuth(r.providerData.RoundTripper), + config.WithEndpoint(r.providerData.PostgresFlexCustomEndpoint), ) } else { apiClient, err = postgresflex.NewAPIClient( - config.WithCustomAuth(providerData.RoundTripper), - config.WithRegion(providerData.GetRegion()), + config.WithCustomAuth(r.providerData.RoundTripper), + config.WithRegion(r.providerData.GetRegion()), ) } @@ -134,11 +168,12 @@ func (r *instanceResource) Configure(ctx context.Context, req resource.Configure func (r *instanceResource) Schema(_ context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { descriptions := map[string]string{ "main": "Postgres Flex instance resource schema. Must have a `region` specified in the provider configuration.", - "id": "Terraform's internal resource ID. It is structured as \"`project_id`,`instance_id`\".", + "id": "Terraform's internal resource ID. It is structured as \"`project_id`,`region`,`instance_id`\".", "instance_id": "ID of the PostgresFlex instance.", "project_id": "STACKIT project ID to which the instance is associated.", "name": "Instance name.", "acl": "The Access Control List (ACL) for the PostgresFlex instance.", + "region": "The resource region. If not defined, the provider region is used.", } resp.Schema = schema.Schema{ @@ -236,6 +271,15 @@ func (r *instanceResource) Schema(_ context.Context, req resource.SchemaRequest, "version": schema.StringAttribute{ Required: true, }, + "region": schema.StringAttribute{ + Optional: true, + // must be computed to allow for storing the override value from the provider + Computed: true, + Description: descriptions["region"], + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, }, } } @@ -250,7 +294,9 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques return } projectId := model.ProjectId.ValueString() + region := model.Region.ValueString() ctx = tflog.SetField(ctx, "project_id", projectId) + ctx = tflog.SetField(ctx, "region", region) var acl []string if !(model.ACL.IsNull() || model.ACL.IsUnknown()) { @@ -289,21 +335,21 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques return } // Create new instance - createResp, err := r.client.CreateInstance(ctx, projectId).CreateInstancePayload(*payload).Execute() + createResp, err := r.client.CreateInstance(ctx, projectId, region).CreateInstancePayload(*payload).Execute() if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Calling API: %v", err)) return } instanceId := *createResp.Id ctx = tflog.SetField(ctx, "instance_id", instanceId) - waitResp, err := wait.CreateInstanceWaitHandler(ctx, r.client, projectId, instanceId).WaitWithContext(ctx) + waitResp, err := wait.CreateInstanceWaitHandler(ctx, r.client, projectId, region, instanceId).WaitWithContext(ctx) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Instance creation waiting: %v", err)) return } // Map response body to schema - err = mapFields(ctx, waitResp, &model, flavor, storage) + err = mapFields(ctx, waitResp, &model, flavor, storage, region) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Processing API payload: %v", err)) return @@ -327,8 +373,13 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r } projectId := model.ProjectId.ValueString() instanceId := model.InstanceId.ValueString() + region := model.Region.ValueString() + if region == "" { + region = r.providerData.GetRegion() + } ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "instance_id", instanceId) + ctx = tflog.SetField(ctx, "region", region) var flavor = &flavorModel{} if !(model.Flavor.IsNull() || model.Flavor.IsUnknown()) { @@ -347,7 +398,7 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r } } - instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).Execute() + instanceResp, err := r.client.GetInstance(ctx, projectId, region, instanceId).Execute() if err != nil { oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped if ok && oapiErr.StatusCode == http.StatusNotFound { @@ -363,7 +414,7 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r } // Map response body to schema - err = mapFields(ctx, instanceResp, &model, flavor, storage) + err = mapFields(ctx, instanceResp, &model, flavor, storage, region) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Processing API payload: %v", err)) return @@ -388,8 +439,10 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques } projectId := model.ProjectId.ValueString() instanceId := model.InstanceId.ValueString() + region := model.Region.ValueString() ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "instance_id", instanceId) + ctx = tflog.SetField(ctx, "region", region) var acl []string if !(model.ACL.IsNull() || model.ACL.IsUnknown()) { @@ -428,19 +481,19 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques return } // Update existing instance - _, err = r.client.PartialUpdateInstance(ctx, projectId, instanceId).PartialUpdateInstancePayload(*payload).Execute() + _, err = r.client.PartialUpdateInstance(ctx, projectId, region, instanceId).PartialUpdateInstancePayload(*payload).Execute() if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", err.Error()) return } - waitResp, err := wait.PartialUpdateInstanceWaitHandler(ctx, r.client, projectId, instanceId).WaitWithContext(ctx) + waitResp, err := wait.PartialUpdateInstanceWaitHandler(ctx, r.client, projectId, region, instanceId).WaitWithContext(ctx) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Instance update waiting: %v", err)) return } // Map response body to schema - err = mapFields(ctx, waitResp, &model, flavor, storage) + err = mapFields(ctx, waitResp, &model, flavor, storage, region) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Processing API payload: %v", err)) return @@ -464,16 +517,18 @@ func (r *instanceResource) Delete(ctx context.Context, req resource.DeleteReques } projectId := model.ProjectId.ValueString() instanceId := model.InstanceId.ValueString() + region := model.Region.ValueString() ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "instance_id", instanceId) + ctx = tflog.SetField(ctx, "region", region) // Delete existing instance - err := r.client.DeleteInstance(ctx, projectId, instanceId).Execute() + err := r.client.DeleteInstance(ctx, projectId, region, instanceId).Execute() if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting instance", fmt.Sprintf("Calling API: %v", err)) return } - _, err = wait.DeleteInstanceWaitHandler(ctx, r.client, projectId, instanceId).SetTimeout(45 * time.Minute).WaitWithContext(ctx) + _, err = wait.DeleteInstanceWaitHandler(ctx, r.client, projectId, region, instanceId).SetTimeout(45 * time.Minute).WaitWithContext(ctx) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting instance", fmt.Sprintf("Instance deletion waiting: %v", err)) return @@ -486,20 +541,21 @@ func (r *instanceResource) Delete(ctx context.Context, req resource.DeleteReques func (r *instanceResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { idParts := strings.Split(req.ID, core.Separator) - if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" { + 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],[instance_id] Got: %q", req.ID), + 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("instance_id"), idParts[1])...) + 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])...) tflog.Info(ctx, "Postgres Flex instance state imported") } -func mapFields(ctx context.Context, resp *postgresflex.InstanceResponse, model *Model, flavor *flavorModel, storage *storageModel) error { +func mapFields(ctx context.Context, resp *postgresflex.InstanceResponse, model *Model, flavor *flavorModel, storage *storageModel, region string) error { if resp == nil { return fmt.Errorf("response input is nil") } @@ -579,6 +635,7 @@ func mapFields(ctx context.Context, resp *postgresflex.InstanceResponse, model * idParts := []string{ model.ProjectId.ValueString(), + region, instanceId, } model.Id = types.StringValue( @@ -592,6 +649,7 @@ func mapFields(ctx context.Context, resp *postgresflex.InstanceResponse, model * model.Replicas = types.Int64PointerValue(instance.Replicas) model.Storage = storageObject model.Version = types.StringPointerValue(instance.Version) + model.Region = types.StringValue(region) return nil } @@ -656,7 +714,7 @@ func toUpdatePayload(model *Model, acl []string, flavor *flavorModel, storage *s } type postgresFlexClient interface { - ListFlavorsExecute(ctx context.Context, projectId string) (*postgresflex.ListFlavorsResponse, error) + ListFlavorsExecute(ctx context.Context, projectId string, region string) (*postgresflex.ListFlavorsResponse, error) } func loadFlavorId(ctx context.Context, client postgresFlexClient, model *Model, flavor *flavorModel) error { @@ -676,7 +734,8 @@ func loadFlavorId(ctx context.Context, client postgresFlexClient, model *Model, } projectId := model.ProjectId.ValueString() - res, err := client.ListFlavorsExecute(ctx, projectId) + region := model.Region.ValueString() + res, err := client.ListFlavorsExecute(ctx, projectId, region) if err != nil { return fmt.Errorf("listing postgresflex flavors: %w", err) } diff --git a/stackit/internal/services/postgresflex/instance/resource_test.go b/stackit/internal/services/postgresflex/instance/resource_test.go index 7bd7b128..4b3c5807 100644 --- a/stackit/internal/services/postgresflex/instance/resource_test.go +++ b/stackit/internal/services/postgresflex/instance/resource_test.go @@ -17,7 +17,7 @@ type postgresFlexClientMocked struct { getFlavorsResp *postgresflex.ListFlavorsResponse } -func (c *postgresFlexClientMocked) ListFlavorsExecute(_ context.Context, _ string) (*postgresflex.ListFlavorsResponse, error) { +func (c *postgresFlexClientMocked) ListFlavorsExecute(_ context.Context, _, _ string) (*postgresflex.ListFlavorsResponse, error) { if c.returnError { return nil, fmt.Errorf("get flavors failed") } @@ -26,12 +26,14 @@ func (c *postgresFlexClientMocked) ListFlavorsExecute(_ context.Context, _ strin } func TestMapFields(t *testing.T) { + const testRegion = "region" tests := []struct { description string state Model input *postgresflex.InstanceResponse flavor *flavorModel storage *storageModel + region string expected Model isValid bool }{ @@ -46,8 +48,9 @@ func TestMapFields(t *testing.T) { }, &flavorModel{}, &storageModel{}, + testRegion, Model{ - Id: types.StringValue("pid,iid"), + Id: types.StringValue("pid,region,iid"), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), Name: types.StringNull(), @@ -65,6 +68,7 @@ func TestMapFields(t *testing.T) { "size": types.Int64Null(), }), Version: types.StringNull(), + Region: types.StringValue(testRegion), }, true, }, @@ -103,8 +107,9 @@ func TestMapFields(t *testing.T) { }, &flavorModel{}, &storageModel{}, + testRegion, Model{ - Id: types.StringValue("pid,iid"), + Id: types.StringValue("pid,region,iid"), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), Name: types.StringValue("name"), @@ -126,6 +131,7 @@ func TestMapFields(t *testing.T) { "size": types.Int64Value(78), }), Version: types.StringValue("version"), + Region: types.StringValue(testRegion), }, true, }, @@ -162,8 +168,9 @@ func TestMapFields(t *testing.T) { Class: types.StringValue("class"), Size: types.Int64Value(78), }, + testRegion, Model{ - Id: types.StringValue("pid,iid"), + Id: types.StringValue("pid,region,iid"), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), Name: types.StringValue("name"), @@ -185,6 +192,7 @@ func TestMapFields(t *testing.T) { "size": types.Int64Value(78), }), Version: types.StringValue("version"), + Region: types.StringValue(testRegion), }, true, }, @@ -226,8 +234,9 @@ func TestMapFields(t *testing.T) { Class: types.StringValue("class"), Size: types.Int64Value(78), }, + testRegion, Model{ - Id: types.StringValue("pid,iid"), + Id: types.StringValue("pid,region,iid"), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), Name: types.StringValue("name"), @@ -249,6 +258,7 @@ func TestMapFields(t *testing.T) { "size": types.Int64Value(78), }), Version: types.StringValue("version"), + Region: types.StringValue(testRegion), }, true, }, @@ -261,6 +271,7 @@ func TestMapFields(t *testing.T) { nil, &flavorModel{}, &storageModel{}, + testRegion, Model{}, false, }, @@ -273,13 +284,14 @@ func TestMapFields(t *testing.T) { &postgresflex.InstanceResponse{}, &flavorModel{}, &storageModel{}, + testRegion, Model{}, false, }, } for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - err := mapFields(context.Background(), tt.input, &tt.state, tt.flavor, tt.storage) + err := mapFields(context.Background(), tt.input, &tt.state, tt.flavor, tt.storage, tt.region) if !tt.isValid && err == nil { t.Fatalf("Should have failed") } diff --git a/stackit/internal/services/postgresflex/postgresflex_acc_test.go b/stackit/internal/services/postgresflex/postgresflex_acc_test.go index 64148217..27dda5bd 100644 --- a/stackit/internal/services/postgresflex/postgresflex_acc_test.go +++ b/stackit/internal/services/postgresflex/postgresflex_acc_test.go @@ -47,7 +47,11 @@ var databaseResource = map[string]string{ "name": fmt.Sprintf("tfaccdb%s", acctest.RandStringFromCharSet(4, acctest.CharSetAlphaNum)), } -func configResources(backupSchedule string) string { +func configResources(backupSchedule string, region *string) string { + var regionConfig string + if region != nil { + regionConfig = fmt.Sprintf(`region = %q`, *region) + } return fmt.Sprintf(` %s @@ -66,6 +70,7 @@ func configResources(backupSchedule string) string { size = %s } version = "%s" + %s } resource "stackit_postgresflex_user" "user" { @@ -93,6 +98,7 @@ func configResources(backupSchedule string) string { instanceResource["storage_class"], instanceResource["storage_size"], instanceResource["version"], + regionConfig, userResource["username"], userResource["role"], databaseResource["name"], @@ -100,13 +106,14 @@ func configResources(backupSchedule string) string { } func TestAccPostgresFlexFlexResource(t *testing.T) { + testRegion := utils.Ptr("eu01") resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories, CheckDestroy: testAccCheckPostgresFlexDestroy, Steps: []resource.TestStep{ // Creation { - Config: configResources(instanceResource["backup_schedule"]), + Config: configResources(instanceResource["backup_schedule"], testRegion), Check: resource.ComposeAggregateTestCheckFunc( // Instance resource.TestCheckResourceAttr("stackit_postgresflex_instance.instance", "project_id", instanceResource["project_id"]), @@ -123,6 +130,7 @@ func TestAccPostgresFlexFlexResource(t *testing.T) { resource.TestCheckResourceAttr("stackit_postgresflex_instance.instance", "storage.class", instanceResource["storage_class"]), resource.TestCheckResourceAttr("stackit_postgresflex_instance.instance", "storage.size", instanceResource["storage_size"]), resource.TestCheckResourceAttr("stackit_postgresflex_instance.instance", "version", instanceResource["version"]), + resource.TestCheckResourceAttr("stackit_postgresflex_instance.instance", "region", *testRegion), // User resource.TestCheckResourceAttrPair( @@ -174,7 +182,7 @@ func TestAccPostgresFlexFlexResource(t *testing.T) { database_id = stackit_postgresflex_database.database.database_id } `, - configResources(instanceResource["backup_schedule"]), + configResources(instanceResource["backup_schedule"], nil), ), Check: resource.ComposeAggregateTestCheckFunc( // Instance data @@ -237,7 +245,7 @@ func TestAccPostgresFlexFlexResource(t *testing.T) { return "", fmt.Errorf("couldn't find attribute instance_id") } - return fmt.Sprintf("%s,%s", testutil.ProjectId, instanceId), nil + return fmt.Sprintf("%s,%s,%s", testutil.ProjectId, testutil.Region, instanceId), nil }, ImportState: true, ImportStateVerify: true, @@ -259,7 +267,7 @@ func TestAccPostgresFlexFlexResource(t *testing.T) { return "", fmt.Errorf("couldn't find attribute user_id") } - return fmt.Sprintf("%s,%s,%s", testutil.ProjectId, instanceId, userId), nil + return fmt.Sprintf("%s,%s,%s,%s", testutil.ProjectId, testutil.Region, instanceId, userId), nil }, ImportState: true, ImportStateVerify: true, @@ -281,14 +289,14 @@ func TestAccPostgresFlexFlexResource(t *testing.T) { return "", fmt.Errorf("couldn't find attribute database_id") } - return fmt.Sprintf("%s,%s,%s", testutil.ProjectId, instanceId, databaseId), nil + return fmt.Sprintf("%s,%s,%s,%s", testutil.ProjectId, testutil.Region, instanceId, databaseId), nil }, ImportState: true, ImportStateVerify: true, }, // Update { - Config: configResources(instanceResource["backup_schedule_updated"]), + Config: configResources(instanceResource["backup_schedule_updated"], nil), Check: resource.ComposeAggregateTestCheckFunc( // Instance data resource.TestCheckResourceAttr("stackit_postgresflex_instance.instance", "project_id", instanceResource["project_id"]), @@ -317,9 +325,7 @@ func testAccCheckPostgresFlexDestroy(s *terraform.State) error { var client *postgresflex.APIClient var err error if testutil.PostgresFlexCustomEndpoint == "" { - client, err = postgresflex.NewAPIClient( - config.WithRegion("eu01"), - ) + client, err = postgresflex.NewAPIClient() } else { client, err = postgresflex.NewAPIClient( config.WithEndpoint(testutil.PostgresFlexCustomEndpoint), @@ -334,12 +340,12 @@ func testAccCheckPostgresFlexDestroy(s *terraform.State) error { if rs.Type != "stackit_postgresflex_instance" { continue } - // instance terraform ID: = "[project_id],[instance_id]" + // instance terraform ID: = "[project_id],[region],[instance_id]" instanceId := strings.Split(rs.Primary.ID, core.Separator)[1] instancesToDestroy = append(instancesToDestroy, instanceId) } - instancesResp, err := client.ListInstances(ctx, testutil.ProjectId).Execute() + instancesResp, err := client.ListInstances(ctx, testutil.ProjectId, testutil.Region).Execute() if err != nil { return fmt.Errorf("getting instancesResp: %w", err) } @@ -350,15 +356,15 @@ func testAccCheckPostgresFlexDestroy(s *terraform.State) error { continue } if utils.Contains(instancesToDestroy, *items[i].Id) { - err := client.DeleteInstanceExecute(ctx, testutil.ProjectId, *items[i].Id) + err := client.DeleteInstanceExecute(ctx, testutil.ProjectId, testutil.Region, *items[i].Id) if err != nil { return fmt.Errorf("deleting instance %s during CheckDestroy: %w", *items[i].Id, err) } - _, err = wait.DeleteInstanceWaitHandler(ctx, client, testutil.ProjectId, *items[i].Id).WaitWithContext(ctx) + _, err = wait.DeleteInstanceWaitHandler(ctx, client, testutil.ProjectId, testutil.Region, *items[i].Id).WaitWithContext(ctx) if err != nil { return fmt.Errorf("deleting instance %s during CheckDestroy: waiting for deletion %w", *items[i].Id, err) } - err = client.ForceDeleteInstanceExecute(ctx, testutil.ProjectId, *items[i].Id) + err = client.ForceDeleteInstanceExecute(ctx, testutil.ProjectId, testutil.Region, *items[i].Id) if err != nil { return fmt.Errorf("force deleting instance %s during CheckDestroy: %w", *items[i].Id, err) } diff --git a/stackit/internal/services/postgresflex/user/datasource.go b/stackit/internal/services/postgresflex/user/datasource.go index 91fa8252..19edd9b4 100644 --- a/stackit/internal/services/postgresflex/user/datasource.go +++ b/stackit/internal/services/postgresflex/user/datasource.go @@ -11,6 +11,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" @@ -34,6 +35,7 @@ type DataSourceModel struct { Roles types.Set `tfsdk:"roles"` Host types.String `tfsdk:"host"` Port types.Int64 `tfsdk:"port"` + Region types.String `tfsdk:"region"` } // NewUserDataSource is a helper function to simplify the provider implementation. @@ -43,7 +45,8 @@ func NewUserDataSource() datasource.DataSource { // userDataSource is the data source implementation. type userDataSource struct { - client *postgresflex.APIClient + client *postgresflex.APIClient + providerData core.ProviderData } // Metadata returns the data source type name. @@ -58,7 +61,8 @@ func (r *userDataSource) Configure(ctx context.Context, req datasource.Configure return } - providerData, ok := req.ProviderData.(core.ProviderData) + var ok bool + r.providerData, ok = req.ProviderData.(core.ProviderData) if !ok { core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData)) return @@ -66,15 +70,15 @@ func (r *userDataSource) Configure(ctx context.Context, req datasource.Configure var apiClient *postgresflex.APIClient var err error - if providerData.PostgresFlexCustomEndpoint != "" { + if r.providerData.PostgresFlexCustomEndpoint != "" { apiClient, err = postgresflex.NewAPIClient( - config.WithCustomAuth(providerData.RoundTripper), - config.WithEndpoint(providerData.PostgresFlexCustomEndpoint), + config.WithCustomAuth(r.providerData.RoundTripper), + config.WithEndpoint(r.providerData.PostgresFlexCustomEndpoint), ) } else { apiClient, err = postgresflex.NewAPIClient( - config.WithCustomAuth(providerData.RoundTripper), - config.WithRegion(providerData.GetRegion()), + config.WithCustomAuth(r.providerData.RoundTripper), + config.WithRegion(r.providerData.GetRegion()), ) } @@ -91,10 +95,11 @@ func (r *userDataSource) Configure(ctx context.Context, req datasource.Configure func (r *userDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { descriptions := map[string]string{ "main": "Postgres Flex user data source schema. Must have a `region` specified in the provider configuration.", - "id": "Terraform's internal data source. ID. It is structured as \"`project_id`,`instance_id`,`user_id`\".", + "id": "Terraform's internal data source. ID. It is structured as \"`project_id`,`region`,`instance_id`,`user_id`\".", "user_id": "User ID.", "instance_id": "ID of the PostgresFlex instance.", "project_id": "STACKIT project ID to which the instance is associated.", + "region": "The resource region. If not defined, the provider region is used.", } resp.Schema = schema.Schema{ @@ -140,6 +145,11 @@ func (r *userDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, r "port": schema.Int64Attribute{ Computed: true, }, + "region": schema.StringAttribute{ + // the region cannot be found automatically, so it has to be passed + Optional: true, + Description: descriptions["region"], + }, }, } } @@ -155,11 +165,18 @@ func (r *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, r projectId := model.ProjectId.ValueString() instanceId := model.InstanceId.ValueString() userId := model.UserId.ValueString() + var region string + if utils.IsUndefined(model.Region) { + region = r.providerData.GetRegion() + } else { + region = model.Region.ValueString() + } ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "user_id", userId) + ctx = tflog.SetField(ctx, "region", region) - recordSetResp, err := r.client.GetUser(ctx, projectId, instanceId, userId).Execute() + recordSetResp, err := r.client.GetUser(ctx, projectId, region, instanceId, userId).Execute() if err != nil { oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped if ok && oapiErr.StatusCode == http.StatusNotFound { @@ -170,7 +187,7 @@ func (r *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, r } // Map response body to schema and populate Computed attribute values - err = mapDataSourceFields(recordSetResp, &model) + err = mapDataSourceFields(recordSetResp, &model, region) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading user", fmt.Sprintf("Processing API payload: %v", err)) return @@ -185,7 +202,7 @@ func (r *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, r tflog.Info(ctx, "Postgres Flex user read") } -func mapDataSourceFields(userResp *postgresflex.GetUserResponse, model *DataSourceModel) error { +func mapDataSourceFields(userResp *postgresflex.GetUserResponse, model *DataSourceModel, region string) error { if userResp == nil || userResp.Item == nil { return fmt.Errorf("response is nil") } @@ -204,6 +221,7 @@ func mapDataSourceFields(userResp *postgresflex.GetUserResponse, model *DataSour } idParts := []string{ model.ProjectId.ValueString(), + region, model.InstanceId.ValueString(), userId, } @@ -228,5 +246,6 @@ func mapDataSourceFields(userResp *postgresflex.GetUserResponse, model *DataSour } model.Host = types.StringPointerValue(user.Host) model.Port = types.Int64PointerValue(user.Port) + model.Region = types.StringValue(region) return nil } diff --git a/stackit/internal/services/postgresflex/user/datasource_test.go b/stackit/internal/services/postgresflex/user/datasource_test.go index cca773d0..ac824ccb 100644 --- a/stackit/internal/services/postgresflex/user/datasource_test.go +++ b/stackit/internal/services/postgresflex/user/datasource_test.go @@ -11,9 +11,11 @@ import ( ) func TestMapDataSourceFields(t *testing.T) { + const testRegion = "region" tests := []struct { description string input *postgresflex.GetUserResponse + region string expected DataSourceModel isValid bool }{ @@ -22,8 +24,9 @@ func TestMapDataSourceFields(t *testing.T) { &postgresflex.GetUserResponse{ Item: &postgresflex.UserResponse{}, }, + testRegion, DataSourceModel{ - Id: types.StringValue("pid,iid,uid"), + Id: types.StringValue("pid,region,iid,uid"), UserId: types.StringValue("uid"), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), @@ -31,6 +34,7 @@ func TestMapDataSourceFields(t *testing.T) { Roles: types.SetNull(types.StringType), Host: types.StringNull(), Port: types.Int64Null(), + Region: types.StringValue(testRegion), }, true, }, @@ -48,8 +52,9 @@ func TestMapDataSourceFields(t *testing.T) { Port: utils.Ptr(int64(1234)), }, }, + testRegion, DataSourceModel{ - Id: types.StringValue("pid,iid,uid"), + Id: types.StringValue("pid,region,iid,uid"), UserId: types.StringValue("uid"), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), @@ -59,8 +64,9 @@ func TestMapDataSourceFields(t *testing.T) { types.StringValue("role_2"), types.StringValue(""), }), - Host: types.StringValue("host"), - Port: types.Int64Value(1234), + Host: types.StringValue("host"), + Port: types.Int64Value(1234), + Region: types.StringValue(testRegion), }, true, }, @@ -75,8 +81,9 @@ func TestMapDataSourceFields(t *testing.T) { Port: utils.Ptr(int64(2123456789)), }, }, + testRegion, DataSourceModel{ - Id: types.StringValue("pid,iid,uid"), + Id: types.StringValue("pid,region,iid,uid"), UserId: types.StringValue("uid"), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), @@ -84,18 +91,21 @@ func TestMapDataSourceFields(t *testing.T) { Roles: types.SetValueMust(types.StringType, []attr.Value{}), Host: types.StringNull(), Port: types.Int64Value(2123456789), + Region: types.StringValue(testRegion), }, true, }, { "nil_response", nil, + testRegion, DataSourceModel{}, false, }, { "nil_response_2", &postgresflex.GetUserResponse{}, + testRegion, DataSourceModel{}, false, }, @@ -104,6 +114,7 @@ func TestMapDataSourceFields(t *testing.T) { &postgresflex.GetUserResponse{ Item: &postgresflex.UserResponse{}, }, + testRegion, DataSourceModel{}, false, }, @@ -115,7 +126,7 @@ func TestMapDataSourceFields(t *testing.T) { InstanceId: tt.expected.InstanceId, UserId: tt.expected.UserId, } - err := mapDataSourceFields(tt.input, state) + err := mapDataSourceFields(tt.input, state, tt.region) if !tt.isValid && err == nil { t.Fatalf("Should have failed") } diff --git a/stackit/internal/services/postgresflex/user/resource.go b/stackit/internal/services/postgresflex/user/resource.go index e779bef0..75ba451e 100644 --- a/stackit/internal/services/postgresflex/user/resource.go +++ b/stackit/internal/services/postgresflex/user/resource.go @@ -34,6 +34,7 @@ var ( _ resource.Resource = &userResource{} _ resource.ResourceWithConfigure = &userResource{} _ resource.ResourceWithImportState = &userResource{} + _ resource.ResourceWithModifyPlan = &userResource{} ) type Model struct { @@ -47,6 +48,7 @@ type Model struct { Host types.String `tfsdk:"host"` Port types.Int64 `tfsdk:"port"` Uri types.String `tfsdk:"uri"` + Region types.String `tfsdk:"region"` } // NewUserResource is a helper function to simplify the provider implementation. @@ -56,7 +58,38 @@ func NewUserResource() resource.Resource { // userResource is the resource implementation. type userResource struct { - client *postgresflex.APIClient + client *postgresflex.APIClient + providerData core.ProviderData +} + +// ModifyPlan implements resource.ResourceWithModifyPlan. +// Use the modifier to set the effective region in the current plan. +func (r *userResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // nolint:gocritic // function signature required by Terraform + var configModel Model + // skip initial empty configuration to avoid follow-up errors + if req.Config.Raw.IsNull() { + return + } + resp.Diagnostics.Append(req.Config.Get(ctx, &configModel)...) + if resp.Diagnostics.HasError() { + return + } + + var planModel Model + resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...) + if resp.Diagnostics.HasError() { + return + } + + utils.AdaptRegion(ctx, configModel.Region, &planModel.Region, r.providerData.GetRegion(), resp) + if resp.Diagnostics.HasError() { + return + } + + resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...) + if resp.Diagnostics.HasError() { + return + } } // Metadata returns the resource type name. @@ -71,7 +104,8 @@ func (r *userResource) Configure(ctx context.Context, req resource.ConfigureRequ return } - providerData, ok := req.ProviderData.(core.ProviderData) + var ok bool + r.providerData, ok = req.ProviderData.(core.ProviderData) if !ok { core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData)) return @@ -79,15 +113,15 @@ func (r *userResource) Configure(ctx context.Context, req resource.ConfigureRequ var apiClient *postgresflex.APIClient var err error - if providerData.PostgresFlexCustomEndpoint != "" { + if r.providerData.PostgresFlexCustomEndpoint != "" { apiClient, err = postgresflex.NewAPIClient( - config.WithCustomAuth(providerData.RoundTripper), - config.WithEndpoint(providerData.PostgresFlexCustomEndpoint), + config.WithCustomAuth(r.providerData.RoundTripper), + config.WithEndpoint(r.providerData.PostgresFlexCustomEndpoint), ) } else { apiClient, err = postgresflex.NewAPIClient( - config.WithCustomAuth(providerData.RoundTripper), - config.WithRegion(providerData.GetRegion()), + config.WithCustomAuth(r.providerData.RoundTripper), + config.WithRegion(r.providerData.GetRegion()), ) } @@ -106,11 +140,12 @@ func (r *userResource) Schema(_ context.Context, _ resource.SchemaRequest, resp descriptions := map[string]string{ "main": "Postgres Flex user resource schema. Must have a `region` specified in the provider configuration.", - "id": "Terraform's internal resource ID. It is structured as \"`project_id`,`instance_id`,`user_id`\".", + "id": "Terraform's internal resource ID. It is structured as \"`project_id`,`region`,`instance_id`,`user_id`\".", "user_id": "User ID.", "instance_id": "ID of the PostgresFlex instance.", "project_id": "STACKIT project ID to which the instance is associated.", "roles": "Database access levels for the user. " + utils.SupportedValuesDocumentation(rolesOptions), + "region": "The resource region. If not defined, the provider region is used.", } resp.Schema = schema.Schema{ @@ -190,6 +225,15 @@ func (r *userResource) Schema(_ context.Context, _ resource.SchemaRequest, resp Computed: true, Sensitive: true, }, + "region": schema.StringAttribute{ + Optional: true, + // must be computed to allow for storing the override value from the provider + Computed: true, + Description: descriptions["region"], + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, }, } } @@ -204,8 +248,10 @@ func (r *userResource) Create(ctx context.Context, req resource.CreateRequest, r } projectId := model.ProjectId.ValueString() instanceId := model.InstanceId.ValueString() + region := model.Region.ValueString() ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "instance_id", instanceId) + ctx = tflog.SetField(ctx, "region", region) var roles []string if !(model.Roles.IsNull() || model.Roles.IsUnknown()) { @@ -223,7 +269,7 @@ func (r *userResource) Create(ctx context.Context, req resource.CreateRequest, r return } // Create new user - userResp, err := r.client.CreateUser(ctx, projectId, instanceId).CreateUserPayload(*payload).Execute() + userResp, err := r.client.CreateUser(ctx, projectId, region, instanceId).CreateUserPayload(*payload).Execute() if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating user", fmt.Sprintf("Calling API: %v", err)) return @@ -236,7 +282,7 @@ func (r *userResource) Create(ctx context.Context, req resource.CreateRequest, r ctx = tflog.SetField(ctx, "user_id", userId) // Map response body to schema - err = mapFieldsCreate(userResp, &model) + err = mapFieldsCreate(userResp, &model, region) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating user", fmt.Sprintf("Processing API payload: %v", err)) return @@ -261,11 +307,16 @@ func (r *userResource) Read(ctx context.Context, req resource.ReadRequest, resp projectId := model.ProjectId.ValueString() instanceId := model.InstanceId.ValueString() userId := model.UserId.ValueString() + region := model.Region.ValueString() + if region == "" { + region = r.providerData.GetRegion() + } ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "user_id", userId) + ctx = tflog.SetField(ctx, "region", region) - recordSetResp, err := r.client.GetUser(ctx, projectId, instanceId, userId).Execute() + recordSetResp, err := r.client.GetUser(ctx, projectId, region, instanceId, userId).Execute() if err != nil { oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped if ok && oapiErr.StatusCode == http.StatusNotFound { @@ -277,7 +328,7 @@ func (r *userResource) Read(ctx context.Context, req resource.ReadRequest, resp } // Map response body to schema - err = mapFields(recordSetResp, &model) + err = mapFields(recordSetResp, &model, region) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading user", fmt.Sprintf("Processing API payload: %v", err)) return @@ -311,16 +362,18 @@ func (r *userResource) Delete(ctx context.Context, req resource.DeleteRequest, r projectId := model.ProjectId.ValueString() instanceId := model.InstanceId.ValueString() userId := model.UserId.ValueString() + region := model.Region.ValueString() ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "user_id", userId) + ctx = tflog.SetField(ctx, "region", region) // Delete existing record set - err := r.client.DeleteUser(ctx, projectId, instanceId, userId).Execute() + err := r.client.DeleteUser(ctx, projectId, region, instanceId, userId).Execute() if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting user", fmt.Sprintf("Calling API: %v", err)) } - _, err = wait.DeleteUserWaitHandler(ctx, r.client, projectId, instanceId, userId).WaitWithContext(ctx) + _, err = wait.DeleteUserWaitHandler(ctx, r.client, projectId, region, instanceId, userId).WaitWithContext(ctx) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting user", fmt.Sprintf("Instance deletion waiting: %v", err)) return @@ -332,17 +385,18 @@ func (r *userResource) Delete(ctx context.Context, req resource.DeleteRequest, r // The expected format of the resource import identifier is: project_id,zone_id,record_set_id func (r *userResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { idParts := strings.Split(req.ID, core.Separator) - if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" { + if len(idParts) != 4 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" || idParts[3] == "" { core.LogAndAddError(ctx, &resp.Diagnostics, "Error importing user", - fmt.Sprintf("Expected import identifier with format [project_id],[instance_id],[user_id], got %q", req.ID), + fmt.Sprintf("Expected import identifier with format [project_id],[region],[instance_id],[user_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("instance_id"), idParts[1])...) - resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("user_id"), idParts[2])...) + 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])...) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("user_id"), idParts[3])...) core.LogAndAddWarning(ctx, &resp.Diagnostics, "Postgresflex user imported with empty password and empty uri", "The user password and uri are not imported as they are only available upon creation of a new user. The password and uri fields will be empty.", @@ -350,7 +404,7 @@ func (r *userResource) ImportState(ctx context.Context, req resource.ImportState tflog.Info(ctx, "Postgresflex user state imported") } -func mapFieldsCreate(userResp *postgresflex.CreateUserResponse, model *Model) error { +func mapFieldsCreate(userResp *postgresflex.CreateUserResponse, model *Model, region string) error { if userResp == nil || userResp.Item == nil { return fmt.Errorf("response is nil") } @@ -365,6 +419,7 @@ func mapFieldsCreate(userResp *postgresflex.CreateUserResponse, model *Model) er userId := *user.Id idParts := []string{ model.ProjectId.ValueString(), + region, model.InstanceId.ValueString(), userId, } @@ -395,10 +450,11 @@ func mapFieldsCreate(userResp *postgresflex.CreateUserResponse, model *Model) er model.Host = types.StringPointerValue(user.Host) model.Port = types.Int64PointerValue(user.Port) model.Uri = types.StringPointerValue(user.Uri) + model.Region = types.StringValue(region) return nil } -func mapFields(userResp *postgresflex.GetUserResponse, model *Model) error { +func mapFields(userResp *postgresflex.GetUserResponse, model *Model, region string) error { if userResp == nil || userResp.Item == nil { return fmt.Errorf("response is nil") } @@ -417,6 +473,7 @@ func mapFields(userResp *postgresflex.GetUserResponse, model *Model) error { } idParts := []string{ model.ProjectId.ValueString(), + region, model.InstanceId.ValueString(), userId, } @@ -441,6 +498,7 @@ func mapFields(userResp *postgresflex.GetUserResponse, model *Model) error { } model.Host = types.StringPointerValue(user.Host) model.Port = types.Int64PointerValue(user.Port) + model.Region = types.StringValue(region) return nil } diff --git a/stackit/internal/services/postgresflex/user/resource_test.go b/stackit/internal/services/postgresflex/user/resource_test.go index 0126b86d..9bd59841 100644 --- a/stackit/internal/services/postgresflex/user/resource_test.go +++ b/stackit/internal/services/postgresflex/user/resource_test.go @@ -11,9 +11,11 @@ import ( ) func TestMapFieldsCreate(t *testing.T) { + const testRegion = "region" tests := []struct { description string input *postgresflex.CreateUserResponse + region string expected Model isValid bool }{ @@ -25,8 +27,9 @@ func TestMapFieldsCreate(t *testing.T) { Password: utils.Ptr(""), }, }, + testRegion, Model{ - Id: types.StringValue("pid,iid,uid"), + Id: types.StringValue("pid,region,iid,uid"), UserId: types.StringValue("uid"), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), @@ -36,6 +39,7 @@ func TestMapFieldsCreate(t *testing.T) { Host: types.StringNull(), Port: types.Int64Null(), Uri: types.StringNull(), + Region: types.StringValue(testRegion), }, true, }, @@ -56,8 +60,9 @@ func TestMapFieldsCreate(t *testing.T) { Uri: utils.Ptr("uri"), }, }, + testRegion, Model{ - Id: types.StringValue("pid,iid,uid"), + Id: types.StringValue("pid,region,iid,uid"), UserId: types.StringValue("uid"), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), @@ -71,6 +76,7 @@ func TestMapFieldsCreate(t *testing.T) { Host: types.StringValue("host"), Port: types.Int64Value(1234), Uri: types.StringValue("uri"), + Region: types.StringValue(testRegion), }, true, }, @@ -87,8 +93,9 @@ func TestMapFieldsCreate(t *testing.T) { Uri: nil, }, }, + testRegion, Model{ - Id: types.StringValue("pid,iid,uid"), + Id: types.StringValue("pid,region,iid,uid"), UserId: types.StringValue("uid"), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), @@ -98,18 +105,21 @@ func TestMapFieldsCreate(t *testing.T) { Host: types.StringNull(), Port: types.Int64Value(2123456789), Uri: types.StringNull(), + Region: types.StringValue(testRegion), }, true, }, { "nil_response", nil, + testRegion, Model{}, false, }, { "nil_response_2", &postgresflex.CreateUserResponse{}, + testRegion, Model{}, false, }, @@ -118,6 +128,7 @@ func TestMapFieldsCreate(t *testing.T) { &postgresflex.CreateUserResponse{ Item: &postgresflex.User{}, }, + testRegion, Model{}, false, }, @@ -128,6 +139,7 @@ func TestMapFieldsCreate(t *testing.T) { Id: utils.Ptr("uid"), }, }, + testRegion, Model{}, false, }, @@ -138,7 +150,7 @@ func TestMapFieldsCreate(t *testing.T) { ProjectId: tt.expected.ProjectId, InstanceId: tt.expected.InstanceId, } - err := mapFieldsCreate(tt.input, state) + err := mapFieldsCreate(tt.input, state, tt.region) if !tt.isValid && err == nil { t.Fatalf("Should have failed") } @@ -156,9 +168,11 @@ func TestMapFieldsCreate(t *testing.T) { } func TestMapFields(t *testing.T) { + const testRegion = "region" tests := []struct { description string input *postgresflex.GetUserResponse + region string expected Model isValid bool }{ @@ -167,8 +181,9 @@ func TestMapFields(t *testing.T) { &postgresflex.GetUserResponse{ Item: &postgresflex.UserResponse{}, }, + testRegion, Model{ - Id: types.StringValue("pid,iid,uid"), + Id: types.StringValue("pid,region,iid,uid"), UserId: types.StringValue("uid"), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), @@ -176,6 +191,7 @@ func TestMapFields(t *testing.T) { Roles: types.SetNull(types.StringType), Host: types.StringNull(), Port: types.Int64Null(), + Region: types.StringValue(testRegion), }, true, }, @@ -193,8 +209,9 @@ func TestMapFields(t *testing.T) { Port: utils.Ptr(int64(1234)), }, }, + testRegion, Model{ - Id: types.StringValue("pid,iid,uid"), + Id: types.StringValue("pid,region,iid,uid"), UserId: types.StringValue("uid"), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), @@ -204,8 +221,9 @@ func TestMapFields(t *testing.T) { types.StringValue("role_2"), types.StringValue(""), }), - Host: types.StringValue("host"), - Port: types.Int64Value(1234), + Host: types.StringValue("host"), + Port: types.Int64Value(1234), + Region: types.StringValue(testRegion), }, true, }, @@ -220,8 +238,9 @@ func TestMapFields(t *testing.T) { Port: utils.Ptr(int64(2123456789)), }, }, + testRegion, Model{ - Id: types.StringValue("pid,iid,uid"), + Id: types.StringValue("pid,region,iid,uid"), UserId: types.StringValue("uid"), InstanceId: types.StringValue("iid"), ProjectId: types.StringValue("pid"), @@ -229,18 +248,21 @@ func TestMapFields(t *testing.T) { Roles: types.SetValueMust(types.StringType, []attr.Value{}), Host: types.StringNull(), Port: types.Int64Value(2123456789), + Region: types.StringValue(testRegion), }, true, }, { "nil_response", nil, + testRegion, Model{}, false, }, { "nil_response_2", &postgresflex.GetUserResponse{}, + testRegion, Model{}, false, }, @@ -249,6 +271,7 @@ func TestMapFields(t *testing.T) { &postgresflex.GetUserResponse{ Item: &postgresflex.UserResponse{}, }, + testRegion, Model{}, false, }, @@ -260,7 +283,7 @@ func TestMapFields(t *testing.T) { InstanceId: tt.expected.InstanceId, UserId: tt.expected.UserId, } - err := mapFields(tt.input, state) + err := mapFields(tt.input, state, tt.region) if !tt.isValid && err == nil { t.Fatalf("Should have failed") }