@puls-atlas/cli
Version:
The Puls Atlas CLI tool for managing Atlas projects
38 lines • 2.15 kB
JavaScript
import { normalizeOptionalString } from '../../utils/value.js';
import { SEARCH_EVENTARC_DEFAULTS } from './eventarc.js';
import { getCommandErrorMessage, parseGcloudJsonOutput, runGcloudFileCommand } from '../../utils/gcloud.js';
export const getConfiguredSearchFirestoreEventarcDatabase = (context, options = {}) => normalizeOptionalString(options.eventarcDatabase) ?? normalizeOptionalString(context?.config?.deploy?.eventarc?.database) ?? SEARCH_EVENTARC_DEFAULTS.database;
export const getConfiguredSearchFirestoreEventarcTriggerRegion = (context, options = {}) => normalizeOptionalString(options.eventarcTriggerRegion) ?? normalizeOptionalString(context?.config?.deploy?.eventarc?.region);
export const resolveSearchFirestoreEventarcTriggerRegion = (context, options = {}) => {
const projectId = normalizeOptionalString(context?.projectId);
const databaseId = getConfiguredSearchFirestoreEventarcDatabase(context, options);
const configuredTriggerRegion = getConfiguredSearchFirestoreEventarcTriggerRegion(context, options);
if (configuredTriggerRegion) {
return {
databaseId,
source: 'config',
triggerRegion: configuredTriggerRegion
};
}
if (!projectId) {
throw new Error('Could not resolve Firestore database location for Eventarc because no Google Cloud project is selected.');
}
try {
const output = runGcloudFileCommand(['firestore', 'databases', 'describe', `--database=${databaseId}`, `--project=${projectId}`, '--format=json'], {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe']
}, options.runCommand);
const firestoreDatabase = parseGcloudJsonOutput(output, `Firestore database ${databaseId} metadata`);
const triggerRegion = normalizeOptionalString(firestoreDatabase?.locationId);
if (!triggerRegion) {
throw new Error(`Firestore database ${databaseId} metadata did not include a locationId value.`);
}
return {
databaseId,
source: 'gcloud',
triggerRegion
};
} catch (error) {
throw new Error(`Could not resolve Firestore database ${databaseId} location from gcloud. ${getCommandErrorMessage(error).trim()}`);
}
};