2021-05-12 14:07:37 -05:00
|
|
|
import { getBackendSrv } from '@grafana/runtime';
|
|
|
|
import { API_ROOT, GRAFANA_API_ROOT } from './constants';
|
2021-09-09 05:20:35 -05:00
|
|
|
import { PluginDetails, Org, LocalPlugin, RemotePlugin, CatalogPlugin, CatalogPluginDetails } from './types';
|
|
|
|
import { mergeLocalsAndRemotes, mergeLocalAndRemote } from './helpers';
|
|
|
|
|
|
|
|
export async function getCatalogPlugins(): Promise<CatalogPlugin[]> {
|
|
|
|
const [localPlugins, remotePlugins] = await Promise.all([getLocalPlugins(), getRemotePlugins()]);
|
|
|
|
|
|
|
|
return mergeLocalsAndRemotes(localPlugins, remotePlugins);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getCatalogPlugin(id: string): Promise<CatalogPlugin> {
|
|
|
|
const { local, remote } = await getPlugin(id);
|
|
|
|
|
|
|
|
return mergeLocalAndRemote(local, remote);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getPluginDetails(id: string): Promise<CatalogPluginDetails> {
|
2021-09-10 04:32:21 -05:00
|
|
|
const localPlugins = await getLocalPlugins();
|
2021-09-09 05:20:35 -05:00
|
|
|
const local = localPlugins.find((p) => p.id === id);
|
|
|
|
const isInstalled = Boolean(local);
|
|
|
|
const [remote, versions] = await Promise.all([getRemotePlugin(id, isInstalled), getPluginVersions(id)]);
|
2021-09-10 04:32:21 -05:00
|
|
|
const dependencies = remote?.json?.dependencies;
|
2021-09-09 05:20:35 -05:00
|
|
|
|
|
|
|
return {
|
2021-09-10 04:32:21 -05:00
|
|
|
grafanaDependency: dependencies?.grafanaDependency || dependencies?.grafanaVersion || '',
|
|
|
|
pluginDependencies: dependencies?.plugins || [],
|
2021-09-09 05:20:35 -05:00
|
|
|
links: remote?.json?.info.links || local?.info.links || [],
|
|
|
|
readme: remote?.readme,
|
|
|
|
versions,
|
|
|
|
};
|
|
|
|
}
|
2021-05-12 14:07:37 -05:00
|
|
|
|
2021-08-04 08:09:57 -05:00
|
|
|
async function getRemotePlugins(): Promise<RemotePlugin[]> {
|
2021-05-12 14:07:37 -05:00
|
|
|
const res = await getBackendSrv().get(`${GRAFANA_API_ROOT}/plugins`);
|
|
|
|
return res.items;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getPlugin(slug: string): Promise<PluginDetails> {
|
2021-09-09 05:20:35 -05:00
|
|
|
const installed = await getLocalPlugins();
|
2021-05-12 14:07:37 -05:00
|
|
|
|
2021-07-08 10:50:42 -05:00
|
|
|
const localPlugin = installed?.find((plugin: LocalPlugin) => {
|
|
|
|
return plugin.id === slug;
|
2021-05-12 14:07:37 -05:00
|
|
|
});
|
|
|
|
|
2021-09-09 05:20:35 -05:00
|
|
|
const [remote, versions] = await Promise.all([getRemotePlugin(slug, Boolean(localPlugin)), getPluginVersions(slug)]);
|
2021-07-08 10:50:42 -05:00
|
|
|
|
2021-05-12 14:07:37 -05:00
|
|
|
return {
|
2021-07-08 10:50:42 -05:00
|
|
|
remote: remote,
|
2021-05-12 14:07:37 -05:00
|
|
|
remoteVersions: versions,
|
2021-07-08 10:50:42 -05:00
|
|
|
local: localPlugin,
|
2021-05-12 14:07:37 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-09 05:20:35 -05:00
|
|
|
async function getRemotePlugin(id: string, isInstalled: boolean): Promise<RemotePlugin | undefined> {
|
2021-07-08 10:50:42 -05:00
|
|
|
try {
|
2021-09-09 05:20:35 -05:00
|
|
|
return await getBackendSrv().get(`${GRAFANA_API_ROOT}/plugins/${id}`);
|
2021-07-08 10:50:42 -05:00
|
|
|
} catch (error) {
|
|
|
|
// this might be a plugin that doesn't exist on gcom.
|
2021-09-09 05:20:35 -05:00
|
|
|
error.isHandled = isInstalled;
|
2021-07-08 10:50:42 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 14:07:37 -05:00
|
|
|
async function getPluginVersions(id: string): Promise<any[]> {
|
2021-07-08 10:50:42 -05:00
|
|
|
try {
|
|
|
|
const versions = await getBackendSrv().get(`${GRAFANA_API_ROOT}/plugins/${id}/versions`);
|
|
|
|
return versions.items;
|
|
|
|
} catch (error) {
|
|
|
|
return [];
|
|
|
|
}
|
2021-05-12 14:07:37 -05:00
|
|
|
}
|
|
|
|
|
2021-09-09 05:20:35 -05:00
|
|
|
async function getLocalPlugins(): Promise<LocalPlugin[]> {
|
2021-07-16 05:33:56 -05:00
|
|
|
const installed = await getBackendSrv().get(`${API_ROOT}`, { embedded: 0 });
|
2021-05-12 14:07:37 -05:00
|
|
|
return installed;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getOrg(slug: string): Promise<Org> {
|
|
|
|
const org = await getBackendSrv().get(`${GRAFANA_API_ROOT}/orgs/${slug}`);
|
|
|
|
return { ...org, avatarUrl: `${GRAFANA_API_ROOT}/orgs/${slug}/avatar` };
|
|
|
|
}
|
|
|
|
|
2021-09-09 05:20:35 -05:00
|
|
|
export async function installPlugin(id: string, version: string) {
|
2021-05-12 14:07:37 -05:00
|
|
|
return await getBackendSrv().post(`${API_ROOT}/${id}/install`, {
|
|
|
|
version,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-09 05:20:35 -05:00
|
|
|
export async function uninstallPlugin(id: string) {
|
2021-05-12 14:07:37 -05:00
|
|
|
return await getBackendSrv().post(`${API_ROOT}/${id}/uninstall`);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const api = {
|
|
|
|
getRemotePlugins,
|
|
|
|
getPlugin,
|
2021-09-09 05:20:35 -05:00
|
|
|
getInstalledPlugins: getLocalPlugins,
|
2021-05-12 14:07:37 -05:00
|
|
|
getOrg,
|
|
|
|
installPlugin,
|
|
|
|
uninstallPlugin,
|
|
|
|
};
|