Skip to content

Commit 81f876a

Browse files
authored
feat: region adjustments SQLServerFlex (#707)
* feat: region adjustment sqlserverflex * adapt acceptance tests * add region to internal id of sqlserverflex resources to support import of different regions
1 parent 4cfdbc5 commit 81f876a

15 files changed

Lines changed: 360 additions & 105 deletions

File tree

docs/data-sources/sqlserverflex_instance.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ data "stackit_sqlserverflex_instance" "example" {
2727
- `instance_id` (String) ID of the SQLServer Flex 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 SQLServer Flex instance.
3337
- `backup_schedule` (String) The backup schedule. Should follow the cron scheduling system format (e.g. "0 0 * * *").
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
- `options` (Attributes) Custom parameters for the SQLServer Flex instance. (see [below for nested schema](#nestedatt--options))
3842
- `replicas` (Number)

docs/data-sources/sqlserverflex_user.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ data "stackit_sqlserverflex_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) Database access levels for the user.
3842
- `username` (String) Username of the SQLServer Flex instance.

docs/resources/sqlserverflex_instance.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ resource "stackit_sqlserverflex_instance" "example" {
4545
- `acl` (List of String) The Access Control List (ACL) for the SQLServer Flex instance.
4646
- `backup_schedule` (String) The backup schedule. Should follow the cron scheduling system format (e.g. "0 0 * * *")
4747
- `options` (Attributes) (see [below for nested schema](#nestedatt--options))
48+
- `region` (String) The resource region. If not defined, the provider region is used.
4849
- `storage` (Attributes) (see [below for nested schema](#nestedatt--storage))
4950
- `version` (String)
5051

5152
### Read-Only
5253

53-
- `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`".
5455
- `instance_id` (String) ID of the SQLServer Flex instance.
5556
- `replicas` (Number)
5657

docs/resources/sqlserverflex_user.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ resource "stackit_sqlserverflex_user" "example" {
3232

3333
### Optional
3434

35+
- `region` (String)
3536
- `roles` (Set of String) Database access levels for the user.
3637

3738
### Read-Only
3839

3940
- `host` (String)
40-
- `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`".
4142
- `password` (String, Sensitive) Password of the user account.
4243
- `port` (Number)
4344
- `user_id` (String) User ID.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ require (
3131
github.com/stackitcloud/stackit-sdk-go/services/serverupdate v0.5.0
3232
github.com/stackitcloud/stackit-sdk-go/services/serviceenablement v0.5.0
3333
github.com/stackitcloud/stackit-sdk-go/services/ske v0.22.0
34-
github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v0.10.0
34+
github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v1.0.0
3535
github.com/teambition/rrule-go v1.8.2
3636
golang.org/x/mod v0.23.0
3737
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ github.com/stackitcloud/stackit-sdk-go/services/ske v0.22.0 h1:3KUVls8zXsbT2tOYR
195195
github.com/stackitcloud/stackit-sdk-go/services/ske v0.22.0/go.mod h1:63IvXpBJTIVONAnGPSDo0sRJ+6n6tzO918OLqfYBxto=
196196
github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v0.10.0 h1:STq6VaVUeHLeXzl1r5E4+MK5lcNVtdKjjP7N0XOowY4=
197197
github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v0.10.0/go.mod h1:hdeLDwSCOmGIYtY4DGN15kjL44DQvo/txHXtTEvZidA=
198+
github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v1.0.0 h1:RYJO0rZea9+sxVfaJDWRo2zgfKNgiUcA5c0nbvZURiU=
199+
github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v1.0.0/go.mod h1:d2ICXCS2h3IMsZW0OanWkEH2XdLiY/XRKx2TcR940nw=
198200
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
199201
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
200202
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=

stackit/internal/services/sqlserverflex/instance/datasource.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
1111
"github.com/hashicorp/terraform-plugin-log/tflog"
1212
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
13+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
1314
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
1415

1516
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
@@ -31,7 +32,8 @@ func NewInstanceDataSource() datasource.DataSource {
3132

3233
// instanceDataSource is the data source implementation.
3334
type instanceDataSource struct {
34-
client *sqlserverflex.APIClient
35+
client *sqlserverflex.APIClient
36+
providerData core.ProviderData
3537
}
3638

3739
// Metadata returns the data source type name.
@@ -46,23 +48,24 @@ func (r *instanceDataSource) Configure(ctx context.Context, req datasource.Confi
4648
return
4749
}
4850

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

5558
var apiClient *sqlserverflex.APIClient
5659
var err error
57-
if providerData.SQLServerFlexCustomEndpoint != "" {
60+
if r.providerData.SQLServerFlexCustomEndpoint != "" {
5861
apiClient, err = sqlserverflex.NewAPIClient(
59-
config.WithCustomAuth(providerData.RoundTripper),
60-
config.WithEndpoint(providerData.SQLServerFlexCustomEndpoint),
62+
config.WithCustomAuth(r.providerData.RoundTripper),
63+
config.WithEndpoint(r.providerData.SQLServerFlexCustomEndpoint),
6164
)
6265
} else {
6366
apiClient, err = sqlserverflex.NewAPIClient(
64-
config.WithCustomAuth(providerData.RoundTripper),
65-
config.WithRegion(providerData.Region),
67+
config.WithCustomAuth(r.providerData.RoundTripper),
68+
config.WithRegion(r.providerData.Region),
6669
)
6770
}
6871

@@ -79,13 +82,14 @@ func (r *instanceDataSource) Configure(ctx context.Context, req datasource.Confi
7982
func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
8083
descriptions := map[string]string{
8184
"main": "SQLServer Flex instance data source schema. Must have a `region` specified in the provider configuration.",
82-
"id": "Terraform's internal data source. ID. It is structured as \"`project_id`,`instance_id`\".",
85+
"id": "Terraform's internal data source. ID. It is structured as \"`project_id`,`region`,`instance_id`\".",
8386
"instance_id": "ID of the SQLServer Flex instance.",
8487
"project_id": "STACKIT project ID to which the instance is associated.",
8588
"name": "Instance name.",
8689
"acl": "The Access Control List (ACL) for the SQLServer Flex instance.",
8790
"backup_schedule": `The backup schedule. Should follow the cron scheduling system format (e.g. "0 0 * * *").`,
8891
"options": "Custom parameters for the SQLServer Flex instance.",
92+
"region": "The resource region. If not defined, the provider region is used.",
8993
}
9094

9195
resp.Schema = schema.Schema{
@@ -170,6 +174,11 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques
170174
},
171175
},
172176
},
177+
"region": schema.StringAttribute{
178+
// the region cannot be found, so it has to be passed
179+
Optional: true,
180+
Description: descriptions["region"],
181+
},
173182
},
174183
}
175184
}
@@ -185,9 +194,16 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
185194

186195
projectId := model.ProjectId.ValueString()
187196
instanceId := model.InstanceId.ValueString()
197+
var region string
198+
if utils.IsUndefined(model.Region) {
199+
region = r.providerData.Region
200+
} else {
201+
region = model.Region.ValueString()
202+
}
188203
ctx = tflog.SetField(ctx, "project_id", projectId)
189204
ctx = tflog.SetField(ctx, "instance_id", instanceId)
190-
instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).Execute()
205+
ctx = tflog.SetField(ctx, "region", region)
206+
instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId, region).Execute()
191207
if err != nil {
192208
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
193209
if ok && oapiErr.StatusCode == http.StatusNotFound {
@@ -222,7 +238,7 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
222238
}
223239
}
224240

225-
err = mapFields(ctx, instanceResp, &model, flavor, storage, options)
241+
err = mapFields(ctx, instanceResp, &model, flavor, storage, options, region)
226242
if err != nil {
227243
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Processing API payload: %v", err))
228244
return

0 commit comments

Comments
 (0)