Skip to content

Commit

Permalink
Add description field to VMs, Devices, Clusters Resource/Data
Browse files Browse the repository at this point in the history
We were missing the ability to add a descriptions to devices, VMs, and
clusters. There are also corresponding data sources that do not have the
description field.

These fields were not available prior to netbox 3.4.3.

This adds the description field, following the example of the comment
field. The code was also slightly modified to be simpler to understand.
  • Loading branch information
tagur87 committed Jul 26, 2023
1 parent a1b7c2b commit a3a0888
Show file tree
Hide file tree
Showing 18 changed files with 93 additions and 41 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ data "netbox_cluster" "vmw_cluster_01" {
- `cluster_id` (Number)
- `cluster_type_id` (Number)
- `comments` (String)
- `description` (String)
- `id` (String) The ID of this resource.
- `tags` (Set of String)

Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/devices.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Read-Only:
- `cluster_id` (Number)
- `comments` (String)
- `custom_fields` (Map of String)
- `description` (String)
- `device_id` (Number)
- `device_type_id` (Number)
- `location_id` (Number)
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/virtual_machines.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Read-Only:
- `comments` (String)
- `config_context` (String)
- `custom_fields` (Map of String)
- `description` (String)
- `disk_size_gb` (Number)
- `local_context_data` (String)
- `memory_mb` (Number)
Expand Down
1 change: 1 addition & 0 deletions docs/resources/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ resource "netbox_cluster" "vmw_cluster_01" {

- `cluster_group_id` (Number)
- `comments` (String)
- `description` (String)
- `site_id` (Number)
- `tags` (Set of String)
- `tenant_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 @@ -57,6 +57,7 @@ resource "netbox_device" "test" {
- `cluster_id` (Number)
- `comments` (String)
- `custom_fields` (Map of String)
- `description` (String)
- `location_id` (Number)
- `platform_id` (Number)
- `rack_face` (String) Valid values are `front` and `rear`. Required when `rack_position` is set.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/virtual_machine.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ resource "netbox_virtual_machine" "full_vm" {
- `cluster_id` (Number) At least one of `site_id` or `cluster_id` must be given.
- `comments` (String)
- `custom_fields` (Map of String)
- `description` (String)
- `device_id` (Number)
- `disk_size_gb` (Number)
- `local_context_data` (String) This is best managed through the use of `jsonencode` and a map of settings.
Expand Down
5 changes: 5 additions & 0 deletions netbox/data_source_netbox_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func dataSourceNetboxCluster() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"site_id": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -85,6 +89,7 @@ func dataSourceNetboxClusterRead(d *schema.ResourceData, m interface{}) error {
d.Set("cluster_group_id", nil)
}
d.Set("comments", result.Comments)
d.Set("description", result.Description)
if result.Site != nil {
d.Set("site_id", result.Site.ID)
} else {
Expand Down
2 changes: 2 additions & 0 deletions netbox/data_source_netbox_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ resource "netbox_cluster" "test" {
cluster_group_id = netbox_cluster_group.test.id
site_id = netbox_site.test.id
comments = "%[1]scomments"
description = "%[1]sdescription"
tags = [netbox_tag.test.name]
}
Expand All @@ -56,6 +57,7 @@ data "netbox_cluster" "by_site_id" {
resource.TestCheckResourceAttrPair("data.netbox_cluster.by_name", "cluster_type_id", "netbox_cluster_type.test", "id"),
resource.TestCheckResourceAttrPair("data.netbox_cluster.by_name", "cluster_group_id", "netbox_cluster_group.test", "id"),
resource.TestCheckResourceAttr("data.netbox_cluster.by_name", "comments", testName+"comments"),
resource.TestCheckResourceAttr("data.netbox_cluster.by_name", "description", testName+"description"),
resource.TestCheckResourceAttrPair("data.netbox_cluster.by_name", "site_id", "netbox_site.test", "id"),
resource.TestCheckResourceAttr("data.netbox_cluster.by_name", "tags.#", "1"),
resource.TestCheckResourceAttr("data.netbox_cluster.by_name", "tags.0", testName),
Expand Down
7 changes: 7 additions & 0 deletions netbox/data_source_netbox_devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func dataSourceNetboxDevices() *schema.Resource {
Type: schema.TypeMap,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"device_id": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -205,6 +209,9 @@ func dataSourceNetboxDevicesRead(d *schema.ResourceData, m interface{}) error {
if device.Comments != "" {
mapping["comments"] = device.Comments
}
if device.Description != "" {
mapping["description"] = device.Description
}
mapping["device_id"] = device.ID
if device.DeviceType != nil {
mapping["device_type_id"] = device.DeviceType.ID
Expand Down
6 changes: 5 additions & 1 deletion netbox/data_source_netbox_devices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestAccNetboxDevicesDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttr("data.netbox_devices.test", "devices.#", "1"),
resource.TestCheckResourceAttr("data.netbox_devices.test", "devices.0.name", testName+"_0"),
resource.TestCheckResourceAttr("data.netbox_devices.test", "devices.0.comments", "this is also a comment"),
resource.TestCheckResourceAttr("data.netbox_devices.test", "devices.0.description", "this is also a description"),
resource.TestCheckResourceAttrPair("data.netbox_devices.test", "devices.0.tenant_id", "netbox_tenant.test", "id"),
resource.TestCheckResourceAttrPair("data.netbox_devices.test", "devices.0.role_id", "netbox_device_role.test", "id"),
resource.TestCheckResourceAttrPair("data.netbox_devices.test", "devices.0.device_type_id", "netbox_device_type.test", "id"),
Expand Down Expand Up @@ -83,6 +84,7 @@ func testAccNetboxDeviceDataSourceDependencies(testName string) string {
resource "netbox_device" "test0" {
name = "%[1]s_0"
comments = "this is also a comment"
description = "this is also a description"
tenant_id = netbox_tenant.test.id
role_id = netbox_device_role.test.id
device_type_id = netbox_device_type.test.id
Expand Down Expand Up @@ -204,6 +206,7 @@ resource "netbox_custom_field" "test" {
resource "netbox_device" "test" {
name = "%[2]s"
comments = "thisisacomment"
description = "thisisadescription"
tenant_id = netbox_tenant.test.id
platform_id = netbox_platform.test.id
role_id = netbox_device_role.test.id
Expand All @@ -214,13 +217,14 @@ resource "netbox_device" "test" {
location_id = netbox_location.test.id
status = "staged"
serial = "ABCDEF"
custom_fields = {"${netbox_custom_field.test.name}" = "81"}
custom_fields = {"${netbox_custom_field.test.name}" = "81"}
}
`, testField, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.netbox_devices.test", "devices.#", "1"),
resource.TestCheckResourceAttr("data.netbox_devices.test", "devices.0.name", testName),
resource.TestCheckResourceAttr("data.netbox_devices.test", "devices.0.comments", "thisisacomment"),
resource.TestCheckResourceAttr("data.netbox_devices.test", "devices.0.description", "thisisadescription"),
resource.TestCheckResourceAttrPair("data.netbox_devices.test", "devices.0.tenant_id", "netbox_tenant.test", "id"),
resource.TestCheckResourceAttrPair("data.netbox_devices.test", "devices.0.role_id", "netbox_device_role.test", "id"),
resource.TestCheckResourceAttrPair("data.netbox_devices.test", "devices.0.device_type_id", "netbox_device_type.test", "id"),
Expand Down
7 changes: 7 additions & 0 deletions netbox/data_source_netbox_virtual_machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ func dataSourceNetboxVirtualMachine() *schema.Resource {
Type: schema.TypeMap,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"disk_size_gb": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -205,6 +209,9 @@ func dataSourceNetboxVirtualMachineRead(d *schema.ResourceData, m interface{}) e
if v.Comments != "" {
mapping["comments"] = v.Comments
}
if v.Description != "" {
mapping["description"] = v.Description
}
if v.ConfigContext != nil {
if configContext, err := json.Marshal(v.ConfigContext); err == nil {
mapping["config_context"] = string(configContext)
Expand Down
7 changes: 7 additions & 0 deletions netbox/resource_netbox_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func resourceNetboxCluster() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"site_id": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -72,6 +76,7 @@ func resourceNetboxClusterCreate(d *schema.ResourceData, m interface{}) error {
}

data.Comments = getOptionalStr(d, "comments", false)
data.Description = getOptionalStr(d, "description", false)

if siteIDValue, ok := d.GetOk("site_id"); ok {
siteID := int64(siteIDValue.(int))
Expand Down Expand Up @@ -127,6 +132,7 @@ func resourceNetboxClusterRead(d *schema.ResourceData, m interface{}) error {
}

d.Set("comments", res.GetPayload().Comments)
d.Set("description", res.GetPayload().Description)

if res.GetPayload().Site != nil {
d.Set("site_id", res.GetPayload().Site.ID)
Expand Down Expand Up @@ -162,6 +168,7 @@ func resourceNetboxClusterUpdate(d *schema.ResourceData, m interface{}) error {
}

data.Comments = getOptionalStr(d, "comments", true)
data.Description = getOptionalStr(d, "description", true)

if siteIDValue, ok := d.GetOk("site_id"); ok {
siteID := int64(siteIDValue.(int))
Expand Down
2 changes: 2 additions & 0 deletions netbox/resource_netbox_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ resource "netbox_cluster" "test" {
cluster_type_id = netbox_cluster_type.test.id
cluster_group_id = netbox_cluster_group.test.id
comments = "%[1]scomments"
description = "%[1]sdescription"
site_id = netbox_site.test.id
tags = [netbox_tag.test.name]
}`, testName),
Expand All @@ -50,6 +51,7 @@ resource "netbox_cluster" "test" {
resource.TestCheckResourceAttrPair("netbox_cluster.test", "cluster_type_id", "netbox_cluster_type.test", "id"),
resource.TestCheckResourceAttrPair("netbox_cluster.test", "cluster_group_id", "netbox_cluster_group.test", "id"),
resource.TestCheckResourceAttr("netbox_cluster.test", "comments", testName+"comments"),
resource.TestCheckResourceAttr("netbox_cluster.test", "description", testName+"description"),
resource.TestCheckResourceAttrPair("netbox_cluster.test", "site_id", "netbox_site.test", "id"),
resource.TestCheckResourceAttr("netbox_cluster.test", "tags.#", "1"),
resource.TestCheckResourceAttr("netbox_cluster.test", "tags.0", testName),
Expand Down
44 changes: 22 additions & 22 deletions netbox/resource_netbox_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func resourceNetboxDevice() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
tagsKey: tagsSchema,
"primary_ipv4": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -121,14 +125,13 @@ func resourceNetboxDeviceCreate(ctx context.Context, d *schema.ResourceData, m i
data.DeviceType = &typeID
}

comments := d.Get("comments").(string)
data.Comments = comments
data.Comments = d.Get("comments").(string)

serial := d.Get("serial").(string)
data.Serial = serial
data.Description = d.Get("description").(string)

status := d.Get("status").(string)
data.Status = status
data.Serial = d.Get("serial").(string)

data.Status = d.Get("status").(string)

tenantIDValue, ok := d.GetOk("tenant_id")
if ok {
Expand Down Expand Up @@ -280,6 +283,8 @@ func resourceNetboxDeviceRead(ctx context.Context, d *schema.ResourceData, m int

d.Set("comments", device.Comments)

d.Set("description", device.Description)

d.Set("serial", device.Serial)

d.Set("status", device.Status.Value)
Expand Down Expand Up @@ -356,15 +361,6 @@ func resourceNetboxDeviceUpdate(ctx context.Context, d *schema.ResourceData, m i
data.Site = &siteID
}

commentsValue, ok := d.GetOk("comments")
if ok {
comments := commentsValue.(string)
data.Comments = comments
} else {
comments := " "
data.Comments = comments
}

primaryIP4Value, ok := d.GetOk("primary_ipv4")
if ok {
primaryIP4 := int64(primaryIP4Value.(int))
Expand All @@ -390,15 +386,19 @@ func resourceNetboxDeviceUpdate(ctx context.Context, d *schema.ResourceData, m i

if d.HasChanges("comments") {
// check if comment is set
commentsValue, ok := d.GetOk("comments")
comments := ""
if !ok {
// Setting an space string deletes the comment
comments = " "
if commentsValue, ok := d.GetOk("comments"); ok {
data.Comments = commentsValue.(string)
} else {
data.Comments = " "
}
}
if d.HasChanges("description") {
// check if description is set
if descriptionValue, ok := d.GetOk("description"); ok {
data.Description = descriptionValue.(string)
} else {
comments = commentsValue.(string)
data.Description = " "
}
data.Comments = comments
}

if d.HasChanges("serial") {
Expand Down
6 changes: 6 additions & 0 deletions netbox/resource_netbox_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func TestAccNetboxDevice_basic(t *testing.T) {
resource "netbox_device" "test" {
name = "%[1]s"
comments = "thisisacomment"
description = "thisisadescription"
tenant_id = netbox_tenant.test.id
platform_id = netbox_platform.test.id
role_id = netbox_device_role.test.id
Expand All @@ -118,6 +119,7 @@ resource "netbox_device" "test" {
resource.TestCheckResourceAttrPair("netbox_device.test", "cluster_id", "netbox_cluster.test", "id"),
resource.TestCheckResourceAttrPair("netbox_device.test", "rack_id", "netbox_rack.test", "id"),
resource.TestCheckResourceAttr("netbox_device.test", "comments", "thisisacomment"),
resource.TestCheckResourceAttr("netbox_device.test", "description", "thisisadescription"),
resource.TestCheckResourceAttr("netbox_device.test", "status", "staged"),
resource.TestCheckResourceAttr("netbox_device.test", "serial", "ABCDEF"),
resource.TestCheckResourceAttr("netbox_device.test", "tags.#", "1"),
Expand All @@ -131,6 +133,7 @@ resource "netbox_device" "test" {
resource "netbox_device" "test" {
name = "%[1]s"
comments = "thisisacomment"
description = "thisisadescription"
tenant_id = netbox_tenant.test.id
platform_id = netbox_platform.test.id
role_id = netbox_device_role.test.id
Expand All @@ -153,6 +156,7 @@ resource "netbox_device" "test" {
resource.TestCheckResourceAttrPair("netbox_device.test", "cluster_id", "netbox_cluster.test", "id"),
resource.TestCheckResourceAttrPair("netbox_device.test", "rack_id", "netbox_rack.test", "id"),
resource.TestCheckResourceAttr("netbox_device.test", "comments", "thisisacomment"),
resource.TestCheckResourceAttr("netbox_device.test", "description", "thisisadescription"),
resource.TestCheckResourceAttr("netbox_device.test", "status", "staged"),
resource.TestCheckResourceAttr("netbox_device.test", "serial", "ABCDEF"),
resource.TestCheckResourceAttr("netbox_device.test", "tags.#", "1"),
Expand All @@ -166,6 +170,7 @@ resource "netbox_device" "test" {
resource "netbox_device" "test" {
name = "%[1]s"
comments = "thisisacomment"
description = "thisisadescription"
tenant_id = netbox_tenant.test.id
platform_id = netbox_platform.test.id
role_id = netbox_device_role.test.id
Expand All @@ -186,6 +191,7 @@ resource "netbox_device" "test" {
resource.TestCheckResourceAttrPair("netbox_device.test", "site_id", "netbox_site.test", "id"),
resource.TestCheckResourceAttrPair("netbox_device.test", "cluster_id", "netbox_cluster.test", "id"),
resource.TestCheckResourceAttr("netbox_device.test", "comments", "thisisacomment"),
resource.TestCheckResourceAttr("netbox_device.test", "description", "thisisadescription"),
resource.TestCheckResourceAttr("netbox_device.test", "status", "staged"),
resource.TestCheckResourceAttr("netbox_device.test", "serial", "ABCDEF"),
resource.TestCheckResourceAttr("netbox_device.test", "tags.#", "1"),
Expand Down
1 change: 1 addition & 0 deletions netbox/resource_netbox_primary_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func resourceNetboxPrimaryIPUpdate(d *schema.ResourceData, m interface{}) error
tag.Display = ""
}
data.Comments = vm.Comments
data.Description = vm.Description
data.Memory = vm.Memory
data.Vcpus = vm.Vcpus
data.Disk = vm.Disk
Expand Down
Loading

0 comments on commit a3a0888

Please sign in to comment.