Skip to content

Commit 1ea5f20

Browse files
committed
feat: add vpc
1 parent 29293c1 commit 1ea5f20

File tree

4 files changed

+338
-0
lines changed

4 files changed

+338
-0
lines changed

src/baas/vpc/apis.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
function HttpError(code, message) {
2+
this.code = code || 0
3+
this.message = message || ''
4+
}
5+
6+
HttpError.prototype = Error.prototype
7+
8+
function apiFactory(actions) {
9+
const apis = {}
10+
actions.forEach((action) => {
11+
apis[action] = async (apig, inputs) => {
12+
const data = {
13+
Version: '2017-03-12',
14+
Action: action,
15+
RequestClient: 'ServerlessComponent',
16+
...inputs
17+
}
18+
19+
if (apig.options.Token) {
20+
data.Token = apig.options.Token
21+
}
22+
try {
23+
const { Response } = await apig.request(
24+
data,
25+
// this is preset options for apigateway
26+
{
27+
debug: false,
28+
ServiceType: 'vpc',
29+
host: 'vpc.tencentcloudapi.com'
30+
},
31+
false
32+
)
33+
if (Response && Response.Error && Response.Error.Code) {
34+
throw new HttpError(Response.Error.Code, Response.Error.Message)
35+
}
36+
return Response
37+
} catch (e) {
38+
throw new HttpError(500, e.message)
39+
}
40+
}
41+
})
42+
43+
return apis
44+
}
45+
46+
const ACTIONS = [
47+
'CreateDefaultVpc',
48+
'CreateVpc',
49+
'DeleteVpc',
50+
'DescribeVpcs',
51+
'CreateSubnet',
52+
'DeleteSubnet',
53+
'DescribeSubnets',
54+
'ModifyVpcAttribute',
55+
'ModifySubnetAttribute'
56+
]
57+
const APIS = apiFactory(ACTIONS)
58+
59+
module.exports = APIS

src/baas/vpc/index.js

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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

src/baas/vpc/index.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const Vpc = require('./index')
2+
3+
async function runTest() {
4+
const credentials = {
5+
SecretId: '',
6+
SecretKey: ''
7+
}
8+
const inputs = {
9+
region: 'ap-guangzhou',
10+
zone: 'ap-guangzhou-2',
11+
vpcName: 'serverless',
12+
subnetName: 'serverless',
13+
cidrBlock: '10.0.0.0/16'
14+
}
15+
const vpc = new Vpc(credentials, inputs.region)
16+
const outputs = await vpc.deploy(inputs)
17+
18+
await vpc.remove(outputs)
19+
}
20+
21+
runTest()
22+
23+
process.on('unhandledRejection', (e) => {
24+
console.log(e);
25+
})

src/baas/vpc/utils.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
const {
2+
DescribeVpcs,
3+
DescribeSubnets,
4+
CreateVpc,
5+
DeleteVpc,
6+
CreateSubnet,
7+
DeleteSubnet,
8+
ModifyVpcAttribute,
9+
ModifySubnetAttribute
10+
} = require('./apis')
11+
12+
const utils = {
13+
/**
14+
*
15+
* @param {object} capi capi instance
16+
* @param {string} vpcId
17+
*/
18+
async getVpcDetail(capi, vpcId) {
19+
// get instance detail
20+
try {
21+
const res = await DescribeVpcs(capi, {
22+
VpcIds: [vpcId]
23+
})
24+
if (res.VpcSet) {
25+
const {
26+
VpcSet: [detail]
27+
} = res
28+
return detail
29+
}
30+
return null
31+
} catch (e) {
32+
console.log(e)
33+
return null
34+
}
35+
},
36+
37+
/**
38+
*
39+
* @param {object} capi capi instance
40+
* @param {string} vpcId
41+
*/
42+
async getSubnetDetail(capi, subnetId) {
43+
try {
44+
const res = await DescribeSubnets(capi, {
45+
SubnetIds: [subnetId]
46+
})
47+
if (res.SubnetSet) {
48+
const {
49+
SubnetSet: [detail]
50+
} = res
51+
return detail
52+
}
53+
return null
54+
} catch (e) {
55+
console.log(e)
56+
return null
57+
}
58+
},
59+
60+
async createVpc(capi, inputs) {
61+
const res = await CreateVpc(capi, inputs)
62+
if (res.Vpc && res.Vpc.VpcId) {
63+
const { Vpc } = res
64+
return Vpc
65+
}
66+
},
67+
68+
async modifyVpc(capi, inputs) {
69+
await ModifyVpcAttribute(capi, inputs)
70+
},
71+
72+
async deleteVpc(capi, vpcId) {
73+
await DeleteVpc(capi, {
74+
VpcId: vpcId
75+
})
76+
},
77+
78+
async createSubnet(capi, inputs) {
79+
const res = await CreateSubnet(capi, inputs)
80+
if (res.Subnet && res.Subnet.SubnetId) {
81+
const { Subnet } = res
82+
return Subnet
83+
}
84+
},
85+
86+
async modifySubnet(capi, inputs) {
87+
await ModifySubnetAttribute(capi, inputs)
88+
},
89+
90+
async deleteSubnet(capi, subnetId) {
91+
await DeleteSubnet(capi, {
92+
SubnetId: subnetId
93+
})
94+
}
95+
}
96+
97+
module.exports = utils

0 commit comments

Comments
 (0)