f0rkz / terraform-provider-netbox

Terraform provider plugin for DigitalOcean's NetBox IPAM and DCIM service - https://github.com/digitalocean/netbox - https://github.com/digitalocean/go-netbox

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status

Terraform Provider Plugin for Netbox

This repository holds a external plugin for a Terraform provider to manage resources within DigitalOcean's Netbox by way of the the golang API client for Netbox, go-netbox. The go-netbox client used here is a patched fork since the autogenerated upstream client has some broken model mappings due to Netbox API documentation errors.

NOTE: The version of go-netbox used here is compatible with Netbox API under version 2.7. Older versions do not work due to upstream Netbox API changes! Tested with Netbox v2.7.9.

Usage

Add the netbox provider to your tf file like so:

provider "netbox" {
    app_id = "abcdef12345678900987654321fedcba"
    endpoint = "https://your-netbox-url.example.com/api"
}

Where app_id is a Netbox token created in the Netbox Admin portal (click your username in the top right -> Admin -> Tokens) and endpoint is a URI to your Netbox instance API.

Once configured, you can use any of the following resources:

  • Ipam Resources:
    • netbox_ipam_prefixes_available_ips - Find and create available IP address in prefix
    • netbox_ipam_ip_address
  • Virtualization Resources:
    • netbox_virtualization_cluster
    • netbox_virtualization_virtual_machine
    • netbox_virtualization_interface - Network interface for Netbox Virtual Machines

And following data sources:

  • Ipam Data Sources:
    • netbox_ip_address - Get data for single IP address
    • netbox_prefixes_available_ips - Get list of available IPs under given prefix

Example (resources)

The following is an example that exercises the currently available functionality:

// Setup provider
provider "netbox" {
    app_id = "abcdef12345678900987654321fedcba"
    endpoint = "https://your-netbox-url.example.com/api"
}

// Create virtualization cluster
resource "netbox_virtualization_cluster" "my_virt_cluster" {
  name     = "my_virtualization_cluster1"
  type_id  = 2 // Your virtualization cluster type ID
  comments = "Created by Terraform"
}

// Create virtual machine under the cluster
resource "netbox_virtualization_virtual_machine" "my_server" {
  name       = "my-virtual-machine1"
  cluster_id = netbox_virtualization_cluster.my_virt_cluster.cluster_id
  disk_gb    = 100
  memory_mb  = 8192
  vcpus      = 2
  status     = "active" // Status fields are lowercase!
}

// Add interface to that virtual machine
resource "netbox_virtualization_interface" "my_server_iface" {
  name               = "eth0"
  virtual_machine_id = netbox_virtualization_virtual_machine.my_server.virtual_machine_id
}

// Create a new IP address under your existing prefix (available IPs API
// chooses next free one) and link it with your interface
resource "netbox_ipam_prefixes_available_ips" "my_server_ip" {
  prefix_id    = 1000 // Your prefix ID
  vrf_id       = 2 // Your VRF ID
  description  = "my-virtual-machine1 IP"
  status       = "active" // Status fields are lowercase
  interface_id = netbox_virtualization_interface.my_server_iface.interface_id
}

Example (data sources)

You can use two available data sources to get information out:

data "netbox_ip_address" "ip_test" {
  id = 4522
}

data "netbox_prefixes_available_ips" "prefix_source_test" {
  prefix_id = 1116
}

These data sources provide data like:

# data.netbox_ip_address.ip_test:
data "netbox_ip_address" "ip_test" {
    address     = "172.23.100.1/23"
    created     = "2020-03-03"
    description = "Example manual IP"
    family      = "IPv4"
    id          = 4522
    status      = "Active"
    tenant      = "Example tenant"
    vrf         = "Example VRF"
}

// Note that by default you get 50 IPs when getting available IPs, this is due
// to Netbox default pagination limit that is set to 50.

# data.netbox_prefixes_available_ips.prefix_source_test:
data "netbox_prefixes_available_ips" "prefix_source_test" {
    id           = "1116"
    prefix_id    = 1116
    v4_addresses = [
        "172.23.100.52/23",
        "172.23.100.53/23",
        "172.23.100.54/23",
        "172.23.100.55/23",
        "172.23.100.56/23",
        "172.23.100.57/23",
        "172.23.100.58/23",
        "172.23.100.59/23",
        "172.23.100.60/23",
        "172.23.100.61/23",
        "172.23.100.62/23",
        "172.23.100.63/23",
        "172.23.100.64/23",
        "172.23.100.65/23",
        "172.23.100.66/23",
        "172.23.100.67/23",
        "172.23.100.68/23",
        "172.23.100.69/23",
        "172.23.100.70/23",
        "172.23.100.71/23",
        "172.23.100.72/23",
        "172.23.100.73/23",
        "172.23.100.74/23",
        "172.23.100.75/23",
        "172.23.100.76/23",
        "172.23.100.77/23",
        "172.23.100.78/23",
        "172.23.100.79/23",
        "172.23.100.80/23",
        "172.23.100.81/23",
        "172.23.100.82/23",
        "172.23.100.83/23",
        "172.23.100.84/23",
        "172.23.100.85/23",
        "172.23.100.86/23",
        "172.23.100.87/23",
        "172.23.100.88/23",
        "172.23.100.89/23",
        "172.23.100.90/23",
        "172.23.100.91/23",
        "172.23.100.92/23",
        "172.23.100.93/23",
        "172.23.100.94/23",
        "172.23.100.95/23",
        "172.23.100.96/23",
        "172.23.100.97/23",
        "172.23.100.98/23",
        "172.23.100.99/23",
        "172.23.100.100/23",
        "172.23.100.101/23",
    ]
    v6_addresses = []
}

About

Terraform provider plugin for DigitalOcean's NetBox IPAM and DCIM service - https://github.com/digitalocean/netbox - https://github.com/digitalocean/go-netbox

License:Apache License 2.0


Languages

Language:Go 98.9%Language:Makefile 1.1%