netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Public demo: https://demo.netbox.dev
http://netboxlabs.com/oss/netbox/
Apache License 2.0
15.68k stars 2.53k forks source link

Provide an API endpoint to enable ASN object assignments #14110

Closed richardjohnallsopp closed 10 months ago

richardjohnallsopp commented 10 months ago

NetBox version

v3.6.2

Feature type

Change to existing functionality

Proposed functionality

It could be something I'm missing, but I don't see a way to assign an ASN object to a site like you can with contact objects.

Use case

We're in the process of migrating from v2.11.11 to v3.6.2 and there are several changes to the way data is handled. We're writing some scripts to handle these changes like pulling contacts from our current version and creating + assigning them on our updated stage version. We don't see a way to handle ASN objects programatically using the API and Pynetbox.

For example with contacts we were able to do this:

import pynetbox

nb_prod = pynetbox.api(url='https://netbox.prod, token='')
nb_dev = pynetbox.api(url='https://netbox.stage', token='')

# fetch all site objects
nb_site_list = nb_prod.dcim.sites.all()

def assigning_site_contacts(data,api):

  for site in data:
    if site.contact_name != "":
      assigning_contacts = api.tenancy.contact_assignments.create(
          content_type = "dcim.site",
          object_id = site.id,
          contact = {'name': site.contact_name},
          role = {'name': 'Site Contact'},
      )
    print("assigning contacts: " + str(assigning_contacts))

assigning_site_contacts(nb_site_list,nb_dev)

Could we add something like this for ASN objects?

Database changes

No response

External dependencies

No response

siemhermans commented 10 months ago

We have a similar usecase where we want to assign ASNs to dcim.device objects. We run a spine-leaf datacenter fabric where each switch has its own ASN. Currently we use the description field to 'assign' an ASN to a device and query the description in our automation tooling: https://netbox.internal/api/ipam/asns/?description=$DEVICE_NAME

This works, but the downside is that a namechange of a device would require a change on the ASN-object descrption as well.

stavr666 commented 10 months ago

This works, but the downside is that a namechange of a device would require a change on the ASN-object descrption as well.

You can use custom field with "device" objects.