Skip to content

smutel/terraform-provider-netbox

Repository files navigation

terraform-provider-netbox

Lisence Conventional Commits Build Status Issues Last release Go Report Card

Terraform provider for Netbox.

Requirements

  • A libc library like libc6-compat or libc-utils
  • General developer tools like make, bash, ... (to build the provider)
  • Go 1.21 minimum (to build the provider)
  • Terraform (to use the provider)

Compatibility with Netbox

Netbox version Provider version
2.8 0.x.y
2.9 1.x.y
2.11 2.x.y
3.0 3.x.y
3.1 4.x.y
3.2 5.x.y
3.3 6.x.y
3.4 7.x.y

go-netbox

This project does not use netbox-community/go-netbox but the same library generated by myself located here smutel/go-netbox for three reasons:

  • By the past this project was not maintained anymore so I decided to create my own project
  • I am using a patch strategy and generation of the library by the CI
  • I does not need to wait if I need a patch quickly

Building the provider

Clone repository to: $GOPATH/src/github.com/smutel/terraform-provider-netbox

mkdir -p $GOPATH/src/github.com/smutel
cd $GOPATH/src/github.com/smutel
git clone git@github.com:smutel/terraform-provider-netbox.git

Enter the provider directory and build the provider

cd $GOPATH/src/github.com/smutel/terraform-provider-netbox
make build

Testing

To run the tests you need to have a running netbox installation. For example netbox-docker.

To run the tests execute:

source utils/netbox_docker_variables.sh
TF_ACC=1 go test -v ./...

Scheme and URL are not needed it the default is used (https and localhost:8000)

To run only some tests execute the following:

source utils/netbox_docker_variables.sh
TF_ACC=1 go test -v ./... -run TestAccNetboxVirtualizationVM

Debugging the provider

To run this provider in delve run:

dlv exec --accept-multiclient --continue --headless ./terraform-provider-netbox -- -debug

For Visual Studio Code a config is provided in this repo. Pressing should run the provider in debug mode.

This will output a value for TF_REATTACH_PROVIDER:

Starting: /home/andy/go/bin/dlv dap --check-go-version=false --listen=127.0.0.1:43501 --log-dest=3 from /home/andy/terraform/terraform-provider-netbox
DAP server listening at: 127.0.0.1:43501
Type 'dlv help' for list of commands.
{"@level":"debug","@message":"plugin address","@timestamp":"2022-09-04T19:19:00.482554+02:00","address":"/tmp/plugin2734508527","network":"unix"}
Provider started. To attach Terraform CLI, set the TF_REATTACH_PROVIDERS environment variable with the following:

	TF_REATTACH_PROVIDERS='{"registry.terraform.io/smutel/netbox":{"Protocol":"grpc","ProtocolVersion":5,"Pid":2519102,"Test":true,"Addr":{"Network":"unix","String":"/tmp/plugin2734508527"}}}'

In your terrafom directory run the following:

export TF_REATTACH_PROVIDER=(Value from above)
terraform plan

More information about debugging a terraform provider can be found in the terraform documentation

Installing the provider


NOTE

Before changing the version of the provider, please remove the temporary folder .terraform and ~/.terraform.d.


Automatic installation from Terraform 0.13

terraform {
  required_providers {
    netbox = {
      source = "smutel/netbox"
      version = "~> 6.0.0"
    }
  }
}

Manual installation

You can install the provider manually in your global terraform provider folder.

export NETBOX_PROVIDER_VERSION=6.0.0
mkdir -p ~/.terraform.d/plugins/registry.terraform.io/smutel/netbox/${NETBOX_PROVIDER_VERSION}/linux_amd64
cp terraform-provider-netbox_v${NETBOX_PROVIDER_VERSION} ~/.terraform.d/plugins/registry.terraform.io/smutel/netbox/${NETBOX_PROVIDER_VERSION}/linux_amd64/terraform-provider-netbox_v${NETBOX_PROVIDER_VERSION}

Manual installation (to test the compiled version = version 0.0.1)

make localinstall
==> Creating folder ~/.terraform.d/plugins/registry.terraform.io/smutel/netbox/0.0.1/linux_amd64
==> Installing provider in this folder

Using the provider

The definition of the provider is optional. All the parameters could be setup by environment variables.

provider netbox {
  # Environment variable NETBOX_URL
  url = "127.0.0.1:8000"

  # Environment variable NETBOX_TOKEN
  token = "0123456789abcdef0123456789abcdef01234567"

  # Environment variable NETBOX_SCHEME
  scheme = "http"

  # Environment variable NETBOX_INSECURE
  insecure = "true"
}

For further information, check this documentation

Contributing to this project

To contribute to this project, please follow the conventional commits rules.