Skip to content

Commit 1500525

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

File tree

6 files changed

+592
-12
lines changed

6 files changed

+592
-12
lines changed

docs/data-sources/vlan_group.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@ 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"
@@ -36,10 +41,11 @@ data "netbox_vlan_group" "example3" {
3641

3742
### Optional
3843

39-
- `name` (String) At least one of `name`, `slug` or `scope_type` must be given.
44+
- `id` (Number) The ID of the VLAN group.
45+
- `name` (String) At least one of `id`, `name`, `slug` or `scope_type` must be given.
4046
- `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.
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

docs/data-sources/vlan_groups.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 scope
37+
data "netbox_vlan_groups" "by_scope" {
38+
filter {
39+
name = "scope_type"
40+
value = "dcim.site"
41+
}
42+
filter {
43+
name = "scope_id"
44+
value = "1"
45+
}
46+
}
47+
```
48+
49+
<!-- schema generated by tfplugindocs -->
50+
## Schema
51+
52+
### Optional
53+
54+
- `filter` (Block Set) (see [below for nested schema](#nestedblock--filter))
55+
- `limit` (Number) Defaults to `0`.
56+
57+
### Read-Only
58+
59+
- `id` (String) The ID of this resource.
60+
- `vlan_groups` (List of Object) (see [below for nested schema](#nestedatt--vlan_groups))
61+
62+
<a id="nestedblock--filter"></a>
63+
### Nested Schema for `filter`
64+
65+
Required:
66+
67+
- `name` (String)
68+
- `value` (String)
69+
70+
71+
<a id="nestedatt--vlan_groups"></a>
72+
### Nested Schema for `vlan_groups`
73+
74+
Read-Only:
75+
76+
- `cluster_id` (Number)
77+
- `clustergroup_id` (Number)
78+
- `description` (String)
79+
- `id` (Number)
80+
- `location_id` (Number)
81+
- `max_vid` (Number)
82+
- `min_vid` (Number)
83+
- `name` (String)
84+
- `region_id` (Number)
85+
- `scope_id` (Number)
86+
- `scope_type` (String)
87+
- `site_id` (Number)
88+
- `sitegroup_id` (Number)
89+
- `slug` (String)
90+
- `tag_ids` (List of 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)