Skip to content

Commit

Permalink
fix: create service data (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
FemiNoviaLina authored May 22, 2024
1 parent 06e1209 commit 2d4ed1f
Show file tree
Hide file tree
Showing 16 changed files with 356 additions and 109 deletions.
1 change: 1 addition & 0 deletions core/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
type Repository interface {
GetByID(ctx context.Context, id string) (Resource, error)
GetByURN(ctx context.Context, urn string) (Resource, error)
Upsert(ctx context.Context, resource Resource) (Resource, error)
Create(ctx context.Context, resource Resource) (Resource, error)
List(ctx context.Context, flt Filter) ([]Resource, error)
Update(ctx context.Context, id string, resource Resource) (Resource, error)
Expand Down
58 changes: 58 additions & 0 deletions core/resource/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,64 @@ func (s Service) Get(ctx context.Context, id string) (Resource, error) {
return s.repository.GetByID(ctx, id)
}

func (s Service) Upsert(ctx context.Context, res Resource) (Resource, error) {
currentUser, err := s.userService.FetchCurrentUser(ctx)
if err != nil {
return Resource{}, err
}

urn := res.CreateURN()

if err != nil {
return Resource{}, err
}

fetchedProject, err := s.projectService.Get(ctx, res.ProjectID)
if err != nil {
return Resource{}, err
}

userId := res.UserID
if strings.TrimSpace(userId) == "" {
userId = currentUser.ID
}

newResource, err := s.repository.Upsert(ctx, Resource{
URN: urn,
Name: res.Name,
OrganizationID: fetchedProject.Organization.ID,
ProjectID: fetchedProject.ID,
NamespaceID: res.NamespaceID,
UserID: userId,
})
if err != nil {
return Resource{}, err
}

if err = s.relationService.DeleteSubjectRelations(ctx, newResource.NamespaceID, newResource.Idxa); err != nil {
return Resource{}, err
}

if err = s.AddProjectToResource(ctx, project.Project{ID: res.ProjectID}, newResource); err != nil {
return Resource{}, err
}

if err = s.AddOrgToResource(ctx, organization.Organization{ID: newResource.OrganizationID}, newResource); err != nil {
return Resource{}, err
}

go func() {
ctx := pkgctx.WithoutCancel(ctx)
resourceLogData := newResource.ToResourceLogData()
actor := activity.Actor{ID: currentUser.ID, Email: currentUser.Email}
if err := s.activityService.Log(ctx, auditKeyResourceCreate, actor, resourceLogData); err != nil {
s.logger.Error(fmt.Sprintf("%s: %s", ErrLogActivity.Error(), err.Error()))
}
}()

return newResource, nil
}

func (s Service) Create(ctx context.Context, res Resource) (Resource, error) {
currentUser, err := s.userService.FetchCurrentUser(ctx)
if err != nil {
Expand Down
14 changes: 6 additions & 8 deletions core/servicedata/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/goto/shield/core/resource"
"github.com/goto/shield/core/user"
"github.com/goto/shield/internal/schema"
"github.com/goto/shield/pkg/uuid"
)

const keyNamespace = "shield/servicedata_key"
Expand Down Expand Up @@ -59,14 +58,13 @@ func (s Service) CreateKey(ctx context.Context, key Key) (Key, error) {
return Key{}, err
}

// convert project slug to project id
if !uuid.IsValid(key.ProjectID) {
project, err := s.projectService.Get(ctx, key.ProjectID)
if err != nil {
return Key{}, err
}
key.ProjectID = project.ID
// Get Project
project, err := s.projectService.Get(ctx, key.ProjectID)
if err != nil {
return Key{}, err
}
key.ProjectID = project.ID
key.ProjectSlug = project.Slug

// create URN
key.URN = key.CreateURN()
Expand Down
39 changes: 26 additions & 13 deletions core/servicedata/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,32 @@ import (
)

var (
testResourceID = "test-resource-id"
testUserID = "test-user-id"
testProjectID = "test-project-id"
testKey = servicedata.Key{
testResourceID = "test-resource-id"
testUserID = "test-user-id"
testProjectID = "test-project-id"
testProjectSlug = "test-project-slug"
testKey = servicedata.Key{
ProjectID: "test-project-slug",
Key: "test-key",
Description: "test key no 01",
}
testCreateKey = servicedata.Key{
URN: "test-project-slug:servicedata_key:test-key",
ProjectID: testProjectID,
ProjectSlug: testProjectSlug,
Key: "test-key",
Description: "test key no 01",
ResourceID: testResourceID,
}
testCreatedKey = servicedata.Key{
URN: "test-project-id:servicedata_key:test-key",
URN: "test-project-slug:servicedata_key:test-key",
ProjectID: testProjectID,
Key: "test-key",
Description: "test key no 01",
ResourceID: testResourceID,
}
testResource = resource.Resource{
Name: "test-project-id:servicedata_key:test-key",
Name: "test-project-slug:servicedata_key:test-key",
ProjectID: testProjectID,
NamespaceID: schema.ServiceDataKeyNamespace,
UserID: testUserID,
Expand Down Expand Up @@ -82,12 +91,13 @@ func TestService_CreateKey(t *testing.T) {
}, nil)
projectService.EXPECT().Get(mock.Anything, "test-project-slug").
Return(project.Project{
ID: testProjectID,
ID: testProjectID,
Slug: testProjectSlug,
}, nil)
resourceService.EXPECT().Create(mock.Anything, testResource).Return(resource.Resource{
Idxa: testResourceID,
}, nil)
repository.EXPECT().CreateKey(mock.Anything, testCreatedKey).Return(testCreatedKey, nil)
repository.EXPECT().CreateKey(mock.Anything, testCreateKey).Return(testCreatedKey, nil)
relationService.EXPECT().Create(mock.Anything, testRelation).Return(relation.RelationV2{}, nil)
return servicedata.NewService(repository, resourceService, relationService, projectService, userService)
},
Expand Down Expand Up @@ -183,7 +193,8 @@ func TestService_CreateKey(t *testing.T) {
}, nil)
projectService.EXPECT().Get(mock.Anything, "test-project-slug").
Return(project.Project{
ID: testProjectID,
ID: testProjectID,
Slug: testProjectSlug,
}, nil)
resourceService.EXPECT().Create(mock.Anything, testResource).Return(resource.Resource{}, resource.ErrConflict)
return servicedata.NewService(repository, resourceService, relationService, projectService, userService)
Expand All @@ -210,12 +221,13 @@ func TestService_CreateKey(t *testing.T) {
}, nil)
projectService.EXPECT().Get(mock.Anything, "test-project-slug").
Return(project.Project{
ID: testProjectID,
ID: testProjectID,
Slug: testProjectSlug,
}, nil)
resourceService.EXPECT().Create(mock.Anything, testResource).Return(resource.Resource{
Idxa: testResourceID,
}, nil)
repository.EXPECT().CreateKey(mock.Anything, testCreatedKey).Return(servicedata.Key{}, servicedata.ErrConflict)
repository.EXPECT().CreateKey(mock.Anything, testCreateKey).Return(servicedata.Key{}, servicedata.ErrConflict)
return servicedata.NewService(repository, resourceService, relationService, projectService, userService)
},
wantErr: servicedata.ErrConflict,
Expand All @@ -240,12 +252,13 @@ func TestService_CreateKey(t *testing.T) {
}, nil)
projectService.EXPECT().Get(mock.Anything, "test-project-slug").
Return(project.Project{
ID: testProjectID,
ID: testProjectID,
Slug: testProjectSlug,
}, nil)
resourceService.EXPECT().Create(mock.Anything, testResource).Return(resource.Resource{
Idxa: testResourceID,
}, nil)
repository.EXPECT().CreateKey(mock.Anything, testCreatedKey).Return(testCreatedKey, nil)
repository.EXPECT().CreateKey(mock.Anything, testCreateKey).Return(testCreatedKey, nil)
relationService.EXPECT().Create(mock.Anything, testRelation).Return(relation.RelationV2{}, relation.ErrCreatingRelationInAuthzEngine)
return servicedata.NewService(repository, resourceService, relationService, projectService, userService)
},
Expand Down
3 changes: 2 additions & 1 deletion core/servicedata/servicedata.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ type Key struct {
ID string
URN string
ProjectID string
ProjectSlug string
Key string
Description string
ResourceID string
}

