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
|
|
@ -6,6 +6,7 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/config"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/conversion"
|
||||
|
|
@ -25,12 +26,22 @@ func NewInstanceDataSource() datasource.DataSource {
|
|||
return &instanceDataSource{}
|
||||
}
|
||||
|
||||
// dataSourceModel maps the data source schema data.
|
||||
type dataSourceModel struct {
|
||||
sqlserverflexbetaGen.InstanceModel
|
||||
TerraformID types.String `tfsdk:"id"`
|
||||
}
|
||||
|
||||
type instanceDataSource struct {
|
||||
client *sqlserverflexbetaPkg.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
func (d *instanceDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||
func (d *instanceDataSource) Metadata(
|
||||
_ context.Context,
|
||||
req datasource.MetadataRequest,
|
||||
resp *datasource.MetadataResponse,
|
||||
) {
|
||||
resp.TypeName = req.ProviderTypeName + "_sqlserverflexbeta_instance"
|
||||
}
|
||||
|
||||
|
|
@ -81,7 +92,7 @@ func (d *instanceDataSource) Configure(
|
|||
}
|
||||
|
||||
func (d *instanceDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
var data sqlserverflexbetaGen.InstanceModel
|
||||
var data dataSourceModel
|
||||
|
||||
// Read Terraform configuration data into the model
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
||||
|
|
|
|||
|
|
@ -84,7 +84,7 @@ func mapResponseToModel(
|
|||
func mapDataResponseToModel(
|
||||
ctx context.Context,
|
||||
resp *sqlserverflexbeta.GetInstanceResponse,
|
||||
m *sqlserverflexbetaDataGen.InstanceModel,
|
||||
m *dataSourceModel,
|
||||
tfDiags diag.Diagnostics,
|
||||
) error {
|
||||
m.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
|
||||
|
|
@ -181,7 +181,7 @@ func handleEncryption(
|
|||
}
|
||||
|
||||
func handleDSEncryption(
|
||||
m *sqlserverflexbetaDataGen.InstanceModel,
|
||||
m *dataSourceModel,
|
||||
resp *sqlserverflexbeta.GetInstanceResponse,
|
||||
) sqlserverflexbetaDataGen.EncryptionValue {
|
||||
if !resp.HasEncryption() ||
|
||||
|
|
|
|||
|
|
@ -43,13 +43,20 @@ type instanceResource struct {
|
|||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
// resourceModel describes the resource data model.
|
||||
type resourceModel = sqlserverflexbetaResGen.InstanceModel
|
||||
|
||||
type InstanceResourceIdentityModel struct {
|
||||
ProjectID types.String `tfsdk:"project_id"`
|
||||
Region types.String `tfsdk:"region"`
|
||||
InstanceID types.String `tfsdk:"instance_id"`
|
||||
}
|
||||
|
||||
func (r *instanceResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||
func (r *instanceResource) Metadata(
|
||||
ctx context.Context,
|
||||
req resource.MetadataRequest,
|
||||
resp *resource.MetadataResponse,
|
||||
) {
|
||||
resp.TypeName = req.ProviderTypeName + "_sqlserverflexbeta_instance"
|
||||
}
|
||||
|
||||
|
|
@ -57,7 +64,11 @@ func (r *instanceResource) Schema(ctx context.Context, req resource.SchemaReques
|
|||
resp.Schema = sqlserverflexbetaResGen.InstanceResourceSchema(ctx)
|
||||
}
|
||||
|
||||
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{
|
||||
|
|
@ -90,7 +101,10 @@ func (r *instanceResource) Configure(
|
|||
utils.UserAgentConfigOption(r.providerData.Version),
|
||||
}
|
||||
if r.providerData.SQLServerFlexCustomEndpoint != "" {
|
||||
apiClientConfigOptions = append(apiClientConfigOptions, config.WithEndpoint(r.providerData.SQLServerFlexCustomEndpoint))
|
||||
apiClientConfigOptions = append(
|
||||
apiClientConfigOptions,
|
||||
config.WithEndpoint(r.providerData.SQLServerFlexCustomEndpoint),
|
||||
)
|
||||
} else {
|
||||
apiClientConfigOptions = append(apiClientConfigOptions, config.WithRegion(r.providerData.GetRegion()))
|
||||
}
|
||||
|
|
@ -121,7 +135,7 @@ func (r *instanceResource) ModifyPlan(
|
|||
if req.Config.Raw.IsNull() {
|
||||
return
|
||||
}
|
||||
var configModel sqlserverflexbetaResGen.InstanceModel
|
||||
var configModel resourceModel
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &configModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
|
|
@ -151,7 +165,7 @@ func (r *instanceResource) ModifyPlan(
|
|||
var modifiersFileByte []byte
|
||||
|
||||
func (r *instanceResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
||||
var data sqlserverflexbetaResGen.InstanceModel
|
||||
var data resourceModel
|
||||
crateErr := "[SQL Server Flex BETA - Create] error"
|
||||
|
||||
// Read Terraform plan data into the model
|
||||
|
|
@ -257,7 +271,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques
|
|||
}
|
||||
|
||||
func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
||||
var data sqlserverflexbetaResGen.InstanceModel
|
||||
var data resourceModel
|
||||
|
||||
// Read Terraform prior state data into the model
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||
|
|
@ -324,7 +338,7 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
|
|||
}
|
||||
|
||||
func (r *instanceResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
||||
var data sqlserverflexbetaResGen.InstanceModel
|
||||
var data resourceModel
|
||||
updateInstanceError := "Error updating instance"
|
||||
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
||||
|
|
@ -411,7 +425,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques
|
|||
}
|
||||
|
||||
func (r *instanceResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
||||
var data sqlserverflexbetaResGen.InstanceModel
|
||||
var data resourceModel
|
||||
|
||||
// Read Terraform prior state data into the model
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||
|
|
@ -484,9 +498,13 @@ func (r *instanceResource) ImportState(
|
|||
idParts := strings.Split(req.ID, core.Separator)
|
||||
|
||||
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics,
|
||||
core.LogAndAddError(
|
||||
ctx, &resp.Diagnostics,
|
||||
"Error importing instance",
|
||||
fmt.Sprintf("Expected import identifier with format: [project_id],[region],[instance_id] Got: %q", req.ID),
|
||||
fmt.Sprintf(
|
||||
"Expected import identifier with format: [project_id],[region],[instance_id] Got: %q",
|
||||
req.ID,
|
||||
),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
|
@ -512,10 +530,23 @@ func (r *instanceResource) ImportState(
|
|||
identityData.Region.ValueString(),
|
||||
identityData.InstanceID.ValueString(),
|
||||
),
|
||||
)...)
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), identityData.ProjectID.ValueString())...)
|
||||
)...,
|
||||
)
|
||||
resp.Diagnostics.Append(
|
||||
resp.State.SetAttribute(
|
||||
ctx,
|
||||
path.Root("project_id"),
|
||||
identityData.ProjectID.ValueString(),
|
||||
)...,
|
||||
)
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), identityData.Region.ValueString())...)
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), identityData.InstanceID.ValueString())...)
|
||||
resp.Diagnostics.Append(
|
||||
resp.State.SetAttribute(
|
||||
ctx,
|
||||
path.Root("instance_id"),
|
||||
identityData.InstanceID.ValueString(),
|
||||
)...,
|
||||
)
|
||||
|
||||
tflog.Info(ctx, "Sqlserverflexbeta instance state imported")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue