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"
|
||||
"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