@backstage-community/plugin-puppetdb
Version:
Backstage plugin to visualize resource information and Puppet facts from PuppetDB.
68 lines (65 loc) • 1.84 kB
JavaScript
import { ResponseError } from '@backstage/errors';
class PuppetDbClient {
discoveryApi;
fetchApi;
constructor({
discoveryApi,
fetchApi
}) {
this.discoveryApi = discoveryApi;
this.fetchApi = fetchApi;
}
async callApi(path, query) {
const apiUrl = `${await this.discoveryApi.getBaseUrl("proxy")}/puppetdb`;
const response = await this.fetchApi.fetch(
`${apiUrl}${path}?${new URLSearchParams(query).toString()}`,
{
headers: {
"Content-Type": "application/json"
}
}
);
if (response.ok) {
return await response.json();
}
throw await ResponseError.fromResponse(response);
}
async getPuppetDbReportEvents(puppetDbReportHash) {
if (!puppetDbReportHash) {
throw new Error("PuppetDB report hash is required");
}
const events = await this.callApi(
`/pdb/query/v4/reports/${puppetDbReportHash}/events`,
{}
);
if (!events || events.length === 0) {
return void 0;
}
return events;
}
async getPuppetDbReportLogs(puppetDbReportHash) {
if (!puppetDbReportHash) {
throw new Error("PuppetDB report hash is required");
}
const events = await this.callApi(
`/pdb/query/v4/reports/${puppetDbReportHash}/logs`,
{}
);
if (!events || events.length === 0) {
return void 0;
}
return events;
}
async getPuppetDbNodeReports(puppetDbCertName) {
if (!puppetDbCertName) {
throw new Error("PuppetDB certname is required");
}
return this.callApi(`/pdb/query/v4/reports`, {
query: `["=","certname","${puppetDbCertName}"]`,
order_by: `[{"field": "start_time", "order": "desc"},{"field": "end_time", "order": "desc"}]`,
limit: 100
});
}
}
export { PuppetDbClient };
//# sourceMappingURL=PuppetDbClient.esm.js.map