ERP items INF-CI-CLD-STD/BUS/ENT (Monat, 149/290/590 EUR) and INF-CI-OPR-STD/BUS/ENT (Jahr, 2900/5900/11900 EUR), priced per runner: a dedicated build machine per customer, never shared (2 vCPU/4 GB, 4 vCPU/8 GB, 8 vCPU/16 GB). Migration 0013 adds the managed service kind ci-runner (SKU ^INF-CI-(CLD|OPR)-(STD|BUS|ENT)$). A runner's url is the Git instance it serves, so the (tenant, kind, url) uniqueness now holds for kind git only (partial unique index); several runners may serve one instance. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
108 lines
2.6 KiB
Go
108 lines
2.6 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"sort"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// serviceTaken reports whether another Git instance of the tenant has the
|
|
// same url (unique index managed_services_git_url, 0013; several runners may
|
|
// serve one instance). Caller holds the lock.
|
|
func (m *Memory) serviceTaken(ms ManagedService) bool {
|
|
if ms.Kind != ManagedServiceKindGit {
|
|
return false
|
|
}
|
|
for _, x := range m.services {
|
|
if x.TenantID == ms.TenantID && x.ID != ms.ID && x.Kind == ms.Kind && x.URL == ms.URL {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (m *Memory) ListManagedServices(_ context.Context, tenantID string) ([]ManagedService, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
out := []ManagedService{}
|
|
for _, ms := range m.services {
|
|
if ms.TenantID == tenantID {
|
|
out = append(out, *ms)
|
|
}
|
|
}
|
|
sort.Slice(out, func(i, j int) bool {
|
|
if out[i].CreatedAt.Equal(out[j].CreatedAt) {
|
|
return out[i].ID < out[j].ID
|
|
}
|
|
return out[i].CreatedAt.Before(out[j].CreatedAt)
|
|
})
|
|
return out, nil
|
|
}
|
|
|
|
func (m *Memory) GetManagedService(_ context.Context, id string) (*ManagedService, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
ms, ok := m.services[id]
|
|
if !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
out := *ms
|
|
return &out, nil
|
|
}
|
|
|
|
func (m *Memory) CreateManagedService(_ context.Context, ms ManagedService) (*ManagedService, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if _, ok := m.tenants[ms.TenantID]; !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
if ms.Status == "" {
|
|
ms.Status = ManagedServiceProvisioning
|
|
}
|
|
if !ValidManagedServiceKind(ms.Kind) || !ValidManagedServiceStatus(ms.Status) {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
ms.ID = uuid.NewString()
|
|
if m.serviceTaken(ms) {
|
|
return nil, ErrConflict
|
|
}
|
|
now := time.Now().UTC()
|
|
ms.CreatedAt, ms.UpdatedAt = now, now
|
|
row := ms
|
|
m.services[ms.ID] = &row
|
|
out := row
|
|
return &out, nil
|
|
}
|
|
|
|
func (m *Memory) UpdateManagedService(_ context.Context, ms ManagedService) (*ManagedService, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
cur, ok := m.services[ms.ID]
|
|
if !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
ms.TenantID, ms.Kind = cur.TenantID, cur.Kind
|
|
if !ValidManagedServiceStatus(ms.Status) {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
if m.serviceTaken(ms) {
|
|
return nil, ErrConflict
|
|
}
|
|
cur.Name, cur.URL, cur.SKU, cur.Status = ms.Name, ms.URL, ms.SKU, ms.Status
|
|
cur.InstanceID, cur.Notes, cur.UpdatedAt = ms.InstanceID, ms.Notes, time.Now().UTC()
|
|
out := *cur
|
|
return &out, nil
|
|
}
|
|
|
|
func (m *Memory) DeleteManagedService(_ context.Context, id string) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if _, ok := m.services[id]; !ok {
|
|
return ErrNotFound
|
|
}
|
|
delete(m.services, id)
|
|
return nil
|
|
}
|