|
| 1 | +const { Capi } = require('@tencent-sdk/capi') |
| 2 | +const utils = require('./utils') |
| 3 | + |
| 4 | +class Vpc { |
| 5 | + constructor(credentials = {}, region) { |
| 6 | + this.region = region || 'ap-guangzhou' |
| 7 | + this.credentials = credentials |
| 8 | + this.capi = new Capi({ |
| 9 | + Region: region, |
| 10 | + AppId: credentials.AppId, |
| 11 | + SecretId: credentials.SecretId, |
| 12 | + SecretKey: credentials.SecretKey, |
| 13 | + Token: credentials.Token |
| 14 | + }) |
| 15 | + } |
| 16 | + |
| 17 | + async deploy(inputs) { |
| 18 | + const { |
| 19 | + zone, |
| 20 | + vpcName, |
| 21 | + subnetName, |
| 22 | + cidrBlock, |
| 23 | + enableMulticast, |
| 24 | + dnsServers, |
| 25 | + domainName, |
| 26 | + tags, |
| 27 | + subnetTags, |
| 28 | + enableSubnetBroadcast |
| 29 | + } = inputs |
| 30 | + |
| 31 | + let { vpcId, subnetId } = inputs |
| 32 | + |
| 33 | + const handleVpc = async (vId) => { |
| 34 | + let existVpc = false |
| 35 | + if (vId) { |
| 36 | + const detail = await utils.getVpcDetail(this.capi, vId) |
| 37 | + if (detail) { |
| 38 | + existVpc = true |
| 39 | + } |
| 40 | + } |
| 41 | + const params = { |
| 42 | + VpcName: vpcName |
| 43 | + } |
| 44 | + if (enableMulticast) { |
| 45 | + params.EnableMulticast = enableMulticast |
| 46 | + } |
| 47 | + if (dnsServers) { |
| 48 | + params.DnsServers = dnsServers |
| 49 | + } |
| 50 | + if (domainName) { |
| 51 | + params.DomainName = domainName |
| 52 | + } |
| 53 | + if (existVpc) { |
| 54 | + console.log(`Updating vpc ${vId}...`) |
| 55 | + params.VpcId = vId |
| 56 | + await utils.modifyVpc(this.capi, params) |
| 57 | + console.log(`Update vpc ${vId} success`) |
| 58 | + } else { |
| 59 | + if (!cidrBlock) { |
| 60 | + throw new Error('cidrBlock is required') |
| 61 | + } |
| 62 | + params.CidrBlock = cidrBlock |
| 63 | + if (tags) { |
| 64 | + params.Tags = tags |
| 65 | + } |
| 66 | + console.log(`Creating vpc ${vpcName}...`) |
| 67 | + const res = await utils.createVpc(this.capi, params) |
| 68 | + console.log(`Create vpc ${vpcName} success.`) |
| 69 | + vId = res.VpcId |
| 70 | + } |
| 71 | + return vId |
| 72 | + } |
| 73 | + |
| 74 | + // check subnetId |
| 75 | + const handleSubnet = async (vId, sId) => { |
| 76 | + let existSubnet = false |
| 77 | + if (sId) { |
| 78 | + const detail = await utils.getSubnetDetail(this.capi, sId) |
| 79 | + if (detail) { |
| 80 | + existSubnet = true |
| 81 | + } |
| 82 | + } |
| 83 | + const params = { |
| 84 | + SubnetName: subnetName |
| 85 | + } |
| 86 | + if (existSubnet) { |
| 87 | + console.log(`Updating subnet ${sId}...`) |
| 88 | + params.SubnetId = sId |
| 89 | + |
| 90 | + if (enableSubnetBroadcast !== undefined) { |
| 91 | + params.EnableBroadcast = enableSubnetBroadcast |
| 92 | + } |
| 93 | + await utils.modifySubnet(this.capi, params) |
| 94 | + console.log(`Update subnet ${sId} success.`) |
| 95 | + } else { |
| 96 | + if (vId) { |
| 97 | + console.log(`Creating subnet ${subnetName}...`) |
| 98 | + params.Zone = zone |
| 99 | + params.VpcId = vId |
| 100 | + params.CidrBlock = cidrBlock |
| 101 | + if (subnetTags) { |
| 102 | + params.Tags = subnetTags |
| 103 | + } |
| 104 | + |
| 105 | + const res = await utils.createSubnet(this.capi, params) |
| 106 | + console.log('handleSubnet', res) |
| 107 | + |
| 108 | + sId = res.SubnetId |
| 109 | + |
| 110 | + if (enableSubnetBroadcast === true) { |
| 111 | + await utils.modifySubnet(this.capi, { |
| 112 | + SubnetId: sId, |
| 113 | + EnableBroadcast: enableSubnetBroadcast |
| 114 | + }) |
| 115 | + } |
| 116 | + console.log(`Create subnet ${subnetName} success.`) |
| 117 | + } |
| 118 | + } |
| 119 | + return sId |
| 120 | + } |
| 121 | + |
| 122 | + if (vpcName) { |
| 123 | + vpcId = await handleVpc(vpcId) |
| 124 | + } |
| 125 | + |
| 126 | + if (subnetName) { |
| 127 | + subnetId = await handleSubnet(vpcId, subnetId) |
| 128 | + } |
| 129 | + |
| 130 | + return { |
| 131 | + region: this.region, |
| 132 | + zone, |
| 133 | + vpcId, |
| 134 | + vpcName, |
| 135 | + subnetId, |
| 136 | + subnetName |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + async remove(inputs) { |
| 141 | + const { vpcId, subnetId } = inputs |
| 142 | + if (subnetId) { |
| 143 | + console.log(`Start removing subnet ${subnetId}`) |
| 144 | + await utils.deleteSubnet(this.capi, subnetId) |
| 145 | + console.log(`Removed subnet ${subnetId}`) |
| 146 | + } |
| 147 | + if (vpcId) { |
| 148 | + console.log(`Start removing vpc ${vpcId}`) |
| 149 | + await utils.deleteVpc(this.capi, vpcId) |
| 150 | + console.log(`Removed vpc ${vpcId}`) |
| 151 | + } |
| 152 | + |
| 153 | + return {} |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +module.exports = Vpc |
0 commit comments