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
This commit is contained in:
Marcel Jacek 2025-03-11 08:06:46 +01:00 committed by GitHub
parent 4cfdbc508f
commit 81f876adea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 360 additions and 105 deletions

View file

@ -27,12 +27,16 @@ data "stackit_sqlserverflex_instance" "example" {
- `instance_id` (String) ID of the SQLServer Flex instance. - `instance_id` (String) ID of the SQLServer Flex instance.
- `project_id` (String) STACKIT project ID to which the instance is associated. - `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 ### Read-Only
- `acl` (List of String) The Access Control List (ACL) for the SQLServer Flex instance. - `acl` (List of String) The Access Control List (ACL) for the SQLServer Flex instance.
- `backup_schedule` (String) The backup schedule. Should follow the cron scheduling system format (e.g. "0 0 * * *"). - `backup_schedule` (String) The backup schedule. Should follow the cron scheduling system format (e.g. "0 0 * * *").
- `flavor` (Attributes) (see [below for nested schema](#nestedatt--flavor)) - `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. - `name` (String) Instance name.
- `options` (Attributes) Custom parameters for the SQLServer Flex instance. (see [below for nested schema](#nestedatt--options)) - `options` (Attributes) Custom parameters for the SQLServer Flex instance. (see [below for nested schema](#nestedatt--options))
- `replicas` (Number) - `replicas` (Number)

View file

@ -29,10 +29,14 @@ data "stackit_sqlserverflex_user" "example" {
- `project_id` (String) STACKIT project ID to which the instance is associated. - `project_id` (String) STACKIT project ID to which the instance is associated.
- `user_id` (String) User ID. - `user_id` (String) User ID.
### Optional
- `region` (String) The resource region. If not defined, the provider region is used.
### Read-Only ### Read-Only
- `host` (String) - `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) - `port` (Number)
- `roles` (Set of String) Database access levels for the user. - `roles` (Set of String) Database access levels for the user.
- `username` (String) Username of the SQLServer Flex instance. - `username` (String) Username of the SQLServer Flex instance.

View file

@ -45,12 +45,13 @@ resource "stackit_sqlserverflex_instance" "example" {
- `acl` (List of String) The Access Control List (ACL) for the SQLServer Flex instance. - `acl` (List of String) The Access Control List (ACL) for the SQLServer Flex instance.
- `backup_schedule` (String) The backup schedule. Should follow the cron scheduling system format (e.g. "0 0 * * *") - `backup_schedule` (String) The backup schedule. Should follow the cron scheduling system format (e.g. "0 0 * * *")
- `options` (Attributes) (see [below for nested schema](#nestedatt--options)) - `options` (Attributes) (see [below for nested schema](#nestedatt--options))
- `region` (String) The resource region. If not defined, the provider region is used.
- `storage` (Attributes) (see [below for nested schema](#nestedatt--storage)) - `storage` (Attributes) (see [below for nested schema](#nestedatt--storage))
- `version` (String) - `version` (String)
### Read-Only ### 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 SQLServer Flex instance. - `instance_id` (String) ID of the SQLServer Flex instance.
- `replicas` (Number) - `replicas` (Number)

View file

@ -32,12 +32,13 @@ resource "stackit_sqlserverflex_user" "example" {
### Optional ### Optional
- `region` (String)
- `roles` (Set of String) Database access levels for the user. - `roles` (Set of String) Database access levels for the user.
### Read-Only ### Read-Only
- `host` (String) - `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) Password of the user account. - `password` (String, Sensitive) Password of the user account.
- `port` (Number) - `port` (Number)
- `user_id` (String) User ID. - `user_id` (String) User ID.

2
go.mod
View file

@ -31,7 +31,7 @@ require (
github.com/stackitcloud/stackit-sdk-go/services/serverupdate v0.5.0 github.com/stackitcloud/stackit-sdk-go/services/serverupdate v0.5.0
github.com/stackitcloud/stackit-sdk-go/services/serviceenablement v0.5.0 github.com/stackitcloud/stackit-sdk-go/services/serviceenablement v0.5.0
github.com/stackitcloud/stackit-sdk-go/services/ske v0.22.0 github.com/stackitcloud/stackit-sdk-go/services/ske v0.22.0
github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v0.10.0 github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex v1.0.0
github.com/teambition/rrule-go v1.8.2 github.com/teambition/rrule-go v1.8.2
golang.org/x/mod v0.23.0 golang.org/x/mod v0.23.0
) )

2
go.sum
View file

@ -195,6 +195,8 @@ github.com/stackitcloud/stackit-sdk-go/services/ske v0.22.0 h1:3KUVls8zXsbT2tOYR
github.com/stackitcloud/stackit-sdk-go/services/ske v0.22.0/go.mod h1:63IvXpBJTIVONAnGPSDo0sRJ+6n6tzO918OLqfYBxto= 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 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 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= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=

View file

@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types/basetypes" "github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" "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/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
@ -32,6 +33,7 @@ func NewInstanceDataSource() datasource.DataSource {
// instanceDataSource is the data source implementation. // instanceDataSource is the data source implementation.
type instanceDataSource struct { type instanceDataSource struct {
client *sqlserverflex.APIClient client *sqlserverflex.APIClient
providerData core.ProviderData
} }
// Metadata returns the data source type name. // Metadata returns the data source type name.
@ -46,7 +48,8 @@ func (r *instanceDataSource) Configure(ctx context.Context, req datasource.Confi
return return
} }
providerData, ok := req.ProviderData.(core.ProviderData) var ok bool
r.providerData, ok = req.ProviderData.(core.ProviderData)
if !ok { if !ok {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData))
return return
@ -54,15 +57,15 @@ func (r *instanceDataSource) Configure(ctx context.Context, req datasource.Confi
var apiClient *sqlserverflex.APIClient var apiClient *sqlserverflex.APIClient
var err error var err error
if providerData.SQLServerFlexCustomEndpoint != "" { if r.providerData.SQLServerFlexCustomEndpoint != "" {
apiClient, err = sqlserverflex.NewAPIClient( apiClient, err = sqlserverflex.NewAPIClient(
config.WithCustomAuth(providerData.RoundTripper), config.WithCustomAuth(r.providerData.RoundTripper),
config.WithEndpoint(providerData.SQLServerFlexCustomEndpoint), config.WithEndpoint(r.providerData.SQLServerFlexCustomEndpoint),
) )
} else { } else {
apiClient, err = sqlserverflex.NewAPIClient( apiClient, err = sqlserverflex.NewAPIClient(
config.WithCustomAuth(providerData.RoundTripper), config.WithCustomAuth(r.providerData.RoundTripper),
config.WithRegion(providerData.Region), config.WithRegion(r.providerData.Region),
) )
} }
@ -79,13 +82,14 @@ func (r *instanceDataSource) Configure(ctx context.Context, req datasource.Confi
func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
descriptions := map[string]string{ descriptions := map[string]string{
"main": "SQLServer Flex instance data source schema. Must have a `region` specified in the provider configuration.", "main": "SQLServer 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 SQLServer Flex instance.", "instance_id": "ID of the SQLServer Flex instance.",
"project_id": "STACKIT project ID to which the instance is associated.", "project_id": "STACKIT project ID to which the instance is associated.",
"name": "Instance name.", "name": "Instance name.",
"acl": "The Access Control List (ACL) for the SQLServer Flex instance.", "acl": "The Access Control List (ACL) for the SQLServer Flex instance.",
"backup_schedule": `The backup schedule. Should follow the cron scheduling system format (e.g. "0 0 * * *").`, "backup_schedule": `The backup schedule. Should follow the cron scheduling system format (e.g. "0 0 * * *").`,
"options": "Custom parameters for the SQLServer Flex instance.", "options": "Custom parameters for the SQLServer Flex instance.",
"region": "The resource region. If not defined, the provider region is used.",
} }
resp.Schema = schema.Schema{ resp.Schema = schema.Schema{
@ -170,6 +174,11 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques
}, },
}, },
}, },
"region": schema.StringAttribute{
// the region cannot be found, so it has to be passed
Optional: true,
Description: descriptions["region"],
},
}, },
} }
} }
@ -185,9 +194,16 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
var region string
if utils.IsUndefined(model.Region) {
region = r.providerData.Region
} else {
region = model.Region.ValueString()
}
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) 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, instanceId, region).Execute()
if err != nil { 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 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 { if ok && oapiErr.StatusCode == http.StatusNotFound {
@ -222,7 +238,7 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
} }
} }
err = mapFields(ctx, instanceResp, &model, flavor, storage, options) err = mapFields(ctx, instanceResp, &model, flavor, storage, options, region)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Processing API payload: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Processing API payload: %v", err))
return return

