feat: refactor data source models and update mapping functions for improved consistency
This commit is contained in:
parent
f0e7c19cdf
commit
184e133a2a
34 changed files with 980 additions and 1017 deletions
|
|
@ -4,6 +4,8 @@ import (
|
|||
"context"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/conversion"
|
||||
|
|
@ -12,6 +14,12 @@ import (
|
|||
sqlserverflexUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/utils"
|
||||
)
|
||||
|
||||
// dataSourceModel maps the data source schema data.
|
||||
type dataSourceModel struct {
|
||||
sqlserverflexalphaGen.DatabaseModel
|
||||
TerraformID types.String `tfsdk:"id"`
|
||||
}
|
||||
|
||||
var _ datasource.DataSource = (*databaseDataSource)(nil)
|
||||
|
||||
func NewDatabaseDataSource() datasource.DataSource {
|
||||
|
|
@ -23,16 +31,31 @@ type databaseDataSource struct {
|
|||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
func (d *databaseDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||
func (d *databaseDataSource) Metadata(
|
||||
_ context.Context,
|
||||
req datasource.MetadataRequest,
|
||||
resp *datasource.MetadataResponse,
|
||||
) {
|
||||
resp.TypeName = req.ProviderTypeName + "_sqlserverflexalpha_database"
|
||||
}
|
||||
|
||||
func (d *databaseDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
resp.Schema = sqlserverflexalphaGen.DatabaseDataSourceSchema(ctx)
|
||||
s := sqlserverflexalphaGen.DatabaseDataSourceSchema(ctx)
|
||||
s.Attributes["id"] = schema.StringAttribute{
|
||||
Description: "Terraform's internal resource ID. It is structured as \\\"`project_id`,`region`,`instance_id`," +
|
||||
"`database_id`\\\".\",",
|
||||
Computed: true,
|
||||
}
|
||||
|
||||
resp.Schema = s
|
||||
}
|
||||
|
||||
// Configure adds the provider configured client to the data source.
|
||||
func (d *databaseDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
||||
func (d *databaseDataSource) Configure(
|
||||
ctx context.Context,
|
||||
req datasource.ConfigureRequest,
|
||||
resp *datasource.ConfigureResponse,
|
||||
) {
|
||||
var ok bool
|
||||
d.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
||||
if !ok {
|
||||
|
|
@ -48,7 +71,7 @@ func (d *databaseDataSource) Configure(ctx context.Context, req datasource.Confi
|
|||
}
|
||||
|
||||
func (d *databaseDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
var data sqlserverflexalphaGen.DatabaseModel
|
||||
var data dataSourceModel
|
||||
|
||||
// Read Terraform configuration data into the model
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
||||
|
|
|
|||
|
|
@ -25,6 +25,9 @@ var (
|
|||
_ resource.ResourceWithModifyPlan = &databaseResource{}
|
||||
)
|
||||
|
||||
// resourceModel describes the resource data model.
|
||||
type resourceModel = sqlserverflexalphaGen.DatabaseModel
|
||||
|
||||
func NewDatabaseResource() resource.Resource {
|
||||
return &databaseResource{}
|
||||
}
|
||||
|
|
@ -59,7 +62,10 @@ func (r *databaseResource) Configure(
|
|||
utils.UserAgentConfigOption(r.providerData.Version),
|
||||
}
|
||||
if r.providerData.PostgresFlexCustomEndpoint != "" {
|
||||
apiClientConfigOptions = append(apiClientConfigOptions, config.WithEndpoint(r.providerData.PostgresFlexCustomEndpoint))
|
||||
apiClientConfigOptions = append(
|
||||
apiClientConfigOptions,
|
||||
config.WithEndpoint(r.providerData.PostgresFlexCustomEndpoint),
|
||||
)
|
||||
} else {
|
||||
apiClientConfigOptions = append(apiClientConfigOptions, config.WithRegion(r.providerData.GetRegion()))
|
||||
}
|
||||
|
|
@ -67,7 +73,10 @@ func (r *databaseResource) Configure(
|
|||
if err != nil {
|
||||
resp.Diagnostics.AddError(
|
||||
"Error configuring API client",
|
||||
fmt.Sprintf("Configuring client: %v. This is an error related to the provider configuration, not to the resource configuration", err),
|
||||
fmt.Sprintf(
|
||||
"Configuring client: %v. This is an error related to the provider configuration, not to the resource configuration",
|
||||
err,
|
||||
),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
|
@ -97,7 +106,7 @@ func (r *databaseResource) Create(ctx context.Context, req resource.CreateReques
|
|||
}
|
||||
|
||||
func (r *databaseResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
||||
var data sqlserverflexalphaGen.DatabaseModel
|
||||
var data resourceModel
|
||||
|
||||
// Read Terraform prior state data into the model
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||
|
|
@ -115,7 +124,7 @@ func (r *databaseResource) Read(ctx context.Context, req resource.ReadRequest, r
|
|||
}
|
||||
|
||||
func (r *databaseResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
||||
var data sqlserverflexalphaGen.DatabaseModel
|
||||
var data resourceModel
|
||||
|
||||
// Read Terraform plan data into the model
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
||||
|
|
|
|||
|
|
@ -26,6 +26,11 @@ var (
|
|||
_ datasource.DataSourceWithConfigure = &flavorDataSource{}
|
||||
)
|
||||
|
||||
// NewFlavorDataSource is a helper function to simplify the provider implementation.
|
||||
func NewFlavorDataSource() datasource.DataSource {
|
||||
return &flavorDataSource{}
|
||||
}
|
||||
|
||||
type FlavorModel struct {
|
||||
ProjectId types.String `tfsdk:"project_id"`
|
||||
Region types.String `tfsdk:"region"`
|
||||
|
|
@ -41,11 +46,6 @@ type FlavorModel struct {
|
|||
StorageClasses types.List `tfsdk:"storage_classes"`
|
||||
}
|
||||
|
||||
// NewFlavorDataSource is a helper function to simplify the provider implementation.
|
||||
func NewFlavorDataSource() datasource.DataSource {
|
||||
return &flavorDataSource{}
|
||||
}
|
||||
|
||||
// flavorDataSource is the data source implementation.
|
||||
type flavorDataSource struct {
|
||||
client *sqlserverflexalpha.APIClient
|
||||
|
|
@ -53,12 +53,20 @@ type flavorDataSource struct {
|
|||
}
|
||||
|
||||
// Metadata returns the data source type name.
|
||||
func (r *flavorDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||
func (r *flavorDataSource) Metadata(
|
||||
_ context.Context,
|
||||
req datasource.MetadataRequest,
|
||||
resp *datasource.MetadataResponse,
|
||||
) {
|
||||
resp.TypeName = req.ProviderTypeName + "_sqlserverflexalpha_flavor"
|
||||
}
|
||||
|
||||
// Configure adds the provider configured client to the data source.
|
||||
func (r *flavorDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
||||
func (r *flavorDataSource) Configure(
|
||||
ctx context.Context,
|
||||
req datasource.ConfigureRequest,
|
||||
resp *datasource.ConfigureResponse,
|
||||
) {
|
||||
var ok bool
|
||||
r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
||||
if !ok {
|
||||
|
|
@ -212,11 +220,13 @@ func (r *flavorDataSource) Read(ctx context.Context, req datasource.ReadRequest,
|
|||
model.MinGb = types.Int64Value(*f.MinGB)
|
||||
|
||||
if f.StorageClasses == nil {
|
||||
model.StorageClasses = types.ListNull(sqlserverflexalphaGen.StorageClassesType{
|
||||
ObjectType: basetypes.ObjectType{
|
||||
AttrTypes: sqlserverflexalphaGen.StorageClassesValue{}.AttributeTypes(ctx),
|
||||
model.StorageClasses = types.ListNull(
|
||||
sqlserverflexalphaGen.StorageClassesType{
|
||||
ObjectType: basetypes.ObjectType{
|
||||
AttrTypes: sqlserverflexalphaGen.StorageClassesValue{}.AttributeTypes(ctx),
|
||||
},
|
||||
},
|
||||
})
|
||||
)
|
||||
} else {
|
||||
var scList []attr.Value
|
||||
for _, sc := range *f.StorageClasses {
|
||||
|
|
|
|||
|
|
@ -13,8 +13,12 @@ import (
|
|||
sqlserverflexalphaGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/flavors/datasources_gen"
|
||||
)
|
||||
|
||||
// dataSourceModel maps the data source schema data.
|
||||
type dataSourceModel = sqlserverflexalphaGen.FlavorsModel
|
||||
|
||||
var _ datasource.DataSource = (*flavorsDataSource)(nil)
|
||||
|
||||
// TODO: Use NewFlavorsDataSource when datasource is implemented
|
||||
func NewFlavorsDataSource() datasource.DataSource {
|
||||
return &flavorsDataSource{}
|
||||
}
|
||||
|
|
@ -24,7 +28,11 @@ type flavorsDataSource struct {
|
|||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
func (d *flavorsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||
func (d *flavorsDataSource) Metadata(
|
||||
_ context.Context,
|
||||
req datasource.MetadataRequest,
|
||||
resp *datasource.MetadataResponse,
|
||||
) {
|
||||
resp.TypeName = req.ProviderTypeName + "_sqlserverflexalpha_flavors"
|
||||
}
|
||||
|
||||
|
|
@ -33,7 +41,11 @@ func (d *flavorsDataSource) Schema(ctx context.Context, _ datasource.SchemaReque
|
|||
}
|
||||
|
||||
// Configure adds the provider configured client to the data source.
|
||||
func (d *flavorsDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
||||
func (d *flavorsDataSource) Configure(
|
||||
ctx context.Context,
|
||||
req datasource.ConfigureRequest,
|
||||
resp *datasource.ConfigureResponse,
|
||||
) {
|
||||
var ok bool
|
||||
d.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
||||
if !ok {
|
||||
|
|
@ -49,7 +61,7 @@ func (d *flavorsDataSource) Configure(ctx context.Context, req datasource.Config
|
|||
}
|
||||
|
||||
func (d *flavorsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
var data sqlserverflexalphaGen.FlavorsModel
|
||||
var data dataSourceModel
|
||||
|
||||
// Read Terraform configuration data into the model
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/conversion"
|
||||
sqlserverflexalpha "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/instance/datasources_gen"
|
||||
sqlserverflexalpha2 "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/instance/resources_gen"
|
||||
|
|
@ -20,6 +22,12 @@ import (
|
|||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
|
||||
)
|
||||
|
||||
// dataSourceModel maps the data source schema data.
|
||||
type dataSourceModel struct {
|
||||
sqlserverflexalpha2.InstanceModel
|
||||
TerraformID types.String `tfsdk:"id"`
|
||||
}
|
||||
|
||||
// Ensure the implementation satisfies the expected interfaces.
|
||||
var (
|
||||
_ datasource.DataSource = &instanceDataSource{}
|
||||
|
|
@ -37,12 +45,20 @@ type instanceDataSource struct {
|
|||
}
|
||||
|
||||
// Metadata returns the data source type name.
|
||||
func (r *instanceDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||
func (r *instanceDataSource) Metadata(
|
||||
_ context.Context,
|
||||
req datasource.MetadataRequest,
|
||||
resp *datasource.MetadataResponse,
|
||||
) {
|
||||
resp.TypeName = req.ProviderTypeName + "_sqlserverflexalpha_instance"
|
||||
}
|
||||
|
||||
// Configure adds the provider configured client to the data source.
|
||||
func (r *instanceDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
||||
func (r *instanceDataSource) Configure(
|
||||
ctx context.Context,
|
||||
req datasource.ConfigureRequest,
|
||||
resp *datasource.ConfigureResponse,
|
||||
) {
|
||||
var ok bool
|
||||
r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
||||
if !ok {
|
||||
|
|
@ -59,167 +75,22 @@ func (r *instanceDataSource) Configure(ctx context.Context, req datasource.Confi
|
|||
|
||||
// Schema defines the schema for the data source.
|
||||
func (r *instanceDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
//descriptions := map[string]string{
|
||||
// "main": "SQLServer Flex ALPHA instance resource schema. Must have a `region` specified in the provider configuration.",
|
||||
// "id": "Terraform's internal resource ID. It is structured as \"`project_id`,`region`,`instance_id`\".",
|
||||
// "instance_id": "ID of the SQLServer Flex instance.",
|
||||
// "project_id": "STACKIT project ID to which the instance is associated.",
|
||||
// "name": "Instance name.",
|
||||
// "access_scope": "The access scope of the instance. (e.g. SNA)",
|
||||
// "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 * * *")`,
|
||||
// "region": "The resource region. If not defined, the provider region is used.",
|
||||
// "encryption": "The encryption block.",
|
||||
// "network": "The network block.",
|
||||
// "keyring_id": "STACKIT KMS - KeyRing ID of the encryption key to use.",
|
||||
// "key_id": "STACKIT KMS - Key ID of the encryption key to use.",
|
||||
// "key_version": "STACKIT KMS - Key version to use in the encryption key.",
|
||||
// "service:account": "STACKIT KMS - service account to use in the encryption key.",
|
||||
// "instance_address": "The returned instance IP address of the SQLServer Flex instance.",
|
||||
// "router_address": "The returned router IP address of the SQLServer Flex instance.",
|
||||
//}
|
||||
s := sqlserverflexalpha.InstanceDataSourceSchema(ctx)
|
||||
s.Attributes["id"] = schema.StringAttribute{
|
||||
Description: "Terraform's internal resource ID. It is structured as \\\"`project_id`,`region`,`instance_id`\\\".",
|
||||
Computed: true,
|
||||
}
|
||||
|
||||
resp.Schema = sqlserverflexalpha.InstanceDataSourceSchema(ctx)
|
||||
|
||||
//resp.Schema = schema.Schema{
|
||||
// Description: descriptions["main"],
|
||||
// Attributes: map[string]schema.Attribute{
|
||||
// "id": schema.StringAttribute{
|
||||
// Description: descriptions["id"],
|
||||
// Computed: true,
|
||||
// },
|
||||
// "instance_id": schema.StringAttribute{
|
||||
// Description: descriptions["instance_id"],
|
||||
// Required: true,
|
||||
// Validators: []validator.String{
|
||||
// validate.UUID(),
|
||||
// validate.NoSeparator(),
|
||||
// },
|
||||
// },
|
||||
// "project_id": schema.StringAttribute{
|
||||
// Description: descriptions["project_id"],
|
||||
// Required: true,
|
||||
// Validators: []validator.String{
|
||||
// validate.UUID(),
|
||||
// validate.NoSeparator(),
|
||||
// },
|
||||
// },
|
||||
// "name": schema.StringAttribute{
|
||||
// Description: descriptions["name"],
|
||||
// Computed: true,
|
||||
// },
|
||||
// "backup_schedule": schema.StringAttribute{
|
||||
// Description: descriptions["backup_schedule"],
|
||||
// Computed: true,
|
||||
// },
|
||||
// "is_deletable": schema.BoolAttribute{
|
||||
// Description: descriptions["is_deletable"],
|
||||
// Computed: true,
|
||||
// },
|
||||
// "flavor": schema.SingleNestedAttribute{
|
||||
// Computed: true,
|
||||
// Attributes: map[string]schema.Attribute{
|
||||
// "id": schema.StringAttribute{
|
||||
// Computed: true,
|
||||
// },
|
||||
// "description": schema.StringAttribute{
|
||||
// Computed: true,
|
||||
// },
|
||||
// "cpu": schema.Int64Attribute{
|
||||
// Computed: true,
|
||||
// },
|
||||
// "ram": schema.Int64Attribute{
|
||||
// Computed: true,
|
||||
// },
|
||||
// "node_type": schema.StringAttribute{
|
||||
// Computed: true,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// "replicas": schema.Int64Attribute{
|
||||
// Computed: true,
|
||||
// },
|
||||
// "storage": schema.SingleNestedAttribute{
|
||||
// Computed: true,
|
||||
// Attributes: map[string]schema.Attribute{
|
||||
// "class": schema.StringAttribute{
|
||||
// Computed: true,
|
||||
// },
|
||||
// "size": schema.Int64Attribute{
|
||||
// Computed: true,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// "version": schema.StringAttribute{
|
||||
// Computed: true,
|
||||
// },
|
||||
// "status": schema.StringAttribute{
|
||||
// Computed: true,
|
||||
// },
|
||||
// "edition": schema.StringAttribute{
|
||||
// Computed: true,
|
||||
// },
|
||||
// "retention_days": schema.Int64Attribute{
|
||||
// Computed: true,
|
||||
// },
|
||||
// "region": schema.StringAttribute{
|
||||
// // the region cannot be found, so it has to be passed
|
||||
// Optional: true,
|
||||
// Description: descriptions["region"],
|
||||
// },
|
||||
// "encryption": schema.SingleNestedAttribute{
|
||||
// Computed: true,
|
||||
// Attributes: map[string]schema.Attribute{
|
||||
// "key_id": schema.StringAttribute{
|
||||
// Description: descriptions["key_id"],
|
||||
// Computed: true,
|
||||
// },
|
||||
// "key_version": schema.StringAttribute{
|
||||
// Description: descriptions["key_version"],
|
||||
// Computed: true,
|
||||
// },
|
||||
// "keyring_id": schema.StringAttribute{
|
||||
// Description: descriptions["keyring_id"],
|
||||
// Computed: true,
|
||||
// },
|
||||
// "service_account": schema.StringAttribute{
|
||||
// Description: descriptions["service_account"],
|
||||
// Computed: true,
|
||||
// },
|
||||
// },
|
||||
// Description: descriptions["encryption"],
|
||||
// },
|
||||
// "network": schema.SingleNestedAttribute{
|
||||
// Computed: true,
|
||||
// Attributes: map[string]schema.Attribute{
|
||||
// "access_scope": schema.StringAttribute{
|
||||
// Description: descriptions["access_scope"],
|
||||
// Computed: true,
|
||||
// },
|
||||
// "instance_address": schema.StringAttribute{
|
||||
// Description: descriptions["instance_address"],
|
||||
// Computed: true,
|
||||
// },
|
||||
// "router_address": schema.StringAttribute{
|
||||
// Description: descriptions["router_address"],
|
||||
// Computed: true,
|
||||
// },
|
||||
// "acl": schema.ListAttribute{
|
||||
// Description: descriptions["acl"],
|
||||
// ElementType: types.StringType,
|
||||
// Computed: true,
|
||||
// },
|
||||
// },
|
||||
// Description: descriptions["network"],
|
||||
// },
|
||||
// },
|
||||
//}
|
||||
resp.Schema = s
|
||||
}
|
||||
|
||||
// Read refreshes the Terraform state with the latest data.
|
||||
func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform
|
||||
//var model sqlserverflexalpha2.InstanceModel
|
||||
var model sqlserverflexalpha2.InstanceModel
|
||||
func (r *instanceDataSource) Read(
|
||||
ctx context.Context,
|
||||
req datasource.ReadRequest,
|
||||
resp *datasource.ReadResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var model dataSourceModel
|
||||
diags := req.Config.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -279,10 +150,15 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
|
|||
// }
|
||||
//}
|
||||
|
||||
err = mapResponseToModel(ctx, instanceResp, &model, resp.Diagnostics)
|
||||
err = mapFields(ctx, instanceResp, &model, resp.Diagnostics)
|
||||
//err = mapFields(ctx, instanceResp, &model, storage, encryption, network, region)
|
||||
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
|
||||
}
|
||||
// Set refreshed state
|
||||
|
|
|
|||
|
|
@ -14,26 +14,21 @@ import (
|
|||
sqlserverflexResGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/instance/resources_gen"
|
||||
)
|
||||
|
||||
func mapResponseToModel(
|
||||
// instanceModel is a type constraint for models that can be mapped from a GetInstanceResponse.
|
||||
type instanceModel interface {
|
||||
*dataSourceModel | *resourceModel
|
||||
}
|
||||
|
||||
func mapFields[T instanceModel](
|
||||
ctx context.Context,
|
||||
resp *sqlserverflex.GetInstanceResponse,
|
||||
m *sqlserverflexResGen.InstanceModel,
|
||||
m T,
|
||||
tfDiags diag.Diagnostics,
|
||||
) error {
|
||||
m.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
|
||||
m.Edition = types.StringValue(string(resp.GetEdition()))
|
||||
m.Encryption = handleEncryption(m, resp)
|
||||
m.FlavorId = types.StringValue(resp.GetFlavorId())
|
||||
m.Id = types.StringValue(resp.GetId())
|
||||
m.InstanceId = types.StringValue(resp.GetId())
|
||||
m.IsDeletable = types.BoolValue(resp.GetIsDeletable())
|
||||
m.Name = types.StringValue(resp.GetName())
|
||||
netAcl, diags := types.ListValueFrom(ctx, types.StringType, resp.Network.GetAcl())
|
||||
tfDiags.Append(diags...)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf(
|
||||
"error converting network acl response value",
|
||||
)
|
||||
return fmt.Errorf("error converting network acl response value")
|
||||
}
|
||||
net, diags := sqlserverflexResGen.NewNetworkValue(
|
||||
sqlserverflexResGen.NetworkValue{}.AttributeTypes(ctx),
|
||||
|
|
@ -46,22 +41,8 @@ func mapResponseToModel(
|
|||
)
|
||||
tfDiags.Append(diags...)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf(
|
||||
"error converting network response value",
|
||||
"access_scope",
|
||||
types.StringValue(string(resp.Network.GetAccessScope())),
|
||||
"acl",
|
||||
netAcl,
|
||||
"instance_address",
|
||||
types.StringValue(resp.Network.GetInstanceAddress()),
|
||||
"router_address",
|
||||
types.StringValue(resp.Network.GetRouterAddress()),
|
||||
)
|
||||
return fmt.Errorf("error converting network response value")
|
||||
}
|
||||
m.Network = net
|
||||
m.Replicas = types.Int64Value(int64(resp.GetReplicas()))
|
||||
m.RetentionDays = types.Int64Value(resp.GetRetentionDays())
|
||||
m.Status = types.StringValue(string(resp.GetStatus()))
|
||||
|
||||
stor, diags := sqlserverflexResGen.NewStorageValue(
|
||||
sqlserverflexResGen.StorageValue{}.AttributeTypes(ctx),
|
||||
|
|
@ -74,14 +55,47 @@ func mapResponseToModel(
|
|||
if diags.HasError() {
|
||||
return fmt.Errorf("error converting storage response value")
|
||||
}
|
||||
m.Storage = stor
|
||||
|
||||
m.Version = types.StringValue(string(resp.GetVersion()))
|
||||
// The interface conversion is safe due to the type constraint.
|
||||
model := any(m)
|
||||
|
||||
if rm, ok := model.(*resourceModel); ok {
|
||||
rm.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
|
||||
rm.Edition = types.StringValue(string(resp.GetEdition()))
|
||||
rm.Encryption = handleEncryption(rm.Encryption, resp)
|
||||
rm.FlavorId = types.StringValue(resp.GetFlavorId())
|
||||
rm.Id = types.StringValue(resp.GetId())
|
||||
rm.InstanceId = types.StringValue(resp.GetId())
|
||||
rm.IsDeletable = types.BoolValue(resp.GetIsDeletable())
|
||||
rm.Name = types.StringValue(resp.GetName())
|
||||
rm.Network = net
|
||||
rm.Replicas = types.Int64Value(int64(resp.GetReplicas()))
|
||||
rm.RetentionDays = types.Int64Value(resp.GetRetentionDays())
|
||||
rm.Status = types.StringValue(string(resp.GetStatus()))
|
||||
rm.Storage = stor
|
||||
rm.Version = types.StringValue(string(resp.GetVersion()))
|
||||
} else if dm, ok := model.(*dataSourceModel); ok {
|
||||
dm.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
|
||||
dm.Edition = types.StringValue(string(resp.GetEdition()))
|
||||
dm.Encryption = handleEncryption(dm.Encryption, resp)
|
||||
dm.FlavorId = types.StringValue(resp.GetFlavorId())
|
||||
dm.Id = types.StringValue(resp.GetId())
|
||||
dm.InstanceId = types.StringValue(resp.GetId())
|
||||
dm.IsDeletable = types.BoolValue(resp.GetIsDeletable())
|
||||
dm.Name = types.StringValue(resp.GetName())
|
||||
dm.Network = net
|
||||
dm.Replicas = types.Int64Value(int64(resp.GetReplicas()))
|
||||
dm.RetentionDays = types.Int64Value(resp.GetRetentionDays())
|
||||
dm.Status = types.StringValue(string(resp.GetStatus()))
|
||||
dm.Storage = stor
|
||||
dm.Version = types.StringValue(string(resp.GetVersion()))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleEncryption(
|
||||
m *sqlserverflexResGen.InstanceModel,
|
||||
encryptionValue sqlserverflexResGen.EncryptionValue,
|
||||
resp *sqlserverflex.GetInstanceResponse,
|
||||
) sqlserverflexResGen.EncryptionValue {
|
||||
if !resp.HasEncryption() ||
|
||||
|
|
@ -91,10 +105,10 @@ func handleEncryption(
|
|||
resp.Encryption.KekKeyVersion == nil ||
|
||||
resp.Encryption.ServiceAccount == nil {
|
||||
|
||||
if m.Encryption.IsNull() || m.Encryption.IsUnknown() {
|
||||
if encryptionValue.IsNull() || encryptionValue.IsUnknown() {
|
||||
return sqlserverflexResGen.NewEncryptionValueNull()
|
||||
}
|
||||
return m.Encryption
|
||||
return encryptionValue
|
||||
}
|
||||
|
||||
enc := sqlserverflexResGen.NewEncryptionValueNull()
|
||||
|
|
|
|||
|
|
@ -37,23 +37,26 @@ var (
|
|||
_ resource.ResourceWithIdentity = &instanceResource{}
|
||||
)
|
||||
|
||||
// NewInstanceResource is a helper function to simplify the provider implementation.
|
||||
func NewInstanceResource() resource.Resource {
|
||||
return &instanceResource{}
|
||||
}
|
||||
|
||||
//nolint:unused // TODO: remove if not needed later
|
||||
var validNodeTypes []string = []string{
|
||||
"Single",
|
||||
"Replica",
|
||||
}
|
||||
|
||||
// resourceModel describes the resource data model.
|
||||
type resourceModel = sqlserverflexalpha2.InstanceModel
|
||||
|
||||
type InstanceResourceIdentityModel struct {
|
||||
ProjectID types.String `tfsdk:"project_id"`
|
||||
Region types.String `tfsdk:"region"`
|
||||
InstanceID types.String `tfsdk:"instance_id"`
|
||||
}
|
||||
|
||||
// NewInstanceResource is a helper function to simplify the provider implementation.
|
||||
func NewInstanceResource() resource.Resource {
|
||||
return &instanceResource{}
|
||||
}
|
||||
|
||||
// instanceResource is the resource implementation.
|
||||
type instanceResource struct {
|
||||
client *sqlserverflexalpha.APIClient
|
||||
|
|
@ -140,27 +143,6 @@ var modifiersFileByte []byte
|
|||
|
||||
// Schema defines the schema for the resource.
|
||||
func (r *instanceResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
//descriptions := map[string]string{
|
||||
// "main": "SQLServer Flex ALPHA instance resource schema. Must have a `region` specified in the provider configuration.",
|
||||
// "id": "Terraform's internal resource ID. It is structured as \"`project_id`,`region`,`instance_id`\".",
|
||||
// "instance_id": "ID of the SQLServer Flex instance.",
|
||||
// "project_id": "STACKIT project ID to which the instance is associated.",
|
||||
// "name": "Instance name.",
|
||||
// "access_scope": "The access scope of the instance. (SNA | PUBLIC)",
|
||||
// "flavor_id": "The flavor ID of the 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 * * *")`,
|
||||
// "region": "The resource region. If not defined, the provider region is used.",
|
||||
// "encryption": "The encryption block.",
|
||||
// "replicas": "The number of replicas of the SQLServer Flex instance.",
|
||||
// "network": "The network block.",
|
||||
// "keyring_id": "STACKIT KMS - KeyRing ID of the encryption key to use.",
|
||||
// "key_id": "STACKIT KMS - Key ID of the encryption key to use.",
|
||||
// "key_version": "STACKIT KMS - Key version to use in the encryption key.",
|
||||
// "service:account": "STACKIT KMS - service account to use in the encryption key.",
|
||||
// "instance_address": "The returned instance IP address of the SQLServer Flex instance.",
|
||||
// "router_address": "The returned router IP address of the SQLServer Flex instance.",
|
||||
//}
|
||||
|
||||
schema := sqlserverflexalpha2.InstanceResourceSchema(ctx)
|
||||
|
||||
|
|
@ -176,234 +158,13 @@ func (r *instanceResource) Schema(ctx context.Context, _ resource.SchemaRequest,
|
|||
return
|
||||
}
|
||||
resp.Schema = schema
|
||||
|
||||
//resp.Schema = schema.Schema{
|
||||
// Description: descriptions["main"],
|
||||
// Attributes: map[string]schema.Attribute{
|
||||
// "id": schema.StringAttribute{
|
||||
// Description: descriptions["id"],
|
||||
// Computed: true,
|
||||
// PlanModifiers: []planmodifier.String{
|
||||
// stringplanmodifier.UseStateForUnknown(),
|
||||
// },
|
||||
// },
|
||||
// "instance_id": schema.StringAttribute{
|
||||
// Description: descriptions["instance_id"],
|
||||
// Computed: true,
|
||||
// PlanModifiers: []planmodifier.String{
|
||||
// stringplanmodifier.UseStateForUnknown(),
|
||||
// },
|
||||
// Validators: []validator.String{
|
||||
// validate.UUID(),
|
||||
// validate.NoSeparator(),
|
||||
// },
|
||||
// },
|
||||
// "project_id": schema.StringAttribute{
|
||||
// Description: descriptions["project_id"],
|
||||
// Required: true,
|
||||
// PlanModifiers: []planmodifier.String{
|
||||
// stringplanmodifier.RequiresReplace(),
|
||||
// },
|
||||
// Validators: []validator.String{
|
||||
// validate.UUID(),
|
||||
// validate.NoSeparator(),
|
||||
// },
|
||||
// },
|
||||
// "name": schema.StringAttribute{
|
||||
// Description: descriptions["name"],
|
||||
// Required: true,
|
||||
// Validators: []validator.String{
|
||||
// stringvalidator.LengthAtLeast(1),
|
||||
// stringvalidator.RegexMatches(
|
||||
// regexp.MustCompile("^[a-z]([-a-z0-9]*[a-z0-9])?$"),
|
||||
// "must start with a letter, must have lower case letters, numbers or hyphens, and no hyphen at the end",
|
||||
// ),
|
||||
// },
|
||||
// },
|
||||
// "backup_schedule": schema.StringAttribute{
|
||||
// Description: descriptions["backup_schedule"],
|
||||
// Optional: true,
|
||||
// Computed: true,
|
||||
// PlanModifiers: []planmodifier.String{
|
||||
// stringplanmodifier.UseStateForUnknown(),
|
||||
// },
|
||||
// },
|
||||
// "is_deletable": schema.BoolAttribute{
|
||||
// Description: descriptions["is_deletable"],
|
||||
// Optional: true,
|
||||
// Computed: true,
|
||||
// PlanModifiers: []planmodifier.Bool{
|
||||
// boolplanmodifier.UseStateForUnknown(),
|
||||
// },
|
||||
// },
|
||||
// "flavor_id": schema.StringAttribute{
|
||||
// PlanModifiers: []planmodifier.String{
|
||||
// stringplanmodifier.RequiresReplace(),
|
||||
// stringplanmodifier.UseStateForUnknown(),
|
||||
// },
|
||||
// Required: true,
|
||||
// },
|
||||
// "replicas": schema.Int64Attribute{
|
||||
// Computed: true,
|
||||
// PlanModifiers: []planmodifier.Int64{
|
||||
// int64planmodifier.UseStateForUnknown(),
|
||||
// },
|
||||
// },
|
||||
// "storage": schema.SingleNestedAttribute{
|
||||
// Optional: true,
|
||||
// Computed: true,
|
||||
// PlanModifiers: []planmodifier.Object{
|
||||
// objectplanmodifier.UseStateForUnknown(),
|
||||
// },
|
||||
// Attributes: map[string]schema.Attribute{
|
||||
// "class": schema.StringAttribute{
|
||||
// Optional: true,
|
||||
// Computed: true,
|
||||
// PlanModifiers: []planmodifier.String{
|
||||
// stringplanmodifier.RequiresReplace(),
|
||||
// stringplanmodifier.UseStateForUnknown(),
|
||||
// },
|
||||
// },
|
||||
// "size": schema.Int64Attribute{
|
||||
// Optional: true,
|
||||
// Computed: true,
|
||||
// PlanModifiers: []planmodifier.Int64{
|
||||
// int64planmodifier.UseStateForUnknown(),
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// "version": schema.StringAttribute{
|
||||
// Optional: true,
|
||||
// Computed: true,
|
||||
// PlanModifiers: []planmodifier.String{
|
||||
// stringplanmodifier.RequiresReplace(),
|
||||
// stringplanmodifier.UseStateForUnknown(),
|
||||
// },
|
||||
// },
|
||||
// "edition": schema.StringAttribute{
|
||||
// Computed: true,
|
||||
// PlanModifiers: []planmodifier.String{
|
||||
// stringplanmodifier.RequiresReplace(),
|
||||
// stringplanmodifier.UseStateForUnknown(),
|
||||
// },
|
||||
// },
|
||||
// "retention_days": schema.Int64Attribute{
|
||||
// Optional: true,
|
||||
// Computed: true,
|
||||
// PlanModifiers: []planmodifier.Int64{
|
||||
// int64planmodifier.UseStateForUnknown(),
|
||||
// },
|
||||
// },
|
||||
// "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(),
|
||||
// },
|
||||
// },
|
||||
// "status": schema.StringAttribute{
|
||||
// Optional: true,
|
||||
// // must be computed to allow for storing the override value from the provider
|
||||
// Computed: true,
|
||||
// Description: descriptions["status"],
|
||||
// },
|
||||
// "encryption": schema.SingleNestedAttribute{
|
||||
// Optional: true,
|
||||
// PlanModifiers: []planmodifier.Object{
|
||||
// objectplanmodifier.RequiresReplace(),
|
||||
// objectplanmodifier.UseStateForUnknown(),
|
||||
// },
|
||||
// Attributes: map[string]schema.Attribute{
|
||||
// "key_id": schema.StringAttribute{
|
||||
// Description: descriptions["key_id"],
|
||||
// Required: true,
|
||||
// PlanModifiers: []planmodifier.String{
|
||||
// stringplanmodifier.RequiresReplace(),
|
||||
// },
|
||||
// Validators: []validator.String{
|
||||
// validate.NoSeparator(),
|
||||
// },
|
||||
// },
|
||||
// "key_version": schema.StringAttribute{
|
||||
// Description: descriptions["key_version"],
|
||||
// Required: true,
|
||||
// PlanModifiers: []planmodifier.String{
|
||||
// stringplanmodifier.RequiresReplace(),
|
||||
// },
|
||||
// Validators: []validator.String{
|
||||
// validate.NoSeparator(),
|
||||
// },
|
||||
// },
|
||||
// "keyring_id": schema.StringAttribute{
|
||||
// Description: descriptions["keyring_id"],
|
||||
// Required: true,
|
||||
// PlanModifiers: []planmodifier.String{
|
||||
// stringplanmodifier.RequiresReplace(),
|
||||
// },
|
||||
// Validators: []validator.String{
|
||||
// validate.NoSeparator(),
|
||||
// },
|
||||
// },
|
||||
// "service_account": schema.StringAttribute{
|
||||
// Description: descriptions["service_account"],
|
||||
// Required: true,
|
||||
// PlanModifiers: []planmodifier.String{
|
||||
// stringplanmodifier.RequiresReplace(),
|
||||
// },
|
||||
// Validators: []validator.String{
|
||||
// validate.NoSeparator(),
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// Description: descriptions["encryption"],
|
||||
// },
|
||||
// "network": schema.SingleNestedAttribute{
|
||||
// Required: true,
|
||||
// Attributes: map[string]schema.Attribute{
|
||||
// "access_scope": schema.StringAttribute{
|
||||
// Description: descriptions["access_scope"],
|
||||
// Required: true,
|
||||
// PlanModifiers: []planmodifier.String{
|
||||
// stringplanmodifier.RequiresReplace(),
|
||||
// stringplanmodifier.UseStateForUnknown(),
|
||||
// },
|
||||
// Validators: []validator.String{
|
||||
// validate.NoSeparator(),
|
||||
// },
|
||||
// },
|
||||
// "acl": schema.ListAttribute{
|
||||
// Description: descriptions["acl"],
|
||||
// ElementType: types.StringType,
|
||||
// Required: true,
|
||||
// PlanModifiers: []planmodifier.List{
|
||||
// listplanmodifier.UseStateForUnknown(),
|
||||
// },
|
||||
// },
|
||||
// "instance_address": schema.StringAttribute{
|
||||
// Description: descriptions["instance_address"],
|
||||
// Computed: true,
|
||||
// PlanModifiers: []planmodifier.String{
|
||||
// stringplanmodifier.UseStateForUnknown(),
|
||||
// },
|
||||
// },
|
||||
// "router_address": schema.StringAttribute{
|
||||
// Description: descriptions["router_address"],
|
||||
// Computed: true,
|
||||
// PlanModifiers: []planmodifier.String{
|
||||
// stringplanmodifier.UseStateForUnknown(),
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// Description: descriptions["network"],
|
||||
// },
|
||||
// },
|
||||
//}
|
||||
}
|
||||
|
||||
func (r *instanceResource) IdentitySchema(_ context.Context, _ resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) {
|
||||
func (r *instanceResource) IdentitySchema(
|
||||
_ context.Context,
|
||||
_ resource.IdentitySchemaRequest,
|
||||
resp *resource.IdentitySchemaResponse,
|
||||
) {
|
||||
resp.IdentitySchema = identityschema.Schema{
|
||||
Attributes: map[string]identityschema.Attribute{
|
||||
"project_id": identityschema.StringAttribute{
|
||||
|
|
@ -425,7 +186,7 @@ func (r *instanceResource) Create(
|
|||
req resource.CreateRequest,
|
||||
resp *resource.CreateResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var model sqlserverflexalpha2.InstanceModel
|
||||
var model resourceModel
|
||||
diags := req.Plan.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -528,7 +289,7 @@ func (r *instanceResource) Create(
|
|||
|
||||
// Map response body to schema
|
||||
// err = mapFields(ctx, waitResp, &model, storage, encryption, network, region)
|
||||
err = mapResponseToModel(ctx, waitResp, &model, resp.Diagnostics)
|
||||
err = mapFields(ctx, waitResp, &model, resp.Diagnostics)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
|
|
@ -554,7 +315,7 @@ func (r *instanceResource) Read(
|
|||
req resource.ReadRequest,
|
||||
resp *resource.ReadResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var model sqlserverflexalpha2.InstanceModel
|
||||
var model resourceModel
|
||||
diags := req.State.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -592,7 +353,7 @@ func (r *instanceResource) Read(
|
|||
ctx = core.LogResponse(ctx)
|
||||
|
||||
// Map response body to schema
|
||||
err = mapResponseToModel(ctx, instanceResp, &model, resp.Diagnostics)
|
||||
err = mapFields(ctx, instanceResp, &model, resp.Diagnostics)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
|
|
@ -629,7 +390,7 @@ func (r *instanceResource) Update(
|
|||
resp *resource.UpdateResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
// Retrieve values from plan
|
||||
var model sqlserverflexalpha2.InstanceModel
|
||||
var model resourceModel
|
||||
diags := req.Plan.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -683,7 +444,7 @@ func (r *instanceResource) Update(
|
|||
}
|
||||
|
||||
// Map response body to schema
|
||||
err = mapResponseToModel(ctx, waitResp, &model, resp.Diagnostics)
|
||||
err = mapFields(ctx, waitResp, &model, resp.Diagnostics)
|
||||
// err = mapFields(ctx, waitResp, &model, storage, encryption, network, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
|
|
@ -709,7 +470,7 @@ func (r *instanceResource) Delete(
|
|||
resp *resource.DeleteResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
// Retrieve values from state
|
||||
var model sqlserverflexalpha2.InstanceModel
|
||||
var model resourceModel
|
||||
diags := req.State.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
|
|||
|
|
@ -28,7 +28,12 @@ var (
|
|||
_ datasource.DataSource = &userDataSource{}
|
||||
)
|
||||
|
||||
type DataSourceModel struct {
|
||||
// NewUserDataSource is a helper function to simplify the provider implementation.
|
||||
func NewUserDataSource() datasource.DataSource {
|
||||
return &userDataSource{}
|
||||
}
|
||||
|
||||
type dataSourceModel struct {
|
||||
Id types.String `tfsdk:"id"` // needed by TF
|
||||
UserId types.Int64 `tfsdk:"user_id"`
|
||||
InstanceId types.String `tfsdk:"instance_id"`
|
||||
|
|
@ -42,11 +47,6 @@ type DataSourceModel struct {
|
|||
DefaultDatabase types.String `tfsdk:"default_database"`
|
||||
}
|
||||
|
||||
// NewUserDataSource is a helper function to simplify the provider implementation.
|
||||
func NewUserDataSource() datasource.DataSource {
|
||||
return &userDataSource{}
|
||||
}
|
||||
|
||||
// userDataSource is the data source implementation.
|
||||
type userDataSource struct {
|
||||
client *sqlserverflexalpha.APIClient
|
||||
|
|
@ -164,7 +164,7 @@ func (r *userDataSource) Read(
|
|||
req datasource.ReadRequest,
|
||||
resp *datasource.ReadResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var model DataSourceModel
|
||||
var model dataSourceModel
|
||||
diags := req.Config.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -226,7 +226,7 @@ func (r *userDataSource) Read(
|
|||
tflog.Info(ctx, "SQLServer Flex instance read")
|
||||
}
|
||||
|
||||
func mapDataSourceFields(userResp *sqlserverflexalpha.GetUserResponse, model *DataSourceModel, region string) error {
|
||||
func mapDataSourceFields(userResp *sqlserverflexalpha.GetUserResponse, model *dataSourceModel, region string) error {
|
||||
if userResp == nil {
|
||||
return fmt.Errorf("response is nil")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,14 +16,14 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
description string
|
||||
input *sqlserverflexalpha.GetUserResponse
|
||||
region string
|
||||
expected DataSourceModel
|
||||
expected dataSourceModel
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_values",
|
||||
&sqlserverflexalpha.GetUserResponse{},
|
||||
testRegion,
|
||||
DataSourceModel{
|
||||
dataSourceModel{
|
||||
Id: types.StringValue("pid,region,iid,1"),
|
||||
UserId: types.Int64Value(1),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
|
|
@ -54,7 +54,7 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
DefaultDatabase: utils.Ptr("default_db"),
|
||||
},
|
||||
testRegion,
|
||||
DataSourceModel{
|
||||
dataSourceModel{
|
||||
Id: types.StringValue("pid,region,iid,1"),
|
||||
UserId: types.Int64Value(1),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
|
|
@ -85,7 +85,7 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
Port: utils.Ptr(int64(2123456789)),
|
||||
},
|
||||
testRegion,
|
||||
DataSourceModel{
|
||||
dataSourceModel{
|
||||
Id: types.StringValue("pid,region,iid,1"),
|
||||
UserId: types.Int64Value(1),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
|
|
@ -102,28 +102,28 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
"nil_response",
|
||||
nil,
|
||||
testRegion,
|
||||
DataSourceModel{},
|
||||
dataSourceModel{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"nil_response_2",
|
||||
&sqlserverflexalpha.GetUserResponse{},
|
||||
testRegion,
|
||||
DataSourceModel{},
|
||||
dataSourceModel{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"no_resource_id",
|
||||
&sqlserverflexalpha.GetUserResponse{},
|
||||
testRegion,
|
||||
DataSourceModel{},
|
||||
dataSourceModel{},
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.description, func(t *testing.T) {
|
||||
state := &DataSourceModel{
|
||||
state := &dataSourceModel{
|
||||
ProjectId: tt.expected.ProjectId,
|
||||
InstanceId: tt.expected.InstanceId,
|
||||
UserId: tt.expected.UserId,
|
||||
|
|
|
|||
|
|
@ -5,11 +5,11 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
|
||||
sqlserverflexalphagen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/user/resources_gen"
|
||||
sqlserverflexalphaUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/utils"
|
||||
sqlserverflexalphaWait "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/wait/sqlserverflexalpha"
|
||||
|
||||
|
|
@ -39,26 +39,14 @@ var (
|
|||
_ resource.ResourceWithModifyPlan = &userResource{}
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
Id types.String `tfsdk:"id"` // needed by TF
|
||||
UserId types.Int64 `tfsdk:"user_id"`
|
||||
InstanceId types.String `tfsdk:"instance_id"`
|
||||
ProjectId types.String `tfsdk:"project_id"`
|
||||
Username types.String `tfsdk:"username"`
|
||||
Roles types.Set `tfsdk:"roles"`
|
||||
Password types.String `tfsdk:"password"`
|
||||
Host types.String `tfsdk:"host"`
|
||||
Port types.Int64 `tfsdk:"port"`
|
||||
Region types.String `tfsdk:"region"`
|
||||
Status types.String `tfsdk:"status"`
|
||||
DefaultDatabase types.String `tfsdk:"default_database"`
|
||||
}
|
||||
|
||||
// NewUserResource is a helper function to simplify the provider implementation.
|
||||
func NewUserResource() resource.Resource {
|
||||
return &userResource{}
|
||||
}
|
||||
|
||||
// resourceModel describes the resource data model.
|
||||
type resourceModel = sqlserverflexalphagen.UserModel
|
||||
|
||||
// userResource is the resource implementation.
|
||||
type userResource struct {
|
||||
client *sqlserverflexalpha.APIClient
|
||||
|
|
@ -93,7 +81,7 @@ func (r *userResource) ModifyPlan(
|
|||
req resource.ModifyPlanRequest,
|
||||
resp *resource.ModifyPlanResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var configModel Model
|
||||
var configModel resourceModel
|
||||
// skip initial empty configuration to avoid follow-up errors
|
||||
if req.Config.Raw.IsNull() {
|
||||
return
|
||||
|
|
@ -103,7 +91,7 @@ func (r *userResource) ModifyPlan(
|
|||
return
|
||||
}
|
||||
|
||||
var planModel Model
|
||||
var planModel resourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
|
|
@ -229,7 +217,7 @@ func (r *userResource) Create(
|
|||
req resource.CreateRequest,
|
||||
resp *resource.CreateResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var model Model
|
||||
var model resourceModel
|
||||
diags := req.Plan.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -313,7 +301,7 @@ func (r *userResource) Read(
|
|||
req resource.ReadRequest,
|
||||
resp *resource.ReadResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var model Model
|
||||
var model resourceModel
|
||||
diags := req.State.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -387,7 +375,7 @@ func (r *userResource) Delete(
|
|||
resp *resource.DeleteResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
// Retrieve values from plan
|
||||
var model Model
|
||||
var model resourceModel
|
||||
diags := req.State.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -455,7 +443,7 @@ func (r *userResource) ImportState(
|
|||
tflog.Info(ctx, "SQLServer Flex user state imported")
|
||||
}
|
||||
|
||||
func mapFieldsCreate(userResp *sqlserverflexalpha.CreateUserResponse, model *Model, region string) error {
|
||||
func mapFieldsCreate(userResp *sqlserverflexalpha.CreateUserResponse, model *resourceModel, region string) error {
|
||||
if userResp == nil {
|
||||
return fmt.Errorf("response is nil")
|
||||
}
|
||||
|
|
@ -468,12 +456,6 @@ func mapFieldsCreate(userResp *sqlserverflexalpha.CreateUserResponse, model *Mod
|
|||
return fmt.Errorf("user id not present")
|
||||
}
|
||||
userId := *user.Id
|
||||
model.Id = utils.BuildInternalTerraformId(
|
||||
model.ProjectId.ValueString(),
|
||||
region,
|
||||
model.InstanceId.ValueString(),
|
||||
strconv.FormatInt(userId, 10),
|
||||
)
|
||||
model.UserId = types.Int64Value(userId)
|
||||
model.Username = types.StringPointerValue(user.Username)
|
||||
|
||||
|
|
@ -491,11 +473,11 @@ func mapFieldsCreate(userResp *sqlserverflexalpha.CreateUserResponse, model *Mod
|
|||
if diags.HasError() {
|
||||
return fmt.Errorf("failed to map roles: %w", core.DiagsToError(diags))
|
||||
}
|
||||
model.Roles = rolesSet
|
||||
model.Roles = types.List(rolesSet)
|
||||
}
|
||||
|
||||
if model.Roles.IsNull() || model.Roles.IsUnknown() {
|
||||
model.Roles = types.SetNull(types.StringType)
|
||||
model.Roles = types.List(types.SetNull(types.StringType))
|
||||
}
|
||||
|
||||
model.Host = types.StringPointerValue(user.Host)
|
||||
|
|
@ -507,7 +489,7 @@ func mapFieldsCreate(userResp *sqlserverflexalpha.CreateUserResponse, model *Mod
|
|||
return nil
|
||||
}
|
||||
|
||||
func mapFields(userResp *sqlserverflexalpha.GetUserResponse, model *Model, region string) error {
|
||||
func mapFields(userResp *sqlserverflexalpha.GetUserResponse, model *resourceModel, region string) error {
|
||||
if userResp == nil {
|
||||
return fmt.Errorf("response is nil")
|
||||
}
|
||||
|
|
@ -524,12 +506,7 @@ func mapFields(userResp *sqlserverflexalpha.GetUserResponse, model *Model, regio
|
|||
} else {
|
||||
return fmt.Errorf("user id not present")
|
||||
}
|
||||
model.Id = utils.BuildInternalTerraformId(
|
||||
model.ProjectId.ValueString(),
|
||||
region,
|
||||
model.InstanceId.ValueString(),
|
||||
strconv.FormatInt(userId, 10),
|
||||
)
|
||||
|
||||
model.UserId = types.Int64Value(userId)
|
||||
model.Username = types.StringPointerValue(user.Username)
|
||||
|
||||
|
|
@ -542,11 +519,11 @@ func mapFields(userResp *sqlserverflexalpha.GetUserResponse, model *Model, regio
|
|||
if diags.HasError() {
|
||||
return fmt.Errorf("failed to map roles: %w", core.DiagsToError(diags))
|
||||
}
|
||||
model.Roles = rolesSet
|
||||
model.Roles = types.List(rolesSet)
|
||||
}
|
||||
|
||||
if model.Roles.IsNull() || model.Roles.IsUnknown() {
|
||||
model.Roles = types.SetNull(types.StringType)
|
||||
model.Roles = types.List(types.SetNull(types.StringType))
|
||||
}
|
||||
|
||||
model.Host = types.StringPointerValue(user.Host)
|
||||
|
|
@ -556,7 +533,7 @@ func mapFields(userResp *sqlserverflexalpha.GetUserResponse, model *Model, regio
|
|||
}
|
||||
|
||||
func toCreatePayload(
|
||||
model *Model,
|
||||
model *resourceModel,
|
||||
roles []sqlserverflexalpha.UserRole,
|
||||
) (*sqlserverflexalpha.CreateUserRequestPayload, error) {
|
||||
if model == nil {
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
description string
|
||||
input *sqlserverflexalpha.CreateUserResponse
|
||||
region string
|
||||
expected Model
|
||||
expected resourceModel
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
|
|
@ -26,13 +26,13 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
Password: utils.Ptr(""),
|
||||
},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,region,iid,1"),
|
||||
resourceModel{
|
||||
Id: types.Int64Value(1),
|
||||
UserId: types.Int64Value(1),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Username: types.StringNull(),
|
||||
Roles: types.SetNull(types.StringType),
|
||||
Roles: types.List(types.SetNull(types.StringType)),
|
||||
Password: types.StringValue(""),
|
||||
Host: types.StringNull(),
|
||||
Port: types.Int64Null(),
|
||||
|
|
@ -57,18 +57,20 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
DefaultDatabase: utils.Ptr("default_db"),
|
||||
},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,region,iid,2"),
|
||||
resourceModel{
|
||||
Id: types.Int64Value(2),
|
||||
UserId: types.Int64Value(2),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Username: types.StringValue("username"),
|
||||
Roles: types.SetValueMust(
|
||||
types.StringType, []attr.Value{
|
||||
types.StringValue("role_1"),
|
||||
types.StringValue("role_2"),
|
||||
types.StringValue(""),
|
||||
},
|
||||
Roles: types.List(
|
||||
types.SetValueMust(
|
||||
types.StringType, []attr.Value{
|
||||
types.StringValue("role_1"),
|
||||
types.StringValue("role_2"),
|
||||
types.StringValue(""),
|
||||
},
|
||||
),
|
||||
),
|
||||
Password: types.StringValue("password"),
|
||||
Host: types.StringValue("host"),
|
||||
|
|
@ -90,13 +92,13 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
Port: utils.Ptr(int64(2123456789)),
|
||||
},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,region,iid,3"),
|
||||
resourceModel{
|
||||
Id: types.Int64Value(3),
|
||||
UserId: types.Int64Value(3),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Username: types.StringNull(),
|
||||
Roles: types.SetValueMust(types.StringType, []attr.Value{}),
|
||||
Roles: types.List(types.SetValueMust(types.StringType, []attr.Value{})),
|
||||
Password: types.StringValue(""),
|
||||
Host: types.StringNull(),
|
||||
Port: types.Int64Value(2123456789),
|
||||
|
|
@ -110,21 +112,21 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
"nil_response",
|
||||
nil,
|
||||
testRegion,
|
||||
Model{},
|
||||
resourceModel{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"nil_response_2",
|
||||
&sqlserverflexalpha.CreateUserResponse{},
|
||||
testRegion,
|
||||
Model{},
|
||||
resourceModel{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"no_resource_id",
|
||||
&sqlserverflexalpha.CreateUserResponse{},
|
||||
testRegion,
|
||||
Model{},
|
||||
resourceModel{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
|
|
@ -133,14 +135,14 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
Id: utils.Ptr(int64(1)),
|
||||
},
|
||||
testRegion,
|
||||
Model{},
|
||||
resourceModel{},
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.description, func(t *testing.T) {
|
||||
state := &Model{
|
||||
state := &resourceModel{
|
||||
ProjectId: tt.expected.ProjectId,
|
||||
InstanceId: tt.expected.InstanceId,
|
||||
}
|
||||
|
|
@ -168,20 +170,20 @@ func TestMapFields(t *testing.T) {
|
|||
description string
|
||||
input *sqlserverflexalpha.GetUserResponse
|
||||
region string
|
||||
expected Model
|
||||
expected resourceModel
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_values",
|
||||
&sqlserverflexalpha.GetUserResponse{},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,region,iid,1"),
|
||||
resourceModel{
|
||||
Id: types.Int64Value(1),
|
||||
UserId: types.Int64Value(1),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Username: types.StringNull(),
|
||||
Roles: types.SetNull(types.StringType),
|
||||
Roles: types.List(types.SetNull(types.StringType)),
|
||||
Host: types.StringNull(),
|
||||
Port: types.Int64Null(),
|
||||
Region: types.StringValue(testRegion),
|
||||
|
|
@ -201,18 +203,20 @@ func TestMapFields(t *testing.T) {
|
|||
Port: utils.Ptr(int64(1234)),
|
||||
},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,region,iid,2"),
|
||||
resourceModel{
|
||||
Id: types.Int64Value(2),
|
||||
UserId: types.Int64Value(2),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Username: types.StringValue("username"),
|
||||
Roles: types.SetValueMust(
|
||||
types.StringType, []attr.Value{
|
||||
types.StringValue("role_1"),
|
||||
types.StringValue("role_2"),
|
||||
types.StringValue(""),
|
||||
},
|
||||
Roles: types.List(
|
||||
types.SetValueMust(
|
||||
types.StringType, []attr.Value{
|
||||
types.StringValue("role_1"),
|
||||
types.StringValue("role_2"),
|
||||
types.StringValue(""),
|
||||
},
|
||||
),
|
||||
),
|
||||
Host: types.StringValue("host"),
|
||||
Port: types.Int64Value(1234),
|
||||
|
|
@ -230,13 +234,13 @@ func TestMapFields(t *testing.T) {
|
|||
Port: utils.Ptr(int64(2123456789)),
|
||||
},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,region,iid,1"),
|
||||
resourceModel{
|
||||
Id: types.Int64Value(1),
|
||||
UserId: types.Int64Value(1),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Username: types.StringNull(),
|
||||
Roles: types.SetValueMust(types.StringType, []attr.Value{}),
|
||||
Roles: types.List(types.SetValueMust(types.StringType, []attr.Value{})),
|
||||
Host: types.StringNull(),
|
||||
Port: types.Int64Value(2123456789),
|
||||
Region: types.StringValue(testRegion),
|
||||
|
|
@ -247,28 +251,28 @@ func TestMapFields(t *testing.T) {
|
|||
"nil_response",
|
||||
nil,
|
||||
testRegion,
|
||||
Model{},
|
||||
resourceModel{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"nil_response_2",
|
||||
&sqlserverflexalpha.GetUserResponse{},
|
||||
testRegion,
|
||||
Model{},
|
||||
resourceModel{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"no_resource_id",
|
||||
&sqlserverflexalpha.GetUserResponse{},
|
||||
testRegion,
|
||||
Model{},
|
||||
resourceModel{},
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.description, func(t *testing.T) {
|
||||
state := &Model{
|
||||
state := &resourceModel{
|
||||
ProjectId: tt.expected.ProjectId,
|
||||
InstanceId: tt.expected.InstanceId,
|
||||
UserId: tt.expected.UserId,
|
||||
|
|
@ -294,14 +298,14 @@ func TestMapFields(t *testing.T) {
|
|||
func TestToCreatePayload(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
input *Model
|
||||
input *resourceModel
|
||||
inputRoles []sqlserverflexalpha.UserRole
|
||||
expected *sqlserverflexalpha.CreateUserRequestPayload
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_values",
|
||||
&Model{},
|
||||
&resourceModel{},
|
||||
[]sqlserverflexalpha.UserRole{},
|
||||
&sqlserverflexalpha.CreateUserRequestPayload{
|
||||
Roles: &[]sqlserverflexalpha.UserRole{},
|
||||
|
|
@ -311,7 +315,7 @@ func TestToCreatePayload(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"default_values",
|
||||
&Model{
|
||||
&resourceModel{
|
||||
Username: types.StringValue("username"),
|
||||
},
|
||||
[]sqlserverflexalpha.UserRole{
|
||||
|
|
@ -329,7 +333,7 @@ func TestToCreatePayload(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"null_fields_and_int_conversions",
|
||||
&Model{
|
||||
&resourceModel{
|
||||
Username: types.StringNull(),
|
||||
},
|
||||
[]sqlserverflexalpha.UserRole{
|
||||
|
|
@ -352,7 +356,7 @@ func TestToCreatePayload(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"nil_roles",
|
||||
&Model{
|
||||
&resourceModel{
|
||||
Username: types.StringValue("username"),
|
||||
},
|
||||
[]sqlserverflexalpha.UserRole{},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue