UNPKG

3.56 kBJavaScriptView Raw
1'use strict';
2
3const _ = require('lodash');
4
5const { getFcClient } = require('./client');
6
7const debug = require('debug')('fun:nas');
8
9const { red } = require('colors');
10const { getProfile } = require('./profile');
11const { throwProcessedException } = require('./error-message');
12
13var requestOption = {
14 method: 'POST'
15};
16
17async function createDefaultVSwitch(vpcClient, region, vpcId, vswitchName) {
18 const vswitchZoneId = await selectAllowedVSwitchZone(vpcClient, region);
19
20 var vswitchId;
21 try {
22 // 创建 vswitch
23 vswitchId = await createVSwitch(vpcClient, {
24 region,
25 vpcId,
26 zoneId: vswitchZoneId,
27 vswitchName: vswitchName
28 });
29
30 } catch (ex) {
31
32 throwProcessedException(ex, 'AliyunVPCFullAccess');
33 }
34 return vswitchId;
35}
36
37async function getVSwitchName(vpcClient, region, vswitchId) {
38 var params = {
39 'RegionId': region,
40 'VSwitchId': vswitchId
41 };
42
43
44 const describeRs = await vpcClient.request('DescribeVSwitchAttributes', params, requestOption);
45
46 const vswitchName = (describeRs || {}).VSwitchName;
47
48 return vswitchName;
49}
50
51async function findVswitchExistByName(vpcClient, region, vswitchIds, searchVSwtichName) {
52
53 if (!_.isEmpty(vswitchIds)) {
54 for (let vswitchId of vswitchIds) {
55
56 const vswitchName = await getVSwitchName(vpcClient, region, vswitchId);
57
58 if (_.isEqual(searchVSwtichName, vswitchName)) {
59 debug('found default vswitchId: ' + vswitchId);
60
61 return vswitchId;
62 }
63 }
64 }
65
66 debug('could not find %s from %j for region %s', searchVSwtichName, vswitchIds, region);
67
68 return null;
69}
70
71async function createVSwitch(vpcClient, {
72 region,
73 vpcId,
74 zoneId,
75 vswitchName
76}) {
77 var params = {
78 'RegionId': region,
79 'VpcId': vpcId,
80 'ZoneId': zoneId,
81 'CidrBlock': '10.20.0.0/16',
82 'VSwitchName': vswitchName,
83 'Description': 'default vswitch created by fc fun'
84 };
85
86 debug('createVSwitch params is %j', params);
87
88 const createRs = await vpcClient.request('CreateVSwitch', params, requestOption);
89
90 return createRs.VSwitchId;
91}
92
93async function selectVSwitchZoneId(fcAllowedZones, zones) {
94
95 const allowedZones = _.filter(zones, z => _.includes(fcAllowedZones, z.ZoneId));
96
97 const sortedZones = _.sortBy(allowedZones, ['ZoneId']);
98
99 return (_.head(sortedZones) || {}).ZoneId;
100}
101
102async function getFcAllowedZones() {
103 const fc = await getFcClient();
104
105 const fcRs = await fc.getAccountSettings();
106
107 const fcAllowedZones = fcRs.data.availableAZs;
108
109 debug('fc allowed zones: %j', fcAllowedZones);
110
111 if (_.isEqual(fcAllowedZones, [''])) {
112
113 const profile = await getProfile();
114
115 throw new Error(red(`No fc vswitch zones allowed, you may need login to fc console to apply for VPC feature: https://fc.console.aliyun.com/overview/${profile.defaultRegion}`));
116 }
117
118 return fcAllowedZones;
119}
120
121async function selectAllowedVSwitchZone(vpcClient, region) {
122 const zones = await describeZones(vpcClient, region);
123
124 const fcAllowedZones = await getFcAllowedZones();
125
126 const usedZoneId = await selectVSwitchZoneId(fcAllowedZones, zones);
127
128 if (!usedZoneId) {
129 throw new Error('no availiable zone for vswitch');
130 }
131
132 debug('select allowed switch zone: ', usedZoneId);
133
134 return usedZoneId;
135}
136
137async function describeZones(vpcClient, region) {
138 const params = {
139 'RegionId': region
140 };
141
142 const zones = await vpcClient.request('DescribeZones', params, requestOption);
143 return zones.Zones.Zone;
144}
145
146module.exports = {
147 findVswitchExistByName,
148 selectVSwitchZoneId,
149 createVSwitch,
150 createDefaultVSwitch
151};
\No newline at end of file