[go: nahoru, domu]

Skip to content

Commit

Permalink
api: add fixes in namespace service and routes return errors
Browse files Browse the repository at this point in the history
Co-authored-by: Vagner Saller <vsaller@yahoo.com>
  • Loading branch information
2 people authored and otavio committed Jul 6, 2021
1 parent fe6d9c4 commit 9f75c72
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 46 deletions.
14 changes: 14 additions & 0 deletions api/nsadm/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package nsadm

import (
"errors"
)

var (
ErrUnauthorized = errors.New("unauthorized")
ErrUserNotFound = errors.New("user not found")
ErrNamespaceNotFound = errors.New("namespace not found")
ErrDuplicateID = errors.New("user already member of this namespace")
ErrConflictName = errors.New("this name already exists")
ErrInvalidFormat = errors.New("invalid name format")
)
19 changes: 5 additions & 14 deletions api/nsadm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"strings"

utils "github.com/shellhub-io/shellhub/api/pkg/namespace"
Expand All @@ -16,15 +15,6 @@ import (
"gopkg.in/go-playground/validator.v9"
)

var (
ErrUnauthorized = errors.New("unauthorized")
ErrUserNotFound = errors.New("user not found")
ErrNamespaceNotFound = errors.New("namespace not found")
ErrDuplicateID = errors.New("user already member of this namespace")
ErrConflictName = errors.New("this name already exists")
ErrInvalidFormat = errors.New("invalid name format")
)

type Service interface {
ListNamespaces(ctx context.Context, pagination paginator.Query, filterB64 string, export bool) ([]models.Namespace, int, error)
CreateNamespace(ctx context.Context, namespace *models.Namespace, ownerUsername string) (*models.Namespace, error)
Expand Down Expand Up @@ -114,7 +104,7 @@ func (s *service) GetNamespace(ctx context.Context, tenantID string) (*models.Na

func (s *service) DeleteNamespace(ctx context.Context, tenantID, ownerID string) error {
if err := utils.IsNamespaceOwner(ctx, s.store, tenantID, ownerID); err != nil {
return ErrUnauthorized
return err
}

return s.store.NamespaceDelete(ctx, tenantID)
Expand Down Expand Up @@ -150,7 +140,7 @@ func (s *service) ListMembers(ctx context.Context, tenantID string) ([]models.Me

func (s *service) EditNamespace(ctx context.Context, tenantID, name, owner string) (*models.Namespace, error) {
if err := utils.IsNamespaceOwner(ctx, s.store, tenantID, owner); err != nil {
return nil, ErrUnauthorized
return nil, err
}

ns, err := s.store.NamespaceGet(ctx, tenantID)
Expand All @@ -173,7 +163,7 @@ func (s *service) EditNamespace(ctx context.Context, tenantID, name, owner strin

func (s *service) AddNamespaceUser(ctx context.Context, tenantID, username, ownerID string) (*models.Namespace, error) {
if err := utils.IsNamespaceOwner(ctx, s.store, tenantID, ownerID); err != nil {
return nil, ErrUnauthorized
return nil, err
}

user, err := s.store.UserGetByUsername(ctx, username)
Expand All @@ -190,8 +180,9 @@ func (s *service) AddNamespaceUser(ctx context.Context, tenantID, username, owne

func (s *service) RemoveNamespaceUser(ctx context.Context, tenantID, username, ownerID string) (*models.Namespace, error) {
if err := utils.IsNamespaceOwner(ctx, s.store, tenantID, ownerID); err != nil {
return nil, ErrUnauthorized
return nil, err
}

user, err := s.store.UserGetByUsername(ctx, username)
if err == store.ErrNoDocuments {
return nil, ErrUserNotFound
Expand Down
62 changes: 30 additions & 32 deletions api/routes/nsadm.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func CreateNamespace(c apicontext.Context) error {
return c.NoContent(http.StatusForbidden)
case nsadm.ErrConflictName:
return c.NoContent(http.StatusConflict)
case nsadm.ErrInvalidFormat:
return c.NoContent(http.StatusBadRequest)
default:
return err
}
Expand Down Expand Up @@ -109,16 +111,16 @@ func DeleteNamespace(c apicontext.Context) error {
id = v.ID
}

if err := svc.DeleteNamespace(c.Ctx(), c.Param("id"), id); err != nil {
if err == nsadm.ErrUnauthorized {
err := svc.DeleteNamespace(c.Ctx(), c.Param("id"), id)
if err != nil {
switch err {
case nsadm.ErrUnauthorized:
return c.NoContent(http.StatusForbidden)
}

if err == nsadm.ErrNamespaceNotFound {
case nsadm.ErrNamespaceNotFound:
return c.String(http.StatusNotFound, err.Error())
default:
return err
}

return err
}

return nil
Expand All @@ -142,14 +144,16 @@ func EditNamespace(c apicontext.Context) error {

namespace, err := svc.EditNamespace(c.Ctx(), c.Param("id"), req.Name, id)
if err != nil {
if err == nsadm.ErrUnauthorized {
switch err {
case nsadm.ErrInvalidFormat:
return c.NoContent(http.StatusBadRequest)
case nsadm.ErrUnauthorized:
return c.NoContent(http.StatusForbidden)
}
if err == nsadm.ErrNamespaceNotFound {
case nsadm.ErrNamespaceNotFound:
return c.String(http.StatusNotFound, err.Error())
default:
return err
}

return err
}

return c.JSON(http.StatusOK, namespace)
Expand All @@ -173,21 +177,18 @@ func AddNamespaceUser(c apicontext.Context) error {

namespace, err := svc.AddNamespaceUser(c.Ctx(), c.Param("id"), req.Username, id)
if err != nil {
if err == nsadm.ErrUnauthorized {
switch err {
case nsadm.ErrUnauthorized:
return c.NoContent(http.StatusForbidden)
}
if err == nsadm.ErrUserNotFound {
case nsadm.ErrUserNotFound:
return c.String(http.StatusNotFound, err.Error())
}
if err == nsadm.ErrNamespaceNotFound {
case nsadm.ErrNamespaceNotFound:
return c.String(http.StatusNotFound, err.Error())
}

if err == nsadm.ErrDuplicateID {
case nsadm.ErrDuplicateID:
return c.String(http.StatusConflict, err.Error())
default:
return err
}

return err
}

return c.JSON(http.StatusOK, namespace)
Expand All @@ -210,21 +211,18 @@ func RemoveNamespaceUser(c apicontext.Context) error {
}
namespace, err := svc.RemoveNamespaceUser(c.Ctx(), c.Param("id"), req.Username, id)
if err != nil {
if err == nsadm.ErrUnauthorized {
switch err {
case nsadm.ErrUnauthorized:
return c.NoContent(http.StatusForbidden)
}
if err == nsadm.ErrUserNotFound {
case nsadm.ErrNamespaceNotFound:
return c.String(http.StatusNotFound, err.Error())
}
if err == nsadm.ErrNamespaceNotFound {
case nsadm.ErrUserNotFound:
return c.String(http.StatusNotFound, err.Error())
}

if err == nsadm.ErrDuplicateID {
case nsadm.ErrDuplicateID:
return c.String(http.StatusConflict, err.Error())
default:
return err
}

return err
}

return c.JSON(http.StatusOK, namespace)
Expand Down

0 comments on commit 9f75c72

Please sign in to comment.