76 lines
2.9 KiB
Go
76 lines
2.9 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 accepted package, whatever its contract: the
|
|
// extensions page shows one list rather than one per kind.
|
|
func (m *Module) listExtensions(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", "error", err)
|
|
return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the accepted packages")
|
|
}
|
|
views := make([]ExtensionView, 0, len(accepted))
|
|
for _, a := range accepted {
|
|
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
|
|
}
|
|
|
|
// removeExtension removes an accepted package without naming its contract
|
|
// first. The contract is read from the row and handed to the same removal the
|
|
// per-kind routes use.
|
|
func (m *Module) removeExtension(ctx context.Context, in *nameInput) (*httpapi.Reply[packageState], error) {
|
|
accepted, err := m.store.List(ctx, m.orgID)
|
|
if err != nil {
|
|
m.log.ErrorContext(ctx, "list accepted packages", "error", err)
|
|
return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the accepted packages")
|
|
}
|
|
i := slices.IndexFunc(accepted, func(a store.Accepted) bool { return 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(accepted[i])
|
|
if err != nil {
|
|
return nil, huma.NewError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
reply, err := m.removeAs(ctx, in, accepted[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.AdminAuthorized(governance.ActionRead, governance.KindConnector), huma.Operation{
|
|
OperationID: "list-extensions", Method: http.MethodGet, Path: "/api/extensions",
|
|
Summary: "List every accepted package, whatever its contract", 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)
|
|
}
|