Skip to content

Commit d7a1231

Browse files
committed
Added data.vlan_groupS resource and added id field to data.vlan_group
1 parent 2ed26bb commit d7a1231

File tree

7 files changed

+603
-14
lines changed

7 files changed

+603
-14
lines changed

docs/data-sources/vlan_group.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,21 @@ data "netbox_vlan_group" "example1" {
1818
name = "example1"
1919
}
2020
21-
# Get VLAN group by stub
21+
# Get VLAN group by slug
2222
data "netbox_vlan_group" "example2" {
2323
slug = "example2"
2424
}
2525
26+
# Get VLAN group by ID
27+
data "netbox_vlan_group" "example_id" {
28+
id = "1"
29+
}
30+
2631
# Get VLAN group by name and scope_type/id
2732
data "netbox_vlan_group" "example3" {
2833
name = "example"
2934
scope_type = "dcim.site"
30-
scope_id = netbox_site.example.id
35+
scope_id = "1"
3136
}
3237
```
3338

@@ -36,15 +41,18 @@ data "netbox_vlan_group" "example3" {
3641

3742
### Optional
3843

39-
- `name` (String) At least one of `name`, `slug` or `scope_type` must be given.
40-
- `scope_id` (Number) Required when `scope_type` is set.
41-
- `scope_type` (String) Valid values are `dcim.location`, `dcim.site`, `dcim.sitegroup`, `dcim.region`, `dcim.rack`, `virtualization.cluster` and `virtualization.clustergroup`. At least one of `name`, `slug` or `scope_type` must be given.
42-
- `slug` (String) At least one of `name`, `slug` or `scope_type` must be given.
44+
- `id` (String) The ID of the VLAN group.
45+
- `name` (String) At least one of `id`, `name`, `slug` or `scope_type` must be given.
46+
- `scope_id` (String) Required when `scope_type` is set.
47+
- `scope_type` (String) Valid values are `dcim.location`, `dcim.site`, `dcim.sitegroup`, `dcim.region`, `dcim.rack`, `virtualization.cluster` and `virtualization.clustergroup`. At least one of `id`, `name`, `slug` or `scope_type` must be given.
48+
- `slug` (String) At least one of `id`, `name`, `slug` or `scope_type` must be given.
4349

4450
### Read-Only
4551

4652
- `description` (String)
4753
- `id` (String) The ID of this resource.
54+
- `scope_id` (String)
55+
- `scope_type` (String)
4856
- `vlan_count` (Number)
4957

5058

docs/data-sources/vlan_groups.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
# generated by https://github.com/fbreckle/terraform-plugin-docs
3+
page_title: "netbox_vlan_groups Data Source - terraform-provider-netbox"
4+
subcategory: "IP Address Management (IPAM)"
5+
description: |-
6+
7+
---
8+
9+
# netbox_vlan_groups (Data Source)
10+
11+
12+
13+
## Example Usage
14+
15+
```terraform
16+
# Get all VLAN groups
17+
data "netbox_vlan_groups" "all" {
18+
}
19+
20+
# Get VLAN groups by name
21+
data "netbox_vlan_groups" "example" {
22+
filter {
23+
name = "name"
24+
value = "example"
25+
}
26+
}
27+
28+
# Get VLAN groups by slug
29+
data "netbox_vlan_groups" "by_slug" {
30+
filter {
31+
name = "slug"
32+
value = "example-slug"
33+
}
34+
}
35+
36+
# Get VLAN groups by site_id
37+
data "netbox_vlan_groups" "by_site" {
38+
filter {
39+
name = "site_id"
40+
value = "1"
41+
}
42+
}
43+
44+
# Get VLAN groups by scope
45+
data "netbox_vlan_groups" "by_scope" {
46+
filter {
47+
name = "scope_type"
48+
value = "dcim.site"
49+
}
50+
filter {
51+
name = "scope_id"
52+
value = "1"
53+
}
54+
}
55+
```
56+
57+
<!-- schema generated by tfplugindocs -->
58+
## Schema
59+
60+
### Optional
61+
62+
- `filter` (Block Set) (see [below for nested schema](#nestedblock--filter))
63+
- `limit` (Number) Defaults to `0`.
64+
65+
### Read-Only
66+
67+
- `id` (String) The ID of this resource.
68+
- `vlan_groups` (List of Object) (see [below for nested schema](#nestedatt--vlan_groups))
69+
70+
<a id="nestedblock--filter"></a>
71+
### Nested Schema for `filter`
72+
73+
Required:
74+
75+
- `name` (String)
76+
- `value` (String)
77+
78+
79+
<a id="nestedatt--vlan_groups"></a>
80+
### Nested Schema for `vlan_groups`
81+
82+
Read-Only:
83+
84+
- `description` (String)
85+
- `id` (Number)
86+
- `name` (String)
87+
- `ranges` (List of Object) (see [below for nested schema](#nestedatt--vlan_groups--ranges))
88+
- `slug` (String)
89+
- `tag_ids` (List of Number)
90+
- `used` (Number) The number of used VLANs in this VLAN group.
91+
92+
<a id="nestedatt--vlan_groups--ranges"></a>
93+
### Nested Schema for `vlan_groups.ranges`
94+
95+
Read-Only:
96+
97+
- `end` (Number)
98+
- `start` (Number)

netbox/data_source_netbox_vlan_group.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,33 @@ func dataSourceNetboxVlanGroup() *schema.Resource {
1414
Read: dataSourceNetboxVlanGroupRead,
1515
Description: `:meta:subcategory:IP Address Management (IPAM):`,
1616
Schema: map[string]*schema.Schema{
17+
"id": {
18+
Type: schema.TypeString,
19+
Computed: true,
20+
Optional: true,
21+
AtLeastOneOf: []string{"id", "name", "slug", "scope_type"},
22+
},
1723
"name": {
1824
Type: schema.TypeString,
1925
Computed: true,
2026
Optional: true,
21-
AtLeastOneOf: []string{"name", "slug", "scope_type"},
27+
AtLeastOneOf: []string{"id", "name", "slug", "scope_type"},
2228
},
2329
"slug": {
2430
Type: schema.TypeString,
2531
Optional: true,
2632
Computed: true,
27-
AtLeastOneOf: []string{"name", "slug", "scope_type"},
33+
AtLeastOneOf: []string{"id", "name", "slug", "scope_type"},
2834
},
2935
"scope_type": {
3036
Type: schema.TypeString,
3137
Optional: true,
3238
ValidateFunc: validation.StringInSlice(resourceNetboxVlanGroupScopeTypeOptions, false),
3339
Description: buildValidValueDescription(resourceNetboxVlanGroupScopeTypeOptions),
34-
AtLeastOneOf: []string{"name", "slug", "scope_type"},
40+
AtLeastOneOf: []string{"id", "name", "slug", "scope_type"},
3541
},
3642
"scope_id": {
37-
Type: schema.TypeInt,
43+
Type: schema.TypeString,
3844
Optional: true,
3945
RequiredWith: []string{"scope_type"},
4046
},
@@ -55,17 +61,21 @@ func dataSourceNetboxVlanGroupRead(d *schema.ResourceData, m interface{}) error
5561
params := ipam.NewIpamVlanGroupsListParams()
5662

5763
params.Limit = int64ToPtr(2)
64+
65+
if id, ok := d.Get("id").(string); ok && id != "" {
66+
params.ID = strToPtr(id)
67+
}
5868
if name, ok := d.Get("name").(string); ok && name != "" {
59-
params.Name = &name
69+
params.Name = strToPtr(name)
6070
}
6171
if slug, ok := d.Get("slug").(string); ok && slug != "" {
62-
params.Slug = &slug
72+
params.Slug = strToPtr(slug)
6373
}
6474
if scopeType, ok := d.Get("scope_type").(string); ok && scopeType != "" {
65-
params.SetScopeType(&scopeType)
75+
params.ScopeType = strToPtr(scopeType)
6676
}
6777
if scopeID, ok := d.Get("scope_id").(string); ok && scopeID != "" {
68-
params.SetScopeID(params.ScopeID)
78+
params.ScopeID = strToPtr(scopeID)
6979
}
7080

7181
res, err := api.Ipam.IpamVlanGroupsList(params, nil)
@@ -82,9 +92,14 @@ func dataSourceNetboxVlanGroupRead(d *schema.ResourceData, m interface{}) error
8292

8393
result := res.GetPayload().Results[0]
8494
d.SetId(strconv.FormatInt(result.ID, 10))
95+
d.Set("id", result.ID)
8596
d.Set("name", result.Name)
8697
d.Set("slug", result.Slug)
8798
d.Set("vlan_count", result.VlanCount)
8899
d.Set("description", result.Description)
100+
if result.ScopeID != nil {
101+
d.Set("scope_id", result.ScopeID)
102+
}
103+
d.Set("scope_type", result.ScopeType)
89104
return nil
90105
}

netbox/data_source_netbox_vlan_group_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ func TestAccNetboxVlanGroupDataSource_basic(t *testing.T) {
2424
Config: setUp + testAccNetboxVlanGroupDataNoResult,
2525
ExpectError: regexp.MustCompile("no vlan group found matching filter"),
2626
},
27+
{
28+
Config: setUp + testAccNetboxVlanGroupDataByID("netbox_vlan_group.test", "id"),
29+
Check: resource.ComposeTestCheckFunc(
30+
resource.TestCheckResourceAttrPair("data.netbox_vlan_group.test", "id", "netbox_vlan_group.test", "id"),
31+
),
32+
},
2733
{
2834
Config: setUp + testAccNetboxVlanGroupDataByName(testName),
2935
Check: resource.ComposeTestCheckFunc(
@@ -109,6 +115,13 @@ data "netbox_vlan_group" "no_result" {
109115
name = "_no_result_"
110116
}`
111117

118+
func testAccNetboxVlanGroupDataByID(resourceRef, field string) string {
119+
return fmt.Sprintf(`
120+
data "netbox_vlan_group" "test" {
121+
id = %[1]s.%[2]s
122+
}`, resourceRef, field)
123+
}
124+
112125
func testAccNetboxVlanGroupDataByName(testName string) string {
113126
return fmt.Sprintf(`
114127
data "netbox_vlan_group" "test" {

0 commit comments

Comments
 (0)