diff --git a/apps/terraform/type/ResourceManager.ts b/apps/terraform/type/ResourceManager.ts new file mode 100644 index 00000000..b1621c51 --- /dev/null +++ b/apps/terraform/type/ResourceManager.ts @@ -0,0 +1,24 @@ +import { NCloudModel } from '../interface/NCloudModel'; + +export class ResourceManager { + private resources: NCloudModel[]; + private readonly nameMap: Map; + + constructor() { + this.resources = []; + this.nameMap = new Map(); + } + + addResource(resource: NCloudModel): void { + this.resources.push(resource); + this.nameMap.set(resource.serviceType, resource.name); + } + + getResources(): NCloudModel[] { + return [...this.resources].sort((a, b) => a.priority - b.priority); + } + + getNameMap(): Map { + return this.nameMap; + } +} diff --git a/apps/terraform/type/TerraformGenerator.ts b/apps/terraform/type/TerraformGenerator.ts new file mode 100644 index 00000000..ed4ce4fd --- /dev/null +++ b/apps/terraform/type/TerraformGenerator.ts @@ -0,0 +1,45 @@ +import { replaceReferences } from '../util/reference'; +import { generateProviderBlock, generateResourceBlock, generateTerraformBlock } from '../util/generator'; +import { ResourceManager } from './ResourceManager'; +import { NCloudProvider } from '../model/NCloudProvider'; + +export class CodeGenerator { + private resourceManager: ResourceManager; + + constructor(resourceManager: ResourceManager) { + this.resourceManager = resourceManager; + } + + generateCode(provider: NCloudProvider): string { + const providerProperties = provider.getProperties(); + + const blocks = [ + generateTerraformBlock( + providerProperties.terraform.required_providers.ncloud.source, + providerProperties.terraform.required_version + ), + generateProviderBlock( + provider.name, + providerProperties.provider + ), + ...this.generateResourceBlocks() + ]; + + return blocks.join('\n'); + } + + private generateResourceBlocks(): string[] { + return this.resourceManager.getResources().map(resource => { + const properties = replaceReferences( + resource.getProperties(), + this.resourceManager.getNameMap() + ); + + return generateResourceBlock( + resource.serviceType, + resource.name, + properties + ); + }); + } +} \ No newline at end of file