func (key Key) CreateURN() string {
return fmt.Sprintf("%s:servicedata_key:%s", key.ProjectID, key.Key)
return fmt.Sprintf("%s:servicedata_key:%s", key.ProjectSlug, key.Key)
}
114 changes: 57 additions & 57 deletions internal/api/v1beta1/mocks/resource_service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/api/v1beta1/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
type ResourceService interface {
Get(ctx context.Context, id string) (resource.Resource, error)
List(ctx context.Context, flt resource.Filter) (resource.PagedResources, error)
Create(ctx context.Context, resource resource.Resource) (resource.Resource, error)
Upsert(ctx context.Context, resource resource.Resource) (resource.Resource, error)
Update(ctx context.Context, id string, resource resource.Resource) (resource.Resource, error)
CheckAuthz(ctx context.Context, resource resource.Resource, action action.Action) (bool, error)
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func (h Handler) CreateResource(ctx context.Context, request *shieldv1beta1.Crea
}
}

newResource, err := h.resourceService.Create(ctx, resource.Resource{
newResource, err := h.resourceService.Upsert(ctx, resource.Resource{
OrganizationID: project.Organization.ID,
ProjectID: request.GetBody().GetProjectId(),
NamespaceID: request.GetBody().GetNamespaceId(),
Expand Down
8 changes: 4 additions & 4 deletions internal/api/v1beta1/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestHandler_CreateResource(t *testing.T) {
setup: func(ctx context.Context, rs *mocks.ResourceService, ps *mocks.ProjectService, rls *mocks.RelationService, _ *mocks.RelationTransformer) context.Context {
ps.EXPECT().Get(mock.AnythingOfType("context.todoCtx"), testResource.ProjectID).Return(project.Project{}, user.ErrInvalidEmail)

rs.EXPECT().Create(mock.AnythingOfType("context.todoCtx"), resource.Resource{
rs.EXPECT().Upsert(mock.AnythingOfType("context.todoCtx"), resource.Resource{
Name: testResource.Name,
ProjectID: testResource.ProjectID,
OrganizationID: testResource.OrganizationID,
Expand Down Expand Up @@ -174,7 +174,7 @@ func TestHandler_CreateResource(t *testing.T) {
},
}, nil)

rs.EXPECT().Create(mock.AnythingOfType("*context.valueCtx"), resource.Resource{
rs.EXPECT().Upsert(mock.AnythingOfType("*context.valueCtx"), resource.Resource{
Name: testResource.Name,
ProjectID: testResource.ProjectID,
NamespaceID: testResource.NamespaceID,
Expand Down Expand Up @@ -207,7 +207,7 @@ func TestHandler_CreateResource(t *testing.T) {
},
}, nil)

rs.EXPECT().Create(mock.AnythingOfType("*context.valueCtx"), resource.Resource{
rs.EXPECT().Upsert(mock.AnythingOfType("*context.valueCtx"), resource.Resource{
Name: testResource.Name,
ProjectID: testResource.ProjectID,
OrganizationID: testResource.OrganizationID,
Expand Down Expand Up @@ -256,7 +256,7 @@ func TestHandler_CreateResource(t *testing.T) {

rls.EXPECT().Create(mock.AnythingOfType("*context.valueCtx"), theRelation).Return(relation.RelationV2{}, nil)

rs.EXPECT().Create(mock.AnythingOfType("*context.valueCtx"), resource.Resource{
rs.EXPECT().Upsert(mock.AnythingOfType("*context.valueCtx"), resource.Resource{
Name: testResource.Name,
ProjectID: testResource.ProjectID,
OrganizationID: testResource.OrganizationID,
Expand Down
Loading

0 comments on commit 2d4ed1f

Please sign in to comment.