From 5ea2cd57bba8725c7a0f866eeab8609ec9c5d262 Mon Sep 17 00:00:00 2001 From: Mike Mason Date: Mon, 7 Oct 2024 08:59:43 -0500 Subject: [PATCH] add missing fields and error handling (#293) This adds missing fields to the role list endpoint as well as corrects some error handling. Signed-off-by: Mike Mason --- internal/api/relationships.go | 10 +++++++++- internal/api/roles_v2.go | 11 ++++++++--- internal/api/router.go | 7 +++++++ internal/api/types.go | 12 +++++++++--- internal/query/roles_v2.go | 22 ++++++++++++++++------ 5 files changed, 49 insertions(+), 13 deletions(-) diff --git a/internal/api/relationships.go b/internal/api/relationships.go index 71709a50..d4ef1c02 100644 --- a/internal/api/relationships.go +++ b/internal/api/relationships.go @@ -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 { @@ -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) } diff --git a/internal/api/roles_v2.go b/internal/api/roles_v2.go index 5e1f05ee..61254ae4 100644 --- a/internal/api/roles_v2.go +++ b/internal/api/roles_v2.go @@ -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) diff --git a/internal/api/router.go b/internal/api/router.go index c2242800..9be0f941 100644 --- a/internal/api/router.go +++ b/internal/api/router.go @@ -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" @@ -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. diff --git a/internal/api/types.go b/internal/api/types.go index dd44dc46..abec3855 100644 --- a/internal/api/types.go +++ b/internal/api/types.go @@ -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 diff --git a/internal/query/roles_v2.go b/internal/query/roles_v2.go index a9f4dc74..dbae851b 100644 --- a/internal/query/roles_v2.go +++ b/internal/query/roles_v2.go @@ -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, } } @@ -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, }) }