diff --git a/pkg/collections/events.go b/pkg/collections/events.go index 516d5c5cbf8ece7bd338e02a9919f4411a10c2fa..5272aa01794b84cdb5f7f2b6b546cf2e1f351eaf 100644 --- a/pkg/collections/events.go +++ b/pkg/collections/events.go @@ -1,8 +1,8 @@ package collections const ( - EventCreate = "collection.create" - EventUpdate = "collection.update" - EventDelete = "collection.delete" - EventSetSchema = "collection.set_schema" + EventCreate = "collections.create" + EventUpdate = "collections.update" + EventDelete = "collections.delete" + EventSetSchema = "collections.set_schema" ) diff --git a/pkg/collections/middleware/logging_middleware.go b/pkg/collections/middleware/logging_middleware.go index 0eca58657aad52da0644294bf5c3077e87cfa562..92e2811bd2f30cd45eb0b1aa66da1d52cfbb2596 100644 --- a/pkg/collections/middleware/logging_middleware.go +++ b/pkg/collections/middleware/logging_middleware.go @@ -31,7 +31,7 @@ func (m *loggingMiddleware) Create(ctx context.Context, collection *collections. } logger := m.logger.With( logzap.Caller(ctx, logzap.WithSpace(spaceID)), - logzap.Event(collections.EventCollectionCreate), + logzap.Event(collections.EventCreate), ) created, err = m.next.Create(ctx, collection) @@ -47,7 +47,7 @@ func (m *loggingMiddleware) Create(ctx context.Context, collection *collections. func (m *loggingMiddleware) Delete(ctx context.Context, spaceId string, envId string, collectionId string) (err error) { logger := m.logger.With( logzap.Caller(ctx, logzap.WithSpace(spaceId)), - logzap.Event(collections.EventCollectionDelete), + logzap.Event(collections.EventDelete), logzap.Object(id.NewCollectionId(spaceId, envId, collectionId)), ) @@ -92,7 +92,7 @@ func (m *loggingMiddleware) List(ctx context.Context, spaceId string, envId stri func (m *loggingMiddleware) SetSchema(ctx context.Context, spaceId string, envId string, collectionId string, schema *schema.Schema) (err error) { logger := m.logger.With( logzap.Caller(ctx, logzap.WithSpace(spaceId)), - logzap.Event(collections.EventCollectionSetSchema), + logzap.Event(collections.EventSetSchema), logzap.Object(id.NewCollectionId(spaceId, envId, collectionId)), ) @@ -128,7 +128,7 @@ func (m *loggingMiddleware) Update(ctx context.Context, coll *collections.Collec } logger := m.logger.With( logzap.Caller(ctx, logzap.WithSpace(spaceID)), - logzap.Event(collections.EventCollectionUpdate), + logzap.Event(collections.EventUpdate), logzap.Object(coll), ) diff --git a/pkg/extension/action.go b/pkg/extension/action.go index a8062d3ddda18f6a7990eb122262aac336d3a919..55827d81e6c5b2237b1f2ee188f88293ea22c08b 100644 --- a/pkg/extension/action.go +++ b/pkg/extension/action.go @@ -1,6 +1,8 @@ package extension import ( + "strings" + "git.perx.ru/perxis/perxis-go/pkg/references" pb "git.perx.ru/perxis/perxis-go/proto/extensions" "github.com/mitchellh/mapstructure" @@ -116,7 +118,7 @@ func ActionToMap(action *Action) map[string]interface{} { if action == nil { return nil } - + res := make(map[string]interface{}) if err := mapstructure.Decode(action, &res); err != nil { return nil @@ -274,3 +276,28 @@ func ActionToPB(a *Action) *pb.Action { Order: a.Order, } } + +func (r *ActionRequest) NewSubRequest() *ActionRequest { + md := map[string]string{ + "request_extension": r.Extension, + "request_action": r.Action, + "request_space_id": r.SpaceID, + "request_env_id": r.EnvID, + "request_collection_id": r.CollectionID, + "request_item_id": r.ItemID, + "request_item_ids": strings.Join(r.ItemIDs, ","), + "request_fields": strings.Join(r.Fields, ","), + "request_refs": references.ReferenceListToString(r.Refs), + "request_params": r.Params.String(), + } + + for k, v := range r.Metadata { + if _, exist := md[k]; !exist { + md[k] = v + } + } + + return &ActionRequest{ + Metadata: md, + } +} diff --git a/pkg/items/events.go b/pkg/items/events.go index 40e07df2865d4bafe4cf51e00aa5199b43483e80..2b32bde1bc34edd99db30ae80b16c1c6be83ff10 100644 --- a/pkg/items/events.go +++ b/pkg/items/events.go @@ -7,14 +7,14 @@ import ( ) const ( - EventCreate = "item.create" - EventUpdate = "item.update" - EventPublish = "item.publish" - EventUnpublish = "item.unpublish" - EventDelete = "item.delete" - EventUndelete = "item.undelete" - EventArchive = "item.archive" - EventUnarchive = "item.unarchive" + EventCreate = "items.create" + EventUpdate = "items.update" + EventPublish = "items.publish" + EventUnpublish = "items.unpublish" + EventDelete = "items.delete" + EventUndelete = "items.undelete" + EventArchive = "items.archive" + EventUnarchive = "items.unarchive" DefaultEventSubject = "content.{{.EventType}}.{{.SpaceID}}.{{.EnvID}}.{{.CollectionID}}.{{.ItemID}}" ) diff --git a/pkg/references/reference.go b/pkg/references/reference.go index 171ded420c3fd5601cac9ad9f74ad69883f964ea..75b9bc745d430ed0984ceedd07cf877adcb643e3 100644 --- a/pkg/references/reference.go +++ b/pkg/references/reference.go @@ -1,6 +1,8 @@ package references import ( + "strings" + "git.perx.ru/perxis/perxis-go/pkg/items" pb "git.perx.ru/perxis/perxis-go/proto/references" "go.mongodb.org/mongo-driver/bson" @@ -82,6 +84,22 @@ func ReferenceListToPB(refs []*Reference) []*pb.Reference { return list } +func ReferenceListToString(refs []*Reference) string { + var res strings.Builder + + for _, ref := range refs { + res.WriteString(ref.String()) + } + return res.String() +} + +func ReferenceListToStrings(refs []*Reference) (res []string) { + for _, ref := range refs { + res = append(res, ref.String()) + } + return +} + func (r *Reference) String() string { if r == nil { return "" diff --git a/pkg/template/builder.go b/pkg/template/builder.go index b1414df21adc6373a4dcd75460991f782e7eaab9..64530be62337de21dc5b93956df261ce5211a302 100644 --- a/pkg/template/builder.go +++ b/pkg/template/builder.go @@ -5,17 +5,14 @@ import ( "context" "text/template" - "git.perx.ru/perxis/perxis-go/pkg/account" "git.perx.ru/perxis/perxis-go/pkg/collections" "git.perx.ru/perxis/perxis-go/pkg/content" "git.perx.ru/perxis/perxis-go/pkg/environments" - "git.perx.ru/perxis/perxis-go/pkg/organizations" "git.perx.ru/perxis/perxis-go/pkg/spaces" ) type Builder struct { ctx context.Context - acc *account.Account cnt *content.Content SpaceID string EnvID string @@ -23,16 +20,14 @@ type Builder struct { data map[string]interface{} // Для кеширования запросов - space *spaces.Space - environment *environments.Environment - collection *collections.Collection - organization *organizations.Organization + space *spaces.Space + environment *environments.Environment + collection *collections.Collection } -func NewBuilder(acc *account.Account, cnt *content.Content, space, env, col string) *Builder { +func NewBuilder(cnt *content.Content, space, env, col string) *Builder { return &Builder{ ctx: context.Background(), - acc: acc, cnt: cnt, SpaceID: space, EnvID: env, diff --git a/pkg/template/builder_test.go b/pkg/template/builder_test.go index f9e7b0a66b7b4ec54f9151136ed0e60d7f386cb9..128f706423f3ecc67097a76b8926fbed619eb1ce 100644 --- a/pkg/template/builder_test.go +++ b/pkg/template/builder_test.go @@ -5,7 +5,6 @@ import ( "errors" "testing" - "git.perx.ru/perxis/perxis-go/pkg/account" "git.perx.ru/perxis/perxis-go/pkg/collections" colsmocks "git.perx.ru/perxis/perxis-go/pkg/collections/mocks" "git.perx.ru/perxis/perxis-go/pkg/content" @@ -13,8 +12,6 @@ import ( envsmocks "git.perx.ru/perxis/perxis-go/pkg/environments/mocks" "git.perx.ru/perxis/perxis-go/pkg/items" mocksitems "git.perx.ru/perxis/perxis-go/pkg/items/mocks" - "git.perx.ru/perxis/perxis-go/pkg/organizations" - orgsmocks "git.perx.ru/perxis/perxis-go/pkg/organizations/mocks" "git.perx.ru/perxis/perxis-go/pkg/spaces" spsmocks "git.perx.ru/perxis/perxis-go/pkg/spaces/mocks" "github.com/stretchr/testify/assert" @@ -31,7 +28,6 @@ func TestBuilder_Execute(t *testing.T) { want any wantErr bool - getAcc func() (acc *account.Account, assertExpectations func(t *testing.T)) getCnt func() (cnt *content.Content, assertExpectations func(t *testing.T)) }{ {name: "error", str: "hello {{ .a }}", data: "world", want: "", wantErr: true}, @@ -100,15 +96,6 @@ func TestBuilder_Execute(t *testing.T) { }}, {name: "lookup without itemID", SpaceID: "space", EnvID: "env", str: "hello {{ lookup \"secrets.pass\" }}", data: "", want: "", wantErr: true}, {name: "system#1", SpaceID: "space", str: "hello {{ system.SpaceID }}", data: "", want: "hello space", wantErr: false}, - {name: "system organization", SpaceID: "space", EnvID: "env", getCnt: func() (cnt *content.Content, assertExpectations func(t *testing.T)) { - spsSvc := &spsmocks.Spaces{} - spsSvc.On("Get", context.Background(), "space").Return(&spaces.Space{OrgID: "orgid"}, nil).Once() - return &content.Content{Spaces: spsSvc}, func(t *testing.T) { spsSvc.AssertExpectations(t) } - }, getAcc: func() (acc *account.Account, assertExpectations func(t *testing.T)) { - orgsSvc := &orgsmocks.Organizations{} - orgsSvc.On("Get", context.Background(), "orgid").Return(&organizations.Organization{Name: "Org"}, nil).Once() - return &account.Account{Organizations: orgsSvc}, func(t *testing.T) { orgsSvc.AssertExpectations(t) } - }, str: "hello {{ system.Organization.Name }}", want: "hello Org", wantErr: false}, {name: "system space", SpaceID: "space", getCnt: func() (cnt *content.Content, assertExpectations func(t *testing.T)) { spsSvc := &spsmocks.Spaces{} spsSvc.On("Get", context.Background(), "space").Return(&spaces.Space{Description: "description"}, nil).Once() @@ -128,14 +115,7 @@ func TestBuilder_Execute(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - var acc *account.Account var cnt *content.Content - - if tt.getAcc != nil { - var checkFn func(*testing.T) - acc, checkFn = tt.getAcc() - defer checkFn(t) - } if tt.getCnt != nil { var checkFn func(*testing.T) cnt, checkFn = tt.getCnt() @@ -144,7 +124,6 @@ func TestBuilder_Execute(t *testing.T) { b := &Builder{ ctx: context.Background(), - acc: acc, cnt: cnt, SpaceID: tt.SpaceID, EnvID: tt.EnvID, diff --git a/pkg/template/system.go b/pkg/template/system.go index 48b464e4cf106f5e90dfc622764391080646ec10..c7dda43f08c852f590cd7cfa16ec00709423c7a3 100644 --- a/pkg/template/system.go +++ b/pkg/template/system.go @@ -3,7 +3,6 @@ package template import ( "git.perx.ru/perxis/perxis-go/pkg/collections" "git.perx.ru/perxis/perxis-go/pkg/environments" - "git.perx.ru/perxis/perxis-go/pkg/organizations" "git.perx.ru/perxis/perxis-go/pkg/spaces" ) @@ -23,14 +22,6 @@ func (s *System) CollectionID() string { return s.builder.CollID } -func (s *System) OrganizationID() (string, error) { - org, err := s.Organization() - if err != nil { - return "", err - } - return org.ID, nil -} - func (s *System) Space() (*spaces.Space, error) { if s.builder.space != nil { return s.builder.space, nil @@ -60,18 +51,3 @@ func (s *System) Collection() (*collections.Collection, error) { s.builder.collection = coll return s.builder.collection, err } - -func (s *System) Organization() (*organizations.Organization, error) { - if s.builder.organization != nil { - return s.builder.organization, nil - } - - sp, err := s.Space() - if err != nil { - return nil, err - } - - org, err := s.builder.acc.Organizations.Get(s.builder.ctx, sp.OrgID) - s.builder.organization = org - return s.builder.organization, err -}