Skip to content

Commit

Permalink
Merge pull request #133 from Gdm0714/feature/convertTerraform
Browse files Browse the repository at this point in the history
NCloud Terraform 변환 코드 구현
  • Loading branch information
Gdm0714 authored Nov 22, 2024
2 parents baa9b48 + 60f46ea commit f38b48f
Show file tree
Hide file tree
Showing 35 changed files with 867 additions and 0 deletions.
32 changes: 32 additions & 0 deletions apps/terraform/convertor/TerraformConvertor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ResourceManager } from '../type/ResourceManager';
import { CodeGenerator } from '../type/TerraformGenerator';
import { NCloudProvider } from '../model/NCloudProvider';
import { CloudCanvasNode } from '../interface/CloudCanvasNode';
import { parseToNCloudModel } from '../util/resourceParser';

export class TerraformConvertor {
private readonly resourceManager: ResourceManager;
private readonly codeGenerator: CodeGenerator;
private readonly provider: NCloudProvider;

constructor(provider: NCloudProvider) {
this.provider = provider;
this.resourceManager = new ResourceManager();
this.codeGenerator = new CodeGenerator(this.resourceManager);
}

addResourceFromJson(jsonData: { nodes?: CloudCanvasNode[] }): void {
jsonData.nodes?.forEach(node => {
try {
const resource = parseToNCloudModel(node);
this.resourceManager.addResource(resource);
} catch (error) {
console.warn(`Skipping unsupported node type: ${node.type}`);
}
});
}

generate(): string {
return this.codeGenerator.generateCode(this.provider);
}
}
11 changes: 11 additions & 0 deletions apps/terraform/enum/ResourcePriority.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export enum ResourcePriority {
VPC = 1,
NETWORK_ACL = 2,
SUBNET = 3,
ACG = 4,
ACG_RULE = 5,
LOGIN_KEY = 6,
NETWORK_INTERFACE = 7,
SERVER = 8,
PUBLIC_IP = 9
}
6 changes: 6 additions & 0 deletions apps/terraform/interface/ACG.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface ACG {
id: string;
name: string;
vpcNo: string;
description: string;
}
6 changes: 6 additions & 0 deletions apps/terraform/interface/ACGRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface ACGRule {
protocol: string;
ipBlock: string;
portRange: string;
description: string;
}
6 changes: 6 additions & 0 deletions apps/terraform/interface/CloudCanvasNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface CloudCanvasNode {
id: string;
type: string;
name: string;
properties: { [key: string]: any };
}
3 changes: 3 additions & 0 deletions apps/terraform/interface/FileOption.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface FileOption {
log?: boolean;
}
8 changes: 8 additions & 0 deletions apps/terraform/interface/NCloudModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ResourcePriority } from '../enum/ResourcePriority';

export interface NCloudModel {
name: string;
serviceType: string;
priority: ResourcePriority;
getProperties(): { [key: string]: any };
}
5 changes: 5 additions & 0 deletions apps/terraform/interface/NetworkACL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface NetworkACL {
id: string;
name: string;
vpcNo: string;
}
6 changes: 6 additions & 0 deletions apps/terraform/interface/NetworkInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface NetworkInterface {
id: string;
name: string;
subnetNo: string;
accessControlGroups: string[];
}
6 changes: 6 additions & 0 deletions apps/terraform/interface/Provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Provider {
accessKey: string;
secretKey: string;
region: string;
site: string;
}
5 changes: 5 additions & 0 deletions apps/terraform/interface/PublicIp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface PublicIp {
id: string;
publicIp: string;
serverInstanceNo: string;
}
9 changes: 9 additions & 0 deletions apps/terraform/interface/Server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface Server {
id: string;
name: string;
subnetNo: string;
serverImageProductCode: string;
serverProductCode: string;
loginKeyName: string;
networkInterfaceNo: string;
}
10 changes: 10 additions & 0 deletions apps/terraform/interface/Subnet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface Subnet {
id: string;
name: string;
vpcNo: string;
subnet: string;
zone: string;
networkAclNo: string;
subnetType: string;
usageType: string;
}
10 changes: 10 additions & 0 deletions apps/terraform/interface/VPC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface VPC {
id: string;
name: string;
region: string;
ipv4CidrBlock: string;
defaultNetworkAclNo: string;
defaultAccessControlGroupNo: string;
defaultPublicRouteTableNo: string;
defaultPrivateRouteTableNo: string;
}
36 changes: 36 additions & 0 deletions apps/terraform/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { CloudCanvasNode } from './interface/CloudCanvasNode';
import { NCloudProvider } from './model/NCloudProvider';
import { TerraformConvertor } from './convertor/TerraformConvertor';
import { sampleNodes } from './sample/sampleData';
import { saveTerraformFiles } from './util/file';

async function generateTerraformCode(): Promise<string> {
const provider = new NCloudProvider({
accessKey: "var.access_key",
secretKey: "var.secret_key",
region: "var.region",
site: "public"
});

const converter = new TerraformConvertor(provider);
converter.addResourceFromJson({ nodes: sampleNodes });

return converter.generate();
}

async function main() {
try {
const terraformCode = await generateTerraformCode();
await saveTerraformFiles(terraformCode, { log: true });

} catch (error) {
if (error instanceof Error) {
console.error('Error generating Terraform configuration:', error.message);
} else {
console.error('An unknown error occurred');
}
process.exit(1);
}
}

