From 049a30d91c7252909490e210ded7ce3fcd0135af Mon Sep 17 00:00:00 2001 From: Kevin Daudt Date: Thu, 21 Dec 2023 17:39:53 +0100 Subject: [PATCH] feat(cluster): support searching by cluster group Make it possible to filter clusters by cluster group. --- docs/data-sources/cluster.md | 2 +- netbox/data_source_netbox_cluster.go | 7 +++++++ netbox/data_source_netbox_cluster_test.go | 6 ++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/docs/data-sources/cluster.md b/docs/data-sources/cluster.md index 2bbecbc6..06c2737e 100644 --- a/docs/data-sources/cluster.md +++ b/docs/data-sources/cluster.md @@ -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) diff --git a/netbox/data_source_netbox_cluster.go b/netbox/data_source_netbox_cluster.go index df7b2577..1752787c 100644 --- a/netbox/data_source_netbox_cluster.go +++ b/netbox/data_source_netbox_cluster.go @@ -2,6 +2,7 @@ package netbox import ( "errors" + "fmt" "strconv" "github.com/fbreckle/go-netbox/netbox/client" @@ -50,6 +51,7 @@ func dataSourceNetboxCluster() *schema.Resource { "cluster_group_id": { Type: schema.TypeInt, Computed: true, + Optional: true, }, "custom_fields": { Type: schema.TypeMap, @@ -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 diff --git a/netbox/data_source_netbox_cluster_test.go b/netbox/data_source_netbox_cluster_test.go index 753ab52b..42cb5bec 100644 --- a/netbox/data_source_netbox_cluster_test.go +++ b/netbox/data_source_netbox_cluster_test.go @@ -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"), @@ -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"), ), }, },