View file

@ -41,6 +41,7 @@ var (
_ resource.Resource = &instanceResource{} _ resource.Resource = &instanceResource{}
_ resource.ResourceWithConfigure = &instanceResource{} _ resource.ResourceWithConfigure = &instanceResource{}
_ resource.ResourceWithImportState = &instanceResource{} _ resource.ResourceWithImportState = &instanceResource{}
_ resource.ResourceWithModifyPlan = &instanceResource{}
) )
type Model struct { type Model struct {
@ -55,6 +56,7 @@ type Model struct {
Version types.String `tfsdk:"version"` Version types.String `tfsdk:"version"`
Replicas types.Int64 `tfsdk:"replicas"` Replicas types.Int64 `tfsdk:"replicas"`
Options types.Object `tfsdk:"options"` Options types.Object `tfsdk:"options"`
Region types.String `tfsdk:"region"`
} }
// Struct corresponding to Model.Flavor // Struct corresponding to Model.Flavor
@ -105,6 +107,7 @@ func NewInstanceResource() resource.Resource {
// instanceResource is the resource implementation. // instanceResource is the resource implementation.
type instanceResource struct { type instanceResource struct {
client *sqlserverflex.APIClient client *sqlserverflex.APIClient
providerData core.ProviderData
} }
// Metadata returns the resource type name. // Metadata returns the resource type name.
@ -119,7 +122,8 @@ func (r *instanceResource) Configure(ctx context.Context, req resource.Configure
return return
} }
providerData, ok := req.ProviderData.(core.ProviderData) var ok bool
r.providerData, ok = req.ProviderData.(core.ProviderData)
if !ok { if !ok {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData))
return return
@ -127,15 +131,15 @@ func (r *instanceResource) Configure(ctx context.Context, req resource.Configure
var apiClient *sqlserverflex.APIClient var apiClient *sqlserverflex.APIClient
var err error var err error
if providerData.SQLServerFlexCustomEndpoint != "" { if r.providerData.SQLServerFlexCustomEndpoint != "" {
apiClient, err = sqlserverflex.NewAPIClient( apiClient, err = sqlserverflex.NewAPIClient(
config.WithCustomAuth(providerData.RoundTripper), config.WithCustomAuth(r.providerData.RoundTripper),
config.WithEndpoint(providerData.SQLServerFlexCustomEndpoint), config.WithEndpoint(r.providerData.SQLServerFlexCustomEndpoint),
) )
} else { } else {
apiClient, err = sqlserverflex.NewAPIClient( apiClient, err = sqlserverflex.NewAPIClient(
config.WithCustomAuth(providerData.RoundTripper), config.WithCustomAuth(r.providerData.RoundTripper),
config.WithRegion(providerData.Region), config.WithRegion(r.providerData.Region),
) )
} }
@ -148,17 +152,48 @@ func (r *instanceResource) Configure(ctx context.Context, req resource.Configure
tflog.Info(ctx, "SQLServer Flex instance client configured") tflog.Info(ctx, "SQLServer Flex instance client configured")
} }
// 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.Region, resp)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...)
if resp.Diagnostics.HasError() {
return
}
}
// Schema defines the schema for the resource. // Schema defines the schema for the resource.
func (r *instanceResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { func (r *instanceResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
descriptions := map[string]string{ descriptions := map[string]string{
"main": "SQLServer Flex instance resource schema. Must have a `region` specified in the provider configuration.", "main": "SQLServer 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 SQLServer Flex instance.", "instance_id": "ID of the SQLServer Flex instance.",
"project_id": "STACKIT project ID to which the instance is associated.", "project_id": "STACKIT project ID to which the instance is associated.",
"name": "Instance name.", "name": "Instance name.",
"acl": "The Access Control List (ACL) for the SQLServer Flex instance.", "acl": "The Access Control List (ACL) for the SQLServer Flex instance.",
"backup_schedule": `The backup schedule. Should follow the cron scheduling system format (e.g. "0 0 * * *")`, "backup_schedule": `The backup schedule. Should follow the cron scheduling system format (e.g. "0 0 * * *")`,
"options": "Custom parameters for the SQLServer Flex instance.", "options": "Custom parameters for the SQLServer Flex instance.",
"region": "The resource region. If not defined, the provider region is used.",
} }
resp.Schema = schema.Schema{ resp.Schema = schema.Schema{
@ -308,6 +343,15 @@ func (r *instanceResource) Schema(_ context.Context, _ resource.SchemaRequest, r
}, },
}, },
}, },
"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(),
},
},
}, },
} }
} }
@ -322,7 +366,9 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
return return
} }
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
region := model.Region.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "region", region)
var acl []string var acl []string
if !(model.ACL.IsNull() || model.ACL.IsUnknown()) { if !(model.ACL.IsNull() || model.ACL.IsUnknown()) {
@ -370,7 +416,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
return return
} }
// Create new instance // 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 { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Calling API: %v", err))
return return
@ -379,14 +425,14 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
// The creation waiter sometimes returns an error from the API: "instance with id xxx has unexpected status Failure" // The creation waiter sometimes returns an error from the API: "instance with id xxx has unexpected status Failure"
// which can be avoided by sleeping before wait // which can be avoided by sleeping before wait
waitResp, err := wait.CreateInstanceWaitHandler(ctx, r.client, projectId, instanceId).SetSleepBeforeWait(30 * time.Second).WaitWithContext(ctx) waitResp, err := wait.CreateInstanceWaitHandler(ctx, r.client, projectId, instanceId, region).SetSleepBeforeWait(30 * time.Second).WaitWithContext(ctx)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Instance creation waiting: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Instance creation waiting: %v", err))
return return
} }
// Map response body to schema // Map response body to schema
err = mapFields(ctx, waitResp, &model, flavor, storage, options) err = mapFields(ctx, waitResp, &model, flavor, storage, options, region)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Processing API payload: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Processing API payload: %v", err))
return return
@ -415,8 +461,14 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
} }
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
region := model.Region.ValueString()
if region == "" {
region = r.providerData.Region
}
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "region", region)
var flavor = &flavorModel{} var flavor = &flavorModel{}
if !(model.Flavor.IsNull() || model.Flavor.IsUnknown()) { if !(model.Flavor.IsNull() || model.Flavor.IsUnknown()) {
@ -444,7 +496,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, instanceId, region).Execute()
if err != nil { 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 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 { if ok && oapiErr.StatusCode == http.StatusNotFound {
@ -456,7 +508,7 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
} }
// Map response body to schema // Map response body to schema
err = mapFields(ctx, instanceResp, &model, flavor, storage, options) err = mapFields(ctx, instanceResp, &model, flavor, storage, options, region)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Processing API payload: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Processing API payload: %v", err))
return return
@ -481,8 +533,11 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
} }
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
region := model.Region.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "region", region)
var acl []string var acl []string
if !(model.ACL.IsNull() || model.ACL.IsUnknown()) { if !(model.ACL.IsNull() || model.ACL.IsUnknown()) {
@ -530,19 +585,19 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
return return
} }
// Update existing instance // Update existing instance
_, err = r.client.PartialUpdateInstance(ctx, projectId, instanceId).PartialUpdateInstancePayload(*payload).Execute() _, err = r.client.PartialUpdateInstance(ctx, projectId, instanceId, region).PartialUpdateInstancePayload(*payload).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", err.Error()) core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", err.Error())
return return
} }
waitResp, err := wait.UpdateInstanceWaitHandler(ctx, r.client, projectId, instanceId).WaitWithContext(ctx) waitResp, err := wait.UpdateInstanceWaitHandler(ctx, r.client, projectId, instanceId, region).WaitWithContext(ctx)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Instance update waiting: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Instance update waiting: %v", err))
return return
} }
// Map response body to schema // Map response body to schema
err = mapFields(ctx, waitResp, &model, flavor, storage, options) err = mapFields(ctx, waitResp, &model, flavor, storage, options, region)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Processing API payload: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Processing API payload: %v", err))
return return
@ -566,16 +621,18 @@ func (r *instanceResource) Delete(ctx context.Context, req resource.DeleteReques
} }
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
region := model.Region.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "region", region)
// Delete existing instance // Delete existing instance
err := r.client.DeleteInstance(ctx, projectId, instanceId).Execute() err := r.client.DeleteInstance(ctx, projectId, instanceId, region).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting instance", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting instance", fmt.Sprintf("Calling API: %v", err))
return return
} }
_, err = wait.DeleteInstanceWaitHandler(ctx, r.client, projectId, instanceId).WaitWithContext(ctx) _, err = wait.DeleteInstanceWaitHandler(ctx, r.client, projectId, instanceId, region).WaitWithContext(ctx)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting instance", fmt.Sprintf("Instance deletion waiting: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting instance", fmt.Sprintf("Instance deletion waiting: %v", err))
return return
@ -588,20 +645,21 @@ func (r *instanceResource) Delete(ctx context.Context, req resource.DeleteReques
func (r *instanceResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { func (r *instanceResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
idParts := strings.Split(req.ID, core.Separator) 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, core.LogAndAddError(ctx, &resp.Diagnostics,
"Error importing instance", "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 return
} }
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...) 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, "SQLServer Flex instance state imported") tflog.Info(ctx, "SQLServer Flex instance state imported")
} }
func mapFields(ctx context.Context, resp *sqlserverflex.GetInstanceResponse, model *Model, flavor *flavorModel, storage *storageModel, options *optionsModel) error { func mapFields(ctx context.Context, resp *sqlserverflex.GetInstanceResponse, model *Model, flavor *flavorModel, storage *storageModel, options *optionsModel, region string) error {
if resp == nil { if resp == nil {
return fmt.Errorf("response input is nil") return fmt.Errorf("response input is nil")
} }
@ -721,6 +779,7 @@ func mapFields(ctx context.Context, resp *sqlserverflex.GetInstanceResponse, mod
idParts := []string{ idParts := []string{
model.ProjectId.ValueString(), model.ProjectId.ValueString(),
region,
instanceId, instanceId,
} }
model.Id = types.StringValue( model.Id = types.StringValue(
@ -734,6 +793,7 @@ func mapFields(ctx context.Context, resp *sqlserverflex.GetInstanceResponse, mod
model.Storage = storageObject model.Storage = storageObject
model.Version = types.StringPointerValue(instance.Version) model.Version = types.StringPointerValue(instance.Version)
model.Options = optionsObject model.Options = optionsObject
model.Region = types.StringValue(region)
return nil return nil
} }
@ -797,7 +857,7 @@ func toUpdatePayload(model *Model, acl []string, flavor *flavorModel) (*sqlserve
} }
type sqlserverflexClient interface { type sqlserverflexClient interface {
ListFlavorsExecute(ctx context.Context, projectId string) (*sqlserverflex.ListFlavorsResponse, error) ListFlavorsExecute(ctx context.Context, projectId, region string) (*sqlserverflex.ListFlavorsResponse, error)
} }
func loadFlavorId(ctx context.Context, client sqlserverflexClient, model *Model, flavor *flavorModel) error { func loadFlavorId(ctx context.Context, client sqlserverflexClient, model *Model, flavor *flavorModel) error {
@ -817,7 +877,8 @@ func loadFlavorId(ctx context.Context, client sqlserverflexClient, model *Model,
} }
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
res, err := client.ListFlavorsExecute(ctx, projectId) region := model.Region.ValueString()
res, err := client.ListFlavorsExecute(ctx, projectId, region)
if err != nil { if err != nil {
return fmt.Errorf("listing sqlserverflex flavors: %w", err) return fmt.Errorf("listing sqlserverflex flavors: %w", err)
} }

View file

@ -17,7 +17,7 @@ type sqlserverflexClientMocked struct {
listFlavorsResp *sqlserverflex.ListFlavorsResponse listFlavorsResp *sqlserverflex.ListFlavorsResponse
} }
func (c *sqlserverflexClientMocked) ListFlavorsExecute(_ context.Context, _ string) (*sqlserverflex.ListFlavorsResponse, error) { func (c *sqlserverflexClientMocked) ListFlavorsExecute(_ context.Context, _, _ string) (*sqlserverflex.ListFlavorsResponse, error) {
if c.returnError { if c.returnError {
return nil, fmt.Errorf("get flavors failed") return nil, fmt.Errorf("get flavors failed")
} }
@ -26,6 +26,7 @@ func (c *sqlserverflexClientMocked) ListFlavorsExecute(_ context.Context, _ stri
} }
func TestMapFields(t *testing.T) { func TestMapFields(t *testing.T) {
const testRegion = "region"
tests := []struct { tests := []struct {
description string description string
state Model state Model
@ -33,6 +34,7 @@ func TestMapFields(t *testing.T) {
flavor *flavorModel flavor *flavorModel
storage *storageModel storage *storageModel
options *optionsModel options *optionsModel
region string
expected Model expected Model
isValid bool isValid bool
}{ }{
@ -48,8 +50,9 @@ func TestMapFields(t *testing.T) {
&flavorModel{}, &flavorModel{},
&storageModel{}, &storageModel{},
&optionsModel{}, &optionsModel{},
testRegion,
Model{ Model{
Id: types.StringValue("pid,iid"), Id: types.StringValue("pid,region,iid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Name: types.StringNull(), Name: types.StringNull(),
@ -71,6 +74,7 @@ func TestMapFields(t *testing.T) {
"retention_days": types.Int64Null(), "retention_days": types.Int64Null(),
}), }),
Version: types.StringNull(), Version: types.StringNull(),
Region: types.StringValue(testRegion),
}, },
true, true,
}, },
@ -114,8 +118,9 @@ func TestMapFields(t *testing.T) {
&flavorModel{}, &flavorModel{},
&storageModel{}, &storageModel{},
&optionsModel{}, &optionsModel{},
testRegion,
Model{ Model{
Id: types.StringValue("pid,iid"), Id: types.StringValue("pid,region,iid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Name: types.StringValue("name"), Name: types.StringValue("name"),
@ -141,6 +146,7 @@ func TestMapFields(t *testing.T) {
"retention_days": types.Int64Value(1), "retention_days": types.Int64Value(1),
}), }),
Version: types.StringValue("version"), Version: types.StringValue("version"),
Region: types.StringValue(testRegion),
}, },
true, true,
}, },
@ -185,8 +191,9 @@ func TestMapFields(t *testing.T) {
Edition: types.StringValue("edition"), Edition: types.StringValue("edition"),
RetentionDays: types.Int64Value(1), RetentionDays: types.Int64Value(1),
}, },
testRegion,
Model{ Model{
Id: types.StringValue("pid,iid"), Id: types.StringValue("pid,region,iid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Name: types.StringValue("name"), Name: types.StringValue("name"),
@ -212,6 +219,7 @@ func TestMapFields(t *testing.T) {
"retention_days": types.Int64Value(1), "retention_days": types.Int64Value(1),
}), }),
Version: types.StringValue("version"), Version: types.StringValue("version"),
Region: types.StringValue(testRegion),
}, },
true, true,
}, },
@ -258,8 +266,9 @@ func TestMapFields(t *testing.T) {
Size: types.Int64Value(78), Size: types.Int64Value(78),
}, },
&optionsModel{}, &optionsModel{},
testRegion,
Model{ Model{
Id: types.StringValue("pid,iid"), Id: types.StringValue("pid,region,iid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
Name: types.StringValue("name"), Name: types.StringValue("name"),
@ -285,6 +294,7 @@ func TestMapFields(t *testing.T) {
"retention_days": types.Int64Value(1), "retention_days": types.Int64Value(1),
}), }),
Version: types.StringValue("version"), Version: types.StringValue("version"),
Region: types.StringValue(testRegion),
}, },
true, true,
}, },
@ -298,6 +308,7 @@ func TestMapFields(t *testing.T) {
&flavorModel{}, &flavorModel{},
&storageModel{}, &storageModel{},
&optionsModel{}, &optionsModel{},
testRegion,
Model{}, Model{},
false, false,
}, },
@ -311,13 +322,14 @@ func TestMapFields(t *testing.T) {
&flavorModel{}, &flavorModel{},
&storageModel{}, &storageModel{},
&optionsModel{}, &optionsModel{},
testRegion,
Model{}, Model{},
false, false,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) { t.Run(tt.description, func(t *testing.T) {
err := mapFields(context.Background(), tt.input, &tt.state, tt.flavor, tt.storage, tt.options) err := mapFields(context.Background(), tt.input, &tt.state, tt.flavor, tt.storage, tt.options, tt.region)
if !tt.isValid && err == nil { if !tt.isValid && err == nil {
t.Fatalf("Should have failed") t.Fatalf("Should have failed")
} }

View file

@ -42,7 +42,11 @@ var userResource = map[string]string{
"project_id": instanceResource["project_id"], "project_id": instanceResource["project_id"],
} }
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(` return fmt.Sprintf(`
%s %s
@ -63,6 +67,7 @@ func configResources(backupSchedule string) string {
retention_days = %s retention_days = %s
} }
backup_schedule = "%s" backup_schedule = "%s"
%s
} }
resource "stackit_sqlserverflex_user" "user" { resource "stackit_sqlserverflex_user" "user" {
@ -70,6 +75,7 @@ func configResources(backupSchedule string) string {
instance_id = stackit_sqlserverflex_instance.instance.instance_id instance_id = stackit_sqlserverflex_instance.instance.instance_id
username = "%s" username = "%s"
roles = ["%s"] roles = ["%s"]
%s
} }
`, `,
testutil.SQLServerFlexProviderConfig(), testutil.SQLServerFlexProviderConfig(),
@ -83,19 +89,22 @@ func configResources(backupSchedule string) string {
instanceResource["version"], instanceResource["version"],
instanceResource["options_retention_days"], instanceResource["options_retention_days"],
backupSchedule, backupSchedule,
regionConfig,
userResource["username"], userResource["username"],
userResource["role"], userResource["role"],
regionConfig,
) )
} }
func TestAccSQLServerFlexResource(t *testing.T) { func TestAccSQLServerFlexResource(t *testing.T) {
testRegion := utils.Ptr("eu01")
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories, ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
CheckDestroy: testAccChecksqlserverflexDestroy, CheckDestroy: testAccChecksqlserverflexDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
// Creation // Creation
{ {
Config: configResources(instanceResource["backup_schedule"]), Config: configResources(instanceResource["backup_schedule"], testRegion),
Check: resource.ComposeAggregateTestCheckFunc( Check: resource.ComposeAggregateTestCheckFunc(
// Instance // Instance
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "project_id", instanceResource["project_id"]), resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "project_id", instanceResource["project_id"]),
@ -113,6 +122,41 @@ func TestAccSQLServerFlexResource(t *testing.T) {
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "version", instanceResource["version"]), resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "version", instanceResource["version"]),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "options.retention_days", instanceResource["options_retention_days"]), resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "options.retention_days", instanceResource["options_retention_days"]),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "backup_schedule", instanceResource["backup_schedule"]), resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "backup_schedule", instanceResource["backup_schedule"]),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "region", *testRegion),
// User
resource.TestCheckResourceAttrPair(
"stackit_sqlserverflex_user.user", "project_id",
"stackit_sqlserverflex_instance.instance", "project_id",
),
resource.TestCheckResourceAttrPair(
"stackit_sqlserverflex_user.user", "instance_id",
"stackit_sqlserverflex_instance.instance", "instance_id",
),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_user.user", "user_id"),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_user.user", "password"),
),
},
// Update
{
Config: configResources(instanceResource["backup_schedule"], nil),
Check: resource.ComposeAggregateTestCheckFunc(
// Instance
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "project_id", instanceResource["project_id"]),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_instance.instance", "instance_id"),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "name", instanceResource["name"]),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "acl.#", "1"),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "acl.0", instanceResource["acl"]),
resource.TestCheckResourceAttrSet("stackit_sqlserverflex_instance.instance", "flavor.id"),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "flavor.description", instanceResource["flavor_description"]),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "replicas", instanceResource["replicas"]),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "flavor.cpu", instanceResource["flavor_cpu"]),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "flavor.ram", instanceResource["flavor_ram"]),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "storage.class", instanceResource["storage_class"]),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "storage.size", instanceResource["storage_size"]),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "version", instanceResource["version"]),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "options.retention_days", instanceResource["options_retention_days"]),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "backup_schedule", instanceResource["backup_schedule"]),
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "region", testutil.Region),
// User // User
resource.TestCheckResourceAttrPair( resource.TestCheckResourceAttrPair(
"stackit_sqlserverflex_user.user", "project_id", "stackit_sqlserverflex_user.user", "project_id",
@ -142,7 +186,7 @@ func TestAccSQLServerFlexResource(t *testing.T) {
user_id = stackit_sqlserverflex_user.user.user_id user_id = stackit_sqlserverflex_user.user.user_id
} }
`, `,
configResources(instanceResource["backup_schedule"]), configResources(instanceResource["backup_schedule"], nil),
), ),
Check: resource.ComposeAggregateTestCheckFunc( Check: resource.ComposeAggregateTestCheckFunc(
// Instance data // Instance data
@ -194,7 +238,7 @@ func TestAccSQLServerFlexResource(t *testing.T) {
return "", fmt.Errorf("couldn't find attribute instance_id") 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, ImportState: true,
ImportStateVerify: true, ImportStateVerify: true,
@ -225,7 +269,7 @@ func TestAccSQLServerFlexResource(t *testing.T) {
return "", fmt.Errorf("couldn't find attribute user_id") 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, ImportState: true,
ImportStateVerify: true, ImportStateVerify: true,
@ -233,7 +277,7 @@ func TestAccSQLServerFlexResource(t *testing.T) {
}, },
// Update // Update
{ {
Config: configResources(instanceResource["backup_schedule_updated"]), Config: configResources(instanceResource["backup_schedule_updated"], nil),
Check: resource.ComposeAggregateTestCheckFunc( Check: resource.ComposeAggregateTestCheckFunc(
// Instance data // Instance data
resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "project_id", instanceResource["project_id"]), resource.TestCheckResourceAttr("stackit_sqlserverflex_instance.instance", "project_id", instanceResource["project_id"]),
@ -263,9 +307,7 @@ func testAccChecksqlserverflexDestroy(s *terraform.State) error {
var client *sqlserverflex.APIClient var client *sqlserverflex.APIClient
var err error var err error
if testutil.SQLServerFlexCustomEndpoint == "" { if testutil.SQLServerFlexCustomEndpoint == "" {
client, err = sqlserverflex.NewAPIClient( client, err = sqlserverflex.NewAPIClient()
config.WithRegion("eu01"),
)
} else { } else {
client, err = sqlserverflex.NewAPIClient( client, err = sqlserverflex.NewAPIClient(
config.WithEndpoint(testutil.SQLServerFlexCustomEndpoint), config.WithEndpoint(testutil.SQLServerFlexCustomEndpoint),
@ -280,12 +322,12 @@ func testAccChecksqlserverflexDestroy(s *terraform.State) error {
if rs.Type != "stackit_sqlserverflex_instance" { if rs.Type != "stackit_sqlserverflex_instance" {
continue 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] instanceId := strings.Split(rs.Primary.ID, core.Separator)[1]
instancesToDestroy = append(instancesToDestroy, instanceId) 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 { if err != nil {
return fmt.Errorf("getting instancesResp: %w", err) return fmt.Errorf("getting instancesResp: %w", err)
} }
@ -296,11 +338,11 @@ func testAccChecksqlserverflexDestroy(s *terraform.State) error {
continue continue
} }
if utils.Contains(instancesToDestroy, *items[i].Id) { if utils.Contains(instancesToDestroy, *items[i].Id) {
err := client.DeleteInstanceExecute(ctx, testutil.ProjectId, *items[i].Id) err := client.DeleteInstanceExecute(ctx, testutil.ProjectId, *items[i].Id, testutil.Region)
if err != nil { if err != nil {
return fmt.Errorf("destroying instance %s during CheckDestroy: %w", *items[i].Id, err) return fmt.Errorf("destroying 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, *items[i].Id, testutil.Region).WaitWithContext(ctx)
if err != nil { if err != nil {
return fmt.Errorf("destroying instance %s during CheckDestroy: waiting for deletion %w", *items[i].Id, err) return fmt.Errorf("destroying instance %s during CheckDestroy: waiting for deletion %w", *items[i].Id, err)
} }

View file

@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" "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/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
@ -34,6 +35,7 @@ type DataSourceModel struct {
Roles types.Set `tfsdk:"roles"` Roles types.Set `tfsdk:"roles"`
Host types.String `tfsdk:"host"` Host types.String `tfsdk:"host"`
Port types.Int64 `tfsdk:"port"` Port types.Int64 `tfsdk:"port"`
Region types.String `tfsdk:"region"`
} }
// NewUserDataSource is a helper function to simplify the provider implementation. // NewUserDataSource is a helper function to simplify the provider implementation.
@ -44,6 +46,7 @@ func NewUserDataSource() datasource.DataSource {
// userDataSource is the data source implementation. // userDataSource is the data source implementation.
type userDataSource struct { type userDataSource struct {
client *sqlserverflex.APIClient client *sqlserverflex.APIClient
providerData core.ProviderData
} }
// Metadata returns the data source type name. // Metadata returns the data source type name.
@ -58,7 +61,8 @@ func (r *userDataSource) Configure(ctx context.Context, req datasource.Configure
return return
} }
providerData, ok := req.ProviderData.(core.ProviderData) var ok bool
r.providerData, ok = req.ProviderData.(core.ProviderData)
if !ok { if !ok {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData))
return return
@ -66,15 +70,15 @@ func (r *userDataSource) Configure(ctx context.Context, req datasource.Configure
var apiClient *sqlserverflex.APIClient var apiClient *sqlserverflex.APIClient
var err error var err error
if providerData.SQLServerFlexCustomEndpoint != "" { if r.providerData.SQLServerFlexCustomEndpoint != "" {
apiClient, err = sqlserverflex.NewAPIClient( apiClient, err = sqlserverflex.NewAPIClient(
config.WithCustomAuth(providerData.RoundTripper), config.WithCustomAuth(r.providerData.RoundTripper),
config.WithEndpoint(providerData.SQLServerFlexCustomEndpoint), config.WithEndpoint(r.providerData.SQLServerFlexCustomEndpoint),
) )
} else { } else {
apiClient, err = sqlserverflex.NewAPIClient( apiClient, err = sqlserverflex.NewAPIClient(
config.WithCustomAuth(providerData.RoundTripper), config.WithCustomAuth(r.providerData.RoundTripper),
config.WithRegion(providerData.Region), config.WithRegion(r.providerData.Region),
) )
} }
@ -91,13 +95,14 @@ func (r *userDataSource) Configure(ctx context.Context, req datasource.Configure
func (r *userDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { func (r *userDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
descriptions := map[string]string{ descriptions := map[string]string{
"main": "SQLServer Flex user data source schema. Must have a `region` specified in the provider configuration.", "main": "SQLServer 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.", "user_id": "User ID.",
"instance_id": "ID of the SQLServer Flex instance.", "instance_id": "ID of the SQLServer Flex instance.",
"project_id": "STACKIT project ID to which the instance is associated.", "project_id": "STACKIT project ID to which the instance is associated.",
"username": "Username of the SQLServer Flex instance.", "username": "Username of the SQLServer Flex instance.",
"roles": "Database access levels for the user.", "roles": "Database access levels for the user.",
"password": "Password of the user account.", "password": "Password of the user account.",
"region": "The resource region. If not defined, the provider region is used.",
} }
resp.Schema = schema.Schema{ resp.Schema = schema.Schema{
@ -145,6 +150,11 @@ func (r *userDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, r
"port": schema.Int64Attribute{ "port": schema.Int64Attribute{
Computed: true, Computed: true,
}, },
"region": schema.StringAttribute{
// the region cannot be found automatically, so it has to be passed
Optional: true,
Description: descriptions["region"],
},
}, },
} }
} }
@ -160,11 +170,18 @@ func (r *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
userId := model.UserId.ValueString() userId := model.UserId.ValueString()
var region string
if utils.IsUndefined(model.Region) {
region = r.providerData.Region
} else {
region = model.Region.ValueString()
}
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "user_id", userId) 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, instanceId, userId, region).Execute()
if err != nil { 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 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 { if ok && oapiErr.StatusCode == http.StatusNotFound {
@ -175,7 +192,7 @@ func (r *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
} }
// Map response body to schema and populate Computed attribute values // Map response body to schema and populate Computed attribute values
err = mapDataSourceFields(recordSetResp, &model) err = mapDataSourceFields(recordSetResp, &model, region)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading user", fmt.Sprintf("Processing API payload: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading user", fmt.Sprintf("Processing API payload: %v", err))
return return
@ -190,7 +207,7 @@ func (r *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
tflog.Info(ctx, "SQLServer Flex user read") tflog.Info(ctx, "SQLServer Flex user read")
} }
func mapDataSourceFields(userResp *sqlserverflex.GetUserResponse, model *DataSourceModel) error { func mapDataSourceFields(userResp *sqlserverflex.GetUserResponse, model *DataSourceModel, region string) error {
if userResp == nil || userResp.Item == nil { if userResp == nil || userResp.Item == nil {
return fmt.Errorf("response is nil") return fmt.Errorf("response is nil")
} }
@ -209,6 +226,7 @@ func mapDataSourceFields(userResp *sqlserverflex.GetUserResponse, model *DataSou
} }
idParts := []string{ idParts := []string{
model.ProjectId.ValueString(), model.ProjectId.ValueString(),
region,
model.InstanceId.ValueString(), model.InstanceId.ValueString(),
userId, userId,
} }
@ -233,5 +251,6 @@ func mapDataSourceFields(userResp *sqlserverflex.GetUserResponse, model *DataSou
} }
model.Host = types.StringPointerValue(user.Host) model.Host = types.StringPointerValue(user.Host)
model.Port = types.Int64PointerValue(user.Port) model.Port = types.Int64PointerValue(user.Port)
model.Region = types.StringValue(region)
return nil return nil
} }

View file

@ -11,9 +11,11 @@ import (
) )
func TestMapDataSourceFields(t *testing.T) { func TestMapDataSourceFields(t *testing.T) {
const testRegion = "region"
tests := []struct { tests := []struct {
description string description string
input *sqlserverflex.GetUserResponse input *sqlserverflex.GetUserResponse
region string
expected DataSourceModel expected DataSourceModel
isValid bool isValid bool
}{ }{
@ -22,8 +24,9 @@ func TestMapDataSourceFields(t *testing.T) {
&sqlserverflex.GetUserResponse{ &sqlserverflex.GetUserResponse{
Item: &sqlserverflex.UserResponseUser{}, Item: &sqlserverflex.UserResponseUser{},
}, },
testRegion,
DataSourceModel{ DataSourceModel{
Id: types.StringValue("pid,iid,uid"), Id: types.StringValue("pid,region,iid,uid"),
UserId: types.StringValue("uid"), UserId: types.StringValue("uid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
@ -31,6 +34,7 @@ func TestMapDataSourceFields(t *testing.T) {
Roles: types.SetNull(types.StringType), Roles: types.SetNull(types.StringType),
Host: types.StringNull(), Host: types.StringNull(),
Port: types.Int64Null(), Port: types.Int64Null(),
Region: types.StringValue(testRegion),
}, },
true, true,
}, },
@ -48,8 +52,9 @@ func TestMapDataSourceFields(t *testing.T) {
Port: utils.Ptr(int64(1234)), Port: utils.Ptr(int64(1234)),
}, },
}, },
testRegion,
DataSourceModel{ DataSourceModel{
Id: types.StringValue("pid,iid,uid"), Id: types.StringValue("pid,region,iid,uid"),
UserId: types.StringValue("uid"), UserId: types.StringValue("uid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
@ -61,6 +66,7 @@ func TestMapDataSourceFields(t *testing.T) {
}), }),
Host: types.StringValue("host"), Host: types.StringValue("host"),
Port: types.Int64Value(1234), Port: types.Int64Value(1234),
Region: types.StringValue(testRegion),
}, },
true, true,
}, },
@ -75,8 +81,9 @@ func TestMapDataSourceFields(t *testing.T) {
Port: utils.Ptr(int64(2123456789)), Port: utils.Ptr(int64(2123456789)),
}, },
}, },
testRegion,
DataSourceModel{ DataSourceModel{
Id: types.StringValue("pid,iid,uid"), Id: types.StringValue("pid,region,iid,uid"),
UserId: types.StringValue("uid"), UserId: types.StringValue("uid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
@ -84,18 +91,21 @@ func TestMapDataSourceFields(t *testing.T) {
Roles: types.SetValueMust(types.StringType, []attr.Value{}), Roles: types.SetValueMust(types.StringType, []attr.Value{}),
Host: types.StringNull(), Host: types.StringNull(),
Port: types.Int64Value(2123456789), Port: types.Int64Value(2123456789),
Region: types.StringValue(testRegion),
}, },
true, true,
}, },
{ {
"nil_response", "nil_response",
nil, nil,
testRegion,
DataSourceModel{}, DataSourceModel{},
false, false,
}, },
{ {
"nil_response_2", "nil_response_2",
&sqlserverflex.GetUserResponse{}, &sqlserverflex.GetUserResponse{},
testRegion,
DataSourceModel{}, DataSourceModel{},
false, false,
}, },
@ -104,6 +114,7 @@ func TestMapDataSourceFields(t *testing.T) {
&sqlserverflex.GetUserResponse{ &sqlserverflex.GetUserResponse{
Item: &sqlserverflex.UserResponseUser{}, Item: &sqlserverflex.UserResponseUser{},
}, },
testRegion,
DataSourceModel{}, DataSourceModel{},
false, false,
}, },
@ -115,7 +126,7 @@ func TestMapDataSourceFields(t *testing.T) {
InstanceId: tt.expected.InstanceId, InstanceId: tt.expected.InstanceId,
UserId: tt.expected.UserId, UserId: tt.expected.UserId,
} }
err := mapDataSourceFields(tt.input, state) err := mapDataSourceFields(tt.input, state, tt.region)
if !tt.isValid && err == nil { if !tt.isValid && err == nil {
t.Fatalf("Should have failed") t.Fatalf("Should have failed")
} }

View file

@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" "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/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
"github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/attr"
@ -30,6 +31,7 @@ var (
_ resource.Resource = &userResource{} _ resource.Resource = &userResource{}
_ resource.ResourceWithConfigure = &userResource{} _ resource.ResourceWithConfigure = &userResource{}
_ resource.ResourceWithImportState = &userResource{} _ resource.ResourceWithImportState = &userResource{}
_ resource.ResourceWithModifyPlan = &userResource{}
) )
type Model struct { type Model struct {
@ -42,6 +44,7 @@ type Model struct {
Password types.String `tfsdk:"password"` Password types.String `tfsdk:"password"`
Host types.String `tfsdk:"host"` Host types.String `tfsdk:"host"`
Port types.Int64 `tfsdk:"port"` Port types.Int64 `tfsdk:"port"`
Region types.String `tfsdk:"region"`
} }
// NewUserResource is a helper function to simplify the provider implementation. // NewUserResource is a helper function to simplify the provider implementation.
@ -52,6 +55,7 @@ func NewUserResource() resource.Resource {
// userResource is the resource implementation. // userResource is the resource implementation.
type userResource struct { type userResource struct {
client *sqlserverflex.APIClient client *sqlserverflex.APIClient
providerData core.ProviderData
} }
// Metadata returns the resource type name. // Metadata returns the resource type name.
@ -66,7 +70,8 @@ func (r *userResource) Configure(ctx context.Context, req resource.ConfigureRequ
return return
} }
providerData, ok := req.ProviderData.(core.ProviderData) var ok bool
r.providerData, ok = req.ProviderData.(core.ProviderData)
if !ok { if !ok {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData))
return return
@ -74,15 +79,15 @@ func (r *userResource) Configure(ctx context.Context, req resource.ConfigureRequ
var apiClient *sqlserverflex.APIClient var apiClient *sqlserverflex.APIClient
var err error var err error
if providerData.SQLServerFlexCustomEndpoint != "" { if r.providerData.SQLServerFlexCustomEndpoint != "" {
apiClient, err = sqlserverflex.NewAPIClient( apiClient, err = sqlserverflex.NewAPIClient(
config.WithCustomAuth(providerData.RoundTripper), config.WithCustomAuth(r.providerData.RoundTripper),
config.WithEndpoint(providerData.SQLServerFlexCustomEndpoint), config.WithEndpoint(r.providerData.SQLServerFlexCustomEndpoint),
) )
} else { } else {
apiClient, err = sqlserverflex.NewAPIClient( apiClient, err = sqlserverflex.NewAPIClient(
config.WithCustomAuth(providerData.RoundTripper), config.WithCustomAuth(r.providerData.RoundTripper),
config.WithRegion(providerData.Region), config.WithRegion(r.providerData.Region),
) )
} }
@ -95,11 +100,41 @@ func (r *userResource) Configure(ctx context.Context, req resource.ConfigureRequ
tflog.Info(ctx, "SQLServer Flex user client configured") tflog.Info(ctx, "SQLServer Flex user client configured")
} }
// 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.Region, resp)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...)
if resp.Diagnostics.HasError() {
return
}
}
// Schema defines the schema for the resource. // Schema defines the schema for the resource.
func (r *userResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { func (r *userResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
descriptions := map[string]string{ descriptions := map[string]string{
"main": "SQLServer Flex user resource schema. Must have a `region` specified in the provider configuration.", "main": "SQLServer 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.", "user_id": "User ID.",
"instance_id": "ID of the SQLServer Flex instance.", "instance_id": "ID of the SQLServer Flex instance.",
"project_id": "STACKIT project ID to which the instance is associated.", "project_id": "STACKIT project ID to which the instance is associated.",
@ -179,6 +214,15 @@ func (r *userResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
"port": schema.Int64Attribute{ "port": schema.Int64Attribute{
Computed: true, Computed: 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(),
},
},
}, },
} }
} }
@ -193,8 +237,11 @@ func (r *userResource) Create(ctx context.Context, req resource.CreateRequest, r
} }
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
region := model.Region.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "region", region)
var roles []string var roles []string
if !(model.Roles.IsNull() || model.Roles.IsUnknown()) { if !(model.Roles.IsNull() || model.Roles.IsUnknown()) {
@ -212,7 +259,7 @@ func (r *userResource) Create(ctx context.Context, req resource.CreateRequest, r
return return
} }
// Create new user // Create new user
userResp, err := r.client.CreateUser(ctx, projectId, instanceId).CreateUserPayload(*payload).Execute() userResp, err := r.client.CreateUser(ctx, projectId, instanceId, region).CreateUserPayload(*payload).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating user", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating user", fmt.Sprintf("Calling API: %v", err))
return return
@ -225,7 +272,7 @@ func (r *userResource) Create(ctx context.Context, req resource.CreateRequest, r
ctx = tflog.SetField(ctx, "user_id", userId) ctx = tflog.SetField(ctx, "user_id", userId)
// Map response body to schema // Map response body to schema
err = mapFieldsCreate(userResp, &model) err = mapFieldsCreate(userResp, &model, region)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating user", fmt.Sprintf("Processing API payload: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating user", fmt.Sprintf("Processing API payload: %v", err))
return return
@ -250,11 +297,16 @@ func (r *userResource) Read(ctx context.Context, req resource.ReadRequest, resp
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
userId := model.UserId.ValueString() userId := model.UserId.ValueString()
region := model.Region.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "user_id", userId) ctx = tflog.SetField(ctx, "user_id", userId)
ctx = tflog.SetField(ctx, "region", region)
if region == "" {
region = r.providerData.Region
}
recordSetResp, err := r.client.GetUser(ctx, projectId, instanceId, userId).Execute() recordSetResp, err := r.client.GetUser(ctx, projectId, instanceId, userId, region).Execute()
if err != nil { 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 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 { if ok && oapiErr.StatusCode == http.StatusNotFound {
@ -266,7 +318,7 @@ func (r *userResource) Read(ctx context.Context, req resource.ReadRequest, resp
} }
// Map response body to schema // Map response body to schema
err = mapFields(recordSetResp, &model) err = mapFields(recordSetResp, &model, region)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading user", fmt.Sprintf("Processing API payload: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading user", fmt.Sprintf("Processing API payload: %v", err))
return return
@ -300,12 +352,14 @@ func (r *userResource) Delete(ctx context.Context, req resource.DeleteRequest, r
projectId := model.ProjectId.ValueString() projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString() instanceId := model.InstanceId.ValueString()
userId := model.UserId.ValueString() userId := model.UserId.ValueString()
region := model.Region.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "instance_id", instanceId)
ctx = tflog.SetField(ctx, "user_id", userId) ctx = tflog.SetField(ctx, "user_id", userId)
ctx = tflog.SetField(ctx, "region", region)
// Delete existing record set // Delete existing record set
err := r.client.DeleteUser(ctx, projectId, instanceId, userId).Execute() err := r.client.DeleteUser(ctx, projectId, instanceId, userId, region).Execute()
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting user", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting user", fmt.Sprintf("Calling API: %v", err))
return return
@ -317,17 +371,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 // 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) { func (r *userResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
idParts := strings.Split(req.ID, core.Separator) 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, core.LogAndAddError(ctx, &resp.Diagnostics,
"Error importing user", "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 return
} }
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...) 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("user_id"), idParts[2])...) 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, core.LogAndAddWarning(ctx, &resp.Diagnostics,
"SQLServer Flex user imported with empty password", "SQLServer Flex user imported with empty password",
"The user password is not imported as it is only available upon creation of a new user. The password field will be empty.", "The user password is not imported as it is only available upon creation of a new user. The password field will be empty.",
@ -335,7 +390,7 @@ func (r *userResource) ImportState(ctx context.Context, req resource.ImportState
tflog.Info(ctx, "SQLServer Flex user state imported") tflog.Info(ctx, "SQLServer Flex user state imported")
} }
func mapFieldsCreate(userResp *sqlserverflex.CreateUserResponse, model *Model) error { func mapFieldsCreate(userResp *sqlserverflex.CreateUserResponse, model *Model, region string) error {
if userResp == nil || userResp.Item == nil { if userResp == nil || userResp.Item == nil {
return fmt.Errorf("response is nil") return fmt.Errorf("response is nil")
} }
@ -350,6 +405,7 @@ func mapFieldsCreate(userResp *sqlserverflex.CreateUserResponse, model *Model) e
userId := *user.Id userId := *user.Id
idParts := []string{ idParts := []string{
model.ProjectId.ValueString(), model.ProjectId.ValueString(),
region,
model.InstanceId.ValueString(), model.InstanceId.ValueString(),
userId, userId,
} }
@ -382,10 +438,11 @@ func mapFieldsCreate(userResp *sqlserverflex.CreateUserResponse, model *Model) e
model.Host = types.StringPointerValue(user.Host) model.Host = types.StringPointerValue(user.Host)
model.Port = types.Int64PointerValue(user.Port) model.Port = types.Int64PointerValue(user.Port)
model.Region = types.StringValue(region)
return nil return nil
} }
func mapFields(userResp *sqlserverflex.GetUserResponse, model *Model) error { func mapFields(userResp *sqlserverflex.GetUserResponse, model *Model, region string) error {
if userResp == nil || userResp.Item == nil { if userResp == nil || userResp.Item == nil {
return fmt.Errorf("response is nil") return fmt.Errorf("response is nil")
} }
@ -404,6 +461,7 @@ func mapFields(userResp *sqlserverflex.GetUserResponse, model *Model) error {
} }
idParts := []string{ idParts := []string{
model.ProjectId.ValueString(), model.ProjectId.ValueString(),
region,
model.InstanceId.ValueString(), model.InstanceId.ValueString(),
userId, userId,
} }
@ -431,6 +489,7 @@ func mapFields(userResp *sqlserverflex.GetUserResponse, model *Model) error {
model.Host = types.StringPointerValue(user.Host) model.Host = types.StringPointerValue(user.Host)
model.Port = types.Int64PointerValue(user.Port) model.Port = types.Int64PointerValue(user.Port)
model.Region = types.StringValue(region)
return nil return nil
} }

View file

@ -11,9 +11,11 @@ import (
) )
func TestMapFieldsCreate(t *testing.T) { func TestMapFieldsCreate(t *testing.T) {
const testRegion = "region"
tests := []struct { tests := []struct {
description string description string
input *sqlserverflex.CreateUserResponse input *sqlserverflex.CreateUserResponse
region string
expected Model expected Model
isValid bool isValid bool
}{ }{
@ -25,8 +27,9 @@ func TestMapFieldsCreate(t *testing.T) {
Password: utils.Ptr(""), Password: utils.Ptr(""),
}, },
}, },
testRegion,
Model{ Model{
Id: types.StringValue("pid,iid,uid"), Id: types.StringValue("pid,region,iid,uid"),
UserId: types.StringValue("uid"), UserId: types.StringValue("uid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
@ -35,6 +38,7 @@ func TestMapFieldsCreate(t *testing.T) {
Password: types.StringValue(""), Password: types.StringValue(""),
Host: types.StringNull(), Host: types.StringNull(),
Port: types.Int64Null(), Port: types.Int64Null(),
Region: types.StringValue(testRegion),
}, },
true, true,
}, },
@ -54,8 +58,9 @@ func TestMapFieldsCreate(t *testing.T) {
Port: utils.Ptr(int64(1234)), Port: utils.Ptr(int64(1234)),
}, },
}, },
testRegion,
Model{ Model{
Id: types.StringValue("pid,iid,uid"), Id: types.StringValue("pid,region,iid,uid"),
UserId: types.StringValue("uid"), UserId: types.StringValue("uid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
@ -68,6 +73,7 @@ func TestMapFieldsCreate(t *testing.T) {
Password: types.StringValue("password"), Password: types.StringValue("password"),
Host: types.StringValue("host"), Host: types.StringValue("host"),
Port: types.Int64Value(1234), Port: types.Int64Value(1234),
Region: types.StringValue(testRegion),
}, },
true, true,
}, },
@ -83,8 +89,9 @@ func TestMapFieldsCreate(t *testing.T) {
Port: utils.Ptr(int64(2123456789)), Port: utils.Ptr(int64(2123456789)),
}, },
}, },
testRegion,
Model{ Model{
Id: types.StringValue("pid,iid,uid"), Id: types.StringValue("pid,region,iid,uid"),
UserId: types.StringValue("uid"), UserId: types.StringValue("uid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
@ -93,18 +100,21 @@ func TestMapFieldsCreate(t *testing.T) {
Password: types.StringValue(""), Password: types.StringValue(""),
Host: types.StringNull(), Host: types.StringNull(),
Port: types.Int64Value(2123456789), Port: types.Int64Value(2123456789),
Region: types.StringValue(testRegion),
}, },
true, true,
}, },
{ {
"nil_response", "nil_response",
nil, nil,
testRegion,
Model{}, Model{},
false, false,
}, },
{ {
"nil_response_2", "nil_response_2",
&sqlserverflex.CreateUserResponse{}, &sqlserverflex.CreateUserResponse{},
testRegion,
Model{}, Model{},
false, false,
}, },
@ -113,6 +123,7 @@ func TestMapFieldsCreate(t *testing.T) {
&sqlserverflex.CreateUserResponse{ &sqlserverflex.CreateUserResponse{
Item: &sqlserverflex.SingleUser{}, Item: &sqlserverflex.SingleUser{},
}, },
testRegion,
Model{}, Model{},
false, false,
}, },
@ -123,6 +134,7 @@ func TestMapFieldsCreate(t *testing.T) {
Id: utils.Ptr("uid"), Id: utils.Ptr("uid"),
}, },
}, },
testRegion,
Model{}, Model{},
false, false,
}, },
@ -133,7 +145,7 @@ func TestMapFieldsCreate(t *testing.T) {
ProjectId: tt.expected.ProjectId, ProjectId: tt.expected.ProjectId,
InstanceId: tt.expected.InstanceId, InstanceId: tt.expected.InstanceId,
} }
err := mapFieldsCreate(tt.input, state) err := mapFieldsCreate(tt.input, state, tt.region)
if !tt.isValid && err == nil { if !tt.isValid && err == nil {
t.Fatalf("Should have failed") t.Fatalf("Should have failed")
} }
@ -151,9 +163,11 @@ func TestMapFieldsCreate(t *testing.T) {
} }
func TestMapFields(t *testing.T) { func TestMapFields(t *testing.T) {
const testRegion = "region"
tests := []struct { tests := []struct {
description string description string
input *sqlserverflex.GetUserResponse input *sqlserverflex.GetUserResponse
region string
expected Model expected Model
isValid bool isValid bool
}{ }{
@ -162,8 +176,9 @@ func TestMapFields(t *testing.T) {
&sqlserverflex.GetUserResponse{ &sqlserverflex.GetUserResponse{
Item: &sqlserverflex.UserResponseUser{}, Item: &sqlserverflex.UserResponseUser{},
}, },
testRegion,
Model{ Model{
Id: types.StringValue("pid,iid,uid"), Id: types.StringValue("pid,region,iid,uid"),
UserId: types.StringValue("uid"), UserId: types.StringValue("uid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
@ -171,6 +186,7 @@ func TestMapFields(t *testing.T) {
Roles: types.SetNull(types.StringType), Roles: types.SetNull(types.StringType),
Host: types.StringNull(), Host: types.StringNull(),
Port: types.Int64Null(), Port: types.Int64Null(),
Region: types.StringValue(testRegion),
}, },
true, true,
}, },
@ -188,8 +204,9 @@ func TestMapFields(t *testing.T) {
Port: utils.Ptr(int64(1234)), Port: utils.Ptr(int64(1234)),
}, },
}, },
testRegion,
Model{ Model{
Id: types.StringValue("pid,iid,uid"), Id: types.StringValue("pid,region,iid,uid"),
UserId: types.StringValue("uid"), UserId: types.StringValue("uid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
@ -201,6 +218,7 @@ func TestMapFields(t *testing.T) {
}), }),
Host: types.StringValue("host"), Host: types.StringValue("host"),
Port: types.Int64Value(1234), Port: types.Int64Value(1234),
Region: types.StringValue(testRegion),
}, },
true, true,
}, },
@ -215,8 +233,9 @@ func TestMapFields(t *testing.T) {
Port: utils.Ptr(int64(2123456789)), Port: utils.Ptr(int64(2123456789)),
}, },
}, },
testRegion,
Model{ Model{
Id: types.StringValue("pid,iid,uid"), Id: types.StringValue("pid,region,iid,uid"),
UserId: types.StringValue("uid"), UserId: types.StringValue("uid"),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"), ProjectId: types.StringValue("pid"),
@ -224,18 +243,21 @@ func TestMapFields(t *testing.T) {
Roles: types.SetValueMust(types.StringType, []attr.Value{}), Roles: types.SetValueMust(types.StringType, []attr.Value{}),
Host: types.StringNull(), Host: types.StringNull(),
Port: types.Int64Value(2123456789), Port: types.Int64Value(2123456789),
Region: types.StringValue(testRegion),
}, },
true, true,
}, },
{ {
"nil_response", "nil_response",
nil, nil,
testRegion,
Model{}, Model{},
false, false,
}, },
{ {
"nil_response_2", "nil_response_2",
&sqlserverflex.GetUserResponse{}, &sqlserverflex.GetUserResponse{},
testRegion,
Model{}, Model{},
false, false,
}, },
@ -244,6 +266,7 @@ func TestMapFields(t *testing.T) {
&sqlserverflex.GetUserResponse{ &sqlserverflex.GetUserResponse{
Item: &sqlserverflex.UserResponseUser{}, Item: &sqlserverflex.UserResponseUser{},
}, },
testRegion,
Model{}, Model{},
false, false,
}, },
@ -255,7 +278,7 @@ func TestMapFields(t *testing.T) {
InstanceId: tt.expected.InstanceId, InstanceId: tt.expected.InstanceId,
UserId: tt.expected.UserId, UserId: tt.expected.UserId,
} }
err := mapFields(tt.input, state) err := mapFields(tt.input, state, tt.region)
if !tt.isValid && err == nil { if !tt.isValid && err == nil {
t.Fatalf("Should have failed") t.Fatalf("Should have failed")
} }

View file

@ -317,7 +317,7 @@ func SecretsManagerProviderConfig() string {
} }
func SQLServerFlexProviderConfig() string { func SQLServerFlexProviderConfig() string {
if MongoDBFlexCustomEndpoint == "" { if SQLServerFlexCustomEndpoint == "" {
return ` return `
provider "stackit" { provider "stackit" {
region = "eu01" region = "eu01"