zitadel/internal/management/repository/eventsourcing/handler/project_grant.go
Fabi 6b3f5b984c
feat: metrics (#1024)
* refactor: switch from opencensus to opentelemetry

* tempo works as designed nooooot

* fix: log traceids

* with grafana agent

* fix: http tracing

* fix: cleanup files

* chore: remove todo

* fix: bad test

* fix: ignore methods in grpc interceptors

* fix: remove test log

* clean up

* typo

* fix(config): configure tracing endpoint

* fix(span): add error id to span

* feat: metrics package

* feat: metrics package

* fix: counter

* fix: metric

* try metrics

* fix: coutner metrics

* fix: active sessin counter

* fix: active sessin counter

* fix: change current Sequence table

* fix: change current Sequence table

* fix: current sequences

* fix: spooler div metrics

* fix: console view

* fix: merge master

* fix: Last spool run on search result instead of eventtimestamp

* fix: go mod

* Update console/src/assets/i18n/de.json

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

* fix: pr review

* fix: map

* update oidc pkg

* fix: handlers

* fix: value observer

* fix: remove fmt

* fix: handlers

* fix: tests

* fix: handler minimum cycle duration 1s

* fix(spooler): handler channel buffer

* fix interceptors

Co-authored-by: adlerhurst <silvan.reusser@gmail.com>
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2020-12-02 08:50:59 +01:00

129 lines
4.2 KiB
Go

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"
org_model "github.com/caos/zitadel/internal/org/model"
org_event "github.com/caos/zitadel/internal/org/repository/eventsourcing"
proj_model "github.com/caos/zitadel/internal/project/model"
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 ProjectGrant struct {
handler
eventstore eventstore.Eventstore
projectEvents *proj_event.ProjectEventstore
orgEvents *org_event.OrgEventstore
}
const (
grantedProjectTable = "management.project_grants"
)
func (p *ProjectGrant) ViewModel() string {
return grantedProjectTable
}
func (p *ProjectGrant) EventQuery() (*models.SearchQuery, error) {
sequence, err := p.view.GetLatestProjectGrantSequence()
if err != nil {
return nil, err
}
return proj_event.ProjectQuery(sequence.CurrentSequence), nil
}
func (p *ProjectGrant) Reduce(event *models.Event) (err error) {
grantedProject := new(view_model.ProjectGrantView)
switch event.Type {
case es_model.ProjectChanged:
project, err := p.view.ProjectByID(event.AggregateID)
if err != nil {
return err
}
return p.updateExistingProjects(project, event.Sequence, event.CreationDate)
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
org, err := p.orgEvents.OrgByID(context.TODO(), org_model.NewOrg(grantedProject.OrgID))
if err != nil {
return err
}
resourceOwner, err := p.orgEvents.OrgByID(context.TODO(), org_model.NewOrg(grantedProject.ResourceOwner))
if err != nil {
return err
}
p.fillOrgData(grantedProject, org, resourceOwner)
case es_model.ProjectGrantChanged, es_model.ProjectGrantCascadeChanged:
grant := new(view_model.ProjectGrant)
err = grant.SetData(event)
if err != nil {
return err
}
grantedProject, err = p.view.ProjectGrantByID(grant.GrantID)
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.DeleteProjectGrant(grant.GrantID, event.Sequence, event.CreationDate)
case es_model.ProjectRemoved:
return p.view.DeleteProjectGrantsByProjectID(event.AggregateID)
default:
return p.view.ProcessedProjectGrantSequence(event.Sequence, event.CreationDate)
}
if err != nil {
return err
}
return p.view.PutProjectGrant(grantedProject, event.CreationDate)
}
func (p *ProjectGrant) fillOrgData(grantedProject *view_model.ProjectGrantView, org, resourceOwner *org_model.Org) {
grantedProject.OrgName = org.Name
grantedProject.ResourceOwnerName = resourceOwner.Name
}
func (p *ProjectGrant) getProject(projectID string) (*proj_model.Project, error) {
return p.projectEvents.ProjectByID(context.Background(), projectID)
}
func (p *ProjectGrant) updateExistingProjects(project *view_model.ProjectView, sequence uint64, eventTimestamp time.Time) error {
projectGrants, err := p.view.ProjectGrantsByProjectID(project.ProjectID)
if err != nil {
logging.LogWithFields("SPOOL-los03", "id", project.ProjectID).WithError(err).Warn("could not update existing projects")
}
for _, existingGrant := range projectGrants {
existingGrant.Name = project.Name
}
return p.view.PutProjectGrants(projectGrants, sequence, eventTimestamp)
}
func (p *ProjectGrant) OnError(event *models.Event, err error) error {
logging.LogWithFields("SPOOL-sQqOg", "id", event.AggregateID).WithError(err).Warn("something went wrong in granted projecthandler")
return spooler.HandleError(event, err, p.view.GetLatestProjectGrantFailedEvent, p.view.ProcessedProjectGrantFailedEvent, p.view.ProcessedProjectGrantSequence, p.errorCountUntilSkip)
}
func (p *ProjectGrant) OnSuccess() error {
return spooler.HandleSuccess(p.view.UpdateProjectGrantSpoolerRunTimestamp)
}