Skip to content

Commit

Permalink
Service Account Resource: improve handling of diagnostics object (#181)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonathan Lacefield <jlacefie@gmail.com>
  • Loading branch information
briankassouf and jlacefie authored Nov 18, 2024
1 parent 4bc8751 commit eb294b3
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions internal/provider/service_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ func (r *serviceAccountResource) Create(ctx context.Context, req resource.Create
ctx, cancel := context.WithTimeout(ctx, createTimeout)
defer cancel()

namespaceAccesses := getNamespaceAccessesFromServiceAccountModel(ctx, resp.Diagnostics, &plan)
namespaceAccesses, d := getNamespaceAccessesFromServiceAccountModel(ctx, &plan)
resp.Diagnostics.Append(d...)
if resp.Diagnostics.HasError() {
return
}
Expand Down Expand Up @@ -240,7 +241,8 @@ func (r *serviceAccountResource) Update(ctx context.Context, req resource.Update
return
}

namespaceAccesses := getNamespaceAccessesFromServiceAccountModel(ctx, resp.Diagnostics, &plan)
namespaceAccesses, d := getNamespaceAccessesFromServiceAccountModel(ctx, &plan)
resp.Diagnostics.Append(d...)
if resp.Diagnostics.HasError() {
return
}
Expand Down Expand Up @@ -338,35 +340,36 @@ func (r *serviceAccountResource) ImportState(ctx context.Context, req resource.I
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

func getNamespaceAccessesFromServiceAccountModel(ctx context.Context, diags diag.Diagnostics, model *serviceAccountResourceModel) map[string]*identityv1.NamespaceAccess {
func getNamespaceAccessesFromServiceAccountModel(ctx context.Context, model *serviceAccountResourceModel) (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 serviceAccountNamespaceAccessModel
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 access permission", err.Error())
return nil
return nil, diags
}
namespaceAccesses[model.NamespaceID.ValueString()] = &identityv1.NamespaceAccess{
Permission: persmission,
}
}

return namespaceAccesses
return namespaceAccesses, diags
}

func updateServiceAccountModelFromSpec(ctx context.Context, state *serviceAccountResourceModel, serviceAccount *identityv1.ServiceAccount) diag.Diagnostics {
Expand Down

0 comments on commit eb294b3

Please sign in to comment.