Add resumable checked migrations and explicit schema administration commands. Port ML budgets, namespaces, identity and group persistence with three-backend contracts. Keep MySQL unavailable as the full application backend.
115 lines
4.2 KiB
Go
115 lines
4.2 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"strings"
|
|
"unicode/utf8"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// MySQLNamespaceRepository exposes only the ported subset. In particular Domain
|
|
// mutations and Campus deletion require auth/federation repositories which are
|
|
// not ported yet. No *Store escapes and no server backend is enabled here.
|
|
type MySQLNamespaceRepository struct{ store *Store }
|
|
|
|
func NewMySQLNamespaceRepository(database *sql.DB) *MySQLNamespaceRepository {
|
|
return &MySQLNamespaceRepository{store: &Store{DB: database, sqlBackend: storeMySQL}}
|
|
}
|
|
|
|
func mysqlStoreError(operation string, err error) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
for _, known := range []error{sql.ErrNoRows, context.Canceled, context.DeadlineExceeded, ErrReadOnly, ErrBackendFeatureUnavailable, ErrAccessDenied} {
|
|
if errors.Is(err, known) {
|
|
return known
|
|
}
|
|
}
|
|
var validation namespaceValidationError
|
|
if errors.As(err, &validation) {
|
|
return validation
|
|
}
|
|
// Duplicate/truncation/encoding errors may contain private names or values.
|
|
// operation is an owned constant, not a request parameter.
|
|
return errors.New("MySQL " + operation + " failed")
|
|
}
|
|
|
|
func (r *MySQLNamespaceRepository) Campuses(ctx context.Context) ([]Campus, error) {
|
|
return r.store.ListCampusesContext(ctx)
|
|
}
|
|
func (r *MySQLNamespaceRepository) Domains(ctx context.Context) ([]Domain, error) {
|
|
return r.store.ListDomainsContext(ctx)
|
|
}
|
|
func (r *MySQLNamespaceRepository) ResolveHost(ctx context.Context, host string) (*Domain, error) {
|
|
return r.store.GetDomainByHostContext(ctx, host)
|
|
}
|
|
func (r *MySQLNamespaceRepository) EffectiveHost(ctx context.Context, domain string) (string, error) {
|
|
if err := mysqlNamespaceUUID(domain); err != nil {
|
|
return "", err
|
|
}
|
|
return r.store.GetDomainEffectiveHostContext(ctx, domain)
|
|
}
|
|
func (r *MySQLNamespaceRepository) CreateCampus(ctx context.Context, name, title, language, mode, fqdn string, enabled bool) error {
|
|
if utf8.RuneCountInString(normalizeCampusName(name)) > 255 {
|
|
return namespaceValidationError("campus name too long")
|
|
}
|
|
return r.store.CreateCampusContext(ctx, name, title, language, mode, fqdn, enabled)
|
|
}
|
|
func (r *MySQLNamespaceRepository) UpdateCampus(ctx context.Context, id int64, name, title, language, mode, fqdn string, enabled bool) error {
|
|
if strings.EqualFold(strings.TrimSpace(mode), "managed") {
|
|
return namespaceValidationError("managed campus must be added through controller enrollment")
|
|
}
|
|
if utf8.RuneCountInString(normalizeCampusName(name)) > 255 {
|
|
return namespaceValidationError("campus name too long")
|
|
}
|
|
return r.store.UpdateCampusContext(ctx, id, name, title, language, mode, fqdn, enabled)
|
|
}
|
|
func mysqlNamespaceUUID(value string) error {
|
|
id, err := uuid.Parse(value)
|
|
if err != nil || id.String() != value {
|
|
return namespaceValidationError("canonical domain UUID required")
|
|
}
|
|
return nil
|
|
}
|
|
func mysqlNamespaceConfigKey(domain, key string) error {
|
|
if err := mysqlNamespaceUUID(domain); err != nil {
|
|
return err
|
|
}
|
|
if !utf8.ValidString(key) || utf8.RuneCountInString(key) > 255 {
|
|
return namespaceValidationError("configuration key invalid or too long")
|
|
}
|
|
return nil
|
|
}
|
|
func (r *MySQLNamespaceRepository) Config(ctx context.Context, domain, key string) (string, error) {
|
|
if err := mysqlNamespaceConfigKey(domain, key); err != nil {
|
|
return "", err
|
|
}
|
|
if _, err := r.store.GetDomainByUUIDContext(ctx, domain); err != nil {
|
|
return "", err
|
|
}
|
|
return r.store.GetConfigInDomainContext(ctx, domain, key)
|
|
}
|
|
func (r *MySQLNamespaceRepository) SetConfig(ctx context.Context, domain, key, value string) error {
|
|
if r.store.ReadOnly {
|
|
return ErrReadOnly
|
|
}
|
|
if err := mysqlNamespaceConfigKey(domain, key); err != nil {
|
|
return err
|
|
}
|
|
// ML policy writes must retain generation CAS; the generic config API is
|
|
// not a second way to overwrite an active reservation's policy.
|
|
if key == machineLearningConfigKey {
|
|
return namespaceValidationError("use ML policy compare-and-swap")
|
|
}
|
|
return r.store.namespaceWrite(ctx, func(ctx context.Context, tx *sql.Tx) error {
|
|
var found int
|
|
if err := tx.QueryRowContext(ctx, `SELECT 1 FROM domains WHERE uuid=?`, domain).Scan(&found); err != nil {
|
|
return err
|
|
}
|
|
return r.store.setConfigWith(ctx, tx, domain, key, value)
|
|
})
|
|
}
|