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
|
|
@ -26,17 +26,21 @@ func NewDatabaseDataSource() datasource.DataSource {
|
|||
return &databaseDataSource{}
|
||||
}
|
||||
|
||||
type dataSourceModel struct {
|
||||
sqlserverflexbetaGen.DatabaseModel
|
||||
TfId types.String `tfsdk:"id"`
|
||||
}
|
||||
|
||||
type databaseDataSource struct {
|
||||
client *sqlserverflexbetaPkg.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
type dsModel struct {
|
||||
sqlserverflexbetaGen.DatabaseModel
|
||||
TfId types.String `tfsdk:"id"`
|
||||
}
|
||||
|
||||
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 + "_sqlserverflexbeta_database"
|
||||
}
|
||||
|
||||
|
|
@ -92,7 +96,7 @@ func (d *databaseDataSource) Configure(
|
|||
}
|
||||
|
||||
func (d *databaseDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
var data dsModel
|
||||
var data dataSourceModel
|
||||
readErr := "Read DB error"
|
||||
|
||||
// Read Terraform configuration data into the model
|
||||
|
|
|
|||
|
|
@ -35,6 +35,9 @@ func NewDatabaseResource() resource.Resource {
|
|||
return &databaseResource{}
|
||||
}
|
||||
|
||||
// resourceModel describes the resource data model.
|
||||
type resourceModel = sqlserverflexbetaResGen.DatabaseModel
|
||||
|
||||
type databaseResource struct {
|
||||
client *sqlserverflexbeta.APIClient
|
||||
providerData core.ProviderData
|
||||
|
|
@ -47,7 +50,11 @@ type DatabaseResourceIdentityModel struct {
|
|||
DatabaseName types.String `tfsdk:"database_name"`
|
||||
}
|
||||
|
||||
func (r *databaseResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||
func (r *databaseResource) Metadata(
|
||||
ctx context.Context,
|
||||
req resource.MetadataRequest,
|
||||
resp *resource.MetadataResponse,
|
||||
) {
|
||||
resp.TypeName = req.ProviderTypeName + "_sqlserverflexbeta_database"
|
||||
}
|
||||
|
||||
|
|
@ -55,7 +62,11 @@ func (r *databaseResource) Schema(ctx context.Context, req resource.SchemaReques
|
|||
resp.Schema = sqlserverflexbetaResGen.DatabaseResourceSchema(ctx)
|
||||
}
|
||||
|
||||
func (r *databaseResource) IdentitySchema(_ context.Context, _ resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) {
|
||||
func (r *databaseResource) IdentitySchema(
|
||||
_ context.Context,
|
||||
_ resource.IdentitySchemaRequest,
|
||||
resp *resource.IdentitySchemaResponse,
|
||||
) {
|
||||
resp.IdentitySchema = identityschema.Schema{
|
||||
Attributes: map[string]identityschema.Attribute{
|
||||
"project_id": identityschema.StringAttribute{
|
||||
|
|
@ -91,7 +102,10 @@ func (r *databaseResource) 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()))
|
||||
}
|
||||
|
|
@ -111,7 +125,7 @@ func (r *databaseResource) Configure(
|
|||
}
|
||||
|
||||
func (r *databaseResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
||||
var data sqlserverflexbetaResGen.DatabaseModel
|
||||
var data resourceModel
|
||||
createErr := "DB create error"
|
||||
|
||||
// Read Terraform plan data into the model
|
||||
|
|
@ -243,7 +257,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 sqlserverflexbetaResGen.DatabaseModel
|
||||
var data resourceModel
|
||||
readErr := "[Database Read]"
|
||||
|
||||
// Read Terraform prior state data into the model
|
||||
|
|
@ -298,7 +312,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 sqlserverflexbetaResGen.DatabaseModel
|
||||
var data resourceModel
|
||||
|
||||
// Read Terraform prior state data into the model
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||
|
|
@ -329,7 +343,7 @@ func (r *databaseResource) Update(ctx context.Context, req resource.UpdateReques
|
|||
}
|
||||
|
||||
func (r *databaseResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
||||
var data sqlserverflexbetaResGen.DatabaseModel
|
||||
var data resourceModel
|
||||
|
||||
// Read Terraform prior state data into the model
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||
|
|
@ -422,9 +436,13 @@ func (r *databaseResource) ImportState(
|
|||
idParts := strings.Split(req.ID, core.Separator)
|
||||
|
||||
if len(idParts) != 4 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" || idParts[3] == "" {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics,
|
||||
core.LogAndAddError(
|
||||
ctx, &resp.Diagnostics,
|
||||
"Error importing database",
|
||||
fmt.Sprintf("Expected import identifier with format: [project_id],[region],[instance_id],[database_name] Got: %q", req.ID),
|
||||
fmt.Sprintf(
|
||||
"Expected import identifier with format: [project_id],[region],[instance_id],[database_name] Got: %q",
|
||||
req.ID,
|
||||
),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
|
@ -455,10 +473,28 @@ func (r *databaseResource) ImportState(
|
|||
return
|
||||
}
|
||||
|
||||
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("database_name"), identityData.DatabaseName.ValueString())...)
|
||||
resp.Diagnostics.Append(
|
||||
resp.State.SetAttribute(
|
||||
ctx,
|
||||
path.Root("instance_id"),
|
||||
identityData.InstanceID.ValueString(),
|
||||
)...,
|
||||
)
|
||||
resp.Diagnostics.Append(
|
||||
resp.State.SetAttribute(
|
||||
ctx,
|
||||
path.Root("database_name"),
|
||||
identityData.DatabaseName.ValueString(),
|
||||
)...,
|
||||
)
|
||||
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, &identityData)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue