Skip to content

Commit bfc0a1a

Browse files
committed
feat: move prefix resource to scope model
1 parent 8e17e2c commit bfc0a1a

5 files changed

Lines changed: 101 additions & 58 deletions

netbox/data_source_netbox_prefix.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,67 +24,70 @@ func dataSourceNetboxPrefix() *schema.Resource {
2424
Deprecated: "The `cidr` parameter is deprecated in favor of the canonical `prefix` attribute.",
2525
ConflictsWith: []string{"prefix"},
2626
ValidateFunc: validation.IsCIDR,
27-
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag", "status"},
27+
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "role_id", "cidr", "tag", "status"},
2828
},
2929
customFieldsKey: customFieldsSchema,
3030
"description": {
3131
Type: schema.TypeString,
3232
Optional: true,
3333
Computed: true,
34-
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag", "status"},
34+
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "role_id", "cidr", "tag", "status"},
3535
Description: "Description to include in the data source filter.",
3636
},
3737
"family": {
3838
Type: schema.TypeInt,
3939
Optional: true,
4040
Computed: true,
41-
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag", "status"},
41+
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "role_id", "cidr", "tag", "status"},
4242
ValidateFunc: validation.IntInSlice([]int{4, 6}),
4343
Description: "The IP family of the prefix. One of 4 or 6",
4444
},
4545
"role_id": {
4646
Type: schema.TypeInt,
4747
Optional: true,
4848
Computed: true,
49-
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag", "status"},
49+
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "role_id", "cidr", "tag", "status"},
5050
},
5151
"prefix": {
5252
Type: schema.TypeString,
5353
Optional: true,
5454
ValidateFunc: validation.IsCIDR,
5555
ConflictsWith: []string{"cidr"},
56-
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag", "status"},
56+
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "role_id", "cidr", "tag", "status"},
5757
},
5858
"vlan_vid": {
5959
Type: schema.TypeFloat,
6060
Optional: true,
61-
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag", "status"},
61+
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "role_id", "cidr", "tag", "status"},
6262
ValidateFunc: validation.FloatBetween(1, 4094),
6363
},
6464
"vrf_id": {
6565
Type: schema.TypeInt,
6666
Optional: true,
67-
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag", "status"},
67+
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "role_id", "cidr", "tag", "status"},
6868
},
6969
"vlan_id": {
7070
Type: schema.TypeInt,
7171
Optional: true,
72-
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag", "status"},
72+
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "role_id", "cidr", "tag", "status"},
7373
},
7474
"tenant_id": {
7575
Type: schema.TypeInt,
7676
Optional: true,
77-
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag", "status"},
77+
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "role_id", "cidr", "tag", "status"},
7878
},
79-
"site_id": {
79+
"scope_type": {
80+
Type: schema.TypeString,
81+
Computed: true,
82+
},
83+
"scope_id": {
8084
Type: schema.TypeInt,
81-
Optional: true,
82-
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag", "status"},
85+
Computed: true,
8386
},
8487
"tag": {
8588
Type: schema.TypeString,
8689
Optional: true,
87-
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag", "status"},
90+
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "role_id", "cidr", "tag", "status"},
8891
Description: "Tag to include in the data source filter (must match the tag's slug).",
8992
},
9093
"tag__n": {
@@ -97,7 +100,7 @@ for more information on available lookup expressions.`,
97100
"status": {
98101
Type: schema.TypeString,
99102
Optional: true,
100-
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag", "status"},
103+
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "role_id", "cidr", "tag", "status"},
101104
},
102105
"tags": tagsSchemaRead,
103106
},
@@ -207,8 +210,15 @@ func dataSourceNetboxPrefixRead(d *schema.ResourceData, m interface{}) error {
207210
if result.Tenant != nil {
208211
d.Set("tenant_id", result.Tenant.ID)
209212
}
210-
if result.Site != nil {
211-
d.Set("site_id", result.Site.ID)
213+
if result.ScopeType != nil {
214+
d.Set("scope_type", result.ScopeType)
215+
} else {
216+
d.Set("scope_type", nil)
217+
}
218+
if result.ScopeID != nil {
219+
d.Set("scope_id", result.ScopeID)
220+
} else {
221+
d.Set("scope_id", nil)
212222
}
213223
d.SetId(strconv.FormatInt(result.ID, 10))
214224
return nil

netbox/data_source_netbox_prefix_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ resource "netbox_prefix" "testv4" {
4646
vrf_id = netbox_vrf.test.id
4747
vlan_id = netbox_vlan.test.id
4848
tenant_id = netbox_tenant.test.id
49-
site_id = netbox_site.test.id
49+
scope_type = "dcim.site"
50+
scope_id = netbox_site.test.id
5051
role_id = netbox_ipam_role.test.id
5152
description = "%[1]s_description_test_idv4"
5253
}
@@ -57,7 +58,8 @@ resource "netbox_prefix" "testv6" {
5758
vrf_id = netbox_vrf.test.id
5859
vlan_id = netbox_vlan.test.id
5960
tenant_id = netbox_tenant.test.id
60-
site_id = netbox_site.test.id
61+
scope_type = "dcim.site"
62+
scope_id = netbox_site.test.id
6163
description = "%[1]s_description_test_idv6"
6264
}
6365
@@ -99,11 +101,12 @@ data "netbox_prefix" "by_tenant_id" {
99101
family = 4
100102
}
101103
102-
data "netbox_prefix" "by_site_id" {
103-
depends_on = [netbox_prefix.testv4]
104-
site_id = netbox_site.test.id
105-
family = 4
106-
}
104+
# TODO
105+
#data "netbox_prefix" "by_site_id" {
106+
# depends_on = [netbox_prefix.testv4]
107+
# site_id = netbox_site.test.id
108+
# family = 4
109+
#}
107110
108111
data "netbox_prefix" "by_role_id" {
109112
depends_on = [netbox_prefix.testv4]
@@ -127,7 +130,8 @@ data "netbox_prefix" "by_family" {
127130
resource.TestCheckResourceAttrPair("data.netbox_prefix.by_vlan_id", "id", "netbox_prefix.testv4", "id"),
128131
resource.TestCheckResourceAttrPair("data.netbox_prefix.by_vlan_vid", "id", "netbox_prefix.testv4", "id"),
129132
resource.TestCheckResourceAttrPair("data.netbox_prefix.by_tenant_id", "id", "netbox_prefix.testv4", "id"),
130-
resource.TestCheckResourceAttrPair("data.netbox_prefix.by_site_id", "id", "netbox_prefix.testv4", "id"),
133+
// TODO
134+
// resource.TestCheckResourceAttrPair("data.netbox_prefix.by_site_id", "id", "netbox_prefix.testv4", "id"),
131135
resource.TestCheckResourceAttrPair("data.netbox_prefix.by_role_id", "id", "netbox_prefix.testv4", "id"),
132136
resource.TestCheckResourceAttrPair("data.netbox_prefix.by_status", "id", "netbox_prefix.testv4", "id"),
133137
resource.TestCheckResourceAttrPair("data.netbox_prefix.by_family", "id", "netbox_prefix.testv6", "id"),

netbox/data_source_netbox_prefixes.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ func dataSourceNetboxPrefixes() *schema.Resource {
6262
Type: schema.TypeInt,
6363
Computed: true,
6464
},
65-
"site_id": {
65+
"scope_type": {
66+
Type: schema.TypeString,
67+
Computed: true,
68+
},
69+
"scope_id": {
6670
Type: schema.TypeInt,
6771
Computed: true,
6872
},
@@ -158,8 +162,11 @@ func dataSourceNetboxPrefixesRead(d *schema.ResourceData, m interface{}) error {
158162
if v.Tenant != nil {
159163
mapping["tenant_id"] = v.Tenant.ID
160164
}
161-
if v.Site != nil {
162-
mapping["site_id"] = v.Site.ID
165+
if v.ScopeType != nil {
166+
mapping["scope_type"] = v.ScopeType
167+
}
168+
if v.ScopeID != nil {
169+
mapping["scope_id"] = v.ScopeID
163170
}
164171
mapping["status"] = v.Status.Value
165172
mapping["tags"] = getTagListFromNestedTagList(v.Tags)

netbox/data_source_netbox_prefixes_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ resource "netbox_site" "test" {
5656
resource "netbox_prefix" "with_site_id" {
5757
prefix = "%[6]s"
5858
status = "container"
59-
site_id = netbox_site.test.id
59+
scope_type = "dcim.site"
60+
scope_id = netbox_site.test.id
6061
}
6162
6263
resource "netbox_site" "test2" {
@@ -67,7 +68,8 @@ resource "netbox_site" "test2" {
6768
resource "netbox_prefix" "with_container" {
6869
prefix = "%[9]s"
6970
status = "container"
70-
site_id = netbox_site.test2.id
71+
scope_type = "dcim.site"
72+
scope_id = netbox_site.test2.id
7173
}
7274
7375
resource "netbox_vrf" "test_vrf" {

netbox/resource_netbox_prefix.go

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
)
1111

1212
var resourceNetboxPrefixStatusOptions = []string{"active", "container", "reserved", "deprecated"}
13+
var resourceNetboxPrefixScopeTypeOptions = []string{"dcim.location", "dcim.site", "dcim.sitegroup", "dcim.region"}
1314

1415
func resourceNetboxPrefix() *schema.Resource {
1516
return &schema.Resource{
@@ -56,10 +57,6 @@ func resourceNetboxPrefix() *schema.Resource {
5657
Type: schema.TypeInt,
5758
Optional: true,
5859
},
59-
"site_id": {
60-
Type: schema.TypeInt,
61-
Optional: true,
62-
},
6360
"vlan_id": {
6461
Type: schema.TypeInt,
6562
Optional: true,
@@ -68,6 +65,18 @@ func resourceNetboxPrefix() *schema.Resource {
6865
Type: schema.TypeInt,
6966
Optional: true,
7067
},
68+
"scope_type": {
69+
Type: schema.TypeString,
70+
Optional: true,
71+
ValidateFunc: validation.StringInSlice(resourceNetboxPrefixScopeTypeOptions, false),
72+
Description: buildValidValueDescription(resourceNetboxPrefixScopeTypeOptions),
73+
RequiredWith: []string{"scope_id"},
74+
},
75+
"scope_id": {
76+
Type: schema.TypeInt,
77+
Optional: true,
78+
RequiredWith: []string{"scope_type"},
79+
},
7180
customFieldsKey: customFieldsSchema,
7281
tagsKey: tagsSchema,
7382
},
@@ -102,8 +111,12 @@ func resourceNetboxPrefixCreate(d *schema.ResourceData, m interface{}) error {
102111
data.Tenant = int64ToPtr(int64(tenantID.(int)))
103112
}
104113

105-
if siteID, ok := d.GetOk("site_id"); ok {
106-
data.Site = int64ToPtr(int64(siteID.(int)))
114+
if scopeType, ok := d.GetOk("scope_type"); ok {
115+
data.ScopeType = strToPtr(scopeType.(string))
116+
}
117+
118+
if scopeID, ok := d.GetOk("scope_id"); ok {
119+
data.ScopeID = int64ToPtr(int64(scopeID.(int)))
107120
}
108121

109122
if vlanID, ok := d.GetOk("vlan_id"); ok {
@@ -153,52 +166,55 @@ func resourceNetboxPrefixRead(d *schema.ResourceData, m interface{}) error {
153166
return err
154167
}
155168

156-
d.Set("description", res.GetPayload().Description)
157-
d.Set("is_pool", res.GetPayload().IsPool)
158-
d.Set("mark_utilized", res.GetPayload().MarkUtilized)
159-
if res.GetPayload().Status != nil {
160-
d.Set("status", res.GetPayload().Status.Value)
169+
prefix := res.GetPayload()
170+
d.Set("description", prefix.Description)
171+
d.Set("is_pool", prefix.IsPool)
172+
d.Set("mark_utilized", prefix.MarkUtilized)
173+
if prefix.Status != nil {
174+
d.Set("status", prefix.Status.Value)
161175
}
162-
if res.GetPayload().Prefix != nil {
163-
d.Set("prefix", res.GetPayload().Prefix)
176+
if prefix.Prefix != nil {
177+
d.Set("prefix", prefix.Prefix)
164178
}
165179

166-
if res.GetPayload().Vrf != nil {
167-
d.Set("vrf_id", res.GetPayload().Vrf.ID)
180+
if prefix.Vrf != nil {
181+
d.Set("vrf_id", prefix.Vrf.ID)
168182
} else {
169183
d.Set("vrf_id", nil)
170184
}
171185

172-
if res.GetPayload().Tenant != nil {
173-
d.Set("tenant_id", res.GetPayload().Tenant.ID)
186+
if prefix.Tenant != nil {
187+
d.Set("tenant_id", prefix.Tenant.ID)
174188
} else {
175189
d.Set("tenant_id", nil)
176190
}
177191

178-
if res.GetPayload().Site != nil {
179-
d.Set("site_id", res.GetPayload().Site.ID)
180-
} else {
181-
d.Set("site_id", nil)
192+
if prefix.ScopeType != nil {
193+
d.Set("scope_type", prefix.ScopeType)
194+
}
195+
196+
if prefix.ScopeID != nil {
197+
d.Set("scope_id", prefix.ScopeID)
182198
}
183199

184-
if res.GetPayload().Vlan != nil {
185-
d.Set("vlan_id", res.GetPayload().Vlan.ID)
200+
if prefix.Vlan != nil {
201+
d.Set("vlan_id", prefix.Vlan.ID)
186202
} else {
187203
d.Set("vlan_id", nil)
188204
}
189205

190-
if res.GetPayload().Role != nil {
191-
d.Set("role_id", res.GetPayload().Role.ID)
206+
if prefix.Role != nil {
207+
d.Set("role_id", prefix.Role.ID)
192208
} else {
193209
d.Set("role_id", nil)
194210
}
195211

196-
cf := getCustomFields(res.GetPayload().CustomFields)
212+
cf := getCustomFields(prefix.CustomFields)
197213
if cf != nil {
198214
d.Set(customFieldsKey, cf)
199215
}
200216

201-
api.readTags(d, res.GetPayload().Tags)
217+
api.readTags(d, prefix.Tags)
202218
// FIGURE OUT NESTED VRF AND NESTED VLAN (from maybe interfaces?)
203219

204220
return nil
@@ -233,8 +249,12 @@ func resourceNetboxPrefixUpdate(d *schema.ResourceData, m interface{}) error {
233249
data.Tenant = int64ToPtr(int64(tenantID.(int)))
234250
}
235251

236-
if siteID, ok := d.GetOk("site_id"); ok {
237-
data.Site = int64ToPtr(int64(siteID.(int)))
252+
if scopeType, ok := d.GetOk("scope_type"); ok {
253+
data.ScopeType = strToPtr(scopeType.(string))
254+
}
255+
256+
if scopeID, ok := d.GetOk("scope_id"); ok {
257+
data.ScopeID = int64ToPtr(int64(scopeID.(int)))
238258
}
239259

240260
if vlanID, ok := d.GetOk("vlan_id"); ok {

0 commit comments

Comments
 (0)