Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions internal/repositories/mock/global_lb_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mock

import (
"context"
"sync"

"github.com/google/uuid"
"github.com/poyrazk/thecloud/internal/core/domain"
Expand All @@ -11,6 +12,7 @@ import (

// MockGlobalLBRepo is a mock implementation of the GlobalLBRepository port.
type MockGlobalLBRepo struct {
mu sync.RWMutex
GLBs map[uuid.UUID]*domain.GlobalLoadBalancer
Endpoints map[uuid.UUID][]*domain.GlobalEndpoint
}
Expand All @@ -24,28 +26,37 @@ func NewMockGlobalLBRepo() *MockGlobalLBRepo {
}

func (m *MockGlobalLBRepo) Create(ctx context.Context, glb *domain.GlobalLoadBalancer) error {
m.mu.Lock()
defer m.mu.Unlock()
m.GLBs[glb.ID] = glb
return nil
}

func (m *MockGlobalLBRepo) GetByID(ctx context.Context, id uuid.UUID) (*domain.GlobalLoadBalancer, error) {
m.mu.RLock()
defer m.mu.RUnlock()
if glb, ok := m.GLBs[id]; ok {
// return copy
return glb, nil
glbCopy := *glb
return &glbCopy, nil
}
return nil, nil // simplified
return nil, nil
}

func (m *MockGlobalLBRepo) GetByHostname(ctx context.Context, hostname string) (*domain.GlobalLoadBalancer, error) {
m.mu.RLock()
defer m.mu.RUnlock()
for _, glb := range m.GLBs {
if glb.Hostname == hostname {
return glb, nil
glbCopy := *glb
return &glbCopy, nil
}
}
return nil, nil
}

func (m *MockGlobalLBRepo) List(ctx context.Context, userID uuid.UUID) ([]*domain.GlobalLoadBalancer, error) {
m.mu.RLock()
defer m.mu.RUnlock()
var list []*domain.GlobalLoadBalancer
for _, glb := range m.GLBs {
if glb.UserID == userID {
Expand All @@ -56,23 +67,31 @@ func (m *MockGlobalLBRepo) List(ctx context.Context, userID uuid.UUID) ([]*domai
}

func (m *MockGlobalLBRepo) Update(ctx context.Context, glb *domain.GlobalLoadBalancer) error {
m.mu.Lock()
defer m.mu.Unlock()
m.GLBs[glb.ID] = glb
return nil
}

func (m *MockGlobalLBRepo) Delete(ctx context.Context, id uuid.UUID, userID uuid.UUID) error {
m.mu.Lock()
defer m.mu.Unlock()
if glb, ok := m.GLBs[id]; ok && glb.UserID == userID {
delete(m.GLBs, id)
}
return nil
}

func (m *MockGlobalLBRepo) AddEndpoint(ctx context.Context, ep *domain.GlobalEndpoint) error {
m.mu.Lock()
defer m.mu.Unlock()
m.Endpoints[ep.GlobalLBID] = append(m.Endpoints[ep.GlobalLBID], ep)
return nil
}

func (m *MockGlobalLBRepo) RemoveEndpoint(ctx context.Context, endpointID uuid.UUID) error {
m.mu.Lock()
defer m.mu.Unlock()
// inefficient but mock
for glbID, eps := range m.Endpoints {
var newEps []*domain.GlobalEndpoint
Expand All @@ -87,21 +106,31 @@ func (m *MockGlobalLBRepo) RemoveEndpoint(ctx context.Context, endpointID uuid.U
}

func (m *MockGlobalLBRepo) GetEndpointByID(ctx context.Context, endpointID uuid.UUID) (*domain.GlobalEndpoint, error) {
m.mu.RLock()
defer m.mu.RUnlock()
for _, eps := range m.Endpoints {
for _, ep := range eps {
if ep.ID == endpointID {
return ep, nil
epCopy := *ep
return &epCopy, nil
}
}
}
return nil, nil
}

func (m *MockGlobalLBRepo) ListEndpoints(ctx context.Context, glbID uuid.UUID) ([]*domain.GlobalEndpoint, error) {
return m.Endpoints[glbID], nil
m.mu.RLock()
defer m.mu.RUnlock()
eps := m.Endpoints[glbID]
epsCopy := make([]*domain.GlobalEndpoint, len(eps))
copy(epsCopy, eps)
return epsCopy, nil
}
Comment on lines 122 to 129

func (m *MockGlobalLBRepo) UpdateEndpointHealth(ctx context.Context, epID uuid.UUID, healthy bool) error {
m.mu.Lock()
defer m.mu.Unlock()
for _, eps := range m.Endpoints {
for _, ep := range eps {
if ep.ID == epID {
Expand Down
25 changes: 22 additions & 3 deletions internal/repositories/mock/lb_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package mock

import (
"context"
"sync"

"github.com/google/uuid"
"github.com/poyrazk/thecloud/internal/core/domain"
"github.com/poyrazk/thecloud/internal/core/ports"
)

type MockLBRepo struct {
mu sync.RWMutex
LBs map[uuid.UUID]*domain.LoadBalancer
}

Expand All @@ -19,46 +21,63 @@ func NewMockLBRepo() *MockLBRepo {
}

func (m *MockLBRepo) Create(ctx context.Context, lb *domain.LoadBalancer) error {
m.mu.Lock()
defer m.mu.Unlock()
m.LBs[lb.ID] = lb
return nil
}

func (m *MockLBRepo) GetByID(ctx context.Context, id uuid.UUID) (*domain.LoadBalancer, error) {
m.mu.RLock()
defer m.mu.RUnlock()
if lb, ok := m.LBs[id]; ok {
return lb, nil
lbCopy := *lb
return &lbCopy, nil
}
Comment on lines 30 to 36
return nil, nil
}

func (m *MockLBRepo) GetByName(ctx context.Context, name string) (*domain.LoadBalancer, error) {
m.mu.RLock()
defer m.mu.RUnlock()
for _, lb := range m.LBs {
if lb.Name == name {
return lb, nil
lbCopy := *lb
return &lbCopy, nil
}
}
return nil, nil
}

func (m *MockLBRepo) Update(ctx context.Context, lb *domain.LoadBalancer) error {
m.mu.Lock()
defer m.mu.Unlock()
m.LBs[lb.ID] = lb
return nil
}

func (m *MockLBRepo) Delete(ctx context.Context, id uuid.UUID) error {
m.mu.Lock()
defer m.mu.Unlock()
delete(m.LBs, id)
return nil
}

func (m *MockLBRepo) GetByIdempotencyKey(ctx context.Context, key string) (*domain.LoadBalancer, error) {
m.mu.RLock()
defer m.mu.RUnlock()
for _, lb := range m.LBs {
if lb.IdempotencyKey == key {
return lb, nil
lbCopy := *lb
return &lbCopy, nil
}
}
return nil, nil
}

func (m *MockLBRepo) List(ctx context.Context) ([]*domain.LoadBalancer, error) {
m.mu.RLock()
defer m.mu.RUnlock()
list := make([]*domain.LoadBalancer, 0, len(m.LBs))
for _, lb := range m.LBs {
list = append(list, lb)
Expand Down
Loading