Skip to content

Commit

Permalink
🤖 Refactor: terrformConvertor 각 기능이 명확하게 파일 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
Gdm0714 committed Nov 22, 2024
1 parent 7b38f1c commit 60f46ea
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
24 changes: 24 additions & 0 deletions apps/terraform/type/ResourceManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { NCloudModel } from '../interface/NCloudModel';

export class ResourceManager {
private resources: NCloudModel[];
private readonly nameMap: Map<string, string>;

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<string, string> {
return this.nameMap;
}
}
45 changes: 45 additions & 0 deletions apps/terraform/type/TerraformGenerator.ts
Original file line number Diff line number Diff line change
@@ -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
);
});
}
}

0 comments on commit 60f46ea

Please sign in to comment.