Packages are accepted once by the instance. Installations configure them at the instance, an organisation or a person; bindings and credentials resolve the person's, then their organisation's, then the instance's. Decisions, approval requests and held chat actions record the installation and scope that acted. The dashboard lists packages separately from installations, shows every installation's scope, and creates, edits and disables installations at their scope.
63 lines
2.5 KiB
Go
63 lines
2.5 KiB
Go
package connectors
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"slices"
|
|
|
|
"github.com/danielgtaylor/huma/v2"
|
|
|
|
"github.com/samcharles93/archie-core/internal/platform/httpapi"
|
|
"github.com/samcharles93/archie-core/modules/connectors/internal/store"
|
|
"github.com/samcharles93/archie-core/modules/governance"
|
|
)
|
|
|
|
// listExtensions returns every installation the caller can see, whatever its
|
|
// contract and scope: the extensions page shows one list rather than one per
|
|
// kind.
|
|
func (m *Module) listExtensions(ctx context.Context, _ *struct{}) (*httpapi.Reply[[]ExtensionView], error) {
|
|
return m.listViews(ctx, func(store.Installed) bool { return true })
|
|
}
|
|
|
|
// removeExtension removes an accepted package without naming its contract
|
|
// first. The contract is read from the package's instance installation and
|
|
// handed to the same removal the per-kind routes use.
|
|
func (m *Module) removeExtension(ctx context.Context, in *nameInput) (*httpapi.Reply[packageState], error) {
|
|
installed, err := m.store.List(ctx)
|
|
if err != nil {
|
|
m.log.ErrorContext(ctx, "list installations", "error", err)
|
|
return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the installations")
|
|
}
|
|
i := slices.IndexFunc(installed, func(a store.Installed) bool { return a.Scope == store.ScopeInstance && a.Name == in.Name })
|
|
if i < 0 {
|
|
return nil, huma.NewError(http.StatusNotFound, fmt.Sprintf("no accepted package named %s", in.Name))
|
|
}
|
|
_, managed, err := packageOf(installed[i])
|
|
if err != nil {
|
|
return nil, huma.NewError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
reply, err := m.removeAs(ctx, in, installed[i].Contract)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if managed {
|
|
if err := m.fetcher.Forget(in.Name); err != nil {
|
|
m.log.WarnContext(ctx, "keep extension artifacts", "package", in.Name, "error", err)
|
|
}
|
|
}
|
|
return reply, nil
|
|
}
|
|
|
|
func (m *Module) registerExtensions(api huma.API) {
|
|
tags := []string{"Extensions"}
|
|
httpapi.Register(api, httpapi.Authorized(governance.ActionRead, governance.KindConnector), huma.Operation{
|
|
OperationID: "list-extensions", Method: http.MethodGet, Path: "/api/extensions",
|
|
Summary: "List every installation the caller can see, whatever its contract and scope", Tags: tags,
|
|
}, m.listExtensions)
|
|
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionDelete, governance.KindConnector), huma.Operation{
|
|
OperationID: "remove-extension", Method: http.MethodDelete, Path: "/api/extensions/{name}",
|
|
Summary: "Remove an accepted package", Tags: tags,
|
|
}, m.removeExtension)
|
|
}
|