2022-04-05 05:58:09 +00:00
|
|
|
package instance
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-04-05 05:58:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
InstanceAddedEventType = instanceEventTypePrefix + "added"
|
|
|
|
InstanceChangedEventType = instanceEventTypePrefix + "changed"
|
|
|
|
InstanceRemovedEventType = instanceEventTypePrefix + "removed"
|
|
|
|
)
|
|
|
|
|
|
|
|
type InstanceAddedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *InstanceAddedEvent) Payload() interface{} {
|
2022-04-05 05:58:09 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *InstanceAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2022-04-05 05:58:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInstanceAddedEvent(ctx context.Context, aggregate *eventstore.Aggregate, name string) *InstanceAddedEvent {
|
|
|
|
return &InstanceAddedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
InstanceAddedEventType,
|
|
|
|
),
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func InstanceAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2022-04-05 05:58:09 +00:00
|
|
|
instanceAdded := &InstanceAddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(instanceAdded)
|
2022-04-05 05:58:09 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "INSTANCE-s9l3F", "unable to unmarshal instance added")
|
2022-04-05 05:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return instanceAdded, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type InstanceChangedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *InstanceChangedEvent) Payload() interface{} {
|
2022-04-05 05:58:09 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *InstanceChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2022-04-05 05:58:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-27 06:58:50 +00:00
|
|
|
func NewInstanceChangedEvent(ctx context.Context, aggregate *eventstore.Aggregate, newName string) *InstanceChangedEvent {
|
2022-04-05 05:58:09 +00:00
|
|
|
return &InstanceChangedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
InstanceChangedEventType,
|
|
|
|
),
|
|
|
|
Name: newName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func InstanceChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2022-04-05 05:58:09 +00:00
|
|
|
instanceChanged := &InstanceChangedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(instanceChanged)
|
2022-04-05 05:58:09 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "INSTANCE-3hfo8", "unable to unmarshal instance changed")
|
2022-04-05 05:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return instanceChanged, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type InstanceRemovedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
name string
|
2022-10-26 13:06:48 +00:00
|
|
|
domains []string
|
2022-04-05 05:58:09 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *InstanceRemovedEvent) Payload() interface{} {
|
2022-10-26 13:06:48 +00:00
|
|
|
return nil
|
2022-04-05 05:58:09 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *InstanceRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
|
|
constraints := make([]*eventstore.UniqueConstraint, len(e.domains)+1)
|
2022-10-26 13:06:48 +00:00
|
|
|
for i, domain := range e.domains {
|
|
|
|
constraints[i] = NewRemoveInstanceDomainUniqueConstraint(domain)
|
|
|
|
}
|
|
|
|
constraints[len(e.domains)] = eventstore.NewRemoveInstanceUniqueConstraints()
|
|
|
|
return constraints
|
2022-04-05 05:58:09 +00:00
|
|
|
}
|
|
|
|
|
2024-12-04 18:10:10 +00:00
|
|
|
func (e *InstanceRemovedEvent) Fields() []*eventstore.FieldOperation {
|
|
|
|
return []*eventstore.FieldOperation{
|
|
|
|
eventstore.RemoveSearchFields(map[eventstore.FieldType]any{
|
|
|
|
eventstore.FieldTypeInstanceID: e.Aggregate().ID,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 13:06:48 +00:00
|
|
|
func NewInstanceRemovedEvent(ctx context.Context, aggregate *eventstore.Aggregate, name string, domains []string) *InstanceRemovedEvent {
|
2022-04-05 05:58:09 +00:00
|
|
|
return &InstanceRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
InstanceRemovedEventType,
|
|
|
|
),
|
2022-10-26 13:06:48 +00:00
|
|
|
name: name,
|
|
|
|
domains: domains,
|
2022-04-05 05:58:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func InstanceRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2022-10-26 13:06:48 +00:00
|
|
|
return &InstanceRemovedEvent{
|
2022-04-05 05:58:09 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
2022-10-26 13:06:48 +00:00
|
|
|
}, nil
|
2022-04-05 05:58:09 +00:00
|
|
|
}
|