e-breuninger / terraform-provider-netbox

Terraform provider to interact with Netbox
https://registry.terraform.io/providers/e-breuninger/netbox/latest/docs
Mozilla Public License 2.0
175 stars 128 forks source link

[Feature request] - Support netbox prefix bulk update #396

Open rslemon opened 1 year ago

rslemon commented 1 year ago

Currently we have netbox_prefix resource to manage prefixes, but it takes lots of time to apply when there are tens of thousands prexes to be manged.

Since bulk object management is supported via API, is it possible for us to introduce a new resource, something like netbox_prefix_bulk?

Maybe this involves creating some PRs under this project and https://github.com/fbreckle/go-netbox/

fbreckle commented 1 year ago

I feel that this style of api endpoint does not play particularly well with how terraform operates. Can you write some mock hcl code how this a netbox_bulk_prefixes resource could look in a terraform configuration? How would one reference one of the created prefixes? How would drift detection work?

As for your initial problem: After your initial apply (which will take long, granted), terraform will only apply things with diffs. You can use -refresh=false to prevent TF from refreshing all prefixes. This will also cost you your drift detection, though.

I find the idea intriguing, but I don't have the time experiment with this at the moment. I saw that netbox added these bulk operations sometime ago, but I did not look at them in detail.

rslemon commented 1 year ago

Hi @fbreckle

Thanks for checking on this issue. Even though we use -refresh=false, it take a few hours due to the big amount prefixes(tens of thousands...) to be manged. Here is the sample hcl code.

locals {
  prefixmap = { for index in range(10, 100) : index => {
    prefix = format("10.10.%s.0/24", index)
    vlanid = index
    }
  }
}

resource "netbox_prefixes_bulk" "default" {
  dynamic "prefix_bulk" {
    for_each = local.prefixmap
    content {
      prefix        = prefix_bulk.value.prefix
      vlan_id       = prefix_bulk.value.vlanid
      site_id       = 123
      status        = "active"
      mark_utilized = true
      tags          = []
      tenant_id     = 111
      vrf_id        = 123
      role_id       = 456
    }
  }
}

This resource would manage a list of prefixes above by one resource ID. So the corressponding TF state file would store the following state in the netbox_prefix_bulk.default resource.

    {
      "mode": "managed",
      "type": "netbox_prefix_bulk",
      "name": "default",
      "provider": "provider[\"registry.terraform.io/e-breuninger/netbox\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": [{
            "description": "",
            "id": "1",
            "is_pool": false,
            "mark_utilized": false,
            "prefix": "10.10.10.0/24",
            "role_id": 456,
            "site_id": 123,
            "status": "active",
            "tags": [],
            "tenant_id": 111,
            "vlan_id": 1,
            "vrf_id": 123
          },
         {
            "description": "",
            "id": "2",
            "is_pool": false,
            "mark_utilized": false,
            "prefix": "10.10.11.0/24",
            "role_id": 456,
            "site_id": 123,
            "status": "active",
            "tags": [],
            "tenant_id": 111,
            "vlan_id": 2,
            "vrf_id": 123
          },
        .
        .
        .
    ]
          "sensitive_attributes": [],

        }
      ]
    },

When it comes to drift detection,

image image

Please let me know your thought, if needed, I can also offer a PR in this regard and you can check later.

Thanks.