|
| 1 | +import { ResourceConfig , ResourceConfig as SchemaResourceConfig } from 'codify-schemas'; |
| 2 | + |
| 3 | +import { InternalError } from '../common/errors.js'; |
| 4 | +import { CommonOrchestrator } from '../common/orchestrator.js'; |
| 5 | +import { Project } from '../entities/project.js'; |
| 6 | +import { ProcessName, SubProcessName, ctx } from '../events/context.js'; |
| 7 | +import { CodifyParser } from '../parser/index.js'; |
| 8 | +import { DependencyMap, PluginManager } from '../plugins/plugin-manager.js'; |
| 9 | + |
| 10 | +export type RequiredProperties = Map<string, RequiredProperty[]>; |
| 11 | +export type UserSuppliedProperties = Map<string, Record<string, unknown>>; |
| 12 | +export type ImportResult = { result: ResourceConfig[], errors: string[] } |
| 13 | + |
| 14 | +export interface RequiredProperty { |
| 15 | + propertyName: string; |
| 16 | + propertyType: string; |
| 17 | + plugin: string; |
| 18 | +} |
| 19 | + |
| 20 | +export class ImportOrchestrator { |
| 21 | + static async initializeAndValidate( |
| 22 | + typeIds: string[], |
| 23 | + path: string, |
| 24 | + secureMode: boolean |
| 25 | + ): Promise<{ |
| 26 | + project: Project; |
| 27 | + pluginManager: PluginManager; |
| 28 | + }> { |
| 29 | + if (typeIds.length === 0) { |
| 30 | + throw new InternalError('importAndGenerateConfigs called with no typeIds passed in'); |
| 31 | + } |
| 32 | + |
| 33 | + ctx.processStarted(ProcessName.IMPORT) |
| 34 | + |
| 35 | + const project = await ImportOrchestrator.parse(path) |
| 36 | + |
| 37 | + const { dependencyMap, pluginManager } = await CommonOrchestrator.initializePlugins(project, secureMode); |
| 38 | + await ImportOrchestrator.validate(typeIds, project, pluginManager, dependencyMap) |
| 39 | + |
| 40 | + return { project, pluginManager }; |
| 41 | + } |
| 42 | + |
| 43 | + static async getRequiredParameters( |
| 44 | + typeIds: string[], |
| 45 | + pluginManager: PluginManager |
| 46 | + ): Promise<RequiredProperties> { |
| 47 | + ctx.subprocessStarted(SubProcessName.GET_REQUIRED_PARAMETERS); |
| 48 | + |
| 49 | + const allRequiredProperties = new Map<string, RequiredProperty[]>(); |
| 50 | + for (const type of typeIds) { |
| 51 | + const resourceInfo = await pluginManager.getResourceInfo(type); |
| 52 | + |
| 53 | + const { schema } = resourceInfo; |
| 54 | + if (!schema) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + const requiredPropertyNames = resourceInfo.import?.requiredParameters; |
| 59 | + if (!requiredPropertyNames || requiredPropertyNames.length === 0) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + requiredPropertyNames |
| 64 | + .forEach((name) => { |
| 65 | + if (!allRequiredProperties.has(type)) { |
| 66 | + allRequiredProperties.set(type, []); |
| 67 | + } |
| 68 | + |
| 69 | + const propertyInfo = (schema.properties as any)[name]; |
| 70 | + |
| 71 | + allRequiredProperties.get(type)!.push({ |
| 72 | + propertyName: name, |
| 73 | + propertyType: propertyInfo.type ?? null, |
| 74 | + plugin: resourceInfo.plugin |
| 75 | + }) |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + ctx.subprocessFinished(SubProcessName.GET_REQUIRED_PARAMETERS); |
| 80 | + |
| 81 | + return allRequiredProperties; |
| 82 | + } |
| 83 | + |
| 84 | + static async getImportedConfigs( |
| 85 | + pluginManager: PluginManager, |
| 86 | + typeIds: string[], |
| 87 | + userSuppliedProperties: UserSuppliedProperties |
| 88 | + ): Promise<ImportResult> { |
| 89 | + const importedConfigs = []; |
| 90 | + const errors = []; |
| 91 | + |
| 92 | + for (const type of typeIds) { |
| 93 | + ctx.subprocessStarted(SubProcessName.IMPORT_RESOURCE, type); |
| 94 | + try { |
| 95 | + const config: SchemaResourceConfig = { |
| 96 | + type, |
| 97 | + ...userSuppliedProperties.get(type), |
| 98 | + }; |
| 99 | + |
| 100 | + const response = await pluginManager.importResource(config); |
| 101 | + |
| 102 | + if (response.result !== null && response.result.length > 0) { |
| 103 | + importedConfigs.push(...response.result); |
| 104 | + } else { |
| 105 | + errors.push(`Unable to import resource '${type}', resource not found`); |
| 106 | + } |
| 107 | + } catch (error: any) { |
| 108 | + errors.push(error.message ?? error); |
| 109 | + } |
| 110 | + |
| 111 | + ctx.subprocessFinished(SubProcessName.IMPORT_RESOURCE, type); |
| 112 | + } |
| 113 | + |
| 114 | + ctx.processFinished(ProcessName.IMPORT) |
| 115 | + |
| 116 | + return { |
| 117 | + result: importedConfigs, |
| 118 | + errors, |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private static async parse(path: string): Promise<Project> { |
| 123 | + ctx.subprocessStarted(SubProcessName.PARSE); |
| 124 | + const project = await CodifyParser.parse(path); |
| 125 | + ctx.subprocessFinished(SubProcessName.PARSE); |
| 126 | + |
| 127 | + return project |
| 128 | + } |
| 129 | + |
| 130 | + private static async validate(typeIds: string[], project: Project, pluginManager: PluginManager, dependencyMap: DependencyMap): Promise<void> { |
| 131 | + ctx.subprocessStarted(SubProcessName.VALIDATE) |
| 132 | + |
| 133 | + project.validateTypeIds(dependencyMap); |
| 134 | + |
| 135 | + const unsupportedTypeIds = typeIds.filter((type) => !dependencyMap.has(type)); |
| 136 | + if (unsupportedTypeIds.length > 0) { |
| 137 | + throw new Error(`The following resources cannot be imported. No plugins found that support the following types: |
| 138 | +${JSON.stringify(unsupportedTypeIds)}`); |
| 139 | + } |
| 140 | + |
| 141 | + ctx.subprocessFinished(SubProcessName.VALIDATE) |
| 142 | + } |
| 143 | +} |
0 commit comments