Skip to content

Commit cc618bb

Browse files
committed
feat: move clusters from site to scope model
1 parent 2c4292d commit cc618bb

3 files changed

Lines changed: 69 additions & 32 deletions

File tree

netbox/data_source_netbox_cluster.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ func dataSourceNetboxCluster() *schema.Resource {
5252
Computed: true,
5353
Optional: true,
5454
},
55+
"scope_type": {
56+
Type: schema.TypeString,
57+
Computed: true,
58+
},
59+
"scope_id": {
60+
Type: schema.TypeInt,
61+
Computed: true,
62+
},
5563
"custom_fields": {
5664
Type: schema.TypeMap,
5765
Computed: true,
@@ -110,11 +118,19 @@ func dataSourceNetboxClusterRead(d *schema.ResourceData, m interface{}) error {
110118
}
111119
d.Set("comments", result.Comments)
112120
d.Set("description", result.Description)
113-
if result.Site != nil {
114-
d.Set("site_id", result.Site.ID)
121+
122+
if result.ScopeType != nil {
123+
d.Set("scope_type", result.ScopeType)
115124
} else {
116-
d.Set("site_id", nil)
125+
d.Set("scope_type", nil)
117126
}
127+
128+
if result.ScopeID != nil {
129+
d.Set("scope_id", result.ScopeID)
130+
} else {
131+
d.Set("scope_id", nil)
132+
}
133+
118134
if result.CustomFields != nil {
119135
d.Set("custom_fields", result.CustomFields)
120136
}

netbox/data_source_netbox_devices_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ resource "netbox_cluster_type" "test" {
121121
resource "netbox_cluster" "test" {
122122
name = "%[1]s"
123123
cluster_type_id = netbox_cluster_type.test.id
124-
site_id = netbox_site.test.id
124+
scope_type = "dcim.site"
125+
scope_id = netbox_site.test.id
125126
}
126127
127128
resource "netbox_location" "test" {

netbox/resource_netbox_cluster.go

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import (
66
"github.com/fbreckle/go-netbox/netbox/client/virtualization"
77
"github.com/fbreckle/go-netbox/netbox/models"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
910
)
1011

12+
var resourceNetboxClusterScopeTypeOptions = []string{"dcim.location", "dcim.site", "dcim.sitegroup", "dcim.region"}
13+
1114
func resourceNetboxCluster() *schema.Resource {
1215
return &schema.Resource{
1316
Create: resourceNetboxClusterCreate,
@@ -16,7 +19,7 @@ func resourceNetboxCluster() *schema.Resource {
1619
Delete: resourceNetboxClusterDelete,
1720

1821
Description: `:meta:subcategory:Virtualization:From the [official documentation](https://docs.netbox.dev/en/stable/features/virtualization/#clusters):
19-
22+
TODO
2023
> A cluster is a logical grouping of physical resources within which virtual machines run. A cluster must be assigned a type (technological classification), and may optionally be assigned to a cluster group, site, and/or tenant. Each cluster must have a unique name within its assigned group and/or site, if any.
2124
>
2225
> Physical devices may be associated with clusters as hosts. This allows users to track on which host(s) a particular virtual machine may reside. However, NetBox does not support pinning a specific VM within a cluster to a particular host device.`,
@@ -42,14 +45,21 @@ func resourceNetboxCluster() *schema.Resource {
4245
Type: schema.TypeString,
4346
Optional: true,
4447
},
45-
"site_id": {
46-
Type: schema.TypeInt,
47-
Optional: true,
48-
},
4948
"tenant_id": {
5049
Type: schema.TypeInt,
5150
Optional: true,
5251
},
52+
"scope_type": {
53+
Type: schema.TypeString,
54+
Optional: true,
55+
ValidateFunc: validation.StringInSlice(resourceNetboxClusterScopeTypeOptions, false),
56+
Description: buildValidValueDescription(resourceNetboxClusterScopeTypeOptions),
57+
},
58+
"scope_id": {
59+
Type: schema.TypeInt,
60+
Optional: true,
61+
RequiredWith: []string{"scope_type"},
62+
},
5363
tagsKey: tagsSchema,
5464
},
5565
Importer: &schema.ResourceImporter{
@@ -69,6 +79,14 @@ func resourceNetboxClusterCreate(d *schema.ResourceData, m interface{}) error {
6979
clusterTypeID := int64(d.Get("cluster_type_id").(int))
7080
data.Type = &clusterTypeID
7181

82+
if scopeType, ok := d.GetOk("scope_type"); ok {
83+
data.ScopeType = strToPtr(scopeType.(string))
84+
}
85+
86+
if scopeID, ok := d.GetOk("scope_id"); ok {
87+
data.ScopeID = int64ToPtr(int64(scopeID.(int)))
88+
}
89+
7290
if clusterGroupIDValue, ok := d.GetOk("cluster_group_id"); ok {
7391
clusterGroupID := int64(clusterGroupIDValue.(int))
7492
data.Group = &clusterGroupID
@@ -77,11 +95,6 @@ func resourceNetboxClusterCreate(d *schema.ResourceData, m interface{}) error {
7795
data.Comments = getOptionalStr(d, "comments", false)
7896
data.Description = getOptionalStr(d, "description", false)
7997

80-
if siteIDValue, ok := d.GetOk("site_id"); ok {
81-
siteID := int64(siteIDValue.(int))
82-
data.Site = &siteID
83-
}
84-
8598
if tenantIDValue, ok := d.GetOk("tenant_id"); ok {
8699
tenantID := int64(tenantIDValue.(int))
87100
data.Tenant = &tenantID
@@ -121,28 +134,32 @@ func resourceNetboxClusterRead(d *schema.ResourceData, m interface{}) error {
121134
return err
122135
}
123136

124-
d.Set("name", res.GetPayload().Name)
125-
d.Set("cluster_type_id", res.GetPayload().Type.ID)
137+
cluster := res.GetPayload()
126138

127-
if res.GetPayload().Group != nil {
128-
d.Set("cluster_group_id", res.GetPayload().Group.ID)
139+
d.Set("name", cluster.Name)
140+
d.Set("cluster_type_id", cluster.Type.ID)
141+
142+
if cluster.Group != nil {
143+
d.Set("cluster_group_id", cluster.Group.ID)
129144
} else {
130145
d.Set("cluster_group_id", nil)
131146
}
132147

133-
d.Set("comments", res.GetPayload().Comments)
134-
d.Set("description", res.GetPayload().Description)
148+
d.Set("comments", cluster.Comments)
149+
d.Set("description", cluster.Description)
135150

136-
if res.GetPayload().Site != nil {
137-
d.Set("site_id", res.GetPayload().Site.ID)
151+
if cluster.Tenant != nil {
152+
d.Set("tenant_id", cluster.Tenant.ID)
138153
} else {
139-
d.Set("site_id", nil)
154+
d.Set("tenant_id", nil)
140155
}
141156

142-
if res.GetPayload().Tenant != nil {
143-
d.Set("tenant_id", res.GetPayload().Tenant.ID)
144-
} else {
145-
d.Set("tenant_id", nil)
157+
if cluster.ScopeType != nil {
158+
d.Set("scope_type", cluster.ScopeType)
159+
}
160+
161+
if cluster.ScopeID != nil {
162+
d.Set("scope_id", cluster.ScopeID)
146163
}
147164

148165
api.readTags(d, res.GetPayload().Tags)
@@ -169,16 +186,19 @@ func resourceNetboxClusterUpdate(d *schema.ResourceData, m interface{}) error {
169186
data.Comments = getOptionalStr(d, "comments", true)
170187
data.Description = getOptionalStr(d, "description", true)
171188

172-
if siteIDValue, ok := d.GetOk("site_id"); ok {
173-
siteID := int64(siteIDValue.(int))
174-
data.Site = &siteID
175-
}
176-
177189
if tenantIDValue, ok := d.GetOk("tenant_id"); ok {
178190
tenantID := int64(tenantIDValue.(int))
179191
data.Tenant = &tenantID
180192
}
181193

194+
if scopeType, ok := d.GetOk("scope_type"); ok {
195+
data.ScopeType = strToPtr(scopeType.(string))
196+
}
197+
198+
if scopeID, ok := d.GetOk("scope_id"); ok {
199+
data.ScopeID = int64ToPtr(int64(scopeID.(int)))
200+
}
201+
182202
tags, _ := getNestedTagListFromResourceDataSet(api, d.Get(tagsAllKey))
183203
data.Tags = tags
184204

0 commit comments

Comments
 (0)