126 lines
3.9 KiB
Go
Raw Normal View History

feat: project view (#90) * init for views (spooler, handler) * init for views (spooler, handler) * start view in management * granted project * implement granted project view * search granted projects * fix search column * update all projects on project change * search roles * filter org * project members * project grant members * fix tests * application view * project grant search * mock * test appendevents * test appendevents * Update internal/view/query.go Co-authored-by: Livio Amstutz <livio.a@gmail.com> * Update internal/eventstore/spooler/spooler.go Co-authored-by: Livio Amstutz <livio.a@gmail.com> * Update internal/view/query.go Co-authored-by: Livio Amstutz <livio.a@gmail.com> * merge request changes * Update internal/project/repository/view/model/application.go Co-authored-by: Livio Amstutz <livio.a@gmail.com> * merge request changes * Project view sql (#92) * sql and configs * error handling * sql start in eventstore * on error handling, config * read user on members * Update internal/project/repository/view/application_view.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/application.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/application.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/application.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/application.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/application.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/application_query.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/project_grant_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/project_grant_member_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/project_grant_member_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/project_member_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/project_member_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/granted_project.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * return caos errors * Update internal/project/repository/view/model/granted_project_query.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/project_grant_member.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/project_grant_member_query.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/project_member.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/project_member_query.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/project_role.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/project_role_query.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/application_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/application_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/project_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/project_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/project_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/project_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * converter fix Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: Silvan <silvan.reusser@gmail.com>
2020-05-11 12:16:29 +02:00
package handler
import (
"context"
"time"
"github.com/caos/logging"
"github.com/caos/zitadel/internal/eventstore"
"github.com/caos/zitadel/internal/eventstore/models"
"github.com/caos/zitadel/internal/eventstore/spooler"
"github.com/caos/zitadel/internal/project/model"
"github.com/caos/zitadel/internal/project/repository/eventsourcing"
proj_event "github.com/caos/zitadel/internal/project/repository/eventsourcing"
es_model "github.com/caos/zitadel/internal/project/repository/eventsourcing/model"
view_model "github.com/caos/zitadel/internal/project/repository/view/model"
)
type GrantedProject struct {
handler
eventstore eventstore.Eventstore
projectEvents *proj_event.ProjectEventstore
}
const (
grantedProjectTable = "management.granted_projects"
)
func (p *GrantedProject) MinimumCycleDuration() time.Duration { return p.cycleDuration }
func (p *GrantedProject) ViewModel() string {
return grantedProjectTable
}
func (p *GrantedProject) EventQuery() (*models.SearchQuery, error) {
sequence, err := p.view.GetLatestGrantedProjectSequence()
if err != nil {
return nil, err
}
return eventsourcing.ProjectQuery(sequence), nil
}
func (p *GrantedProject) Process(event *models.Event) (err error) {
grantedProject := new(view_model.GrantedProjectView)
switch event.Type {
case es_model.ProjectAdded:
grantedProject.AppendEvent(event)
case es_model.ProjectChanged:
grantedProject, err = p.view.GrantedProjectByIDs(event.AggregateID, event.ResourceOwner)
if err != nil {
return err
}
err = grantedProject.AppendEvent(event)
if err != nil {
return err
}
p.updateExistingProjects(grantedProject)
case es_model.ProjectDeactivated, es_model.ProjectReactivated:
grantedProject, err = p.view.GrantedProjectByIDs(event.AggregateID, event.ResourceOwner)
if err != nil {
return err
}
err = grantedProject.AppendEvent(event)
case es_model.ProjectGrantAdded:
err = grantedProject.AppendEvent(event)
if err != nil {
return err
}
project, err := p.getProject(grantedProject.ProjectID)
if err != nil {
return err
}
grantedProject.Name = project.Name
//TODO: read org
case es_model.ProjectGrantChanged:
grant := new(view_model.ProjectGrant)
err := grant.SetData(event)
if err != nil {
return err
}
grantedProject, err = p.view.GrantedProjectByIDs(event.AggregateID, grant.GrantedOrgID)
if err != nil {
return err
}
err = grantedProject.AppendEvent(event)
case es_model.ProjectGrantRemoved:
grant := new(view_model.ProjectGrant)
err := grant.SetData(event)
if err != nil {
return err
}
return p.view.DeleteGrantedProject(event.AggregateID, grant.GrantedOrgID, event.Sequence)
default:
return p.view.ProcessedGrantedProjectSequence(event.Sequence)
}
if err != nil {
return err
}
return p.view.PutGrantedProject(grantedProject)
}
func (p *GrantedProject) getOrg(orgID string) {
//TODO: Get Org
}
func (p *GrantedProject) getProject(projectID string) (*model.Project, error) {
return p.projectEvents.ProjectByID(context.Background(), projectID)
}
func (p *GrantedProject) updateExistingProjects(project *view_model.GrantedProjectView) {
projects, err := p.view.GrantedProjectsByID(project.ProjectID)
if err != nil {
logging.LogWithFields("SPOOL-los03", "id", project.ProjectID).WithError(err).Warn("could not update existing projects")
}
for _, existing := range projects {
existing.Name = project.Name
err := p.view.PutGrantedProject(existing)
logging.LogWithFields("SPOOL-sjwi3", "id", existing.ProjectID).WithError(err).Warn("could not update existing project")
}
}
func (p *GrantedProject) OnError(event *models.Event, err error) error {
logging.LogWithFields("SPOOL-is8wa", "id", event.AggregateID).WithError(err).Warn("something went wrong in granted projecthandler")
return spooler.HandleError(event, p.view.GetLatestGrantedProjectFailedEvent, p.view.ProcessedGrantedProjectFailedEvent, p.view.ProcessedGrantedProjectSequence, p.errorCountUntilSkip)
}