e-breuninger / terraform-provider-netbox

Terraform provider to interact with Netbox
https://registry.terraform.io/providers/e-breuninger/netbox/latest/docs
Mozilla Public License 2.0
178 stars 130 forks source link

resource "netbox_available_ip_address" : panic: runtime error: invalid memory address or nil pointer dereference #583

Closed deanfr closed 4 months ago

deanfr commented 5 months ago

Issue Reporting Guide

Hi there,

Thank you for opening an issue. Please note that we try to keep the Terraform issue tracker reserved for bug reports and feature requests. For general usage questions, please see: https://www.terraform.io/community.html.

Terraform Version

1.3.5 / 1.5.7

netbox server is 3.6.9

Affected Resource(s)

Please list the resources as a list, for example:

If this issue appears to affect multiple resources, it may be an issue with Terraform's core, so please mention this.

Terraform Configuration Files

terraform {
  required_version = ">= 1.3.5"
  required_providers {
    http = {
      source = "hashicorp/http"
    }
    netbox = {
      source = "e-breuninger/netbox"
      version = "3.7.7"
    }
  }
}

Debug Output

https://gist.github.com/deanfr/d7fc098e26543c64654a241500db46ea [...] Stack trace from the terraform-provider-netbox_v3.7.7 plugin:

panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x118d763] [...]

Panic Output

If Terraform produced a panic, please provide a link to a GitHub Gist containing the output of the crash.log.

Expected Behavior

netbox_available_ip_address resource should be created

Actual Behavior

netbox terraform provider crash

Steps to Reproduce

  1. terraform apply

Important Factoids

fbreckle commented 5 months ago

Does the prefix have IPs left?

It says the error happens in https://github.com/e-breuninger/terraform-provider-netbox/blob/master/netbox/resource_netbox_available_ip_address.go#L123 . I suppose the payload is nonexistant, but I cannot tell why that is from the output.

Error handling at that part of the code should be better.

deanfr commented 5 months ago

yes, have tried various prefixes passing the prefix ID

ipam.NewIpamPrefixesAvailableIpsCreateParams() call get null

jacksonwg commented 5 months ago

I can reproduce this as well with slightly different versions:

Netbox: 3.7.5 Terraform: 1.5.7 and 1.8.2 Provider version: 3.8.5

The crash output is almost identical, I've uploaded the logs to https://gist.github.com/jacksonwg/6206b8834a0a73af200e0ed2d71d3df3

I've validated that I get an IP address when I try the API manually with my API key. The account I'm using against this instance only has permissions to items with a specific tenant or tag. If I don't specify the proper tenant name when making the API request, it fails with permission denied.

I cannot reproduce this in a Netbox 3.7.7 instance with an API key on a super-user account.

When I look at the change log of the address on the successful 3.7.7 instance, I see that there are two entries. One for creation and the second for update, that adds the tenant.

I don't see a crash when I try to use the netbox_available_prefix but I do get a permission error, likely caused by this same two-step workflow.

sanderskjulsvik commented 5 months ago

If you want a test example for reproducing the issue you can checkout this commit: https://github.com/pexip/terraform-provider-netbox-e-breuninger/commit/4065bac9e7852fec954de049300f4cada36c2b9b, and run TEST_FUNC=TestAccNetboxAvailableIPAddress_multiple_cidrs_ranges make testacc-specific-test. It gives:

 TEST_FUNC=TestAccNetboxAvailableIPAddress_multiple_cidrs_ranges  make testacc-specific-test                                                            6s  Mon 06 May 2024 04:25:28 PM CEST
⌛ Startup acceptance tests on http://localhost:8001 with version v3.7.6
⌛ Testing function TestAccNetboxAvailableIPAddress_multiple_cidrs_ranges
TF_ACC=1 go test -timeout 20m -v -cover netbox/*.go -run TestAccNetboxAvailableIPAddress_multiple_cidrs_ranges
=== RUN   TestAccNetboxAvailableIPAddress_multiple_cidrs_ranges
=== PAUSE TestAccNetboxAvailableIPAddress_multiple_cidrs_ranges
=== CONT  TestAccNetboxAvailableIPAddress_multiple_cidrs_ranges
    resource_netbox_available_ip_address_test.go:334: Step 1/1 error: Error running apply: exit status 1

        Error: unable to create a ip address for prefixes: [6 5], err: [POST /ipam/prefixes/{id}/available-ips/][404] ipam_prefixes_available-ips_create default  map[detail:Not found.]

          with netbox_available_ip_address.test,
          on terraform_plugin_test.tf line 11, in resource "netbox_available_ip_address" "test":
          11: resource "netbox_available_ip_address" "test" {

        Error: unable to handle payload: payload is nil

          with netbox_available_ip_address.test2,
          on terraform_plugin_test.tf line 17, in resource "netbox_available_ip_address" "test2":
          17: resource "netbox_available_ip_address" "test2" {

--- FAIL: TestAccNetboxAvailableIPAddress_multiple_cidrs_ranges (2.99s)
FAIL
coverage: 4.0% of statements
FAIL    command-line-arguments  3.013s
FAIL
make: *** [GNUmakefile:23: testacc-specific-test] Error 1

I added handling for the error.

sanderskjulsvik commented 5 months ago

My error was caused by a typo I made :cry:

deanfr commented 4 months ago

and mine by a wrong token ...