Skip to content

Commit

Permalink
feat: add avail subnet
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammed abdelkader committed Nov 21, 2024
1 parent 37fe33f commit e8f8255
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
28 changes: 28 additions & 0 deletions reports/available_subnets/ip.py
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)
49 changes: 49 additions & 0 deletions reports/available_subnets/main.tf
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

}

0 comments on commit e8f8255

Please sign in to comment.