All files / src/models config-provider.js

38.23% Statements 13/34
100% Branches 1/1
0% Functions 0/1
38.23% Lines 13/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x                                            
import { get as getAddon } from '@clevercloud/client/esm/api/v2/addon.js';
import { findAddonsByNameOrId } from './ids-resolver.js';
import { sendToApi } from './send-to-api.js';
 
/**
 * Resolve a config provider ID from a name, ID or real ID
 * @param {string} addonIdOrRealIdOrName The add-on ID (addon_xxx), real ID (config_xxx) or name
 * @returns {Promise<{ ownerId: string, realId: string, addonId: string }>} The owner ID, real ID and addon ID
 * @throws {Error} If the add-on is not found
 * @throws {Error} If multiple add-ons are found with the same name
 * @throws {Error} If the add-on is not a configuration provider
 */
export async function resolveConfigProviderId(addonIdOrRealIdOrName) {
  const candidates = await findAddonsByNameOrId(addonIdOrRealIdOrName);

  if (candidates.length === 0) {
    throw new Error(`Config provider not found: ${addonIdOrRealIdOrName}`);
  }

  if (candidates.length > 1) {
    throw new Error(`Ambiguous config provider name '${addonIdOrRealIdOrName}', please use the ID`);
  }

  const { ownerId, addonId, realId } = candidates[0];

  // Verify that the addon is a config provider
  const addon = await getAddon({ id: ownerId, addonId }).then(sendToApi);

  if (addon.provider.id !== 'config-provider') {
    throw new Error(`The add-on '${addonIdOrRealIdOrName}' is not a configuration provider`);
  }

  return { ownerId, realId, addonId };
}