hashicorp / terraform-cidr-subnets

A Terraform module for calculating subnet IP address prefixes
Mozilla Public License 2.0
78 stars 41 forks source link

Ability to reserve a cidr_block #7

Open braunsonm opened 2 years ago

braunsonm commented 2 years ago

I would like the ability to provide a cidr_block attribute to denote that network as already reserved. For instance:

base_cidr_block = "10.0.0.0/8"
network = [
  {
    name = "reserved",
    new_bits = 8,
    cidr_block = "10.1.0.0/16"
  },
  {
    name = "new",
    new_bits = 8
  }
]

The new block should not try to use the reserved cidr_block

apparentlymart commented 2 years ago

Hi @braunsonm,

Perhaps I misunderstand what you are asking, but I think what you asked for here is already supported if you set the name of the first element to null. As described in the README, that will make it still consume the described address space but the resulting prefix won't be included in the result map.

braunsonm commented 2 years ago

Sorry for the confusion, that does answer my question but what I'm looking for is for a way to be able to actually specify the cidr_block with a name. There are cases where I want to use a network (indexing the array by key) but I already know what the cidr has to be.

network = [
  {
    name = "reserved",
    new_bits = 8,
    cidr_block = "10.2.0.0/16"
  }
]

If name is set, then cidr_block seems to get ignored. What I'm asking is if the cidr_block is user provided, then it should respect that. The output from the above will not use 10.2.0.0/16

braunsonm commented 2 years ago

Are you sure what you described works?

network = [
  {
    name = null,
    new_bits = 7,
    cidr_block = "10.0.0.0/15"
  },
  {
    name = null,
    new_bits = 8,
    cidr_block = "10.42.0.0/16"
  },
  {
    name = "test,
    new_bits = 8
  },
]

test is output with a cidr block of 10.3.0.0/16 when the first available is 10.2.0.0/16 because the second null on 10.42.0.0/16 is getting ignored and placed in 10.2.0.0/16.

apparentlymart commented 2 years ago

Hi @braunsonm,

Indeed, this module does not allow you to specify CIDR blocks as the caller. This module's intended purpose is to calculate new CIDR blocks based on a specification.

What you are asking for seems to be outside the scope of this module, so I don't expect we would add such a capability here. You could fork this module and change it to work in the way you need in principle, although I think the specific functionality you'd need here (to tightly pack blocks while "working around" reserved areas) is perhaps beyond the capabilities of the Terraform functionality this module uses and so it might also require a custom Terraform provider to encapsulate the address calculation logic.