Skip to content

Commit

Permalink
add missing fields and error handling (#293)
Browse files Browse the repository at this point in the history
This adds missing fields to the role list endpoint as well as corrects some error handling.

Signed-off-by: Mike Mason <[email protected]>
  • Loading branch information
mikemrm authored Oct 7, 2024
1 parent 10238a4 commit 5ea2cd5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
10 changes: 9 additions & 1 deletion internal/api/relationships.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package api

import (
"errors"
"net/http"

"github.com/labstack/echo/v4"
"go.infratographer.com/x/gidx"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"

"go.infratographer.com/permissions-api/internal/query"
)

func (r *Router) relationshipListFrom(c echo.Context) error {
Expand Down Expand Up @@ -63,7 +66,12 @@ func (r *Router) relationshipListTo(c echo.Context) error {
}

rels, err := r.engine.ListRelationshipsTo(ctx, resource)
if err != nil {

switch {
case err == nil:
case errors.Is(err, query.ErrInvalidType):
return echo.NewHTTPError(http.StatusBadRequest, "resource doesn't support relationships")
default:
return echo.NewHTTPError(http.StatusInternalServerError, "error listing relationships").SetInternal(err)
}

Expand Down
11 changes: 8 additions & 3 deletions internal/api/roles_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,14 @@ func (r *Router) roleV2sList(c echo.Context) error {

for _, role := range roles {
roleResp := listRolesV2Role{
ID: role.ID,
Name: role.Name,
Manager: role.Manager,
ID: role.ID,
Name: role.Name,
Manager: role.Manager,
ResourceID: role.ResourceID,
CreatedBy: role.CreatedBy,
UpdatedBy: role.UpdatedBy,
CreatedAt: role.CreatedAt.Format(time.RFC3339),
UpdatedAt: role.UpdatedAt.Format(time.RFC3339),
}

resp.Data = append(resp.Data, roleResp)
Expand Down
7 changes: 7 additions & 0 deletions internal/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"go.infratographer.com/x/gidx"
"go.opentelemetry.io/otel"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"go.infratographer.com/permissions-api/internal/query"
"go.infratographer.com/permissions-api/internal/types"
Expand Down Expand Up @@ -116,6 +118,11 @@ func errorMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
checkErr = eerr.Internal
}

// GRPC returns it's own canceled context status. Here we convert it so we may use the same logic.
if grpcStatus, ok := status.FromError(checkErr); ok && grpcStatus.Code() == codes.Canceled {
checkErr = context.Canceled
}

switch {
// Only if the error is a context canceled error and the request context has been canceled.
// If the request was not canceled, then the context canceled error probably came from the service.
Expand Down
12 changes: 9 additions & 3 deletions internal/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,15 @@ type listRolesV2Response struct {
}

type listRolesV2Role struct {
ID gidx.PrefixedID `json:"id"`
Name string `json:"name"`
Manager string `json:"manager"`
ID gidx.PrefixedID `json:"id"`
Name string `json:"name"`
Manager string `json:"manager"`
ResourceID gidx.PrefixedID `json:"resource_id"`

CreatedBy gidx.PrefixedID `json:"created_by"`
UpdatedBy gidx.PrefixedID `json:"updated_by"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}

// RoleBindings
Expand Down
22 changes: 16 additions & 6 deletions internal/query/roles_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,14 @@ func (e *engine) ListRolesV2(ctx context.Context, owner types.Resource) ([]types

for i, r := range storageRoles {
roles[i] = types.Role{
Name: r.Name,
ID: r.ID,
Manager: r.Manager,
Name: r.Name,
ID: r.ID,
Manager: r.Manager,
ResourceID: r.ResourceID,
CreatedBy: r.CreatedBy,
UpdatedBy: r.UpdatedBy,
CreatedAt: r.CreatedAt,
UpdatedAt: r.UpdatedAt,
}
}

Expand Down Expand Up @@ -251,9 +256,14 @@ func (e *engine) ListManagerRolesV2(ctx context.Context, manager string, owner t
}

roles = append(roles, types.Role{
Name: r.Name,
Manager: r.Manager,
ID: r.ID,
Name: r.Name,
Manager: r.Manager,
ID: r.ID,
ResourceID: r.ResourceID,
CreatedBy: r.CreatedBy,
UpdatedBy: r.UpdatedBy,
CreatedAt: r.CreatedAt,
UpdatedAt: r.UpdatedAt,
})
}

Expand Down

0 comments on commit 5ea2cd5

Please sign in to comment.