Skip to content

Commit

Permalink
Merge pull request #33 from jake2184/datasource_vm_search_limit
Browse files Browse the repository at this point in the history
Add 'limit' to data_source_netbox_virtual_machines
  • Loading branch information
fbreckle authored Feb 15, 2021
2 parents 2a357f9 + f2feeb5 commit 843f822
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
9 changes: 9 additions & 0 deletions netbox/data_source_netbox_virtual_machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func dataSourceNetboxVirtualMachine() *schema.Resource {
Optional: true,
ValidateFunc: validation.StringIsValidRegExp,
},
"limit": {
Type: schema.TypeInt,
Optional: true,
},
"vms": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -164,6 +168,11 @@ func dataSourceNetboxVirtualMachineRead(d *schema.ResourceData, m interface{}) e
}
}

if limit, ok := d.GetOk("limit"); ok {
limitInt := int64(limit.(int))
params.Limit = &limitInt
}

res, err := api.Virtualization.VirtualizationVirtualMachinesList(params, nil)
if err != nil {
return err
Expand Down
16 changes: 16 additions & 0 deletions netbox/data_source_netbox_virtual_machines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ func TestAccNetboxVirtualMachinesDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrPair("data.netbox_virtual_machines.test", "vms.1.name", "netbox_virtual_machine.test3", "name"),
),
},
{
Config: dependencies + testAccNetboxVirtualMachineDataSourceLimit,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.netbox_virtual_machines.test", "vms.#", "1"),
resource.TestCheckResourceAttrPair("data.netbox_virtual_machines.test", "vms.0.cluster_id", "netbox_cluster.test", "id"),
),
},
},
})
}
Expand Down Expand Up @@ -107,3 +114,12 @@ const testAccNetboxVirtualMachineDataSourceNameRegex = `
data "netbox_virtual_machines" "test" {
name_regex = "test.*_regex"
}`

const testAccNetboxVirtualMachineDataSourceLimit = `
data "netbox_virtual_machines" "test" {
limit = 1
filter {
name = "cluster_id"
value = netbox_cluster.test.id
}
}`
2 changes: 1 addition & 1 deletion netbox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Provider() *schema.Provider {
},
DataSourcesMap: map[string]*schema.Resource{
"netbox_cluster": dataSourceNetboxCluster(),
"netbox_cluster_group": dataSourceNetboxClusterGroup(),
"netbox_cluster_group": dataSourceNetboxClusterGroup(),
"netbox_tenant": dataSourceNetboxTenant(),
"netbox_vrf": dataSourceNetboxVrf(),
"netbox_platform": dataSourceNetboxPlatform(),
Expand Down

0 comments on commit 843f822

Please sign in to comment.