Managed Git (one Gitea instance per customer, INF-GIT-{CLD,OPR}-{STD,BUS,ENT})
is provisioned by hand; the registry now keeps the record so the portal can
show it under "Verwaltete Infrastruktur" and staff can maintain it.
- 0012_managed_services: table with kind/status checks, UNIQUE (tenant_id,
kind, url), FK cascade on tenant delete (mirrored in the memory store).
- GET /v1/tenants/{id}/managed-services: tenant view without instance_id and
notes; user tokens need IT_ADMIN, OWNER or CXO (else FORBIDDEN_ROLE).
- GET/POST /v1/admin/tenants/{id}/managed-services and PATCH/DELETE
.../{sid}: staff view and maintenance, validation codes invalid_kind,
invalid_name, invalid_url, invalid_sku, invalid_status,
invalid_instance_id, invalid_notes; 409 duplicate.
- Audit managed_service.created/updated/deleted with the staff actor;
updated carries the changed fields and old/new status.
- OpenAPI spec and tests.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
104 lines
2.5 KiB
Go
104 lines
2.5 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"sort"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// serviceTaken reports whether another service of the tenant has the same
|
|
// kind and url (UNIQUE (tenant_id, kind, url) in 0012). Caller holds the lock.
|
|
func (m *Memory) serviceTaken(ms ManagedService) bool {
|
|
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
|
|
}
|