-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Muhammed abdelkader
committed
Nov 21, 2024
1 parent
37fe33f
commit e8f8255
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import ipaddress | ||
|
||
# Define the VPC CIDR and allocated subnets | ||
vpc_cidr = "" | ||
allocated = [ | ||
"", | ||
] | ||
|
||
# Create the VPC network object | ||
vpc = ipaddress.IPv4Network(vpc_cidr) | ||
allocated_networks = [ipaddress.IPv4Network(cidr) for cidr in allocated] | ||
|
||
# Start with the entire VPC CIDR and subtract allocated networks | ||
remaining = [vpc] | ||
for network in allocated_networks: | ||
new_remaining = [] | ||
for r in remaining: | ||
# Only exclude if the allocated network is within the current range | ||
if network.overlaps(r): | ||
new_remaining.extend(r.address_exclude(network)) | ||
else: | ||
new_remaining.append(r) # Keep the current range as-is | ||
remaining = new_remaining | ||
|
||
# Print the available CIDR ranges | ||
print("Available CIDR ranges:") | ||
for r in remaining: | ||
print(r) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
provider "aws" { | ||
region = "eu-west-1" | ||
} | ||
|
||
# Variable to select a VPC by name | ||
variable "selected_vpc_name" { | ||
description = "The name of the VPC to use for further operations" | ||
type = string | ||
} | ||
|
||
# Fetch the VPC by Name | ||
data "aws_vpc" "selected_vpc" { | ||
filter { | ||
name = "tag:Name" | ||
values = [var.selected_vpc_name] | ||
} | ||
} | ||
|
||
# Fetch all subnets in the VPC | ||
data "aws_subnets" "vpc_subnets" { | ||
filter { | ||
name = "vpc-id" | ||
values = [data.aws_vpc.selected_vpc.id] | ||
} | ||
} | ||
|
||
# Fetch details for each subnet | ||
data "aws_subnet" "details" { | ||
for_each = toset(data.aws_subnets.vpc_subnets.ids) | ||
id = each.value | ||
} | ||
|
||
# Output all allocated CIDR blocks | ||
output "allocated_cidr_blocks" { | ||
value = [ | ||
for subnet in data.aws_subnet.details : | ||
subnet.cidr_block | ||
] | ||
} | ||
|
||
# Available CIDR block calculation (placeholder for custom logic) | ||
output "available_cidr_ranges" { | ||
value = "Custom logic required: Subtract allocated CIDRs from VPC CIDR" | ||
} | ||
|
||
output "vpc_cidr" { | ||
value = data.aws_vpc.selected_vpc.cidr_block | ||
|
||
} |