Skip to content

Commit

Permalink
feat(cluster): support searching by cluster group
Browse files Browse the repository at this point in the history
Make it possible to filter clusters by cluster group.
  • Loading branch information
Ikke authored and fbreckle committed Jan 2, 2024
1 parent 2d1bfa8 commit 049a30d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/data-sources/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ data "netbox_cluster" "vmw_cluster_01" {

### Optional

- `cluster_group_id` (Number)
- `id` (String) At least one of `name`, `site_id` or `id` must be given.
- `name` (String) At least one of `name`, `site_id` or `id` must be given.
- `site_id` (Number) At least one of `name`, `site_id` or `id` must be given.

### Read-Only

- `cluster_group_id` (Number)
- `cluster_id` (Number)
- `cluster_type_id` (Number)
- `comments` (String)
Expand Down
7 changes: 7 additions & 0 deletions netbox/data_source_netbox_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package netbox

import (
"errors"
"fmt"
"strconv"

"github.com/fbreckle/go-netbox/netbox/client"
Expand Down Expand Up @@ -50,6 +51,7 @@ func dataSourceNetboxCluster() *schema.Resource {
"cluster_group_id": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
},
"custom_fields": {
Type: schema.TypeMap,
Expand Down Expand Up @@ -77,6 +79,11 @@ func dataSourceNetboxClusterRead(d *schema.ResourceData, m interface{}) error {
params.SetID(&id)
}

if clustergroupID, ok := d.Get("cluster_group_id").(int); ok && clustergroupID != 0 {
clustGroupStr := fmt.Sprintf("%d", clustergroupID)
params.GroupID = &clustGroupStr
}

limit := int64(2) // Limit of 2 is enough
params.Limit = &limit

Expand Down
6 changes: 6 additions & 0 deletions netbox/data_source_netbox_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ data "netbox_cluster" "by_site_id" {
data "netbox_cluster" "by_id" {
id = netbox_cluster.test.id
}
data "netbox_cluster" "by_site_id_and_group_id" {
site_id = netbox_cluster.test.site_id
cluster_group_id = netbox_cluster.test.cluster_group_id
}
`, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair("data.netbox_cluster.by_name", "id", "netbox_cluster.test", "id"),
Expand All @@ -66,6 +71,7 @@ data "netbox_cluster" "by_id" {
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),
resource.TestCheckResourceAttrPair("data.netbox_cluster.by_site_id_and_group_id", "id", "netbox_cluster.test", "id"),
),
},
},
Expand Down

0 comments on commit 049a30d

Please sign in to comment.