Skip to content

Commit

Permalink
adding name property for secondary ip address
Browse files Browse the repository at this point in the history
  • Loading branch information
shikha372 committed Aug 6, 2024
1 parent 0b7a56f commit 6bcf8b7
Show file tree
Hide file tree
Showing 38 changed files with 253 additions and 246 deletions.
18 changes: 15 additions & 3 deletions packages/@aws-cdk/aws-vpcv2-alpha/lib/subnet-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ export interface SubnetV2Props {

}

/**
* Interface with additional properties for SubnetV2
*/
export interface ISubnetV2 extends ISubnet {

/**
* The IPv6 CIDR block for this subnet
*/
readonly ipv6CidrBlock?: string;

}

/**
* The SubnetV2 class represents a subnet within a VPC (Virtual Private Cloud) in AWS.
* It extends the Resource class and implements the ISubnet interface.
Expand All @@ -96,7 +108,7 @@ export interface SubnetV2Props {
* @resource AWS::EC2::Subnet
*
*/
export class SubnetV2 extends Resource implements ISubnet {
export class SubnetV2 extends Resource implements ISubnetV2 {

/**
* The Availability Zone the subnet is located in
Expand Down Expand Up @@ -334,7 +346,7 @@ function checkCidrRanges(vpc: IVpcV2, cidrRange: string) {

function validateOverlappingCidrRanges(vpc: IVpcV2, ipv4CidrBlock: string): boolean {

let allSubnets: ISubnet[];
let allSubnets: ISubnetV2[];
try {
allSubnets = vpc.selectSubnets().subnets;
} catch (e) {
Expand Down Expand Up @@ -373,7 +385,7 @@ function validateOverlappingCidrRanges(vpc: IVpcV2, ipv4CidrBlock: string): bool
*/
function validateOverlappingCidrRangesipv6(vpc: IVpcV2, ipv6CidrBlock: string): boolean {

let allSubnets: ISubnet[];
let allSubnets: ISubnetV2[];
try {
allSubnets = vpc.selectSubnets().subnets;
} catch (e) {
Expand Down
54 changes: 27 additions & 27 deletions packages/@aws-cdk/aws-vpcv2-alpha/lib/vpc-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@ export class IpAddresses {
/**
* An IPv4 CIDR Range
*/
public static ipv4(ipv4Cidr: string): IIpAddresses {
return new ipv4CidrAllocation(ipv4Cidr);
public static ipv4(ipv4Cidr: string, cidrBlockName?: string): IIpAddresses {
return new ipv4CidrAllocation(ipv4Cidr, cidrBlockName);
}

/**
* An Ipv4 Ipam Pool
*/
public static ipv4Ipam(ipv4IpamOptions: IpamOptions): IIpAddresses {
return new IpamIpv4(ipv4IpamOptions);
public static ipv4Ipam(ipv4IpamOptions: IpamOptions, cidrBlockName?: string): IIpAddresses {
return new IpamIpv4(ipv4IpamOptions, cidrBlockName);
}

/**
* An Ipv6 Ipam Pool
*/
public static ipv6Ipam(ipv6IpamOptions: IpamOptions): IIpAddresses {
return new IpamIpv6(ipv6IpamOptions);
public static ipv6Ipam(ipv6IpamOptions: IpamOptions, cidrBlockName: string): IIpAddresses {
return new IpamIpv6(ipv6IpamOptions, cidrBlockName);
}

/**
* Amazon Provided Ipv6 range
*/
public static amazonProvidedIpv6() : IIpAddresses {
return new AmazonProvided();
public static amazonProvidedIpv6(cidrBlockName: string) : IIpAddresses {
return new AmazonProvided(cidrBlockName);
}
}

Expand Down Expand Up @@ -95,6 +95,13 @@ export interface VpcCidrOptions {
* @default - No dependency
*/
readonly dependencies?: CfnResource[];

/**
* Required to set Secondary cidr block resource name
* in order to generate unique logical id for the resource.
* @default : no name for primary addresses
*/
readonly cidrBlockName?: string;
}

/**
Expand Down Expand Up @@ -289,13 +296,13 @@ export class VpcV2 extends VpcV2Base {
if (props.secondaryAddressBlocks) {
const secondaryAddressBlocks: IIpAddresses[] = props.secondaryAddressBlocks;

let ipCount = 0;
for (const secondaryAddressBlock of secondaryAddressBlocks) {
//Counter to generate a random string for input to hash function
ipCount+=1;
const hash = pathHash('Secondary'+ipCount);

const secondaryVpcOptions: VpcCidrOptions = secondaryAddressBlock.allocateVpcCidr();
if (!secondaryVpcOptions.cidrBlockName) {
throw new Error('Cidr Block Name is required to create secondary IP address');
}
const hash = pathHash(secondaryVpcOptions.cidrBlockName);

if (secondaryVpcOptions.amazonProvided || secondaryVpcOptions.ipv6IpamPool) {
this.useIpv6 = true;
Expand Down Expand Up @@ -353,8 +360,7 @@ export class VpcV2 extends VpcV2Base {
*/
class ipv4CidrAllocation implements IIpAddresses {

constructor(private readonly cidrBlock: string) {

constructor(private readonly cidrBlock: string, private readonly cidrBlockName?: string) {
}

/**
Expand All @@ -363,6 +369,7 @@ class ipv4CidrAllocation implements IIpAddresses {
allocateVpcCidr(): VpcCidrOptions {
return {
ipv4CidrBlock: this.cidrBlock,
cidrBlockName: this.cidrBlockName,
};
}
}
Expand All @@ -379,11 +386,12 @@ class AmazonProvided implements IIpAddresses {
* Amazon will automatically assign an IPv6 CIDR range from its pool of available addresses.
*/

constructor() {};
constructor(private readonly cidrBlockName?: string) {};

allocateVpcCidr(): VpcCidrOptions {
return {
amazonProvided: true,
cidrBlockName: this.cidrBlockName,
};
}

Expand All @@ -395,14 +403,15 @@ class AmazonProvided implements IIpAddresses {
*/
class IpamIpv6 implements IIpAddresses {

constructor(private readonly props: IpamOptions) {
constructor(private readonly props: IpamOptions, private readonly cidrBlockName?: string) {
}

allocateVpcCidr(): VpcCidrOptions {
return {
ipv6NetmaskLength: this.props.netmaskLength,
ipv6IpamPool: this.props.ipamPool,
dependencies: this.props.ipamPool?.ipamCidrs.map(c => c as CfnResource),
cidrBlockName: this.cidrBlockName,
};
}
}
Expand All @@ -413,13 +422,14 @@ class IpamIpv6 implements IIpAddresses {
*/
class IpamIpv4 implements IIpAddresses {

constructor(private readonly props: IpamOptions) {
constructor(private readonly props: IpamOptions, private readonly cidrBlockName?: string) {
}
allocateVpcCidr(): VpcCidrOptions {

return {
ipv4NetmaskLength: this.props.netmaskLength,
ipv4IpamPool: this.props.ipamPool,
cidrBlockName: this.cidrBlockName,
};
}
}
Expand Down Expand Up @@ -475,13 +485,3 @@ function pathHash(path: string): string {
const md5 = md5hash(path);
return md5.slice(0, 4).toUpperCase();
}

// function generateRandomString(length: number): string {
// const pattern = /[a-zA-Z0-9]/;
// const characters = pattern.source.replace(/\\/g, '');;
// let result = '';
// for (let i = 0; i < length; i++) {
// result += characters.charAt(Math.floor(Math.random() * characters.length));
// }
// return result;
// }

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"InstanceTenancy": "default"
}
},
"defaultSecondaryIp47C3A8F96659": {
"defaultSecondaryIp4EEF1E2A80B8": {
"Type": "AWS::EC2::VPCCidrBlock",
"Properties": {
"AmazonProvidedIpv6CidrBlock": true,
Expand Down Expand Up @@ -57,7 +57,7 @@
}
},
"DependsOn": [
"defaultSecondaryIp47C3A8F96659"
"defaultSecondaryIp4EEF1E2A80B8"
]
},
"defaultSubnetRouteTableAssociationF1D85D29": {
Expand All @@ -74,7 +74,7 @@
}
},
"DependsOn": [
"defaultSecondaryIp47C3A8F96659"
"defaultSecondaryIp4EEF1E2A80B8"
]
}
},
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"InstanceTenancy": "default"
}
},
"dynamodbSecondaryIp47C3D307DC85": {
"dynamodbSecondaryIp4EEF58BFBEFB": {
"Type": "AWS::EC2::VPCCidrBlock",
"Properties": {
"AmazonProvidedIpv6CidrBlock": true,
Expand Down Expand Up @@ -57,7 +57,7 @@
}
},
"DependsOn": [
"dynamodbSecondaryIp47C3D307DC85"
"dynamodbSecondaryIp4EEF58BFBEFB"
]
},
"dynamodbSubnetRouteTableAssociationC38B30F3": {
Expand All @@ -74,7 +74,7 @@
}
},
"DependsOn": [
"dynamodbSecondaryIp47C3D307DC85"
"dynamodbSecondaryIp4EEF58BFBEFB"
]
},
"testDynamoEndpoint03D5BDE5": {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"InstanceTenancy": "default"
}
},
"eigwSecondaryIp47C35F6F8767": {
"eigwSecondaryIp4EEFA473D0E8": {
"Type": "AWS::EC2::VPCCidrBlock",
"Properties": {
"AmazonProvidedIpv6CidrBlock": true,
Expand Down Expand Up @@ -46,7 +46,7 @@
}
},
"DependsOn": [
"eigwSecondaryIp47C35F6F8767"
"eigwSecondaryIp4EEFA473D0E8"
]
},
"eigwSubnetRouteTableAssociation887F4A97": {
Expand All @@ -63,7 +63,7 @@
}
},
"DependsOn": [
"eigwSecondaryIp47C35F6F8767"
"eigwSecondaryIp4EEFA473D0E8"
]
},
"testEOIGWEIGW54CCAD37": {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"InstanceTenancy": "default"
}
},
"igwSecondaryIp47C345C8DF7D": {
"igwSecondaryIp4EEF3CD924BF": {
"Type": "AWS::EC2::VPCCidrBlock",
"Properties": {
"AmazonProvidedIpv6CidrBlock": true,
Expand Down Expand Up @@ -57,7 +57,7 @@
}
},
"DependsOn": [
"igwSecondaryIp47C345C8DF7D"
"igwSecondaryIp4EEF3CD924BF"
]
},
"igwSubnetRouteTableAssociationA48C27F3": {
Expand All @@ -74,7 +74,7 @@
}
},
"DependsOn": [
"igwSecondaryIp47C345C8DF7D"
"igwSecondaryIp4EEF3CD924BF"
]
},
"testIGW8D947AF2": {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6bcf8b7

Please sign in to comment.