Skip to content

Commit

Permalink
Support Cluster tenant (e-breuninger#275)
Browse files Browse the repository at this point in the history
* Support Cluster tenant

* docs: Update index

Co-authored-by: arjenvri <arjenvri>
Co-authored-by: Fabian Breckle <fabian.breckle@breuninger.de>
  • Loading branch information
2 people authored and twink0r committed Sep 15, 2023
1 parent 9675591 commit 726f144
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/resources/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ resource "netbox_cluster" "vmw_cluster_01" {
- `cluster_group_id` (Number)
- `site_id` (Number)
- `tags` (Set of String)
- `tenant_id` (Number)

### Read-Only

Expand Down
20 changes: 20 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.TypeInt,
Optional: true,
},
"tenant_id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
tagsKey: tagsSchema,
},
Importer: &schema.ResourceImporter{
Expand Down Expand Up @@ -68,6 +72,11 @@ func resourceNetboxClusterCreate(d *schema.ResourceData, m interface{}) error {
data.Site = &siteID
}

if tenantIDValue, ok := d.GetOk("tenant_id"); ok {
tenantID := int64(tenantIDValue.(int))
data.Tenant = &tenantID
}

tags, _ := getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))
data.Tags = tags

Expand Down Expand Up @@ -115,6 +124,12 @@ func resourceNetboxClusterRead(d *schema.ResourceData, m interface{}) error {
d.Set("site_id", nil)
}

if res.GetPayload().Tenant != nil {
d.Set("tenant_id", res.GetPayload().Tenant.ID)
} else {
d.Set("tenant_id", nil)
}

d.Set(tagsKey, getTagListFromNestedTagList(res.GetPayload().Tags))
return nil
}
Expand All @@ -141,6 +156,11 @@ func resourceNetboxClusterUpdate(d *schema.ResourceData, m interface{}) error {
data.Site = &siteID
}

if tenantIDValue, ok := d.GetOk("tenant_id"); ok {
tenantID := int64(tenantIDValue.(int))
data.Tenant = &tenantID
}

tags, _ := getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))
data.Tags = tags

Expand Down
7 changes: 6 additions & 1 deletion netbox/resource_netbox_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ resource "netbox_tag" "test_updatetag" {
name = "%[1]s-a"
}
resource "netbox_cluster_type" "test" {
name = "%[1]s"
}
Expand All @@ -73,6 +72,10 @@ resource "netbox_cluster_group" "test" {
name = "%[1]s"
}
resource "netbox_tenant" "test" {
name = "%[1]s"
}
resource "netbox_site" "test" {
name = "%[1]s"
status = "active"
Expand All @@ -82,12 +85,14 @@ resource "netbox_cluster" "test" {
name = "%[1]s"
cluster_type_id = netbox_cluster_type.test.id
cluster_group_id = netbox_cluster_group.test.id
tenant_id = netbox_tenant.test.id
tags = [netbox_tag.test.name, netbox_tag.test_updatetag.name]
}`, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_cluster.test", "name", testName),
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.TestCheckResourceAttrPair("netbox_cluster.test", "tenant_id", "netbox_tenant.test", "id"),
resource.TestCheckResourceAttr("netbox_cluster.test", "tags.#", "2"),
resource.TestCheckResourceAttr("netbox_cluster.test", "tags.0", testName),
resource.TestCheckResourceAttr("netbox_cluster.test", "tags.1", fmt.Sprintf("%[1]s-a", testName)),
Expand Down

0 comments on commit 726f144

Please sign in to comment.