Skip to content

Commit 6cc1dff

Browse files
authored
feat(postgresql): Region adjustment (#713)
Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
1 parent e989102 commit 6cc1dff

19 files changed

+458
-148
lines changed

docs/data-sources/postgresflex_database.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ data "stackit_postgresflex_database" "example" {
2929
- `instance_id` (String) ID of the Postgres Flex instance.
3030
- `project_id` (String) STACKIT project ID to which the instance is associated.
3131

32+
### Optional
33+
34+
- `region` (String) The resource region. If not defined, the provider region is used.
35+
3236
### Read-Only
3337

34-
- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`instance_id`,`database_id`".
38+
- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`region`,`instance_id`,`database_id`".
3539
- `name` (String) Database name.
3640
- `owner` (String) Username of the database owner.

docs/data-sources/postgresflex_instance.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ data "stackit_postgresflex_instance" "example" {
2727
- `instance_id` (String) ID of the PostgresFlex instance.
2828
- `project_id` (String) STACKIT project ID to which the instance is associated.
2929

30+
### Optional
31+
32+
- `region` (String) The resource region. If not defined, the provider region is used.
33+
3034
### Read-Only
3135

3236
- `acl` (List of String) The Access Control List (ACL) for the PostgresFlex instance.
3337
- `backup_schedule` (String)
3438
- `flavor` (Attributes) (see [below for nested schema](#nestedatt--flavor))
35-
- `id` (String) Terraform's internal data source. ID. It is structured as "`project_id`,`instance_id`".
39+
- `id` (String) Terraform's internal data source. ID. It is structured as "`project_id`,`region`,`instance_id`".
3640
- `name` (String) Instance name.
3741
- `replicas` (Number)
3842
- `storage` (Attributes) (see [below for nested schema](#nestedatt--storage))

docs/data-sources/postgresflex_user.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ data "stackit_postgresflex_user" "example" {
2929
- `project_id` (String) STACKIT project ID to which the instance is associated.
3030
- `user_id` (String) User ID.
3131

32+
### Optional
33+
34+
- `region` (String) The resource region. If not defined, the provider region is used.
35+
3236
### Read-Only
3337

3438
- `host` (String)
35-
- `id` (String) Terraform's internal data source. ID. It is structured as "`project_id`,`instance_id`,`user_id`".
39+
- `id` (String) Terraform's internal data source. ID. It is structured as "`project_id`,`region`,`instance_id`,`user_id`".
3640
- `port` (Number)
3741
- `roles` (Set of String)
3842
- `username` (String)

docs/resources/postgresflex_database.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ resource "stackit_postgresflex_database" "example" {
3131
- `owner` (String) Username of the database owner.
3232
- `project_id` (String) STACKIT project ID to which the instance is associated.
3333

34+
### Optional
35+
36+
- `region` (String) The resource region. If not defined, the provider region is used.
37+
3438
### Read-Only
3539

3640
- `database_id` (String) Database ID.
37-
- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`instance_id`,`database_id`".
41+
- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`region`,`instance_id`,`database_id`".

docs/resources/postgresflex_instance.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,13 @@ resource "stackit_postgresflex_instance" "example" {
4545
- `storage` (Attributes) (see [below for nested schema](#nestedatt--storage))
4646
- `version` (String)
4747

48+
### Optional
49+
50+
- `region` (String) The resource region. If not defined, the provider region is used.
51+
4852
### Read-Only
4953

50-
- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`instance_id`".
54+
- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`region`,`instance_id`".
5155
- `instance_id` (String) ID of the PostgresFlex instance.
5256

5357
<a id="nestedatt--flavor"></a>

docs/resources/postgresflex_user.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ resource "stackit_postgresflex_user" "example" {
3131
- `roles` (Set of String) Database access levels for the user. Supported values are: `login`, `createdb`.
3232
- `username` (String)
3333

34+
### Optional
35+
36+
- `region` (String) The resource region. If not defined, the provider region is used.
37+
3438
### Read-Only
3539

3640
- `host` (String)
37-
- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`instance_id`,`user_id`".
41+
- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`region`,`instance_id`,`user_id`".
3842
- `password` (String, Sensitive)
3943
- `port` (Number)
4044
- `uri` (String, Sensitive)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ require (
2222
github.com/stackitcloud/stackit-sdk-go/services/objectstorage v1.1.0
2323
github.com/stackitcloud/stackit-sdk-go/services/observability v0.3.0
2424
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.20.0
25-
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.18.0
25+
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.0.1
2626
github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v0.21.0
2727
github.com/stackitcloud/stackit-sdk-go/services/redis v0.21.0
2828
github.com/stackitcloud/stackit-sdk-go/services/resourcemanager v0.13.0

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ github.com/stackitcloud/stackit-sdk-go/services/observability v0.3.0 h1:Hn4BwKCz
175175
github.com/stackitcloud/stackit-sdk-go/services/observability v0.3.0/go.mod h1:PxfwA6YFtxwOajB4iTp1Eq7G02qUC3HdQPJvHGjQ1hk=
176176
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.20.0 h1:y83IhdbQv8EHovWPTqeulGgyZKJ39AQW1klo0g7a7og=
177177
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.20.0/go.mod h1:Gk3hWaQDCJGgaixjGkUmoIr74VNWwdAakiUrvizpOWQ=
178-
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.18.0 h1:cwdmiwbSml70kE9xV9C25t9WggDT98NdSfWD9w/r4wU=
179-
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.18.0/go.mod h1:wNPezvzJUgUj+C50EqyMAj5PSkhawT+2Zsdh01WQpAM=
178+
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.0.1 h1:vUi9//CyfS6UMv0hftYMamimjJLco5lxT/KW9y4QPqM=
179+
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.0.1/go.mod h1:7TqfCUZRW7sjv8qOrLV5IvS6jqvY9Uxka165zdjYwD4=
180180
github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v0.21.0 h1:zEJXwsuasmYH8dONZrCsZzcann/+6HZDKUPhN3mOmY0=
181181
github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v0.21.0/go.mod h1:SaL9BCTeWcEmU9JiKgNihEXKnFKDTn91L9ehgvauWuM=
182182
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
195195
github.com/stackitcloud/stackit-sdk-go/services/serviceenablement v0.5.0/go.mod h1:16dOVT052cMuHhUJ3NIcPuY7TrpCr9QlxmvvfjLZubA=
196196
github.com/stackitcloud/stackit-sdk-go/services/ske v0.22.0 h1:3KUVls8zXsbT2tOYRSHyp3/l0Kpjl4f3INmQKYTe65Y=
197197
github.com/stackitcloud/stackit-sdk-go/services/ske v0.22.0/go.mod h1:63IvXpBJTIVONAnGPSDo0sRJ+6n6tzO918OLqfYBxto=
198-
github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v0.10.0 h1:STq6VaVUeHLeXzl1r5E4+MK5lcNVtdKjjP7N0XOowY4=
199-
github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v0.10.0/go.mod h1:hdeLDwSCOmGIYtY4DGN15kjL44DQvo/txHXtTEvZidA=
200198
github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v1.0.0 h1:RYJO0rZea9+sxVfaJDWRo2zgfKNgiUcA5c0nbvZURiU=
201199
github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v1.0.0/go.mod h1:d2ICXCS2h3IMsZW0OanWkEH2XdLiY/XRKx2TcR940nw=
202200
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

stackit/internal/services/postgresflex/database/datasource.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1010
"github.com/hashicorp/terraform-plugin-log/tflog"
1111
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
12+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
1213
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
1314

1415
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
@@ -29,7 +30,8 @@ func NewDatabaseDataSource() datasource.DataSource {
2930

3031
// databaseDataSource is the data source implementation.
3132
type databaseDataSource struct {
32-
client *postgresflex.APIClient
33+
client *postgresflex.APIClient
34+
providerData core.ProviderData
3335
}
3436

3537
// Metadata returns the data source type name.
@@ -44,23 +46,24 @@ func (r *databaseDataSource) Configure(ctx context.Context, req datasource.Confi
4446
return
4547
}
4648

47-
providerData, ok := req.ProviderData.(core.ProviderData)
49+
var ok bool
50+
r.providerData, ok = req.ProviderData.(core.ProviderData)
4851
if !ok {
4952
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData))
5053
return
5154
}
5255

5356
var apiClient *postgresflex.APIClient
5457
var err error
55-
if providerData.PostgresFlexCustomEndpoint != "" {
58+
if r.providerData.PostgresFlexCustomEndpoint != "" {
5659
apiClient, err = postgresflex.NewAPIClient(
57-
config.WithCustomAuth(providerData.RoundTripper),
58-
config.WithEndpoint(providerData.PostgresFlexCustomEndpoint),
60+
config.WithCustomAuth(r.providerData.RoundTripper),
61+
config.WithEndpoint(r.providerData.PostgresFlexCustomEndpoint),
5962
)
6063
} else {
6164
apiClient, err = postgresflex.NewAPIClient(
62-
config.WithCustomAuth(providerData.RoundTripper),
63-
config.WithRegion(providerData.GetRegion()),
65+
config.WithCustomAuth(r.providerData.RoundTripper),
66+
config.WithRegion(r.providerData.GetRegion()),
6467
)
6568
}
6669

@@ -77,12 +80,13 @@ func (r *databaseDataSource) Configure(ctx context.Context, req datasource.Confi
7780
func (r *databaseDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
7881
descriptions := map[string]string{
7982
"main": "Postgres Flex database resource schema. Must have a `region` specified in the provider configuration.",
80-
"id": "Terraform's internal resource ID. It is structured as \"`project_id`,`instance_id`,`database_id`\".",
83+
"id": "Terraform's internal resource ID. It is structured as \"`project_id`,`region`,`instance_id`,`database_id`\".",
8184
"database_id": "Database ID.",
8285
"instance_id": "ID of the Postgres Flex instance.",
8386
"project_id": "STACKIT project ID to which the instance is associated.",
8487
"name": "Database name.",
8588
"owner": "Username of the database owner.",
89+
"region": "The resource region. If not defined, the provider region is used.",
8690
}
8791

8892
resp.Schema = schema.Schema{
@@ -123,6 +127,11 @@ func (r *databaseDataSource) Schema(_ context.Context, _ datasource.SchemaReques
123127
Description: descriptions["owner"],
124128
Computed: true,
125129
},
130+
"region": schema.StringAttribute{
131+
// the region cannot be found, so it has to be passed
132+
Optional: true,
133+
Description: descriptions["region"],
134+
},
126135
},
127136
}
128137
}
@@ -138,11 +147,18 @@ func (r *databaseDataSource) Read(ctx context.Context, req datasource.ReadReques
138147
projectId := model.ProjectId.ValueString()
139148
instanceId := model.InstanceId.ValueString()
140149
databaseId := model.DatabaseId.ValueString()
150+
var region string
151+
if utils.IsUndefined(model.Region) {
152+
region = r.providerData.GetRegion()
153+
} else {
154+
region = model.Region.ValueString()
155+
}
141156
ctx = tflog.SetField(ctx, "project_id", projectId)
142157
ctx = tflog.SetField(ctx, "instance_id", instanceId)
143158
ctx = tflog.SetField(ctx, "database_id", databaseId)
159+
ctx = tflog.SetField(ctx, "region", region)
144160

145-
databaseResp, err := getDatabase(ctx, r.client, projectId, instanceId, databaseId)
161+
databaseResp, err := getDatabase(ctx, r.client, projectId, region, instanceId, databaseId)
146162
if err != nil {
147163
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
148164
if ok && oapiErr.StatusCode == http.StatusNotFound {
@@ -153,7 +169,7 @@ func (r *databaseDataSource) Read(ctx context.Context, req datasource.ReadReques
153169
}
154170

155171
// Map response body to schema and populate Computed attribute values
156-
err = mapFields(databaseResp, &model)
172+
err = mapFields(databaseResp, &model, region)
157173
if err != nil {
158174
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading database", fmt.Sprintf("Processing API payload: %v", err))
159175
return

0 commit comments

Comments
 (0)