52 lines
1.8 KiB
Go
52 lines
1.8 KiB
Go
package statestore
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/distribution"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
domain "github.com/samcharles93/archie-core/internal/domain/storepkg"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanestore"
|
|
registry "github.com/samcharles93/archie-core/internal/infrastructure/storepkg"
|
|
)
|
|
|
|
// settingsCatalogue fetches the catalogue from the source the extension
|
|
// settings name, read on every fetch so an edit applies without a restart.
|
|
type settingsCatalogue struct {
|
|
resources auditRetentionResources
|
|
}
|
|
|
|
// liveCatalogue is the catalogue the dashboard lists; a store that cannot
|
|
// read resources lists the official catalogue.
|
|
func (b *server) liveCatalogue() domain.CatalogueSource {
|
|
resources, ok := b.st.(auditRetentionResources)
|
|
if !ok {
|
|
return registry.Catalogue(distribution.DefaultCatalogue())
|
|
}
|
|
return settingsCatalogue{resources: resources}
|
|
}
|
|
|
|
func (c settingsCatalogue) Fetch(ctx context.Context) (domain.Catalogue, error) {
|
|
var settings controlplanerpc.ExtensionSettings
|
|
resource, err := c.resources.Resource(ctx, storecontract.DefaultOrgID, controlplanestore.ExtensionSettingsKind)
|
|
switch {
|
|
case errors.Is(err, storecontract.ErrResourceNotFound):
|
|
case err != nil:
|
|
return domain.Catalogue{}, fmt.Errorf("read extension settings: %w", err)
|
|
}
|
|
if len(resource.Value) > 0 {
|
|
if err := json.Unmarshal(resource.Value, &settings); err != nil {
|
|
return domain.Catalogue{}, fmt.Errorf("decode extension settings: %w", err)
|
|
}
|
|
}
|
|
source, err := settings.Catalogue()
|
|
if err != nil {
|
|
return domain.Catalogue{}, err
|
|
}
|
|
return registry.Catalogue(source).Fetch(ctx)
|
|
}
|