main().catch(console.error);
26 changes: 26 additions & 0 deletions apps/terraform/model/NCloudACG.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ACG } from '../interface/ACG';
import { NCloudModel } from '../interface/NCloudModel';
import { ResourcePriority } from '../enum/ResourcePriority';

export class NCloudACG implements ACG, NCloudModel {
id: string;
name: string;
vpcNo: string;
description: string;
serviceType: string;
priority: ResourcePriority;

constructor(json: any) {
this.serviceType = 'ncloud_access_control_group';
this.priority = ResourcePriority.ACG;
Object.assign(this, json);
}

getProperties() {
return {
name: this.name,
vpc_no: "VPC_ID_PLACEHOLDER",
description: this.description
};
}
}
34 changes: 34 additions & 0 deletions apps/terraform/model/NCloudACGRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { NCloudModel } from '../interface/NCloudModel';
import { ResourcePriority } from '../enum/ResourcePriority';

export class NCloudACGRule implements NCloudModel {
name: string;
protocol: string;
ipBlock: string;
portRange: string;
description: string;
serviceType: string;
priority: ResourcePriority;

constructor(json: any) {
this.serviceType = 'ncloud_access_control_group_rule';
this.priority = ResourcePriority.ACG_RULE;
this.name = json.name || 'acg-rule';
this.protocol = json.protocol;
this.ipBlock = json.ipBlock;
this.portRange = json.portRange;
this.description = json.description;
}

getProperties() {
return {
access_control_group_no: "ACG_ID_PLACEHOLDER",
inbound: {
protocol: this.protocol,
ip_block: this.ipBlock,
port_range: this.portRange,
description: this.description
}
};
}
}
20 changes: 20 additions & 0 deletions apps/terraform/model/NCloudLoginKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { NCloudModel } from '../interface/NCloudModel';
import { ResourcePriority } from '../enum/ResourcePriority';

export class NCloudLoginKey implements NCloudModel {
name: string;
serviceType: string;
priority: ResourcePriority;

constructor(json: any) {
this.serviceType = 'ncloud_login_key';
this.priority = ResourcePriority.LOGIN_KEY;
this.name = json.name;
}

getProperties() {
return {
key_name: this.name
};
}
}
25 changes: 25 additions & 0 deletions apps/terraform/model/NCloudNetworkACL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { NetworkACL } from '../interface/NetworkACL';
import { NCloudModel } from '../interface/NCloudModel';
import { ResourcePriority } from '../enum/ResourcePriority';

export class NCloudNetworkACL implements NetworkACL, NCloudModel {
id: string;
name: string;
vpcNo: string;
serviceType: string;
priority: ResourcePriority;

constructor(json: any) {
this.serviceType = 'ncloud_network_acl';
this.priority = ResourcePriority.NETWORK_ACL;
this.id = json.id;
this.name = json.name || 'nacl';
this.vpcNo = json.vpcNo;
}

getProperties() {
return {
vpc_no: "VPC_ID_PLACEHOLDER"
};
}
}
27 changes: 27 additions & 0 deletions apps/terraform/model/NCloudNetworkInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { NetworkInterface } from '../interface/NetworkInterface';
import { NCloudModel } from '../interface/NCloudModel';
import { ResourcePriority } from '../enum/ResourcePriority';

export class NCloudNetworkInterface implements NetworkInterface, NCloudModel {
id: string;
name: string;
subnetNo: string;
accessControlGroups: string[];
serviceType: string;
priority: ResourcePriority;

constructor(json: any) {
this.serviceType = 'ncloud_network_interface';
this.priority = ResourcePriority.NETWORK_INTERFACE;
Object.assign(this, json);
}

getProperties() {
return {
subnet_no: 'SUBNET_ID_PLACEHOLDER',
name: this.name,
access_control_groups: ['ACG_ID_PLACEHOLDER']
};
}
}

44 changes: 44 additions & 0 deletions apps/terraform/model/NCloudProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Provider } from '../interface/Provider';

export class NCloudProvider implements Provider {
accessKey: string;
secretKey: string;
region: string;
site: string;
name: string;
serviceType: string;
requiredVersion: string;
source: string;

constructor(json: any) {
this.serviceType = 'provider';
this.name = 'ncloud';
this.accessKey = json.accessKey;
this.secretKey = json.secretKey;
this.region = json.region;
this.site = json.site || 'public';
this.requiredVersion = '>= 0.13';
this.source = 'NaverCloudPlatform/ncloud';
}

getProperties() {
return {
terraform: {
required_providers: {
ncloud: {
source: this.source
}
},
required_version: this.requiredVersion
},
provider: {
access_key: "var.access_key",
secret_key: "var.secret_key",
region: "var.region",
site: this.site,
support_vpc: true
}
};
}

}
20 changes: 20 additions & 0 deletions apps/terraform/model/NCloudPublicIP.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ResourcePriority } from '../enum/ResourcePriority';
import { NCloudModel } from '../interface/NCloudModel';

export class NCloudPublicIP implements NCloudModel {
name: string;
serviceType: string;
priority: ResourcePriority;

constructor(json: any) {
this.serviceType = 'ncloud_public_ip';
this.priority = ResourcePriority.PUBLIC_IP;
this.name = json.name;
}

getProperties() {
return {
server_instance_no: "SERVER_ID_PLACEHOLDER"
};
}
}
Loading

0 comments on commit f38b48f

Please sign in to comment.