smutel / terraform-provider-netbox

Terraform provider for Netbox
ISC License
58 stars 19 forks source link

netbox_ip_address does not set virtualization.vminterface value correctly #179

Closed vitaliyboch closed 1 year ago

vitaliyboch commented 1 year ago

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 on GitHub.

3. Check the API reply results at https://netbox_server/api/ipam/ip-addresses/ to review the assigned values.

4. 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"` be able to detect or manually set type of assinned resources
amhn commented 1 year ago

I believe you are using the e-breuninger/netbox provider. This provider is smutel/netbox.

You can report issues there: https://github.com/e-breuninger/terraform-provider-netbox/issues

vitaliyboch commented 1 year ago

You are right, the provider author is another one. The ticket has been copied to https://github.com/e-breuninger/terraform-provider-netbox/issues/334