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

✨ Add support for device ip addresses #341

Merged
merged 1 commit into from
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/data-sources/devices.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Read-Only:
- `asset_tag` (String)
- `cluster_id` (Number)
- `comments` (String)
- `custom_fields` (Map of String)
fbreckle marked this conversation as resolved.
Show resolved Hide resolved
- `device_id` (Number)
- `device_type_id` (Number)
- `location_id` (Number)
Expand Down
1 change: 1 addition & 0 deletions docs/resources/device.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ resource "netbox_device" "test" {

- `cluster_id` (Number)
- `comments` (String)
- `custom_fields` (Map of String)
fbreckle marked this conversation as resolved.
Show resolved Hide resolved
- `location_id` (Number)
- `platform_id` (Number)
- `serial` (String)
Expand Down
1 change: 1 addition & 0 deletions docs/resources/ip_address.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ resource "netbox_ip_address" "myvm_ip" {
- `description` (String)
- `dns_name` (String)
- `interface_id` (Number)
- `object_type` (String) Defaults to `virtualization.vminterface`.
- `role` (String)
- `tags` (Set of String)
- `tenant_id` (Number)
Expand Down
2 changes: 2 additions & 0 deletions docs/resources/site.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ resource "netbox_site" "example1" {
- `group_id` (Number)
- `latitude` (Number)
- `longitude` (Number)
- `physical_address` (String)
fbreckle marked this conversation as resolved.
Show resolved Hide resolved
- `region_id` (Number)
- `shipping_address` (String)
fbreckle marked this conversation as resolved.
Show resolved Hide resolved
fbreckle marked this conversation as resolved.
Show resolved Hide resolved
- `slug` (String)
- `status` (String) Defaults to `active`.
- `tags` (Set of String)
Expand Down
12 changes: 10 additions & 2 deletions netbox/resource_netbox_ip_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ func resourceNetboxIPAddress() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"object_type": {
Type: schema.TypeString,
Optional: true,
Default: "virtualization.vminterface",
ValidateFunc: validation.StringInSlice([]string{"virtualization.vminterface", "dcim.interface"}, false),
},
tagsKey: tagsSchema,
"description": {
Type: schema.TypeString,
Expand Down Expand Up @@ -114,8 +120,10 @@ func resourceNetboxIPAddressRead(d *schema.ResourceData, m interface{}) error {

if res.GetPayload().AssignedObjectID != nil {
d.Set("interface_id", res.GetPayload().AssignedObjectID)
d.Set("object_type", res.GetPayload().AssignedObjectType)
} else {
d.Set("interface_id", nil)
d.Set("object_type", nil)
}

if res.GetPayload().Vrf != nil {
Expand Down Expand Up @@ -156,6 +164,7 @@ func resourceNetboxIPAddressUpdate(d *schema.ResourceData, m interface{}) error

ipAddress := d.Get("ip_address").(string)
status := d.Get("status").(string)
objectType := d.Get("object_type").(string)

descriptionValue, ok := d.GetOk("description")
if ok {
Expand All @@ -179,8 +188,7 @@ func resourceNetboxIPAddressUpdate(d *schema.ResourceData, m interface{}) error
}

if interfaceID, ok := d.GetOk("interface_id"); ok {
// The other possible type is dcim.interface for devices
data.AssignedObjectType = strToPtr("virtualization.vminterface")
data.AssignedObjectType = strToPtr(objectType)
data.AssignedObjectID = int64ToPtr(int64(interfaceID.(int)))
}

Expand Down
77 changes: 77 additions & 0 deletions netbox/resource_netbox_ip_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,41 @@ resource "netbox_interface" "test" {
`, testName)
}

func testAccNetboxIPAddressFullDeviceDependencies(testName string) string {
return fmt.Sprintf(`
resource "netbox_site" "test" {
name = "%[1]s"
status = "active"
}

resource "netbox_device_role" "test" {
name = "%[1]s"
color_hex = "123456"
}

resource "netbox_manufacturer" "test" {
name = "%[1]s"
}

resource "netbox_device_type" "test" {
model = "%[1]s"
manufacturer_id = netbox_manufacturer.test.id
}

resource "netbox_device" "test" {
name = "%[1]s"
site_id = netbox_site.test.id
device_type_id = netbox_device_type.test.id
role_id = netbox_device_role.test.id
}
resource "netbox_device_interface" "test" {
name = "%[1]s"
device_id = netbox_device.test.id
type = "1000base-t"
}
`, testName)
}

func TestAccNetboxIPAddress_basic(t *testing.T) {

testIP := "1.1.1.1/32"
Expand All @@ -66,10 +101,12 @@ resource "netbox_ip_address" "test" {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_ip_address.test", "ip_address", testIP),
resource.TestCheckResourceAttr("netbox_ip_address.test", "status", "active"),
resource.TestCheckResourceAttr("netbox_ip_address.test", "object_type", "virtualization.vminterface"),
resource.TestCheckResourceAttr("netbox_ip_address.test", "tags.#", "1"),
resource.TestCheckResourceAttr("netbox_ip_address.test", "tags.0", testName),
resource.TestCheckResourceAttr("netbox_ip_address.test", "tenant_id", "0"),
resource.TestCheckResourceAttr("netbox_ip_address.test", "vrf_id", "0"),
resource.TestCheckResourceAttrPair("netbox_ip_address.test", "interface_id", "netbox_interface.test", "id"),
),
},
{
Expand All @@ -87,10 +124,12 @@ resource "netbox_ip_address" "test" {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_ip_address.test", "ip_address", testIP),
resource.TestCheckResourceAttr("netbox_ip_address.test", "status", "reserved"),
resource.TestCheckResourceAttr("netbox_ip_address.test", "object_type", "virtualization.vminterface"),
resource.TestCheckResourceAttrPair("netbox_ip_address.test", "tenant_id", "netbox_tenant.test", "id"),
resource.TestCheckResourceAttrPair("netbox_ip_address.test", "vrf_id", "netbox_vrf.test", "id"),
resource.TestCheckResourceAttr("netbox_ip_address.test", "description", fmt.Sprintf("description for %[1]s", testIP)),
resource.TestCheckResourceAttr("netbox_ip_address.test", "role", "loopback"),
resource.TestCheckResourceAttrPair("netbox_ip_address.test", "interface_id", "netbox_interface.test", "id"),
),
},
{
Expand All @@ -104,9 +143,11 @@ resource "netbox_ip_address" "test" {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_ip_address.test", "ip_address", testIP),
resource.TestCheckResourceAttr("netbox_ip_address.test", "status", "dhcp"),
resource.TestCheckResourceAttr("netbox_ip_address.test", "object_type", "virtualization.vminterface"),
resource.TestCheckResourceAttr("netbox_ip_address.test", "tenant_id", "0"),
resource.TestCheckResourceAttr("netbox_ip_address.test", "vrf_id", "0"),
resource.TestCheckResourceAttr("netbox_ip_address.test", "role", ""),
resource.TestCheckResourceAttrPair("netbox_ip_address.test", "interface_id", "netbox_interface.test", "id"),
),
},
{
Expand All @@ -130,6 +171,8 @@ resource "netbox_ip_address" "test" {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_ip_address.test", "ip_address", testIP),
resource.TestCheckResourceAttr("netbox_ip_address.test", "status", "deprecated"),
resource.TestCheckResourceAttr("netbox_ip_address.test", "object_type", "virtualization.vminterface"),
resource.TestCheckResourceAttrPair("netbox_ip_address.test", "interface_id", "netbox_interface.test", "id"),
),
},
{
Expand All @@ -144,8 +187,42 @@ resource "netbox_ip_address" "test" {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_ip_address.test", "ip_address", testIP),
resource.TestCheckResourceAttr("netbox_ip_address.test", "status", "active"),
resource.TestCheckResourceAttr("netbox_ip_address.test", "object_type", "virtualization.vminterface"),
resource.TestCheckResourceAttr("netbox_ip_address.test", "tags.#", "1"),
resource.TestCheckResourceAttr("netbox_ip_address.test", "dns_name", "mytest.example.com"),
resource.TestCheckResourceAttrPair("netbox_ip_address.test", "interface_id", "netbox_interface.test", "id"),
),
},
{
ResourceName: "netbox_ip_address.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccNetboxIPAddressDevice_basic(t *testing.T) {

testIP := "1.1.1.2/32"
testSlug := "ipaddress"
testName := testAccGetTestName(testSlug)
resource.ParallelTest(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccNetboxIPAddressFullDeviceDependencies(testName) + fmt.Sprintf(`
resource "netbox_ip_address" "test" {
ip_address = "%s"
object_type = "dcim.interface"
interface_id = netbox_device_interface.test.id
status = "active"
}`, testIP),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_ip_address.test", "ip_address", testIP),
resource.TestCheckResourceAttr("netbox_ip_address.test", "status", "active"),
resource.TestCheckResourceAttr("netbox_ip_address.test", "object_type", "dcim.interface"),
resource.TestCheckResourceAttrPair("netbox_ip_address.test", "interface_id", "netbox_device_interface.test", "id"),
),
},
{
Expand Down