-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from Gdm0714/feature/convertTerraform
NCloud Terraform 변환 코드 구현
- Loading branch information
Showing
35 changed files
with
867 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface FileOption { | ||
log?: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface NetworkACL { | ||
id: string; | ||
name: string; | ||
vpcNo: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface PublicIp { | ||
id: string; | ||
publicIp: string; | ||
serverInstanceNo: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] | ||
}; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}; | ||
} | ||
} |
Oops, something went wrong.