145 lines
5.1 KiB
Go
145 lines
5.1 KiB
Go
package connectors
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json/jsontext"
|
|
json "encoding/json/v2"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/danielgtaylor/huma/v2"
|
|
|
|
"github.com/samcharles93/archie-core/internal/platform/httpapi"
|
|
"github.com/samcharles93/archie-core/modules/connectors/internal/store"
|
|
)
|
|
|
|
// ExtensionView is one accepted package as the dashboard shows it: what it is,
|
|
// whether it is running, and the settings and credential slots it was given.
|
|
// It never carries a secret: a credential is named, and a secret setting is
|
|
// the reference the extension resolves.
|
|
type ExtensionView struct {
|
|
Name string `json:"name"`
|
|
Contract string `json:"contract"`
|
|
Group string `json:"group" enum:"platform,integration,agent"`
|
|
State string `json:"state"`
|
|
Error string `json:"error,omitempty"`
|
|
Version string `json:"version,omitempty"`
|
|
Digest string `json:"digest"`
|
|
Path string `json:"path,omitempty"`
|
|
URL string `json:"url,omitempty"`
|
|
Args []string `json:"args,omitempty"`
|
|
Egress []string `json:"egress"`
|
|
Env []string `json:"env"`
|
|
Settings map[string]string `json:"settings"`
|
|
SettingsSchema []Setting `json:"settings_schema"`
|
|
Credentials map[string]string `json:"credentials"`
|
|
Tools jsontext.Value `json:"tools,omitempty"`
|
|
}
|
|
|
|
// list returns the accepted packages of one contract, in name order.
|
|
func (m *Module) list(contract string) func(context.Context, *struct{}) (*httpapi.Reply[[]ExtensionView], error) {
|
|
return func(ctx context.Context, _ *struct{}) (*httpapi.Reply[[]ExtensionView], error) {
|
|
accepted, err := m.store.List(ctx, m.orgID)
|
|
if err != nil {
|
|
m.log.ErrorContext(ctx, "list accepted packages", "contract", contract, "error", err)
|
|
return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the accepted packages")
|
|
}
|
|
views := make([]ExtensionView, 0, len(accepted))
|
|
for _, a := range accepted {
|
|
if a.Contract != contract {
|
|
continue
|
|
}
|
|
view, err := m.viewOf(a)
|
|
if err != nil {
|
|
m.log.ErrorContext(ctx, "read accepted package", "package", a.Name, "error", err)
|
|
return nil, huma.NewError(http.StatusInternalServerError, "couldn't read "+a.Name)
|
|
}
|
|
views = append(views, view)
|
|
}
|
|
return httpapi.OK(views), nil
|
|
}
|
|
}
|
|
|
|
func (m *Module) viewOf(a store.Accepted) (ExtensionView, error) {
|
|
view := ExtensionView{
|
|
Name: a.Name, Contract: a.Contract, Group: string(store.GroupOf(a.Contract)), Digest: a.SHA256, Path: a.Path, URL: a.URL,
|
|
Args: a.Args, Egress: a.Egress, Env: a.Env, Tools: a.Tools,
|
|
Settings: map[string]string{}, Credentials: map[string]string{}, SettingsSchema: []Setting{},
|
|
}
|
|
if len(a.Settings) > 0 {
|
|
if err := json.Unmarshal(a.Settings, &view.Settings); err != nil {
|
|
return ExtensionView{}, fmt.Errorf("settings: %w", err)
|
|
}
|
|
}
|
|
if len(a.Credentials) > 0 {
|
|
if err := json.Unmarshal(a.Credentials, &view.Credentials); err != nil {
|
|
return ExtensionView{}, fmt.Errorf("credentials: %w", err)
|
|
}
|
|
}
|
|
state, reason, version, schema := m.stateOf(a)
|
|
view.State, view.Error, view.Version, view.SettingsSchema = state, reason, version, schema
|
|
return view, nil
|
|
}
|
|
|
|
// stateOf reports whether a package is running, and what it declared.
|
|
func (m *Module) stateOf(a store.Accepted) (state, reason, version string, schema []Setting) {
|
|
m.mu.RLock()
|
|
reason = m.failed[a.Name]
|
|
connector, running := m.running[a.Name]
|
|
engine, isEngine := m.engines[a.Name]
|
|
guard, isGuard := m.guards[a.Name]
|
|
server, isServer := m.servers[a.Name]
|
|
m.mu.RUnlock()
|
|
|
|
switch {
|
|
case reason != "":
|
|
return "failed", reason, "", nil
|
|
case running:
|
|
return "running", "", connector.Version(), connector.SettingsSchema()
|
|
case isEngine:
|
|
return "running", "", engine.Version(), engine.SettingsSchema()
|
|
case isGuard:
|
|
return "running", "", guard.Version(), guard.SettingsSchema()
|
|
case isServer:
|
|
if err := server.Ready(); err != nil {
|
|
return "failed", err.Error(), "", server.SettingsSchema()
|
|
}
|
|
return "running", "", "", server.SettingsSchema()
|
|
}
|
|
return "stopped", "", "", nil
|
|
}
|
|
|
|
// schemaOf returns the settings schema of a package that is running now.
|
|
func (m *Module) schemaOf(name string) ([]Setting, bool) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
switch {
|
|
case m.running[name] != nil:
|
|
return m.running[name].SettingsSchema(), true
|
|
case m.engines[name] != nil:
|
|
return m.engines[name].SettingsSchema(), true
|
|
case m.guards[name] != nil:
|
|
return m.guards[name].SettingsSchema(), true
|
|
case m.servers[name] != nil:
|
|
return m.servers[name].SettingsSchema(), true
|
|
}
|
|
return nil, false
|
|
}
|
|
|
|
// checkAccepted checks an acceptance's settings against the schema the package
|
|
// declared, once it is known.
|
|
func (m *Module) checkAccepted(a store.Accepted) error {
|
|
schema, ok := m.schemaOf(a.Name)
|
|
if !ok || len(schema) == 0 {
|
|
return nil
|
|
}
|
|
var values map[string]string
|
|
if len(a.Settings) > 0 {
|
|
if err := json.Unmarshal(a.Settings, &values); err != nil {
|
|
return fmt.Errorf("settings: %w", err)
|
|
}
|
|
}
|
|
_, err := checkSettings(schema, values)
|
|
return err
|
|
}
|