netbox-community / netbox-bgp

NetBox plugin for BGP related objects documentation
Apache License 2.0
239 stars 45 forks source link

Unable to Create Sessions Using API after Update to 0.8.1 #125

Closed chris-windscribe closed 9 months ago

chris-windscribe commented 1 year ago

NetBox version 3.3.10

Describe the bug We use the Python pynetbox library to create data in Netbox programatically. After upgrading to Netbox 3.3.10 and BGP Plugin 0.8.1 we find we are unable to create BGP sessions any longer. The error returned is

Related Field got invalid lookup: number

This is a snippit of the data we are using to feed pynetbox:

ipv4_bgp_session = get_or_create (
            netbox.plugins.bgp.session,
            get = {
                "q": _host["host_name"]+'-ip4'
            },
            create = {
                'peer_group': 1,
                'local_address': {'address': _host["unicast_v4_mgmt"]},
                'remote_address': {'address': _host["peering_ipv4_remote"]},
                'local_as': {'asn': _host["asn_ipv4_local"]},
                'remote_as': {'asn': _host["asn_ipv4_remote"]},
                'name': _host["host_name"]+'-ip4',
                'device': {'name': _host["host_name"]}
            }
        )

To Reproduce Steps to reproduce the behavior:

  1. Upgrade from an older versio of netbox BGP (prior to the ASN model)
  2. Install Latest pynetbox
  3. Attempt to create a BGP session

Expected behavior BGP Session is created

Additional context Prior to the migration to the ASN model the following code worked flawlessly

ipv4_bgp_session = get_or_create (
            netbox.plugins.bgp.session,
            get = {
                "q": _host["host_name"]+'-ip4'
            },
            create = {
                'peer_group': 1,
                'local_address': {'address': _host["unicast_v4_mgmt"]},
                'remote_address': {'address': _host["peering_ipv4_remote"]},
                'local_as': {'number': _host["asn_ipv4_local"]},
                'remote_as': {'number': _host["asn_ipv4_remote"]},
                'name': _host["host_name"]+'-ip4',
                'device': {'name': _host["host_name"]}
            }
        )
bgianpetro commented 1 year ago

Are the ASNs you are passing in the script numbers or references to ASN objects in the Core Netbox Model? After your upgrade to 0.8.1, only the latter will work.

chris-windscribe commented 1 year ago

Thanks for the reply. The ASNs already exist in the ASN model, so I assumed I would be able to pass it the AS number, based on the response I was getting from a GET request to api/plugins/bgp/session/, if there's a different way I should be specifying existing AS numbers let me know.

bgianpetro commented 1 year ago

Sorry, didn't look closely enough at the original code - the issue is simply that you are using 'number' to reference the ASN instead of 'asn'; try replacing 'number' with 'asn' in your lookup and you should be good.

bgianpetro commented 1 year ago

Sorry, didn't look closely enough at the original code - the issue is simply that you are using 'number' to reference the ASN instead of 'asn'; try replacing 'number' with 'asn' in your lookup and you should be good.

Never mind this - didn't read the first code segment. How are you populating the references to the ASN objects?

chris-windscribe commented 1 year ago

Sorry, didn't look closely enough at the original code - the issue is simply that you are using 'number' to reference the ASN instead of 'asn'; try replacing 'number' with 'asn' in your lookup and you should be good.

Never mind this - didn't read the first code segment. How are you populating the references to the ASN objects?

The ASNs were originally populated using the 'Migrate' button in version 0.7.0 of the BGP Plugin. Currently, the ASN object looks like this in the API:

...
{
            "id": 18,
            "url": "https://netbox.int.controld.com/api/ipam/asns/18/",
            "display": "AS64515",
            "asn": 64515,
            "rir": 12,
            "tenant": {
                "id": 2,
                "url": "https://netbox.int.controld.com/api/tenancy/tenants/2/",
                "display": "ControlD",
                "name": "ControlD",
                "slug": "controld"
            },
            "description": "",
            "site_count": 0,
            "provider_count": 0,
            "tags": [
                {
                    "id": 15,
                    "url": "https://netbox.int.controld.com/api/extras/tags/15/",
                    "display": "prd",
                    "name": "prd",
                    "slug": "prd",
                    "color": "4caf50"
                }
            ],
            "custom_fields": {},
            "created": "2023-01-04T21:59:05.855207Z",
            "last_updated": "2023-01-04T21:59:05.914261Z"
        },
        ...
bgianpetro commented 1 year ago

Based on the error, it looks like it's trying to perform a lookup based on the 'number' key - is it possible that the 'number' key is referenced somewhere further downstream in the code? If it's possible it would be nice to see more context (i.e., what ever is performing the 'create'). Also, have you tried just adding a session manually in the REPL?