UNPKG

@roadiehq/backstage-plugin-argo-cd

Version:
160 lines (157 loc) 5.08 kB
import { createApiRef } from '@backstage/core-plugin-api'; import { argoCDAppList, argoCDAppDeployRevisionDetails, argoCDAppDetails, argoCDServiceList } from '../types.esm.js'; import { decode, isDecodeError } from 'io-ts-promise'; import reporter from 'io-ts-reporters'; const argoCDApiRef = createApiRef({ id: "plugin.argocd.service" }); const APP_NAMESPACE_QUERY_PARAM = "appNamespace"; class ArgoCDApiClient { discoveryApi; backendBaseUrl; searchInstances; identityApi; useNamespacedApps; constructor(options) { this.discoveryApi = options.discoveryApi; this.backendBaseUrl = options.backendBaseUrl; this.searchInstances = options.searchInstances; this.identityApi = options.identityApi; this.useNamespacedApps = options.useNamespacedApps; } async getBaseUrl() { if (this.searchInstances) { return `${this.backendBaseUrl}/api/argocd`; } return await this.discoveryApi.getBaseUrl("proxy"); } getQueryParams(params) { const result = Object.keys(params).filter((key) => params[key] !== void 0).filter( (key) => key !== APP_NAMESPACE_QUERY_PARAM || this.useNamespacedApps ).map( (k) => `${encodeURIComponent(k)}=${encodeURIComponent(params[k])}` ).join("&"); return result ? `?${result}` : ""; } async fetchDecode(url, typeCodec) { const { token } = await this.identityApi.getCredentials(); const response = await fetch(url, { headers: token ? { "Content-Type": "application/json", Authorization: `Bearer ${token}` } : void 0 }); if (!response.ok) { throw new Error( `failed to fetch data, status ${response.status}: ${response.statusText}` ); } const json = await response.json(); try { return await decode(typeCodec, json); } catch (e) { if (isDecodeError(e)) { throw new Error( `remote data validation failed: ${reporter.report(typeCodec.decode(json)).join("; ")}` ); } else { throw e; } } } async listApps(options) { const proxyUrl = await this.getBaseUrl(); const query = this.getQueryParams({ selector: options.appSelector, project: options.projectName, appNamespace: options.appNamespace }); return this.fetchDecode( `${proxyUrl}${options.url}/applications${query}`, argoCDAppList ); } async getRevisionDetails(options) { const proxyUrl = await this.getBaseUrl(); const query = this.getQueryParams({ appNamespace: options.appNamespace }); if (this.searchInstances) { return this.fetchDecode( `${proxyUrl}/argoInstance/${options.instanceName}/applications/name/${encodeURIComponent( options.app )}/revisions/${encodeURIComponent( options.revisionID )}/metadata${query}`, argoCDAppDeployRevisionDetails ); } return this.fetchDecode( `${proxyUrl}${options.url}/applications/${encodeURIComponent( options.app )}/revisions/${encodeURIComponent( options.revisionID )}/metadata${query}`, argoCDAppDeployRevisionDetails ); } async getAppDetails(options) { const proxyUrl = await this.getBaseUrl(); const query = this.getQueryParams({ appNamespace: options.appNamespace }); if (this.searchInstances) { return this.fetchDecode( `${proxyUrl}/argoInstance/${options.instance}/applications/name/${encodeURIComponent( options.appName )}${query}`, argoCDAppDetails ); } return this.fetchDecode( `${proxyUrl}${options.url}/applications/${encodeURIComponent( options.appName )}${query}`, argoCDAppDetails ); } async getAppListDetails(options) { const proxyUrl = await this.getBaseUrl(); const query = this.getQueryParams({ appNamespace: options.appNamespace }); if (this.searchInstances) { return this.fetchDecode( `${proxyUrl}/argoInstance/${options.instance}/applications/selector/${encodeURIComponent( options.appSelector )}${query}`, argoCDAppList ); } return this.fetchDecode( `${proxyUrl}${options.url}/applications/selector/${encodeURIComponent( options.appSelector )}${query}`, argoCDAppList ); } async serviceLocatorUrl(options) { if (!options.appName && !options.appSelector) { throw new Error("Need to provide appName or appSelector"); } const baseUrl = await this.getBaseUrl(); const query = this.getQueryParams({ appNamespace: options.appNamespace }); const url = options.appName ? `${baseUrl}/find/name/${encodeURIComponent( options.appName )}${query}` : `${baseUrl}/find/selector/${encodeURIComponent( options.appSelector )}${query}`; return this.fetchDecode(url, argoCDServiceList).catch((_) => { throw new Error("Cannot get argo location(s) for service"); }); } } export { ArgoCDApiClient, argoCDApiRef }; //# sourceMappingURL=index.esm.js.map