import { createResourceType as _createResourceType, deleteResourceType as _deleteResourceType, getResourceType as _getResourceType, getResourceTypeByName as _getResourceTypeByName, getResourceTypes as _getResourceTypes, putResourceType as _putResourceType, type ResourceTypeSkeleton, } from '../api/ResourceTypesApi'; import { State } from '../shared/State'; import { createProgressIndicator, debugMessage, stopProgressIndicator, updateProgressIndicator, } from '../utils/Console'; import { getMetadata, updateRemote } from '../utils/ExportImportUtils'; import { getCurrentRealmName } from '../utils/ForgeRockUtils'; import { FrodoError } from './FrodoError'; import { ExportMetaData, ResultCallback } from './OpsTypes'; export type ResourceType = { /** * Read resource type * @param resourceTypeUuid resource type uuid * @returns {Promise} a promise that resolves to a resource type object */ readResourceType(resourceTypeUuid: string): Promise; /** * Read all resource types * @returns {Promise} a promise that resolves to an array of resource type objects */ readResourceTypes(): Promise; /** * Read resource type by name * @param {string} resourceTypeName resource type name * @returns {Promise} a promise that resolves to a resource type object */ readResourceTypeByName( resourceTypeName: string ): Promise; /** * Create resource type * @param resourceTypeData resource type data * @param resourceTypeUuid resource type uuid * @returns {Promise} a promise that resolves to a resource type object */ createResourceType( resourceTypeData: ResourceTypeSkeleton, resourceTypeUuid?: string ): Promise; /** * Update resource type * @param resourceTypeUuid resource type uuid * @param {string} resourceTypeData resource type data * @returns {Promise} a promise that resolves to a resource type object, or null if no update was made */ updateResourceType( resourceTypeUuid: string, resourceTypeData: ResourceTypeSkeleton ): Promise; /** * Delete resource type * @param {string} resourceTypeUuid resource type uuid * @returns {Promise} Promise resolvig to a resource type object */ deleteResourceType(resourceTypeUuid: string): Promise; /** * Delete resource type by name * @param {string} resourceTypeName resource type name * @returns {Promise} Promise resolvig to a resource type object */ deleteResourceTypeByName( resourceTypeName: string ): Promise; /** * Export resource type * @param {string} resourceTypeUuid resource type uuid * @returns {Promise} a promise that resolves to an ResourceTypeExportInterface object */ exportResourceType( resourceTypeUuid: string ): Promise; /** * Export resource type by name * @param {string} resourceTypeName resource type name * @returns {Promise} a promise that resolves to an ResourceTypeExportInterface object */ exportResourceTypeByName( resourceTypeName: string ): Promise; /** * Export resource types * @returns {Promise} a promise that resolves to an ResourceTypeExportInterface object */ exportResourceTypes(): Promise; /** * Import resource type by uuid * @param {string} resourceTypeUuid client uuid * @param {ResourceTypeExportInterface} importData import data * @returns {Promise} a promise that resolves to the imported resource type object, or null if no update was made */ importResourceType( resourceTypeUuid: string, importData: ResourceTypeExportInterface ): Promise; /** * Import resource type by name * @param {string} resourceTypeName client id * @param {ResourceTypeExportInterface} importData import data * @returns {Promise} a promise that resolves to the imported resource type object, or null if no update was made */ importResourceTypeByName( resourceTypeName: string, importData: ResourceTypeExportInterface ): Promise; /** * Import first resource type * @param {ResourceTypeExportInterface} importData import data * @returns {Promise} a promise that resolves to the imported resource type object, or null if no update was made */ importFirstResourceType( importData: ResourceTypeExportInterface ): Promise; /** * Import resource types * @param {ResourceTypeExportInterface} importData import data * @param {ResultCallback} resultCallback Optional callback to process individual results. When given, the outcome of every resource type is passed to it once, as it happens: a resource type that was imported as the resource type (a resource type that needed no update as it is in the import data), a resource type that could not be imported as an error that names the resource type and carries the cause. Such a resource type is not part of the result and does not stop the others, and the function does not throw for it. Without it, the failures are collected and thrown as one error after every resource type was tried. * @returns {Promise} a promise that resolves to the imported resource type objects */ importResourceTypes( importData: ResourceTypeExportInterface, resultCallback?: ResultCallback ): Promise; }; export default (state: State): ResourceType => { return { async readResourceType(resourceTypeUuid: string) { return readResourceType({ resourceTypeUuid, state }); }, async readResourceTypes(): Promise { return readResourceTypes({ state }); }, async readResourceTypeByName( resourceTypeName: string ): Promise { return readResourceTypeByName({ resourceTypeName, state }); }, async createResourceType( resourceTypeData: ResourceTypeSkeleton, resourceTypeUuid: string = undefined ): Promise { return createResourceType({ resourceTypeData, resourceTypeUuid, state }); }, async updateResourceType( resourceTypeUuid: string, resourceTypeData: ResourceTypeSkeleton ): Promise { return updateResourceType({ resourceTypeUuid, resourceTypeData, state, }); }, async deleteResourceType(resourceTypeUuid: string) { return deleteResourceType({ resourceTypeUuid, state }); }, async deleteResourceTypeByName( resourceTypeName: string ): Promise { return deleteResourceTypeByName({ resourceTypeName, state }); }, async exportResourceType( resourceTypeUuid: string ): Promise { return exportResourceType({ resourceTypeUuid, state }); }, async exportResourceTypeByName( resourceTypeName: string ): Promise { return exportResourceTypeByName({ resourceTypeName, state }); }, async exportResourceTypes(): Promise { return exportResourceTypes({ state }); }, async importResourceType( resourceTypeUuid: string, importData: ResourceTypeExportInterface ): Promise { return importResourceType({ resourceTypeUuid, importData, state, }); }, async importResourceTypeByName( resourceTypeName: string, importData: ResourceTypeExportInterface ): Promise { return importResourceTypeByName({ resourceTypeName, importData, state, }); }, async importFirstResourceType( importData: ResourceTypeExportInterface ): Promise { return importFirstResourceType({ importData, state }); }, async importResourceTypes( importData: ResourceTypeExportInterface, resultCallback: ResultCallback = void 0 ): Promise { return importResourceTypes({ importData, resultCallback, state }); }, }; }; export interface ResourceTypeExportInterface { meta?: ExportMetaData; resourcetype: Record; } /** * Create an empty export template * @returns {ResourceTypeExportInterface} an empty export template */ export function createResourceTypeExportTemplate({ state, }: { state: State; }): ResourceTypeExportInterface { return { meta: getMetadata({ state }), script: {}, policy: {}, policyset: {}, resourcetype: {}, } as ResourceTypeExportInterface; } export async function readResourceType({ resourceTypeUuid, state, }: { resourceTypeUuid: string; state: State; }) { try { const response = await _getResourceType({ resourceTypeUuid, state }); return response; } catch (error) { throw new FrodoError( `Error reading ${getCurrentRealmName(state) + ' realm'} resource type ${resourceTypeUuid}`, error ); } } /** * Get all resource types * @returns {Promise} a promise that resolves to an array of resource type objects */ export async function readResourceTypes({ state, }: { state: State; }): Promise { try { const { result } = await _getResourceTypes({ state }); return result; } catch (error) { throw new FrodoError( `Error reading ${getCurrentRealmName(state) + ' realm'} resource types`, error ); } } /** * Get resource type by name * @param {string} resourceTypeName resource type name * @returns {Promise} a promise that resolves to a resource type object */ export async function readResourceTypeByName({ resourceTypeName, state, }: { resourceTypeName: string; state: State; }): Promise { try { const { result } = await _getResourceTypeByName({ resourceTypeName, state, }); switch (result.length) { case 1: return result[0]; case 0: throw new FrodoError( `Resource Type with name ${resourceTypeName} does not exist in realm ${state.getRealm()}` ); default: throw new FrodoError( `${result.length} ${getCurrentRealmName(state) + ' realm'} resource types '${resourceTypeName}' found` ); } } catch (error) { throw new FrodoError( `Error reading ${getCurrentRealmName(state) + ' realm'} resource type ${resourceTypeName}`, error ); } } /** * Update resource type * @param resourceTypeUuid resource type uuid * @param {string} resourceTypeData resource type data * @returns {Promise} a promise that resolves to a resource type object, or null if no update was made */ export async function updateResourceType({ resourceTypeUuid, resourceTypeData, state, }: { resourceTypeUuid: string; resourceTypeData: ResourceTypeSkeleton; state: State; }): Promise { try { return await updateRemote({ data: resourceTypeData, type: 'resource type', readFn: async () => await readResourceType({ resourceTypeUuid, state }), updateFn: async () => await _putResourceType({ resourceTypeUuid, resourceTypeData, state }), state, }); } catch (error) { throw new FrodoError( `Error updating ${getCurrentRealmName(state) + ' realm'} resource type ${resourceTypeUuid}`, error ); } } export async function deleteResourceType({ resourceTypeUuid, state, }: { resourceTypeUuid: string; state: State; }) { try { const response = await _deleteResourceType({ resourceTypeUuid, state }); return response; } catch (error) { throw new FrodoError( `Error deleting ${getCurrentRealmName(state) + ' realm'} resource type ${resourceTypeUuid}`, error ); } } /** * Delete resource type by name * @param {string} resourceTypeName resource type name * @returns {Promise} Promise resolvig to a resource type object */ export async function deleteResourceTypeByName({ resourceTypeName, state, }: { resourceTypeName: string; state: State; }): Promise { try { const resourceTypeUuid = ( await readResourceTypeByName({ resourceTypeName, state }) ).uuid; const response = await _deleteResourceType({ resourceTypeUuid, state }); return response; } catch (error) { throw new FrodoError( `Error deleting ${getCurrentRealmName(state) + ' realm'} resource type ${resourceTypeName}`, error ); } } /** * Export resource type * @param {string} resourceTypeUuid resource type uuid * @returns {Promise} a promise that resolves to an ResourceTypeExportInterface object */ export async function exportResourceType({ resourceTypeUuid, state, }: { resourceTypeUuid: string; state: State; }): Promise { debugMessage({ message: `ResourceTypeOps.exportResourceType: start`, state }); const exportData = createResourceTypeExportTemplate({ state }); try { const resourceTypeData = await _getResourceType({ resourceTypeUuid, state, }); exportData.resourcetype[resourceTypeData.uuid] = resourceTypeData; debugMessage({ message: `ResourceTypeOps.exportResourceType: end`, state }); return exportData; } catch (error) { if (error.response?.status === 404) { throw new FrodoError( `${getCurrentRealmName(state) + ' realm'} resource type ${resourceTypeUuid} does not exist`, error ); } else { throw new FrodoError( `Error exporting ${getCurrentRealmName(state) + ' realm'} resource type ${resourceTypeUuid}`, error ); } } } /** * Export resource type by name * @param {string} resourceTypeName resource type name * @returns {Promise} a promise that resolves to an ResourceTypeExportInterface object */ export async function exportResourceTypeByName({ resourceTypeName, state, }: { resourceTypeName: string; state: State; }): Promise { debugMessage({ message: `ResourceTypeOps.exportResourceTypeByName: start`, state, }); const exportData = createResourceTypeExportTemplate({ state }); try { const resourceTypeData = await readResourceTypeByName({ resourceTypeName, state, }); exportData.resourcetype[resourceTypeData.uuid] = resourceTypeData; debugMessage({ message: `ResourceTypeOps.exportResourceTypeByName: end`, state, }); return exportData; } catch (error) { throw new FrodoError( `Error exporting ${getCurrentRealmName(state) + ' realm'} resource type ${resourceTypeName}`, error ); } } /** * Export resource types * @returns {Promise} a promise that resolves to an ResourceTypeExportInterface object */ export async function exportResourceTypes({ state, }: { state: State; }): Promise { debugMessage({ message: `ResourceTypeOps.exportResourceType: start`, state }); const exportData = createResourceTypeExportTemplate({ state }); let indicatorId: string; try { const resourceTypes = await readResourceTypes({ state }); indicatorId = createProgressIndicator({ total: resourceTypes.length, message: `Exporting ${getCurrentRealmName(state) + ' realm'} resource types...`, state, }); for (const resourceTypeData of resourceTypes) { updateProgressIndicator({ id: indicatorId, message: `Exporting ${getCurrentRealmName(state) + ' realm'} resource type ${resourceTypeData._id}`, state, }); exportData.resourcetype[resourceTypeData.uuid] = resourceTypeData; } stopProgressIndicator({ id: indicatorId, message: `Exported ${resourceTypes.length} ${getCurrentRealmName(state) + ' realm'} resource types.`, state, }); debugMessage({ message: `ResourceTypeOps.exportResourceType: end`, state }); return exportData; } catch (error) { stopProgressIndicator({ id: indicatorId, message: `Error exporting ${getCurrentRealmName(state) + ' realm'} resource types`, status: 'fail', state, }); throw new FrodoError( `Error exporting ${getCurrentRealmName(state) + ' realm'} resource types`, error ); } } /** * Import resource type by uuid * @param {string} resourceTypeUuid client uuid * @param {ResourceTypeExportInterface} importData import data * @returns {Promise} a promise that resolves to the imported resource type object, or null if no update was made */ export async function importResourceType({ resourceTypeUuid, importData, state, }: { resourceTypeUuid: string; importData: ResourceTypeExportInterface; state: State; }): Promise { let response = null; const errors = []; const imported = []; for (const id of Object.keys(importData.resourcetype)) { if (id === resourceTypeUuid) { try { const resourceTypeData = importData.resourcetype[id]; delete resourceTypeData._rev; response = await updateRemote({ data: resourceTypeData, type: 'resource type', readFn: async () => await readResourceType({ resourceTypeUuid, state }), updateFn: async () => await _putResourceType({ resourceTypeUuid, resourceTypeData, state, }), createFn: async () => await _createResourceType({ resourceTypeData, state }), state, }); imported.push(id); } catch (error) { errors.push(error); } } } if (errors.length > 0) { throw new FrodoError( `Error importing ${getCurrentRealmName(state) + ' realm'} resource type ${resourceTypeUuid}`, errors ); } if (0 === imported.length) { throw new FrodoError( `Resource type ${resourceTypeUuid} not found in import data` ); } return response; } /** * Import resource type by name * @param {string} resourceTypeName client id * @param {ResourceTypeExportInterface} importData import data * @returns {Promise} a promise that resolves to the imported resource type object, or null if no update was made */ export async function importResourceTypeByName({ resourceTypeName, importData, state, }: { resourceTypeName: string; importData: ResourceTypeExportInterface; state: State; }): Promise { let response = null; const errors = []; const imported = []; for (const id of Object.keys(importData.resourcetype)) { if (importData.resourcetype[id].name === resourceTypeName) { try { const resourceTypeData = importData.resourcetype[id]; delete resourceTypeData._rev; response = await updateRemote({ data: resourceTypeData, type: 'resource type', readFn: async () => await readResourceType({ resourceTypeUuid: id, state }), updateFn: async () => await _putResourceType({ resourceTypeUuid: id, resourceTypeData, state, }), createFn: async () => await _createResourceType({ resourceTypeData, state }), state, }); imported.push(id); } catch (error) { errors.push(error); } } } if (errors.length > 0) { throw new FrodoError( `Error importing ${getCurrentRealmName(state) + ' realm'} resource type ${resourceTypeName}`, errors ); } if (0 === imported.length) { throw new FrodoError( `Resource type ${resourceTypeName} not found in import data` ); } return response; } /** * Import first resource type * @param {ResourceTypeExportInterface} importData import data * @returns {Promise} a promise that resolves to the imported resource type object, or null if no update was made */ export async function importFirstResourceType({ importData, state, }: { importData: ResourceTypeExportInterface; state: State; }): Promise { let response = null; const errors = []; const imported = []; for (const id of Object.keys(importData.resourcetype)) { try { const resourceTypeData = importData.resourcetype[id]; delete resourceTypeData._provider; delete resourceTypeData._rev; response = await updateRemote({ data: resourceTypeData, type: 'resource type', readFn: async () => await readResourceType({ resourceTypeUuid: id, state }), updateFn: async () => await _putResourceType({ resourceTypeUuid: id, resourceTypeData, state, }), createFn: async () => await _createResourceType({ resourceTypeData, state }), state, }); imported.push(id); } catch (error) { errors.push(error); } break; } if (errors.length > 0) { throw new FrodoError( `Error importing first ${getCurrentRealmName(state) + ' realm'} resource type`, errors ); } if (0 === imported.length) { throw new FrodoError(`No resource types found in import data!`); } return response; } /** * Import resource types * @param {ResourceTypeExportInterface} importData import data * @param {ResultCallback} resultCallback Optional callback to process individual results. When given, the outcome of every resource type is passed to it once, as it happens: a resource type that was imported as the resource type (a resource type that needed no update as it is in the import data), a resource type that could not be imported as an error that names the resource type and carries the cause. Such a resource type is not part of the result and does not stop the others, and the function does not throw for it. Without it, the failures are collected and thrown as one error after every resource type was tried. * @returns {Promise} a promise that resolves to the imported resource type objects */ export async function importResourceTypes({ importData, resultCallback = void 0, state, }: { importData: ResourceTypeExportInterface; resultCallback?: ResultCallback; state: State; }): Promise { const response = []; const errors = []; for (const id of Object.keys(importData.resourcetype)) { let imported: ResourceTypeSkeleton | null; try { const resourceTypeData = importData.resourcetype[id]; delete resourceTypeData._rev; imported = await updateRemote({ data: resourceTypeData, type: 'resource type', readFn: async () => await readResourceType({ resourceTypeUuid: id, state }), updateFn: async () => await _putResourceType({ resourceTypeUuid: id, resourceTypeData, state, }), createFn: async () => await _createResourceType({ resourceTypeData, state }), state, }); } catch (error) { if (!resultCallback) { errors.push(error); } else { // the caller reports the failure: name the resource type and keep the cause const name = importData.resourcetype[id]?.name; resultCallback( new FrodoError( `Error importing ${getCurrentRealmName(state) + ' realm'} resource type ${name ? `${name} (${id})` : id}`, error ) ); } continue; } response.push(imported); // a resource type that needed no update is reported as it is in the import data if (resultCallback) resultCallback(undefined, imported ?? importData.resourcetype[id]); } if (errors.length > 0) { throw new FrodoError( `Error importing ${getCurrentRealmName(state) + ' realm'} resource types`, errors ); } return response.filter((r) => r); } export async function createResourceType({ resourceTypeData, resourceTypeUuid, state, }: { resourceTypeData: ResourceTypeSkeleton; resourceTypeUuid?: string; state: State; }): Promise { try { if (resourceTypeUuid) return await _putResourceType({ resourceTypeUuid, resourceTypeData, failIfExists: true, state, }); const response = await _createResourceType({ resourceTypeData, state }); return response; } catch (error) { throw new FrodoError( `Error creating ${getCurrentRealmName(state) + ' realm'} resource type${ resourceTypeUuid ? ' ' + resourceTypeUuid : '' }`, error ); } }