From e8f8255d46081dd228b71f6c39a296368fd0880b Mon Sep 17 00:00:00 2001 From: Muhammed abdelkader Date: Thu, 21 Nov 2024 09:54:59 +0100 Subject: [PATCH] feat: add avail subnet --- reports/available_subnets/ip.py | 28 ++++++++++++++++++ reports/available_subnets/main.tf | 49 +++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 reports/available_subnets/ip.py create mode 100644 reports/available_subnets/main.tf diff --git a/reports/available_subnets/ip.py b/reports/available_subnets/ip.py new file mode 100644 index 0000000..5432414 --- /dev/null +++ b/reports/available_subnets/ip.py @@ -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) diff --git a/reports/available_subnets/main.tf b/reports/available_subnets/main.tf new file mode 100644 index 0000000..426f842 --- /dev/null +++ b/reports/available_subnets/main.tf @@ -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 + +} \ No newline at end of file