Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new functions to manage config schema #483

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions src/ops/IdmConfigOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,30 @@ export type IdmConfig = {
* @returns {IdObjectSkeletonInterface} promise resolving to a config entity
*/
deleteConfigEntity(entityId: string): Promise<IdObjectSkeletonInterface>;
/**
* Read a idm sub config entity.
* @param {string} entityId entity id for the parent config entity of the sub config entity that is being read
* @param {string} name name of the sub config entity that is being read
* @param {ConfigEntityExportOptions} options export options
* @returns {Promise<IdObjectSkeletonInterface>} a promise resolving to a sub config entity object
*/
readSubConfigEntity(
entityId: string,
name: string,
options?: ConfigEntityExportOptions
): Promise<NoIdObjectSkeletonInterface>;
/**
* Import a idm sub config entity.
* @param {string} entityId entity id for parent config entity of the sub config that is being updated
* @param {NoIdObjectSkeletonInterface} updatedSubConfigEntity the updated sub config entity
* @param {ConfigEntityImportOptions} options import options
* @returns {Promise<IdObjectSkeletonInterface[]>} a promise resolving to an array of config entity objects
*/
importSubConfigEntity(
entityId: string,
updatedSubConfigEntity: IdObjectSkeletonInterface,
options?: ConfigEntityImportOptions
): Promise<IdObjectSkeletonInterface[]>;

// Deprecated

Expand Down Expand Up @@ -274,6 +298,33 @@ export default (state: State): IdmConfig => {
): Promise<IdObjectSkeletonInterface> {
return deleteConfigEntity({ entityId, state });
},
async readSubConfigEntity(
entityId: string,
name: string,
options: ConfigEntityExportOptions = {
envReplaceParams: undefined,
entitiesToExport: undefined,
}
): Promise<NoIdObjectSkeletonInterface> {
return readSubConfigEntity({
entityId,
name,
options,
state,
});
},
async importSubConfigEntity(
entityId: string,
updatedSubConfigEntity: IdObjectSkeletonInterface,
options: ConfigEntityImportOptions = { validate: false }
): Promise<IdObjectSkeletonInterface[]> {
return importSubConfigEntity({
entityId,
updatedSubConfigEntity,
options,
state,
});
},

// Deprecated

Expand Down Expand Up @@ -816,6 +867,102 @@ export async function deleteConfigEntity({
}
}

export async function readSubConfigEntity({
entityId,
name,
options = { envReplaceParams: undefined, entitiesToExport: undefined },
state,
}: {
entityId: string;
name: string;
options?: ConfigEntityExportOptions;
state: State;
}): Promise<NoIdObjectSkeletonInterface> {
try {
const entity = substituteEntityWithEnv(
await readConfigEntity({ entityId, state }),
options.envReplaceParams
);

const subEntityKey = Object.keys(entity).find((key) => key !== '_id');

if (!Array.isArray(entity[subEntityKey])) {
throw new FrodoError(`Error reading sub config ${entityId} ${name}`);
}

const subEntity = (
entity[subEntityKey] as NoIdObjectSkeletonInterface[]
).find((item) => item.name === name);

if (!subEntity) {
throw new FrodoError(`Error reading sub config ${entityId} ${name}`);
}
return subEntity;
} catch (error) {
printError(error);
}
}

export async function importSubConfigEntity({
entityId,
updatedSubConfigEntity,
options = {
envReplaceParams: undefined,
entitiesToImport: undefined,
validate: false,
},
state,
}: {
entityId: string;
updatedSubConfigEntity: IdObjectSkeletonInterface;
options: ConfigEntityImportOptions;
state: State;
}): Promise<IdObjectSkeletonInterface[]> {
try {
const entityExport = await exportConfigEntity({
cparkertrivir marked this conversation as resolved.
Show resolved Hide resolved
entityId,
state,
});

const subEntityKey = Object.keys(entityExport.idm?.[entityId]).find(
(key) => key !== '_id'
);

if (!Array.isArray(entityExport.idm?.[entityId]?.[subEntityKey])) {
throw new FrodoError(`Error importing sub config of ${entityId}`);
}

const existingSubEntityIndex = (
entityExport.idm?.[entityId]?.[
subEntityKey
] as NoIdObjectSkeletonInterface[]
).findIndex((item) => item.name === updatedSubConfigEntity.name);

if (existingSubEntityIndex !== -1) {
(
entityExport.idm[entityId][
subEntityKey
] as NoIdObjectSkeletonInterface[]
)[existingSubEntityIndex] = updatedSubConfigEntity;
} else {
(
entityExport.idm[entityId][
subEntityKey
] as NoIdObjectSkeletonInterface[]
).push(updatedSubConfigEntity);
}

return importConfigEntities({
entityId,
importData: entityExport,
options,
state,
});
} catch (error) {
throw new FrodoError(`Error importing sub config ${entityId}`, error);
}
}

function substituteEntityWithEnv(
entity: IdObjectSkeletonInterface,
envReplaceParams: string[][]
Expand Down