UNPKG

@backstage/plugin-catalog-backend

Version:

The Backstage backend plugin that provides the Backstage catalog

63 lines (59 loc) 2.13 kB
'use strict'; var catalogModel = require('@backstage/catalog-model'); var errors = require('@backstage/errors'); const createGetCatalogEntityAction = ({ catalog, actionsRegistry }) => { actionsRegistry.register({ name: "get-catalog-entity", title: "Get Catalog Entity", description: ` This allows you to get a single entity from the software catalog. Each entity in the software catalog has a unique name, kind, and namespace. The default namespace is "default". Each entity is identified by a unique entity reference, which is a string of the form "kind:namespace/name". `, schema: { input: (z) => z.object({ kind: z.string().describe( "The kind of the entity to query. If the kind is unknown it can be omitted." ).optional(), namespace: z.string().describe( "The namespace of the entity to query. If the namespace is unknown it can be omitted." ).optional(), name: z.string().describe("The name of the entity to query") }), // TODO: is there a better way to do this? output: (z) => z.object({}).passthrough() }, action: async ({ input, credentials }) => { const filter = { "metadata.name": input.name }; if (input.kind) { filter.kind = input.kind; } if (input.namespace) { filter["metadata.namespace"] = input.namespace; } const { items } = await catalog.queryEntities( { filter }, { credentials } ); if (items.length === 0) { throw new errors.InputError(`No entity found with name "${input.name}"`); } if (items.length > 1) { throw new Error( `Multiple entities found with name "${input.name}", please provide more specific filters. Entities found: ${items.map((item) => `"${catalogModel.stringifyEntityRef(item)}"`).join(", ")}` ); } const [entity] = items; return { output: entity }; } }); }; exports.createGetCatalogEntityAction = createGetCatalogEntityAction; //# sourceMappingURL=createGetCatalogEntityAction.cjs.js.map