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() {
|
||||
|
|
|
|||
|
|
@ -14,9 +14,9 @@ import (
|
|||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/core"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
|
||||
|
||||
sqlserverflexbetaPkg "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
|
||||
sqlserverflexbetaPkg "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
|
||||
|
||||
sqlserverflexbetaGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/flavors/datasources_gen"
|
||||
sqlserverflexbetaGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/flavors/datasources_gen"
|
||||
)
|
||||
|
||||
var _ datasource.DataSource = (*flavorsDataSource)(nil)
|
||||
|
|
@ -27,27 +27,31 @@ func NewFlavorsDataSource() datasource.DataSource {
|
|||
return &flavorsDataSource{}
|
||||
}
|
||||
|
||||
type dsModel struct {
|
||||
type dataSourceModel struct {
|
||||
sqlserverflexbetaGen.FlavorsModel
|
||||
TfId types.String `tfsdk:"id"`
|
||||
TerraformId types.String `tfsdk:"id"`
|
||||
}
|
||||
|
||||
type flavorsDataSource struct{
|
||||
type flavorsDataSource struct {
|
||||
client *sqlserverflexbetaPkg.APIClient
|
||||
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 + "_sqlserverflexbeta_flavors"
|
||||
}
|
||||
|
||||
func (d *flavorsDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
resp.Schema = sqlserverflexbetaGen.FlavorsDataSourceSchema(ctx)
|
||||
resp.Schema.Attributes["id"] = schema.StringAttribute{
|
||||
Computed: true,
|
||||
Description: "The terraform internal identifier.",
|
||||
MarkdownDescription: "The terraform internal identifier.",
|
||||
}
|
||||
resp.Schema.Attributes["id"] = schema.StringAttribute{
|
||||
Computed: true,
|
||||
Description: "The terraform internal identifier.",
|
||||
MarkdownDescription: "The terraform internal identifier.",
|
||||
}
|
||||
}
|
||||
|
||||
// Configure adds the provider configured client to the data source.
|
||||
|
|
@ -66,10 +70,10 @@ func (d *flavorsDataSource) Configure(
|
|||
config.WithCustomAuth(d.providerData.RoundTripper),
|
||||
utils.UserAgentConfigOption(d.providerData.Version),
|
||||
}
|
||||
if d.providerData.SqlserverflexbetaCustomEndpoint != "" {
|
||||
if d.providerData.SQLServerFlexCustomEndpoint != "" {
|
||||
apiClientConfigOptions = append(
|
||||
apiClientConfigOptions,
|
||||
config.WithEndpoint(d.providerData.SqlserverflexbetaCustomEndpoint),
|
||||
config.WithEndpoint(d.providerData.SQLServerFlexCustomEndpoint),
|
||||
)
|
||||
} else {
|
||||
apiClientConfigOptions = append(
|
||||
|
|
@ -93,7 +97,7 @@ func (d *flavorsDataSource) Configure(
|
|||
}
|
||||
|
||||
func (d *flavorsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
var data dsModel
|
||||
var data dataSourceModel
|
||||
|
||||
// Read Terraform configuration data into the model
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
||||
|
|
@ -106,7 +110,8 @@ func (d *flavorsDataSource) Read(ctx context.Context, req datasource.ReadRequest
|
|||
|
||||
projectId := data.ProjectId.ValueString()
|
||||
region := d.providerData.GetRegionWithOverride(data.Region)
|
||||
flavorsId := data.FlavorsId.ValueString()
|
||||
// TODO: implement right identifier for flavors
|
||||
flavorsId := data.FlavorsModel.Flavors
|
||||
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
|
|
@ -114,8 +119,8 @@ func (d *flavorsDataSource) Read(ctx context.Context, req datasource.ReadRequest
|
|||
// TODO: implement needed fields
|
||||
ctx = tflog.SetField(ctx, "flavors_id", flavorsId)
|
||||
|
||||
// TODO: refactor to correct implementation
|
||||
flavorsResp, err := d.client.GetFlavorsRequest(ctx, projectId, region, flavorsId).Execute()
|
||||
// TODO: refactor to correct implementation
|
||||
_, err := d.client.GetFlavorsRequest(ctx, projectId, region).Execute()
|
||||
if err != nil {
|
||||
utils.LogError(
|
||||
ctx,
|
||||
|
|
@ -133,17 +138,17 @@ func (d *flavorsDataSource) Read(ctx context.Context, req datasource.ReadRequest
|
|||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
|
||||
data.TfId = utils.BuildInternalTerraformId(projectId, region, ..)
|
||||
// TODO: refactor to correct implementation of internal tf id
|
||||
data.TerraformId = utils.BuildInternalTerraformId(projectId, region)
|
||||
|
||||
// TODO: fill remaining fields
|
||||
// data.Flavors = types.Sometype(apiResponse.GetFlavors())
|
||||
// data.Page = types.Sometype(apiResponse.GetPage())
|
||||
// data.Pagination = types.Sometype(apiResponse.GetPagination())
|
||||
// data.ProjectId = types.Sometype(apiResponse.GetProjectId())
|
||||
// data.Region = types.Sometype(apiResponse.GetRegion())
|
||||
// data.Size = types.Sometype(apiResponse.GetSize())
|
||||
// data.Sort = types.Sometype(apiResponse.GetSort())// Save data into Terraform state
|
||||
// data.Flavors = types.Sometype(apiResponse.GetFlavors())
|
||||
// data.Page = types.Sometype(apiResponse.GetPage())
|
||||
// data.Pagination = types.Sometype(apiResponse.GetPagination())
|
||||
// data.ProjectId = types.Sometype(apiResponse.GetProjectId())
|
||||
// data.Region = types.Sometype(apiResponse.GetRegion())
|
||||
// data.Size = types.Sometype(apiResponse.GetSize())
|
||||
// data.Sort = types.Sometype(apiResponse.GetSort())// Save data into Terraform state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
|
||||
tflog.Info(ctx, fmt.Sprintf("%s read successful", errorPrefix))
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/core"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
|
||||
|
|
@ -25,12 +26,30 @@ func NewUserDataSource() datasource.DataSource {
|
|||
return &userDataSource{}
|
||||
}
|
||||
|
||||
type dataSourceModel struct {
|
||||
DefaultDatabase types.String `tfsdk:"default_database"`
|
||||
Host types.String `tfsdk:"host"`
|
||||
Id types.Int64 `tfsdk:"id"`
|
||||
InstanceId types.String `tfsdk:"instance_id"`
|
||||
Port types.Int64 `tfsdk:"port"`
|
||||
ProjectId types.String `tfsdk:"project_id"`
|
||||
Region types.String `tfsdk:"region"`
|
||||
Roles types.List `tfsdk:"roles"`
|
||||
Status types.String `tfsdk:"status"`
|
||||
UserId types.Int64 `tfsdk:"user_id"`
|
||||
Username types.String `tfsdk:"username"`
|
||||
}
|
||||
|
||||
type userDataSource struct {
|
||||
client *sqlserverflexbetaPkg.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
func (d *userDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||
func (d *userDataSource) Metadata(
|
||||
_ context.Context,
|
||||
req datasource.MetadataRequest,
|
||||
resp *datasource.MetadataResponse,
|
||||
) {
|
||||
resp.TypeName = req.ProviderTypeName + "_sqlserverflexbeta_user"
|
||||
}
|
||||
|
||||
|
|
@ -59,7 +78,7 @@ func (d *userDataSource) Configure(
|
|||
}
|
||||
|
||||
func (d *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
var data sqlserverflexbetaGen.UserModel
|
||||
var data dataSourceModel
|
||||
|
||||
// Read Terraform configuration data into the model
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
||||
|
|
@ -72,13 +91,15 @@ func (d *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
|
|||
|
||||
projectId := data.ProjectId.ValueString()
|
||||
region := d.providerData.GetRegionWithOverride(data.Region)
|
||||
userId := data.UserId.ValueString()
|
||||
instanceId := data.InstanceId.ValueString()
|
||||
userId := data.UserId.ValueInt64()
|
||||
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
||||
ctx = tflog.SetField(ctx, "user_id", userId)
|
||||
|
||||
userResp, err := d.client.GetUserRequest(ctx, projectId, region, userId).Execute()
|
||||
userResp, err := d.client.GetUserRequest(ctx, projectId, region, instanceId, userId).Execute()
|
||||
if err != nil {
|
||||
utils.LogError(
|
||||
ctx,
|
||||
|
|
|
|||
|
|
@ -3,13 +3,9 @@ package sqlserverflexbeta
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/conversion"
|
||||
|
||||
sqlserverflexbeta "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
|
||||
sqlserverflexbetaResGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/instance/resources_gen"
|
||||
|
|
@ -18,11 +14,39 @@ import (
|
|||
func mapResponseToModel(
|
||||
ctx context.Context,
|
||||
resp *sqlserverflexbeta.GetUserResponse,
|
||||
m *sqlserverflexbetaResGen.UserModel,
|
||||
m *dataSourceModel,
|
||||
tfDiags diag.Diagnostics,
|
||||
) error {
|
||||
if resp == nil {
|
||||
return fmt.Errorf("response is nil")
|
||||
}
|
||||
|
||||
m.Id = types.Int64Value(resp.GetId())
|
||||
m.UserId = types.Int64Value(resp.GetId())
|
||||
m.Username = types.StringValue(resp.GetUsername())
|
||||
m.Port = types.Int64Value(resp.GetPort())
|
||||
m.Host = types.StringValue(resp.GetHost())
|
||||
m.DefaultDatabase = types.StringValue(resp.GetDefaultDatabase())
|
||||
m.Status = types.StringValue(resp.GetStatus())
|
||||
|
||||
if resp.Roles != nil {
|
||||
roles, diags := types.ListValueFrom(ctx, types.StringType, *resp.Roles)
|
||||
tfDiags.Append(diags...)
|
||||
if tfDiags.HasError() {
|
||||
return fmt.Errorf("failed to map roles")
|
||||
}
|
||||
m.Roles = roles
|
||||
} else {
|
||||
m.Roles = types.ListNull(types.StringType)
|
||||
}
|
||||
|
||||
if resp.Status != nil {
|
||||
m.Status = types.StringValue(*resp.Status)
|
||||
} else {
|
||||
m.Status = types.StringNull()
|
||||
}
|
||||
|
||||
// TODO: complete and refactor
|
||||
m.Id = types.StringValue(resp.GetId())
|
||||
|
||||
/*
|
||||
sampleList, diags := types.ListValueFrom(ctx, types.StringType, resp.GetList())
|
||||
|
|
@ -51,48 +75,63 @@ func mapResponseToModel(
|
|||
return nil
|
||||
}
|
||||
|
||||
// TODO: handle encryption field mapping when API supports it
|
||||
func handleEncryption(
|
||||
m *sqlserverflexbetaResGen.UserModel,
|
||||
m *dataSourceModel,
|
||||
resp *sqlserverflexbeta.GetUserResponse,
|
||||
) sqlserverflexbetaResGen.EncryptionValue {
|
||||
if !resp.HasEncryption() ||
|
||||
resp.Encryption == nil ||
|
||||
resp.Encryption.KekKeyId == nil ||
|
||||
resp.Encryption.KekKeyRingId == nil ||
|
||||
resp.Encryption.KekKeyVersion == nil ||
|
||||
resp.Encryption.ServiceAccount == nil {
|
||||
/*
|
||||
if !resp.HasEncryption() ||
|
||||
|
||||
if m.Encryption.IsNull() || m.Encryption.IsUnknown() {
|
||||
return sqlserverflexbetaResGen.NewEncryptionValueNull()
|
||||
resp.Encryption == nil ||
|
||||
resp.Encryption.KekKeyId == nil ||
|
||||
resp.Encryption.KekKeyRingId == nil ||
|
||||
resp.Encryption.KekKeyVersion == nil ||
|
||||
resp.Encryption.ServiceAccount == nil {
|
||||
|
||||
if m.Encryption.IsNull() || m.Encryption.IsUnknown() {
|
||||
return sqlserverflexbetaResGen.NewEncryptionValueNull()
|
||||
}
|
||||
return m.Encryption
|
||||
}
|
||||
return m.Encryption
|
||||
}
|
||||
|
||||
enc := sqlserverflexbetaResGen.NewEncryptionValueNull()
|
||||
if kVal, ok := resp.Encryption.GetKekKeyIdOk(); ok {
|
||||
enc.KekKeyId = types.StringValue(kVal)
|
||||
}
|
||||
if kkVal, ok := resp.Encryption.GetKekKeyRingIdOk(); ok {
|
||||
enc.KekKeyRingId = types.StringValue(kkVal)
|
||||
}
|
||||
if kkvVal, ok := resp.Encryption.GetKekKeyVersionOk(); ok {
|
||||
enc.KekKeyVersion = types.StringValue(kkvVal)
|
||||
}
|
||||
if sa, ok := resp.Encryption.GetServiceAccountOk(); ok {
|
||||
enc.ServiceAccount = types.StringValue(sa)
|
||||
}
|
||||
return enc
|
||||
enc := sqlserverflexbetaResGen.NewEncryptionValueNull()
|
||||
if kVal, ok := resp.Encryption.GetKekKeyIdOk(); ok {
|
||||
enc.KekKeyId = types.StringValue(kVal)
|
||||
}
|
||||
if kkVal, ok := resp.Encryption.GetKekKeyRingIdOk(); ok {
|
||||
enc.KekKeyRingId = types.StringValue(kkVal)
|
||||
}
|
||||
if kkvVal, ok := resp.Encryption.GetKekKeyVersionOk(); ok {
|
||||
enc.KekKeyVersion = types.StringValue(kkvVal)
|
||||
}
|
||||
if sa, ok := resp.Encryption.GetServiceAccountOk(); ok {
|
||||
enc.ServiceAccount = types.StringValue(sa)
|
||||
}
|
||||
return enc
|
||||
*/
|
||||
return sqlserverflexbetaResGen.NewEncryptionValueNull()
|
||||
}
|
||||
|
||||
func toCreatePayload(
|
||||
ctx context.Context,
|
||||
model *sqlserverflexbetaResGen.UserModel,
|
||||
model *dataSourceModel,
|
||||
) (*sqlserverflexbeta.CreateUserRequestPayload, error) {
|
||||
if model == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
}
|
||||
|
||||
var roles []sqlserverflexbeta.UserRole
|
||||
if !model.Roles.IsNull() && !model.Roles.IsUnknown() {
|
||||
diags := model.Roles.ElementsAs(ctx, &roles, false)
|
||||
if diags.HasError() {
|
||||
return nil, fmt.Errorf("failed to convert roles: %v", diags)
|
||||
}
|
||||
}
|
||||
|
||||
return &sqlserverflexbeta.CreateUserRequestPayload{
|
||||
// TODO: fill fields
|
||||
DefaultDatabase: model.DefaultDatabase.ValueStringPointer(),
|
||||
Username: model.Username.ValueStringPointer(),
|
||||
Roles: &roles,
|
||||
}, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,6 +33,9 @@ func NewUserResource() resource.Resource {
|
|||
return &userResource{}
|
||||
}
|
||||
|
||||
// resourceModel describes the resource data model.
|
||||
type resourceModel = sqlserverflexbetaResGen.UserModel
|
||||
|
||||
type userResource struct {
|
||||
client *sqlserverflexbeta.APIClient
|
||||
providerData core.ProviderData
|
||||
|
|
@ -53,7 +56,11 @@ func (r *userResource) Schema(ctx context.Context, req resource.SchemaRequest, r
|
|||
resp.Schema = sqlserverflexbetaResGen.UserResourceSchema(ctx)
|
||||
}
|
||||
|
||||
func (r *instanceResource) IdentitySchema(_ context.Context, _ resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) {
|
||||
func (r *userResource) IdentitySchema(
|
||||
_ context.Context,
|
||||
_ resource.IdentitySchemaRequest,
|
||||
resp *resource.IdentitySchemaResponse,
|
||||
) {
|
||||
resp.IdentitySchema = identityschema.Schema{
|
||||
Attributes: map[string]identityschema.Attribute{
|
||||
"project_id": identityschema.StringAttribute{
|
||||
|
|
@ -85,8 +92,11 @@ func (r *userResource) Configure(
|
|||
config.WithCustomAuth(r.providerData.RoundTripper),
|
||||
utils.UserAgentConfigOption(r.providerData.Version),
|
||||
}
|
||||
if r.providerData.SqlserverflexbetaCustomEndpoint != "" {
|
||||
apiClientConfigOptions = append(apiClientConfigOptions, config.WithEndpoint(r.providerData.sqlserverflexbetaCustomEndpoint))
|
||||
if r.providerData.SQLServerFlexCustomEndpoint != "" {
|
||||
apiClientConfigOptions = append(
|
||||
apiClientConfigOptions,
|
||||
config.WithEndpoint(r.providerData.SQLServerFlexCustomEndpoint),
|
||||
)
|
||||
} else {
|
||||
apiClientConfigOptions = append(apiClientConfigOptions, config.WithRegion(r.providerData.GetRegion()))
|
||||
}
|
||||
|
|
@ -106,7 +116,7 @@ func (r *userResource) Configure(
|
|||
}
|
||||
|
||||
func (r *userResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
||||
var data sqlserverflexbetaResGen.UserModel
|
||||
var data resourceModel
|
||||
|
||||
// Read Terraform plan data into the model
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
||||
|
|
@ -159,14 +169,14 @@ func (r *userResource) Create(ctx context.Context, req resource.CreateRequest, r
|
|||
*/
|
||||
|
||||
// Example data value setting
|
||||
data.UserId = types.StringValue("id-from-response")
|
||||
//data.UserId = types.StringValue("id-from-response")
|
||||
|
||||
// TODO: Set data returned by API in identity
|
||||
identity := UserResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
// TODO: add missing values
|
||||
UserID: types.StringValue(UserId),
|
||||
// UserID: types.StringValue(UserId),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -228,7 +238,7 @@ func (r *userResource) Create(ctx context.Context, req resource.CreateRequest, r
|
|||
}
|
||||
|
||||
func (r *userResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
||||
var data sqlserverflexbetaResGen.UserModel
|
||||
var data resourceModel
|
||||
|
||||
// Read Terraform prior state data into the model
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||
|
|
@ -270,7 +280,7 @@ func (r *userResource) Read(ctx context.Context, req resource.ReadRequest, resp
|
|||
}
|
||||
|
||||
func (r *userResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
||||
var data sqlserverflexbetaResGen.UserModel
|
||||
var data resourceModel
|
||||
|
||||
// Read Terraform prior state data into the model
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||
|
|
@ -301,7 +311,7 @@ func (r *userResource) Update(ctx context.Context, req resource.UpdateRequest, r
|
|||
}
|
||||
|
||||
func (r *userResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
||||
var data sqlserverflexbetaResGen.UserModel
|
||||
var data resourceModel
|
||||
|
||||
// Read Terraform prior state data into the model
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||
|
|
@ -335,7 +345,7 @@ func (r *userResource) ModifyPlan(
|
|||
req resource.ModifyPlanRequest,
|
||||
resp *resource.ModifyPlanResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var configModel sqlserverflexbetaResGen.UserModel
|
||||
var configModel resourceModel
|
||||
// skip initial empty configuration to avoid follow-up errors
|
||||
if req.Config.Raw.IsNull() {
|
||||
return
|
||||
|
|
@ -345,7 +355,7 @@ func (r *userResource) ModifyPlan(
|
|||
return
|
||||
}
|
||||
|
||||
var planModel sqlserverflexbetaResGen.UserModel
|
||||
var planModel resourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue