Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ishanarya0 committed Jun 15, 2024
1 parent f4cf2ac commit e259c45
Show file tree
Hide file tree
Showing 18 changed files with 90 additions and 100 deletions.
4 changes: 3 additions & 1 deletion core/group/filter.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package group

type Filter struct {
OrganizationID string
OrganizationID string
Project string
ServicedataKeyResourceIds []string
}
2 changes: 1 addition & 1 deletion core/group/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Repository interface {
GetByID(ctx context.Context, id string) (Group, error)
GetByIDs(ctx context.Context, groupIDs []string) ([]Group, error)
GetBySlug(ctx context.Context, slug string) (Group, error)
List(ctx context.Context, flt Filter, project string, serviceDataKeyResourceIds []string) ([]Group, error)
List(ctx context.Context, flt Filter) ([]Group, error)
UpdateByID(ctx context.Context, toUpdate Group) (Group, error)
UpdateBySlug(ctx context.Context, toUpdate Group) (Group, error)
ListUserGroups(ctx context.Context, userId string, roleId string) ([]Group, error)
Expand Down
4 changes: 2 additions & 2 deletions core/group/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ func (s Service) GetByIDs(ctx context.Context, groupIDs []string) ([]Group, erro
return s.repository.GetByIDs(ctx, groupIDs)
}

func (s Service) List(ctx context.Context, flt Filter, project string, servicedataKeyResourceIds []string) ([]Group, error) {
return s.repository.List(ctx, flt, project, servicedataKeyResourceIds)
func (s Service) List(ctx context.Context, flt Filter) ([]Group, error) {
return s.repository.List(ctx, flt)
}

func (s Service) Update(ctx context.Context, grp Group) (Group, error) {
Expand Down
4 changes: 2 additions & 2 deletions core/servicedata/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewService(repository Repository, resourceService ResourceService, relation

func (s Service) CreateKey(ctx context.Context, key Key) (Key, error) {
// check if key contains ':'
if key.Key == "" {
if key.Name == "" {
return Key{}, ErrInvalidDetail
}

Expand Down Expand Up @@ -138,7 +138,7 @@ func (s Service) CreateKey(ctx context.Context, key Key) (Key, error) {
}

func (s Service) Upsert(ctx context.Context, sd ServiceData) (ServiceData, error) {
if sd.Key.Key == "" {
if sd.Key.Name == "" {
return ServiceData{}, ErrInvalidDetail
}

Expand Down
4 changes: 2 additions & 2 deletions core/servicedata/servicedata.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Key struct {
URN string
ProjectID string
ProjectSlug string
Key string
Name string
Description string
ResourceID string
}
Expand All @@ -46,5 +46,5 @@ type Filter struct {
}

func (key Key) CreateURN() string {
return fmt.Sprintf("%s:servicedata_key:%s", key.ProjectSlug, key.Key)
return fmt.Sprintf("%s:servicedata_key:%s", key.ProjectSlug, key.Name)
}
8 changes: 5 additions & 3 deletions core/user/filter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package user

type Filter struct {
Limit int32
Page int32
Keyword string
Limit int32
Page int32
Keyword string
Project string
ServiceDataKeyResourceIds []string
}
4 changes: 2 additions & 2 deletions core/user/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ func (s Service) CreateMetadataKey(ctx context.Context, key UserMetadataKey) (Us
return newUserMetadataKey, nil
}

func (s Service) List(ctx context.Context, flt Filter, project string, serviceDataKeyResourceIds []string) (PagedUsers, error) {
users, err := s.repository.List(ctx, flt, project, serviceDataKeyResourceIds)
func (s Service) List(ctx context.Context, flt Filter) (PagedUsers, error) {
users, err := s.repository.List(ctx, flt)
if err != nil {
return PagedUsers{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion core/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Repository interface {
GetByEmail(ctx context.Context, email string) (User, error)
GetByIDs(ctx context.Context, userIds []string) ([]User, error)
Create(ctx context.Context, user User) (User, error)
List(ctx context.Context, flt Filter, project string, serviceDataKeysResourceIds []string) ([]User, error)
List(ctx context.Context, flt Filter) ([]User, error)
UpdateByID(ctx context.Context, toUpdate User) (User, error)
UpdateByEmail(ctx context.Context, toUpdate User) (User, error)
CreateMetadataKey(ctx context.Context, key UserMetadataKey) (UserMetadataKey, error)
Expand Down
18 changes: 10 additions & 8 deletions internal/api/v1beta1/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
type GroupService interface {
Create(ctx context.Context, grp group.Group) (group.Group, error)
Get(ctx context.Context, id string) (group.Group, error)
List(ctx context.Context, flt group.Filter, projectId string, servicedataKeyResourceIds []string) ([]group.Group, error)
List(ctx context.Context, flt group.Filter) ([]group.Group, error)
Update(ctx context.Context, grp group.Group) (group.Group, error)
ListUserGroups(ctx context.Context, userId string, roleId string) ([]group.Group, error)
ListGroupRelations(ctx context.Context, objectId, subjectType, role string) ([]user.User, []group.Group, map[string][]string, map[string][]string, error)
Expand Down Expand Up @@ -64,8 +64,10 @@ func (h Handler) ListGroups(ctx context.Context, request *shieldv1beta1.ListGrou
}

groupList, err := h.groupService.List(ctx, group.Filter{
OrganizationID: request.GetOrgId(),
}, prj.ID, servicedataKeyResourceIds)
OrganizationID: request.GetOrgId(),
Project: prj.ID,
ServicedataKeyResourceIds: servicedataKeyResourceIds,
})
if err != nil {
logger.Error(err.Error())
return nil, grpcInternalServerError
Expand Down Expand Up @@ -141,7 +143,7 @@ func (h Handler) CreateGroup(ctx context.Context, request *shieldv1beta1.CreateG
EntityID: newGroup.ID,
NamespaceID: groupNamespaceID,
Key: servicedata.Key{
Key: k,
Name: k,
ProjectID: h.serviceDataConfig.DefaultServiceDataProject,
},
Value: v,
Expand All @@ -161,7 +163,7 @@ func (h Handler) CreateGroup(ctx context.Context, request *shieldv1beta1.CreateG
return nil, grpcInternalServerError
}
}
serviceDataMap[serviceDataResp.Key.Key] = serviceDataResp.Value
serviceDataMap[serviceDataResp.Key.Name] = serviceDataResp.Value
}

newGroup.Metadata = metaDataMap
Expand Down Expand Up @@ -209,7 +211,7 @@ func (h Handler) GetGroup(ctx context.Context, request *shieldv1beta1.GetGroupRe
} else {
metadata := map[string]any{}
for _, sd := range groupSD {
metadata[sd.Key.Key] = sd.Value
metadata[sd.Key.Name] = sd.Value
}
fetchedGroup.Metadata = metadata
}
Expand Down Expand Up @@ -279,7 +281,7 @@ func (h Handler) UpdateGroup(ctx context.Context, request *shieldv1beta1.UpdateG
EntityID: updatedGroup.ID,
NamespaceID: groupNamespaceID,
Key: servicedata.Key{
Key: k,
Name: k,
ProjectID: h.serviceDataConfig.DefaultServiceDataProject,
},
Value: v,
Expand All @@ -299,7 +301,7 @@ func (h Handler) UpdateGroup(ctx context.Context, request *shieldv1beta1.UpdateG
return nil, grpcInternalServerError
}
}
serviceDataMap[serviceDataResp.Key.Key] = serviceDataResp.Value
serviceDataMap[serviceDataResp.Key.Name] = serviceDataResp.Value
}

//Note: this would return only the keys that are updated in the current request
Expand Down
66 changes: 26 additions & 40 deletions internal/api/v1beta1/servicedata.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/goto/shield/core/servicedata"
"github.com/goto/shield/core/user"
"github.com/goto/shield/internal/schema"
"github.com/goto/shield/pkg/metadata"
shieldv1beta1 "github.com/goto/shield/proto/v1beta1"
grpczap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"golang.org/x/exp/maps"
Expand Down Expand Up @@ -46,7 +45,7 @@ func (h Handler) CreateServiceDataKey(ctx context.Context, request *shieldv1beta

keyResp, err := h.serviceDataService.CreateKey(ctx, servicedata.Key{
ProjectID: requestBody.GetProject(),
Key: requestBody.GetKey(),
Name: requestBody.GetKey(),
Description: requestBody.GetDescription(),
})
if err != nil {
Expand Down Expand Up @@ -88,10 +87,11 @@ func (h Handler) UpsertUserServiceData(ctx context.Context, request *shieldv1bet
return nil, grpcBadBodyError
}

sdMap, err := metadata.Build(request.GetBody().GetData().AsMap())
if err != nil {
data := requestBody.GetData()
if data == nil {
return nil, grpcBadBodyError
}
sdMap := data.AsMap()

if len(sdMap) > h.serviceDataConfig.MaxUpsert {
return nil, grpcBadBodyError
Expand All @@ -116,7 +116,7 @@ func (h Handler) UpsertUserServiceData(ctx context.Context, request *shieldv1bet
EntityID: userEntity.ID,
NamespaceID: userNamespaceID,
Key: servicedata.Key{
Key: k,
Name: k,
ProjectID: requestBody.Project,
},
Value: v,
Expand All @@ -134,11 +134,16 @@ func (h Handler) UpsertUserServiceData(ctx context.Context, request *shieldv1bet
return nil, grpcInternalServerError
}
}
serviceDataMap[serviceDataResp.Key.Key] = serviceDataResp.Value
serviceDataMap[serviceDataResp.Key.Name] = serviceDataResp.Value
}

serviceDataMapPB, err := structpb.NewStruct(serviceDataMap)
if err != nil {
return nil, grpcInternalServerError
}

return &shieldv1beta1.UpsertUserServiceDataResponse{
Data: requestBody.GetData(),
Data: serviceDataMapPB,
}, nil
}

Expand All @@ -154,10 +159,11 @@ func (h Handler) UpsertGroupServiceData(ctx context.Context, request *shieldv1be
return nil, grpcBadBodyError
}

sdMap, err := Build(request.GetBody().GetData().AsMap())
if err != nil {
data := requestBody.GetData()
if data == nil {
return nil, grpcBadBodyError
}
sdMap := data.AsMap()

if len(sdMap) > h.serviceDataConfig.MaxUpsert {
return nil, grpcBadBodyError
Expand All @@ -182,7 +188,7 @@ func (h Handler) UpsertGroupServiceData(ctx context.Context, request *shieldv1be
EntityID: groupEntity.ID,
NamespaceID: groupNamespaceID,
Key: servicedata.Key{
Key: k,
Name: k,
ProjectID: requestBody.Project,
},
Value: v,
Expand All @@ -200,11 +206,16 @@ func (h Handler) UpsertGroupServiceData(ctx context.Context, request *shieldv1be
return nil, grpcInternalServerError
}
}
serviceDataMap[serviceDataResp.Key.Key] = serviceDataResp.Value
serviceDataMap[serviceDataResp.Key.Name] = serviceDataResp.Value
}

serviceDataMapPB, err := structpb.NewStruct(serviceDataMap)
if err != nil {
return nil, grpcInternalServerError
}

return &shieldv1beta1.UpsertGroupServiceDataResponse{
Data: requestBody.GetData(),
Data: serviceDataMapPB,
}, nil
}

Expand Down Expand Up @@ -333,14 +344,14 @@ func transformServiceDataListToPB(from []servicedata.ServiceData) (*structpb.Str
if ok {
ent, ok := prj[entKey]
if ok {
ent[sd.Key.Key] = sd.Value
ent[sd.Key.Name] = sd.Value
} else {
prj[entKey] = map[string]any{
sd.Key.Key: sd.Value,
sd.Key.Name: sd.Value,
}
}
} else {
kv := map[string]any{sd.Key.Key: sd.Value}
kv := map[string]any{sd.Key.Name: sd.Value}
data[prjKey] = map[string]map[string]any{
entKey: kv,
}
Expand All @@ -364,28 +375,3 @@ func transformServiceDataListToPB(from []servicedata.ServiceData) (*structpb.Str

return serviceData, nil
}

func ToStructPB(m map[string]any) (*structpb.Struct, error) {
newMap := make(map[string]interface{})

for key, value := range m {
newMap[key] = value
}

return structpb.NewStruct(newMap)
}

func Build(m map[string]interface{}) (map[string]any, error) {
newMap := make(map[string]any)

for key, value := range m {
switch value := value.(type) {
case any:
newMap[key] = value
default:
return map[string]any{}, fmt.Errorf("value for %s key is not string", key)
}
}

return newMap, nil
}
22 changes: 12 additions & 10 deletions internal/api/v1beta1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type UserService interface {
GetByIDs(ctx context.Context, userIDs []string) ([]user.User, error)
GetByEmail(ctx context.Context, email string) (user.User, error)
Create(ctx context.Context, user user.User) (user.User, error)
List(ctx context.Context, flt user.Filter, project string, serviceDataKeyResourceIds []string) (user.PagedUsers, error)
List(ctx context.Context, flt user.Filter) (user.PagedUsers, error)
UpdateByID(ctx context.Context, toUpdate user.User) (user.User, error)
UpdateByEmail(ctx context.Context, toUpdate user.User) (user.User, error)
FetchCurrentUser(ctx context.Context) (user.User, error)
Expand Down Expand Up @@ -63,10 +63,12 @@ func (h Handler) ListUsers(ctx context.Context, request *shieldv1beta1.ListUsers
}

userResp, err := h.userService.List(ctx, user.Filter{
Limit: request.GetPageSize(),
Page: request.GetPageNum(),
Keyword: request.GetKeyword(),
}, prj.ID, servicedataKeyResourceIds)
Limit: request.GetPageSize(),
Page: request.GetPageNum(),
Keyword: request.GetKeyword(),
Project: prj.ID,
ServiceDataKeyResourceIds: servicedataKeyResourceIds,
})
if err != nil {
logger.Error(err.Error())
return nil, grpcInternalServerError
Expand Down Expand Up @@ -149,7 +151,7 @@ func (h Handler) CreateUser(ctx context.Context, request *shieldv1beta1.CreateUs
EntityID: newUser.ID,
NamespaceID: userNamespaceID,
Key: servicedata.Key{
Key: k,
Name: k,
ProjectID: h.serviceDataConfig.DefaultServiceDataProject,
},
Value: v,
Expand All @@ -169,7 +171,7 @@ func (h Handler) CreateUser(ctx context.Context, request *shieldv1beta1.CreateUs
return nil, grpcInternalServerError
}
}
serviceDataMap[serviceDataResp.Key.Key] = serviceDataResp.Value
serviceDataMap[serviceDataResp.Key.Name] = serviceDataResp.Value
}

newUser.Metadata = metaDataMap
Expand Down Expand Up @@ -247,7 +249,7 @@ func (h Handler) GetUser(ctx context.Context, request *shieldv1beta1.GetUserRequ
} else {
metadata := map[string]any{}
for _, sd := range userSD {
metadata[sd.Key.Key] = sd.Value
metadata[sd.Key.Name] = sd.Value
}
fetchedUser.Metadata = metadata
}
Expand Down Expand Up @@ -392,7 +394,7 @@ func (h Handler) UpdateUser(ctx context.Context, request *shieldv1beta1.UpdateUs
EntityID: updatedUser.ID,
NamespaceID: userNamespaceID,
Key: servicedata.Key{
Key: k,
Name: k,
ProjectID: h.serviceDataConfig.DefaultServiceDataProject,
},
Value: v,
Expand All @@ -412,7 +414,7 @@ func (h Handler) UpdateUser(ctx context.Context, request *shieldv1beta1.UpdateUs
return nil, grpcInternalServerError
}
}
serviceDataMap[serviceDataResp.Key.Key] = serviceDataResp.Value
serviceDataMap[serviceDataResp.Key.Name] = serviceDataResp.Value
}

//Note: this would return only the keys that are updated in the current request
Expand Down
Loading

0 comments on commit e259c45

Please sign in to comment.