Skip to content

Commit

Permalink
removing non-required implementations from subnet utils
Browse files Browse the repository at this point in the history
  • Loading branch information
shikha372 committed Jun 14, 2024
1 parent 462dae2 commit 7b9885f
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 82 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-vpcv2-alpha/lib/subnet-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class SubnetV2 extends Resource implements ISubnet {

/**
* custom route table
* can be moved to a function definition if we plan to change association
* can be moved to a function definition if we plan to change association
* after subnet creation
*/
if (props.routeTable) {
Expand Down
82 changes: 1 addition & 81 deletions packages/@aws-cdk/aws-vpcv2-alpha/lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Construct } from 'constructs';
import { ISubnet, Subnet, SubnetType } from 'aws-cdk-lib/aws-ec2';
import { ISubnet, SubnetType } from 'aws-cdk-lib/aws-ec2';

/**
* Turn an arbitrary string into one that can be used as a CloudFormation identifier by stripping special characters
Expand Down Expand Up @@ -42,85 +41,6 @@ export function subnetId(name: string, i: number) {
return `${name}Subnet${i + 1}`;
}

export class ImportSubnetGroup {
private readonly subnetIds: string[];
private readonly names: string[];
private readonly routeTableIds: string[];
private readonly ipv4CidrBlocks: string[];
private readonly groups: number;

constructor(
subnetIds: string[] | undefined,
names: string[] | undefined,
routeTableIds: string[] | undefined,
ipv4CidrBlocks: string[] | undefined,
type: SubnetType,
private readonly availabilityZones: string[],
idField: string,
nameField: string,
routeTableIdField: string,
ipv4CidrBlockField: string) {

this.subnetIds = subnetIds || [];
this.routeTableIds = routeTableIds || [];
this.ipv4CidrBlocks = ipv4CidrBlocks || [];
this.groups = this.subnetIds.length / this.availabilityZones.length;

if (Math.floor(this.groups) !== this.groups) {
// eslint-disable-next-line max-len
throw new Error(`Number of ${idField} (${this.subnetIds.length}) must be a multiple of availability zones (${this.availabilityZones.length}).`);
}
if (this.routeTableIds.length !== this.subnetIds.length && routeTableIds != null) {
// We don't err if no routeTableIds were provided to maintain backwards-compatibility. See https://github.com/aws/aws-cdk/pull/3171
/* eslint-disable max-len */
throw new Error(`Number of ${routeTableIdField} (${this.routeTableIds.length}) must be equal to the amount of ${idField} (${this.subnetIds.length}).`);
}
if (this.ipv4CidrBlocks.length !== this.subnetIds.length && ipv4CidrBlocks != null) {
// We don't err if no ipv4CidrBlocks were provided to maintain backwards-compatibility.
/* eslint-disable max-len */
throw new Error(`Number of ${ipv4CidrBlockField} (${this.ipv4CidrBlocks.length}) must be equal to the amount of ${idField} (${this.subnetIds.length}).`);
}

this.names = this.normalizeNames(names, defaultSubnetName(type), nameField);
}

public import(scope: Construct): ISubnet[] {
return range(this.subnetIds.length).map(i => {
const k = Math.floor(i / this.availabilityZones.length);
return Subnet.fromSubnetAttributes(scope, subnetId(this.names[k], i), {
availabilityZone: this.pickAZ(i),
subnetId: this.subnetIds[i],
routeTableId: this.routeTableIds[i],
ipv4CidrBlock: this.ipv4CidrBlocks[i],
});
});
}

/**
* Return a list with a name for every subnet
*/
private normalizeNames(names: string[] | undefined, defaultName: string, fieldName: string) {
// If not given, return default
if (names === undefined || names.length === 0) {
return [defaultName];
}

// If given, must match given subnets
if (names.length !== this.groups) {
throw new Error(`${fieldName} must have an entry for every corresponding subnet group, got: ${JSON.stringify(names)}`);
}

return names;
}

/**
* Return the i'th AZ
*/
private pickAZ(i: number) {
return this.availabilityZones[i % this.availabilityZones.length];
}
}

/**
* Generate the list of numbers of [0..n)
*/
Expand Down

0 comments on commit 7b9885f

Please sign in to comment.