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

netbox_ip_address does not set virtualization.vminterface value correctly #334

Closed
vitaliyboch opened this issue Feb 6, 2023 · 2 comments

Comments

@vitaliyboch
Copy link

vitaliyboch commented Feb 6, 2023

System spec

NetBox version: 3.4.4
Python version: 3.8
Terrafrom version: 1.3.6
Netbox Terrafrom plugin version: 3.0.13

Steps to Reproduce

  1. Assign an ip address from a physical server:
resource "netbox_device_role" "hypervisor" {
  name      = "Hypervisor"
  slug      = "hypervisor"
  color_hex = "808080"  #gray
}

resource "netbox_manufacturer" "dell" {
  name = "Dell"
  slug = "dell"
}

resource "netbox_device_type" "server_dell" {
  model           = "PowerEdge R440"
  slug            = "poweredge-r440"
  manufacturer_id = netbox_manufacturer.dell.id
}

resource "netbox_device" "pyshical_server" {
  name           = "pyshical_server"
  device_type_id = netbox_device_type.server_dell.id
  role_id        = netbox_device_role.hypervisor.id
  site_id        = netbox_site.stack24_kazan.id
}

resource "netbox_device_interface" "pyshical_server_nic" {
  name        = "pyshical_server"
  type        = "1000base-kx"
  device_id   = netbox_device.pyshical_server.id
}

resource "netbox_ip_address" "pyshical_server_ip" {
  ip_address   = "10.70.23.2/25"
  status       = "active"
  dns_name     = "pyshical_server"
  interface_id = netbox_device_interface.pyshical_server_nic.id
}
  1. Open the netbox portal at https://netbox_server/ipam/ip-addresses/ and get the error:
Server Error
There was a problem with your request. Please contact an administrator.

The complete exception is provided below:

<class 'AttributeError'>

'NoneType' object has no attribute 'get_absolute_url'

Python version: 3.8.10
NetBox version: 3.4.4
If further assistance is required, please post to the [NetBox discussion forum](https://github.com/netbox-community/netbox/discussions) on GitHub.
  1. Check the API reply results at https://netbox_server/api/ipam/ip-addresses/ to review the assigned values.

  2. Comment interface_id = netbox_device_interface.pyshical_server_nic.id in the code and apply the code again. The issue is gone.

Analysys

The issue is in missing attribute assigned_object_type": "dcim.interface when the terraform plugin applies the code. By default the attribute equals "virtualization.vminterface".

These are fragments of API replies of implemeting physical interface assigment via the plugin and via the web-interface:

not assigned:

        {
            "id": 8,
            "url": "https://10.70.23.31/api/ipam/ip-addresses/8/",
            "display": "10.70.23.1/25",
            "family": {
                "value": 4,
                "label": "IPv4"
            },
            "address": "10.70.23.1/25",
            "vrf": null,
            "tenant": null,
            "status": {
                "value": "active",
                "label": "Active"
            },
            "role": null,
            "assigned_object_type": null,
            "assigned_object_id": null,
            "assigned_object": null,
            "nat_inside": null,
            "nat_outside": [],
            "comments": "",
            "tags": [],
            "custom_fields": {},
            "created": "2023-02-06T15:17:07.330362Z",
            "last_updated": "2023-02-06T15:56:00.283768Z"
        }

wrong:

        {
            "id": 8,
            "url": "https://10.70.23.31/api/ipam/ip-addresses/8/",
            "display": "10.70.23.1/25",
            "family": {
                "value": 4,
                "label": "IPv4"
            },
            "address": "10.70.23.1/25",
            "vrf": null,
            "tenant": null,
            "status": {
                "value": "active",
                "label": "Active"
            },
            "role": null,
            "assigned_object_type": "virtualization.vminterface",     <<<==== wrong type
            "assigned_object_id": 4,
            "assigned_object": {                                      <<<==== got wrong interface description by id from VM table
                "id": 4,
                "url": "https://10.70.23.31/api/virtualization/interfaces/4/",
                "display": "netbox-nic0",
                "virtual_machine": {
                    "id": 5,
                    "url": "https://10.70.23.31/api/virtualization/virtual-machines/5/",
                    "display": "netbox",
                    "name": "netbox"
                },
                "name": "netbox-nic0"
            },
            "nat_inside": null,
            "nat_outside": [],
            "dns_name": "osp-kvm-01",
            "comments": "",
            "tags": [],
            "custom_fields": {},
            "created": "2023-02-06T15:17:07.330362Z",
            "last_updated": "2023-02-06T15:57:30.014865Z"
        }

correct:

        {
            "id": 8,
            "url": "https://10.70.23.31/api/ipam/ip-addresses/8/",
            "display": "10.70.23.1/25",
            "family": {
                "value": 4,
                "label": "IPv4"
            },
            "address": "10.70.23.1/25",
            "vrf": null,
            "tenant": null,
            "status": {
                "value": "active",
                "label": "Active"
            },
            "role": null,
            "assigned_object_type": "dcim.interface",     <<<==== correct type
            "assigned_object_id": 4,
            "assigned_object": {                          <<<==== got corect interface description by id from DEVICES table
                "id": 4,
                "url": "https://10.70.23.31/api/dcim/interfaces/4/",
                "display": "osp-kvm-01-management",
                "device": {
                    "id": 5,
                    "url": "https://10.70.23.31/api/dcim/devices/5/",
                    "display": "osp-kvm-01",
                    "name": "osp-kvm-01"
                },
                "name": "osp-kvm-01-management",
                "cable": null,
                "_occupied": false
            },
            "nat_inside": null,
            "nat_outside": [],
            "dns_name": "osp-kvm-01",
            "comments": "",
            "tags": [],
            "custom_fields": {},
            "created": "2023-02-06T15:17:07.330362Z",
            "last_updated": "2023-02-06T16:00:29.262894Z"
        }

Expected Behavior

netbox_ip_address command should be able to detect or manually set type of assinned resources

Observed Behavior

netbox_ip_address command always set "assigned_object_type": "virtualization.vminterface" value

@fbreckle
Copy link
Collaborator

A refactoring for IP addresses is underway in #422 . Then you will be able to use virtual_machine_interface_id or device_interface_id.

As for the current versions:

netbox_ip_address command should be able to detect or manually set type of assinned resources

You should already be able to set the object_type attribute to dcim.interface. It merely defaults to virtualization.vminterface.

Detecting the type automatically will not work, because if you have a vm interface with id 1 and a device interface with id 1, how would the provider find out what type you want to link if you set interface_id = 1?

@fbreckle
Copy link
Collaborator

Closed in v3.5.0

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