UNPKG

serverless-vpc-discovery

Version:
131 lines (130 loc) 5.79 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EC2Wrapper = void 0; const utils_1 = require("../utils"); const client_ec2_1 = require("@aws-sdk/client-ec2"); const globals_1 = __importDefault(require("../globals")); class EC2Wrapper { ec2; constructor(credentials) { this.ec2 = new client_ec2_1.EC2Client({ credentials, region: globals_1.default.getRegion(), retryStrategy: globals_1.default.getRetryStrategy() }); } /** * Returns the promise that contains the vpc list * @returns {Promise.<Vpc[]>} */ async getVpcs() { return await (0, utils_1.getAWSPagedResults)(this.ec2, "Vpcs", "NextToken", "NextToken", new client_ec2_1.DescribeVpcsCommand({})); } /** * Returns the promise that contains the vpc-id * @param {string} vpcName * @returns {Promise.<string>} */ async getVpcId(vpcName) { const vpcItems = await (0, utils_1.getAWSPagedResults)(this.ec2, "Vpcs", "NextToken", "NextToken", new client_ec2_1.DescribeVpcsCommand({ Filters: [{ Name: "tag:Name", Values: [vpcName] }] })); if (vpcItems.length === 0) { throw new Error(`VPC with tag key 'Name' and tag value '${vpcName}' does not exist.`); } return vpcItems[0].VpcId; } /** * Returns the promise that contains the subnet IDs * * @param {string} vpcId * @param {string} tagKey * @param {string[]} tagValues * @returns {Promise.<string[]>} */ async getSubnetIds(vpcId, tagKey, tagValues) { const subnets = await (0, utils_1.getAWSPagedResults)(this.ec2, "Subnets", "NextToken", "NextToken", new client_ec2_1.DescribeSubnetsCommand({ Filters: [{ Name: "vpc-id", Values: [vpcId] }, { Name: `tag:${tagKey}`, Values: tagValues }] })); const missingSubnetValues = tagValues.filter((tagValue) => { // collect subnets by name const subnetsByName = subnets.filter((subnet) => { return (0, utils_1.wildcardMatches)(tagValue, (0, utils_1.getValueFromTags)(subnet.Tags, tagKey)); }); return subnetsByName.length === 0; }); if (!subnets.length || missingSubnetValues.length) { throw new Error(`Subnets with vpc id '${vpcId}', tag key '${tagKey}' and tag values '${missingSubnetValues}' do not exist. ` + "Please check the `tagKey` and `tagValues` are correct or remove it."); } return subnets.map((subnet) => subnet.SubnetId); } /** * Returns the promise that contains the security group IDs * @param {string} vpcId * @param {string[]} names * @param {string[]} tagKey * @param {string[]} tagValues * @returns {Promise.<string[]>} */ async getSecurityGroupIds(vpcId, names, tagKey, tagValues) { // init filter by vpc id const input = { Filters: [{ Name: "vpc-id", Values: [vpcId] }] }; // update filters with names if specified if (names) { input.Filters.push({ Name: "group-name", Values: names }); } // update filters with tag and values if specified if (tagKey && tagValues) { input.Filters.push({ Name: `tag:${tagKey}`, Values: tagValues }); } const securityGroups = await (0, utils_1.getAWSPagedResults)(this.ec2, "SecurityGroups", "NextToken", "NextToken", new client_ec2_1.DescribeSecurityGroupsCommand(input)); if (securityGroups.length === 0) { const namesErrorText = names ? `, names '${names}'` : ""; const tagErrorText = tagKey && tagValues ? `, tag key '${tagKey}' and tag values '${tagValues}'` : ""; throw new Error(`Security groups with vpc id '${vpcId}'${namesErrorText}${tagErrorText} do not exist`); } if (names) { const missingGroupsNames = names.filter((groupName) => { // collect security groups by name const securityGroupsByName = securityGroups.filter((securityGroup) => { return (0, utils_1.wildcardMatches)(groupName, securityGroup.GroupName); }); return securityGroupsByName.length === 0; }); if (missingGroupsNames.length) { throw new Error(`Security groups do not exist for the names: ${missingGroupsNames}. ` + "Please check the 'names' are correct or remove it."); } } if (tagKey && tagValues) { tagValues = tagValues || []; const missingGroupsTagNames = tagValues.filter((tagValue) => { // collect subnets by name const groupsByName = securityGroups.filter((securityGroup) => { const groupTagValue = (0, utils_1.getValueFromTags)(securityGroup.Tags, tagKey); return groupTagValue === tagValue; }); return groupsByName.length === 0; }); if (missingGroupsTagNames.length) { throw new Error(`Security groups do not exist for the tag '${tagKey}' and tag values: '${missingGroupsTagNames}'. ` + "Please check the 'tagKey' and 'tagValues' are correct or remove it."); } } return securityGroups.map((group) => group.GroupId); } } exports.EC2Wrapper = EC2Wrapper;