Skip to content

Commit

Permalink
Add device_id and device_name and tests for virtual_machine (#477)
Browse files Browse the repository at this point in the history
* Add device_id and device_name and tests

* Restore removed var
  • Loading branch information
zeddD1abl0 authored Sep 20, 2023
1 parent 8dad61b commit 29f856e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/data-sources/virtual_machines.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ Read-Only:
- `comments` (String)
- `config_context` (String)
- `custom_fields` (Map of String)
- `device_id` (Number)
- `device_name` (String)
- `description` (String)
- `disk_size_gb` (Number)
- `local_context_data` (String)
Expand Down
18 changes: 18 additions & 0 deletions netbox/data_source_netbox_virtual_machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ func dataSourceNetboxVirtualMachine() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"device_id": {
Type: schema.TypeInt,
Computed: true,
},
"device_name": {
Type: schema.TypeString,
Computed: true,
},
"disk_size_gb": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -157,6 +165,12 @@ func dataSourceNetboxVirtualMachineRead(d *schema.ResourceData, m interface{}) e
case "cluster_group":
var clusterGroupString = v.(string)
params.ClusterGroup = &clusterGroupString
case "device_id":
var deviceIDstring = v.(string)
params.Name = &deviceIDstring
case "device":
var deviceString = v.(string)
params.Name = &deviceString
case "name":
var nameString = v.(string)
params.Name = &nameString
Expand Down Expand Up @@ -217,6 +231,10 @@ func dataSourceNetboxVirtualMachineRead(d *schema.ResourceData, m interface{}) e
if v.Description != "" {
mapping["description"] = v.Description
}
if v.Device != nil {
mapping["device_id"] = v.Device.ID
mapping["device_name"] = v.Device.Name
}
if v.ConfigContext != nil {
if configContext, err := json.Marshal(v.ConfigContext); err == nil {
mapping["config_context"] = string(configContext)
Expand Down
3 changes: 3 additions & 0 deletions netbox/data_source_netbox_virtual_machines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func TestAccNetboxVirtualMachinesDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrPair("data.netbox_virtual_machines.test", "vms.0.tenant_id", "netbox_tenant.test", "id"),
resource.TestCheckResourceAttrPair("data.netbox_virtual_machines.test", "vms.0.role_id", "netbox_device_role.test", "id"),
resource.TestCheckResourceAttrPair("data.netbox_virtual_machines.test", "vms.0.platform_id", "netbox_platform.test", "id"),
resource.TestCheckResourceAttrPair("data.netbox_virtual_machines.test", "vms.0.device_id", "netbox_device.test", "id"),
resource.TestCheckResourceAttrPair("data.netbox_virtual_machines.test", "vms.0.device_name", "netbox_device.test", "name"),
),
},
{
Expand Down Expand Up @@ -89,6 +91,7 @@ resource "netbox_virtual_machine" "test0" {
name = "%[1]s_0"
cluster_id = netbox_cluster.test.id
site_id = netbox_site.test.id
device_id = netbox_device.test.id
comments = "thisisacomment"
memory_mb = 1024
disk_size_gb = 256
Expand Down

0 comments on commit 29f856e

Please sign in to comment.