Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Resource: use the correct object attributes when building the namespace access list #177

Merged
merged 8 commits into from
Nov 20, 2024
55 changes: 37 additions & 18 deletions internal/provider/user_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ func (r *userResource) Create(ctx context.Context, req resource.CreateRequest, r
ctx, cancel := context.WithTimeout(ctx, createTimeout)
defer cancel()

namespaceAccesses := getNamespaceAccessesFromModel(ctx, resp.Diagnostics, &plan)
namespaceAccesses, d := getNamespaceAccessesFromModel(ctx, &plan)
resp.Diagnostics.Append(d...)
if resp.Diagnostics.HasError() {
return
}
Expand Down Expand Up @@ -204,7 +205,11 @@ func (r *userResource) Create(ctx context.Context, req resource.CreateRequest, r
return
}

updateUserModelFromSpec(ctx, resp.Diagnostics, &plan, user.User)
resp.Diagnostics.Append(updateUserModelFromSpec(ctx, &plan, user.User)...)
if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)
}

Expand All @@ -223,7 +228,11 @@ func (r *userResource) Read(ctx context.Context, req resource.ReadRequest, resp
return
}

updateUserModelFromSpec(ctx, resp.Diagnostics, &state, user.User)
resp.Diagnostics.Append(updateUserModelFromSpec(ctx, &state, user.User)...)
if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}

Expand All @@ -234,7 +243,8 @@ func (r *userResource) Update(ctx context.Context, req resource.UpdateRequest, r
return
}

namespaceAccesses := getNamespaceAccessesFromModel(ctx, resp.Diagnostics, &plan)
namespaceAccesses, d := getNamespaceAccessesFromModel(ctx, &plan)
resp.Diagnostics.Append(d...)
if resp.Diagnostics.HasError() {
return
}
Expand Down Expand Up @@ -283,7 +293,11 @@ func (r *userResource) Update(ctx context.Context, req resource.UpdateRequest, r
return
}

updateUserModelFromSpec(ctx, resp.Diagnostics, &plan, user.User)
resp.Diagnostics.Append(updateUserModelFromSpec(ctx, &plan, user.User)...)
if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)
}

Expand Down Expand Up @@ -329,50 +343,53 @@ func (r *userResource) ImportState(ctx context.Context, req resource.ImportState
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

func getNamespaceAccessesFromModel(ctx context.Context, diags diag.Diagnostics, model *userResourceModel) map[string]*identityv1.NamespaceAccess {
func getNamespaceAccessesFromModel(ctx context.Context, model *userResourceModel) (map[string]*identityv1.NamespaceAccess, diag.Diagnostics) {
var diags diag.Diagnostics

elements := make([]types.Object, 0, len(model.NamespaceAccesses.Elements()))
diags.Append(model.NamespaceAccesses.ElementsAs(ctx, &elements, false)...)
if diags.HasError() {
return nil
return nil, diags
}

if len(elements) == 0 {
return nil
return nil, diags
}

namespaceAccesses := make(map[string]*identityv1.NamespaceAccess, len(elements))
for _, access := range elements {
var model userNamespaceAccessModel
diags.Append(access.As(ctx, &model, basetypes.ObjectAsOptions{})...)
if diags.HasError() {
return nil
return nil, diags
}
persmission, err := enums.ToNamespaceAccessPermission(model.Permission.ValueString())
if err != nil {
diags.AddError("Failed to convert namespace permission", err.Error())
return nil
return nil, diags
}
namespaceAccesses[model.NamespaceID.ValueString()] = &identityv1.NamespaceAccess{
Permission: persmission,
}
}

return namespaceAccesses
return namespaceAccesses, diags
}

func updateUserModelFromSpec(ctx context.Context, diags diag.Diagnostics, state *userResourceModel, user *identityv1.User) {
func updateUserModelFromSpec(ctx context.Context, state *userResourceModel, user *identityv1.User) diag.Diagnostics {
var diags diag.Diagnostics
state.ID = types.StringValue(user.GetId())
stateStr, err := enums.FromResourceState(user.GetState())
if err != nil {
diags.AddError("Failed to convert resource state", err.Error())
return
return diags
}
state.State = types.StringValue(stateStr)
state.Email = types.StringValue(user.GetSpec().GetEmail())
role, err := enums.FromAccountAccessRole(user.GetSpec().GetAccess().GetAccountAccess().GetRole())
if err != nil {
diags.AddError("Failed to convert account access role", err.Error())
return
return diags
}
state.AccountAccess = internaltypes.CaseInsensitiveString(role)

Expand All @@ -383,7 +400,7 @@ func updateUserModelFromSpec(ctx context.Context, diags diag.Diagnostics, state
permission, err := enums.FromNamespaceAccessPermission(namespaceAccess.GetPermission())
if err != nil {
diags.AddError("Failed to convert namespace access permission", err.Error())
return
return diags
}
model := userNamespaceAccessModel{
NamespaceID: types.StringValue(ns),
Expand All @@ -392,18 +409,20 @@ func updateUserModelFromSpec(ctx context.Context, diags diag.Diagnostics, state
obj, d := types.ObjectValueFrom(ctx, userNamespaceAccessAttrs, model)
diags.Append(d...)
if diags.HasError() {
return
return diags
}
namespaceAccessObjects = append(namespaceAccessObjects, obj)
}

accesses, d := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: namespaceCertificateFilterAttrs}, namespaceAccessObjects)
accesses, d := types.ListValueFrom(ctx, types.ObjectType{AttrTypes: userNamespaceAccessAttrs}, namespaceAccessObjects)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was using the wrong attribute type to build the new list

diags.Append(d...)
if diags.HasError() {
return
return diags
}

namespaceAccesses = accesses
}
state.NamespaceAccesses = namespaceAccesses

return diags
}
Loading