mirror of
https://github.com/zitadel/zitadel.git
synced 2025-03-01 00:27:24 +00:00
remove test code
This commit is contained in:
parent
b532335b9d
commit
fcdb2750cf
@ -1,51 +0,0 @@
|
|||||||
package instance
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"google.golang.org/protobuf/types/known/timestamppb"
|
|
||||||
|
|
||||||
"github.com/zitadel/zitadel/pkg/grpc/object"
|
|
||||||
system_pb "github.com/zitadel/zitadel/pkg/grpc/system"
|
|
||||||
)
|
|
||||||
|
|
||||||
type AddInstanceRequest struct {
|
|
||||||
system_pb.AddInstanceRequest
|
|
||||||
|
|
||||||
ID string
|
|
||||||
CreatedAt time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
type AddInstanceResponse struct {
|
|
||||||
system_pb.AddInstanceResponse
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bl *BusinessLogic) AddInstance(ctx context.Context, request *AddInstanceRequest) (_ *AddInstanceResponse, err error) {
|
|
||||||
tx, err := bl.client.Begin(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
err = tx.End(ctx, err)
|
|
||||||
}()
|
|
||||||
|
|
||||||
request.ID = time.Since(time.Time{}).String()
|
|
||||||
|
|
||||||
err = bl.storage.WriteInstanceAdded(ctx, tx, request)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &AddInstanceResponse{
|
|
||||||
AddInstanceResponse: system_pb.AddInstanceResponse{
|
|
||||||
InstanceId: request.ID,
|
|
||||||
Details: &object.ObjectDetails{
|
|
||||||
Sequence: 1,
|
|
||||||
CreationDate: timestamppb.New(request.CreatedAt),
|
|
||||||
ChangeDate: timestamppb.New(request.CreatedAt),
|
|
||||||
ResourceOwner: request.ID,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}, nil
|
|
||||||
}
|
|
@ -1,62 +0,0 @@
|
|||||||
package instance_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"log/slog"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/zitadel/zitadel/internal/v3/instance"
|
|
||||||
repo_log "github.com/zitadel/zitadel/internal/v3/repository/log"
|
|
||||||
repo_mem "github.com/zitadel/zitadel/internal/v3/repository/memory"
|
|
||||||
"github.com/zitadel/zitadel/internal/v3/storage"
|
|
||||||
"github.com/zitadel/zitadel/internal/v3/storage/memory"
|
|
||||||
system_pb "github.com/zitadel/zitadel/pkg/grpc/system"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestBusinessLogic_AddInstance(t *testing.T) {
|
|
||||||
type fields struct {
|
|
||||||
client storage.Client
|
|
||||||
stores []instance.InstanceStorage
|
|
||||||
}
|
|
||||||
type args struct {
|
|
||||||
ctx context.Context
|
|
||||||
request *instance.AddInstanceRequest
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
fields fields
|
|
||||||
args args
|
|
||||||
wantErr bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "ok",
|
|
||||||
fields: fields{
|
|
||||||
client: &memory.Client{},
|
|
||||||
stores: []instance.InstanceStorage{
|
|
||||||
repo_mem.NewInstanceMemory(),
|
|
||||||
repo_log.NewInstanceLogger(slog.Default()),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
args: args{
|
|
||||||
ctx: context.Background(),
|
|
||||||
request: &instance.AddInstanceRequest{
|
|
||||||
AddInstanceRequest: system_pb.AddInstanceRequest{
|
|
||||||
InstanceName: "test",
|
|
||||||
CustomDomain: "test",
|
|
||||||
DefaultLanguage: "en",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
bl := instance.NewBusinessLogic(tt.fields.client, tt.fields.stores...)
|
|
||||||
_, err := bl.AddInstance(tt.args.ctx, tt.args.request)
|
|
||||||
if (err != nil) != tt.wantErr {
|
|
||||||
t.Errorf("BusinessLogic.AddInstance() error = %v, wantErr %v", err, tt.wantErr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
package instance
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/zitadel/zitadel/internal/v3/storage"
|
|
||||||
)
|
|
||||||
|
|
||||||
type InstanceStorage interface {
|
|
||||||
WriteInstanceAdded(ctx context.Context, tx storage.Transaction, instance *AddInstanceRequest) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type BusinessLogic struct {
|
|
||||||
client storage.Client
|
|
||||||
storage InstanceStorage
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewBusinessLogic(client storage.Client, stores ...InstanceStorage) *BusinessLogic {
|
|
||||||
return &BusinessLogic{
|
|
||||||
client: client,
|
|
||||||
storage: chainedStorage[InstanceStorage](stores),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type chainedStorage[S InstanceStorage] []S
|
|
||||||
|
|
||||||
func (cs chainedStorage[S]) WriteInstanceAdded(ctx context.Context, tx storage.Transaction, instance *AddInstanceRequest) error {
|
|
||||||
for _, store := range cs {
|
|
||||||
if err := store.WriteInstanceAdded(ctx, tx, instance); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -1,29 +0,0 @@
|
|||||||
// Logs the operation of an instance
|
|
||||||
package log
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"log/slog"
|
|
||||||
|
|
||||||
"github.com/zitadel/zitadel/internal/v3/instance"
|
|
||||||
"github.com/zitadel/zitadel/internal/v3/storage"
|
|
||||||
)
|
|
||||||
|
|
||||||
var _ instance.InstanceStorage = (*InstanceLogger)(nil)
|
|
||||||
|
|
||||||
type InstanceLogger struct {
|
|
||||||
*Logger
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewInstanceLogger(logger *slog.Logger) *InstanceLogger {
|
|
||||||
return &InstanceLogger{Logger: NewLogger(logger)}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteInstanceAdded implements instance.InstanceStorage.
|
|
||||||
func (l *InstanceLogger) WriteInstanceAdded(ctx context.Context, tx storage.Transaction, instance *instance.AddInstanceRequest) error {
|
|
||||||
tx.OnCommit(func(ctx context.Context) error {
|
|
||||||
l.InfoContext(ctx, "Instance added", slog.Any("instance", instance))
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
// Logs the operation of an instance
|
|
||||||
package log
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log/slog"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Logger struct {
|
|
||||||
*slog.Logger
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewLogger(logger *slog.Logger) *Logger {
|
|
||||||
return &Logger{logger}
|
|
||||||
}
|
|
@ -1,71 +0,0 @@
|
|||||||
package memory
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"golang.org/x/text/language"
|
|
||||||
|
|
||||||
"github.com/zitadel/zitadel/internal/v3/instance"
|
|
||||||
"github.com/zitadel/zitadel/internal/v3/storage"
|
|
||||||
)
|
|
||||||
|
|
||||||
var _ instance.InstanceStorage = (*InstanceMemory)(nil)
|
|
||||||
|
|
||||||
type InstanceMemory struct {
|
|
||||||
instances map[string]*memoryInstance
|
|
||||||
mu sync.RWMutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewInstanceMemory() *InstanceMemory {
|
|
||||||
return &InstanceMemory{
|
|
||||||
instances: make(map[string]*memoryInstance),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteInstanceAdded implements instance.InstanceStorage.
|
|
||||||
func (i *InstanceMemory) WriteInstanceAdded(ctx context.Context, tx storage.Transaction, instance *instance.AddInstanceRequest) error {
|
|
||||||
defaultLanguage, err := language.Parse(instance.DefaultLanguage)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if instance.CreatedAt.IsZero() {
|
|
||||||
instance.CreatedAt = time.Now()
|
|
||||||
}
|
|
||||||
|
|
||||||
i.mu.Lock()
|
|
||||||
|
|
||||||
if i.instances[instance.ID] != nil {
|
|
||||||
return errors.New("instance already exists")
|
|
||||||
}
|
|
||||||
|
|
||||||
i.instances[instance.ID] = &memoryInstance{
|
|
||||||
id: instance.ID,
|
|
||||||
name: instance.InstanceName,
|
|
||||||
customDomain: instance.CustomDomain,
|
|
||||||
defaultLanguage: defaultLanguage,
|
|
||||||
}
|
|
||||||
|
|
||||||
tx.OnCommit(func(ctx context.Context) error {
|
|
||||||
i.mu.Unlock()
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
tx.OnRollback(func(ctx context.Context) error {
|
|
||||||
delete(i.instances, instance.ID)
|
|
||||||
i.mu.Unlock()
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type memoryInstance struct {
|
|
||||||
id string
|
|
||||||
name string
|
|
||||||
customDomain string
|
|
||||||
defaultLanguage language.Tag
|
|
||||||
}
|
|
@ -1,64 +0,0 @@
|
|||||||
package memory
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"log/slog"
|
|
||||||
|
|
||||||
"github.com/zitadel/zitadel/internal/v3/storage"
|
|
||||||
)
|
|
||||||
|
|
||||||
var _ storage.Client = (*Client)(nil)
|
|
||||||
|
|
||||||
type Client struct{}
|
|
||||||
|
|
||||||
func (c *Client) Begin(ctx context.Context) (storage.Transaction, error) {
|
|
||||||
return new(Transaction), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ storage.Transaction = (*Transaction)(nil)
|
|
||||||
|
|
||||||
type Transaction struct {
|
|
||||||
commitHooks []func(ctx context.Context) error
|
|
||||||
rollbackHooks []func(ctx context.Context) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// Commit implements storage.Transaction.
|
|
||||||
func (t *Transaction) Commit(ctx context.Context) error {
|
|
||||||
for _, hook := range t.commitHooks {
|
|
||||||
if err := hook(ctx); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// End implements storage.Transaction.
|
|
||||||
func (t *Transaction) End(ctx context.Context, err error) error {
|
|
||||||
if err != nil {
|
|
||||||
rollbackErr := t.Rollback(ctx)
|
|
||||||
slog.WarnContext(ctx, "Rollback failed", slog.Any("cause", rollbackErr))
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return t.Commit(ctx)
|
|
||||||
}
|
|
||||||
|
|
||||||
// OnCommit implements storage.Transaction.
|
|
||||||
func (t *Transaction) OnCommit(hook func(ctx context.Context) error) {
|
|
||||||
t.commitHooks = append(t.commitHooks, hook)
|
|
||||||
}
|
|
||||||
|
|
||||||
// OnRollback implements storage.Transaction.
|
|
||||||
func (t *Transaction) OnRollback(hook func(ctx context.Context) error) {
|
|
||||||
t.rollbackHooks = append(t.rollbackHooks, hook)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Rollback implements storage.Transaction.
|
|
||||||
func (t *Transaction) Rollback(ctx context.Context) error {
|
|
||||||
for _, hook := range t.rollbackHooks {
|
|
||||||
if err := hook(ctx); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -1,31 +0,0 @@
|
|||||||
package storage
|
|
||||||
|
|
||||||
import "context"
|
|
||||||
|
|
||||||
type Client interface {
|
|
||||||
Begin(ctx context.Context) (Transaction, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// type Command interface {
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type Query[R any] interface {
|
|
||||||
// Result() R
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type Executor interface {
|
|
||||||
// Execute(ctx context.Context, command Command) error
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type Querier interface {
|
|
||||||
// Query[R](ctx context.Context, query Query[R]) error
|
|
||||||
// }
|
|
||||||
|
|
||||||
type Transaction interface {
|
|
||||||
Commit(ctx context.Context) error
|
|
||||||
Rollback(ctx context.Context) error
|
|
||||||
// End the transaction based on err. If err is nil the transaction is committed, otherwise it is rolled back.
|
|
||||||
End(ctx context.Context, err error) error
|
|
||||||
OnCommit(hook func(ctx context.Context) error)
|
|
||||||
OnRollback(hook func(ctx context.Context) error)
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user