fix: refactor identity data handling and improve API response mapping
This commit is contained in:
parent
be5c3b5430
commit
8b2d76482b
7 changed files with 87 additions and 35 deletions
|
|
@ -159,14 +159,10 @@ func (r *userResource) Create(
|
|||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
arg, errExt := r.extractIdentityData(model, identityData)
|
||||
if errExt != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
extractErrorSummary,
|
||||
fmt.Sprintf(extractErrorMessage, errExt),
|
||||
)
|
||||
arg := &clientArg{
|
||||
projectId: model.ProjectId.ValueString(),
|
||||
instanceId: model.InstanceId.ValueString(),
|
||||
region: r.providerData.GetRegionWithOverride(model.Region),
|
||||
}
|
||||
|
||||
ctx = r.setTFLogFields(ctx, arg)
|
||||
|
|
@ -204,11 +200,13 @@ func (r *userResource) Create(
|
|||
)
|
||||
return
|
||||
}
|
||||
model.Id = types.Int64PointerValue(userResp.Id)
|
||||
model.UserId = types.Int64PointerValue(userResp.Id)
|
||||
model.Password = types.StringPointerValue(userResp.Password)
|
||||
model.Id = types.Int64Value(userResp.GetId())
|
||||
model.UserId = types.Int64Value(userResp.GetId())
|
||||
model.Password = types.StringValue(userResp.GetPassword())
|
||||
model.Status = types.StringValue(userResp.GetStatus())
|
||||
model.ConnectionString = types.StringValue(userResp.GetConnectionString())
|
||||
|
||||
ctx = tflog.SetField(ctx, "user_id", *userResp.Id)
|
||||
ctx = tflog.SetField(ctx, "user_id", userResp.GetId())
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
|
|
@ -217,7 +215,7 @@ func (r *userResource) Create(
|
|||
ProjectID: types.StringValue(arg.projectId),
|
||||
Region: types.StringValue(arg.region),
|
||||
InstanceID: types.StringValue(arg.instanceId),
|
||||
UserID: types.Int64PointerValue(userResp.Id),
|
||||
UserID: types.Int64Value(userResp.GetId()),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -261,23 +259,12 @@ func (r *userResource) Read(
|
|||
return
|
||||
}
|
||||
|
||||
// Read identity data
|
||||
var identityData UserResourceIdentityModel
|
||||
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
arg, errExt := r.extractIdentityData(model, identityData)
|
||||
if errExt != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
extractErrorSummary,
|
||||
fmt.Sprintf(extractErrorMessage, errExt),
|
||||
)
|
||||
arg := &clientArg{
|
||||
projectId: model.ProjectId.ValueString(),
|
||||
instanceId: model.InstanceId.ValueString(),
|
||||
region: r.providerData.GetRegionWithOverride(model.Region),
|
||||
}
|
||||
|
||||
ctx = r.setTFLogFields(ctx, arg)
|
||||
|
|
@ -299,6 +286,18 @@ func (r *userResource) Read(
|
|||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
// Set data returned by API in identity
|
||||
identity := UserResourceIdentityModel{
|
||||
ProjectID: types.StringValue(arg.projectId),
|
||||
Region: types.StringValue(arg.region),
|
||||
InstanceID: types.StringValue(arg.instanceId),
|
||||
UserID: types.Int64Value(arg.userId),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Set refreshed state
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
|
|
@ -385,6 +384,18 @@ func (r *userResource) Update(
|
|||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
// Set data returned by API in identity
|
||||
identity := UserResourceIdentityModel{
|
||||
ProjectID: types.StringValue(arg.projectId),
|
||||
Region: types.StringValue(arg.region),
|
||||
InstanceID: types.StringValue(arg.instanceId),
|
||||
UserID: types.Int64Value(userId64),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Verify update
|
||||
exists, err := r.getUserResource(ctx, &stateModel, arg)
|
||||
|
||||
|
|
|
|||
|
|
@ -375,6 +375,7 @@ func (r *databaseResource) Delete(ctx context.Context, req resource.DeleteReques
|
|||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
resp.State.RemoveResource(ctx)
|
||||
|
||||
tflog.Info(ctx, "sqlserverflexalpha.Database deleted")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -140,7 +140,7 @@ func mapFieldsCreate(userResp *sqlserverflexalpha.CreateUserResponse, model *res
|
|||
if user.Roles != nil {
|
||||
var roles []attr.Value
|
||||
for _, role := range *user.Roles {
|
||||
roles = append(roles, types.StringValue(string(role)))
|
||||
roles = append(roles, types.StringValue(role))
|
||||
}
|
||||
rolesSet, diags := types.SetValue(types.StringType, roles)
|
||||
if diags.HasError() {
|
||||
|
|
@ -165,7 +165,7 @@ func mapFieldsCreate(userResp *sqlserverflexalpha.CreateUserResponse, model *res
|
|||
// toCreatePayload converts a resourceModel to an API CreateUserRequestPayload.
|
||||
func toCreatePayload(
|
||||
model *resourceModel,
|
||||
roles []sqlserverflexalpha.UserRole,
|
||||
roles []string,
|
||||
) (*sqlserverflexalpha.CreateUserRequestPayload, error) {
|
||||
if model == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
|
|
|
|||
|
|
@ -179,7 +179,7 @@ func (r *userResource) Create(
|
|||
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
|
||||
var roles []sqlserverflexalpha.UserRole
|
||||
var roles []string
|
||||
if !model.Roles.IsNull() && !model.Roles.IsUnknown() {
|
||||
diags = model.Roles.ElementsAs(ctx, &roles, false)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
|
|
@ -220,6 +220,18 @@ func (r *userResource) Create(
|
|||
userId := *userResp.Id
|
||||
ctx = tflog.SetField(ctx, "user_id", userId)
|
||||
|
||||
// Set data returned by API in identity
|
||||
identity := UserResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
InstanceID: types.StringValue(instanceId),
|
||||
UserID: types.Int64Value(userResp.GetId()),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Map response body to schema
|
||||
err = mapFieldsCreate(userResp, &model, region)
|
||||
if err != nil {
|
||||
|
|
@ -282,6 +294,18 @@ func (r *userResource) Read(
|
|||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
// Set data returned by API in identity
|
||||
identity := UserResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
InstanceID: types.StringValue(instanceId),
|
||||
UserID: types.Int64Value(userId),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Map response body to schema
|
||||
err = mapFields(recordSetResp, &model, region)
|
||||
if err != nil {
|
||||
|
|
@ -351,6 +375,7 @@ func (r *userResource) Delete(
|
|||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
resp.State.RemoveResource(ctx)
|
||||
|
||||
tflog.Info(ctx, "SQLServer Flex user deleted")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -69,7 +69,7 @@ func (r *databaseResource) Metadata(
|
|||
//go:embed planModifiers.yaml
|
||||
var modifiersFileByte []byte
|
||||
|
||||
func (r *databaseResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
func (r *databaseResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
|
||||
s := sqlserverflexbetaResGen.DatabaseResourceSchema(ctx)
|
||||
|
||||
|
|
@ -395,7 +395,7 @@ func (r *databaseResource) Read(ctx context.Context, req resource.ReadRequest, r
|
|||
tflog.Info(ctx, "sqlserverflexbeta.Database read")
|
||||
}
|
||||
|
||||
func (r *databaseResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
||||
func (r *databaseResource) Update(ctx context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) {
|
||||
// TODO: Check update api endpoint - not available at the moment, so return an error for now
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating database", "Database can't be updated")
|
||||
}
|
||||
|
|
@ -440,6 +440,7 @@ func (r *databaseResource) Delete(ctx context.Context, req resource.DeleteReques
|
|||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
resp.State.RemoveResource(ctx)
|
||||
|
||||
tflog.Info(ctx, "sqlserverflexbeta.Database deleted")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -165,7 +165,7 @@ func mapFieldsCreate(userResp *sqlserverflexbeta.CreateUserResponse, model *reso
|
|||
// toCreatePayload converts a resourceModel to an API CreateUserRequestPayload.
|
||||
func toCreatePayload(
|
||||
model *resourceModel,
|
||||
roles []sqlserverflexbeta.UserRole,
|
||||
roles []string,
|
||||
) (*sqlserverflexbeta.CreateUserRequestPayload, error) {
|
||||
if model == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
|
|
|
|||
|
|
@ -177,7 +177,7 @@ func (r *userResource) Create(
|
|||
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
|
||||
var roles []sqlserverflexbeta.UserRole
|
||||
var roles []string
|
||||
if !model.Roles.IsNull() && !model.Roles.IsUnknown() {
|
||||
diags = model.Roles.ElementsAs(ctx, &roles, false)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
|
|
@ -292,6 +292,18 @@ func (r *userResource) Read(
|
|||
return
|
||||
}
|
||||
|
||||
// Set data returned by API in identity
|
||||
identity := UserResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
InstanceID: types.StringValue(instanceId),
|
||||
UserID: types.Int64Value(userId),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Set refreshed state
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
|
|
@ -350,6 +362,8 @@ func (r *userResource) Delete(
|
|||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
resp.State.RemoveResource(ctx)
|
||||
|
||||
tflog.Info(ctx, "SQLServer Flex user deleted")
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue