2022-03-07 13:22:37 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/notification/channels/fs"
|
|
|
|
iam_repo "github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2022-03-07 13:22:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Commands) AddDebugNotificationProviderFile(ctx context.Context, fileSystemProvider *fs.FSConfig) (*domain.ObjectDetails, error) {
|
2022-04-05 05:58:09 +00:00
|
|
|
writeModel := NewInstanceDebugNotificationFileWriteModel(ctx)
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg := InstanceAggregateFromWriteModel(&writeModel.WriteModel)
|
|
|
|
events, err := c.addDefaultDebugNotificationFile(ctx, instanceAgg, writeModel, fileSystemProvider)
|
2022-03-07 13:22:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, events...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(writeModel, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&writeModel.DebugNotificationWriteModel.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (c *Commands) addDefaultDebugNotificationFile(ctx context.Context, instanceAgg *eventstore.Aggregate, addedWriteModel *InstanceDebugNotificationFileWriteModel, fileSystemProvider *fs.FSConfig) ([]eventstore.Command, error) {
|
2022-03-07 13:22:37 +00:00
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, addedWriteModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if addedWriteModel.State.Exists() {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, caos_errs.ThrowAlreadyExists(nil, "INSTANCE-d93nfs", "Errors.IAM.DebugNotificationProvider.AlreadyExists")
|
2022-03-07 13:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
events := []eventstore.Command{
|
|
|
|
iam_repo.NewDebugNotificationProviderFileAddedEvent(ctx,
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg,
|
2022-03-07 13:22:37 +00:00
|
|
|
fileSystemProvider.Compact),
|
|
|
|
}
|
|
|
|
return events, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) ChangeDefaultNotificationFile(ctx context.Context, fileSystemProvider *fs.FSConfig) (*domain.ObjectDetails, error) {
|
2022-04-05 05:58:09 +00:00
|
|
|
writeModel := NewInstanceDebugNotificationFileWriteModel(ctx)
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg := InstanceAggregateFromWriteModel(&writeModel.WriteModel)
|
|
|
|
events, err := c.changeDefaultDebugNotificationProviderFile(ctx, instanceAgg, writeModel, fileSystemProvider)
|
2022-03-07 13:22:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, events...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(writeModel, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&writeModel.DebugNotificationWriteModel.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (c *Commands) changeDefaultDebugNotificationProviderFile(ctx context.Context, instanceAgg *eventstore.Aggregate, existingProvider *InstanceDebugNotificationFileWriteModel, fileSystemProvider *fs.FSConfig) ([]eventstore.Command, error) {
|
2022-03-07 13:22:37 +00:00
|
|
|
err := c.defaultDebugNotificationProviderFileWriteModelByID(ctx, existingProvider)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !existingProvider.State.Exists() {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "INSTANCE-fm9wl", "Errors.IAM.DebugNotificationProvider.NotFound")
|
2022-03-07 13:22:37 +00:00
|
|
|
}
|
|
|
|
events := make([]eventstore.Command, 0)
|
|
|
|
changedEvent, hasChanged := existingProvider.NewChangedEvent(ctx,
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg,
|
2022-03-07 13:22:37 +00:00
|
|
|
fileSystemProvider.Compact)
|
|
|
|
if hasChanged {
|
|
|
|
events = append(events, changedEvent)
|
|
|
|
}
|
|
|
|
if len(events) == 0 {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "INSTANCE-5M9vdd", "Errors.IAM.LoginPolicy.NotChanged")
|
2022-03-07 13:22:37 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
return events, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) RemoveDefaultNotificationFile(ctx context.Context) (*domain.ObjectDetails, error) {
|
2022-04-05 05:58:09 +00:00
|
|
|
existingProvider := NewInstanceDebugNotificationFileWriteModel(ctx)
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg := InstanceAggregateFromWriteModel(&existingProvider.WriteModel)
|
2022-03-07 13:22:37 +00:00
|
|
|
err := c.defaultDebugNotificationProviderFileWriteModelByID(ctx, existingProvider)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !existingProvider.State.Exists() {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "INSTANCE-dj9ew", "Errors.IAM.DebugNotificationProvider.NotFound")
|
2022-03-07 13:22:37 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
events, err := c.eventstore.Push(ctx, iam_repo.NewDebugNotificationProviderFileRemovedEvent(ctx, instanceAgg))
|
2022-03-07 13:22:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(existingProvider, events...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&existingProvider.DebugNotificationWriteModel.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (c *Commands) defaultDebugNotificationProviderFileWriteModelByID(ctx context.Context, writeModel *InstanceDebugNotificationFileWriteModel) (err error) {
|
2022-03-07 13:22:37 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|