Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rslemon opened this issue May 10, 2023 · 2 comments
Open

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

rslemon opened this issue May 10, 2023 · 2 comments

Comments

@rslemon
Copy link

rslemon commented May 10, 2023

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
Copy link
Collaborator

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
Copy link
Author

rslemon commented May 15, 2023

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,

  • With current netbox version 3.4.10, maybe we get all prefixes first, then just filter the target prefixes by prefix_id from TF state file?
  • Or if we bump the netbox version to something like 3.5.1, we might be able use the followig API endpoint. It seems it can get selected prefixes specified by array list.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants