Skip to content

Commit

Permalink
Query VLANs by group, role, or tenant
Browse files Browse the repository at this point in the history
  • Loading branch information
tstarck committed Oct 31, 2022
1 parent 3ec765b commit 574e292
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.dll
*.exe
.envrc
.DS_Store
example.tf
terraform.tfplan
Expand Down
19 changes: 15 additions & 4 deletions docs/data-sources/vlan.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,19 @@ data "netbox_vlan" "vlan1" {
name = "vlan-1"
}
# Get VLAN by VLAN ID
# Get VLAN by VID and IPAM role ID
data "netbox_vlan" "vlan2" {
vid = 1234
vid = 1234
role = netbox_ipam_role.example.id
}
# Get VLAN by name and tenant name
data "netbox_tenant" "tenant1" {
name = "Tenant A"
}
data "netbox_vlan" "vlan3" {
name = "vlan-3"
tenant = data.netbox_tenant.tenant1.id
}
```

Expand All @@ -29,16 +39,17 @@ data "netbox_vlan" "vlan2" {

### Optional

- `group` (Number)
- `name` (String) At least one of `name` or `vid` must be given.
- `role` (Number)
- `tenant` (Number)
- `vid` (Number) At least one of `name` or `vid` must be given.

### Read-Only

- `description` (String)
- `id` (String) The ID of this resource.
- `role` (Number)
- `site` (Number)
- `status` (String)
- `tenant` (Number)


14 changes: 12 additions & 2 deletions examples/data-sources/netbox_vlan/data-source.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@ data "netbox_vlan" "vlan1" {
name = "vlan-1"
}

# Get VLAN by VLAN ID
# Get VLAN by VID and IPAM role ID
data "netbox_vlan" "vlan2" {
vid = 1234
vid = 1234
role = netbox_ipam_role.example.id
}

# Get VLAN by name and tenant name
data "netbox_tenant" "tenant1" {
name = "Tenant A"
}
data "netbox_vlan" "vlan3" {
name = "vlan-3"
tenant = data.netbox_tenant.tenant1.id
}
6 changes: 3 additions & 3 deletions netbox/data_source_netbox_tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ func dataSourceNetboxTenant() *schema.Resource {
Read: dataSourceNetboxTenantRead,
Description: `:meta:subcategory:Tenancy:`,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
AtLeastOneOf: []string{"name", "slug"},
},
"slug": &schema.Schema{
"slug": {
Type: schema.TypeString,
Optional: true,
Computed: true,
AtLeastOneOf: []string{"name", "slug"},
},
"group_id": &schema.Schema{
"group_id": {
Type: schema.TypeInt,
Computed: true,
},
Expand Down
19 changes: 19 additions & 0 deletions netbox/data_source_netbox_vlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ func dataSourceNetboxVlan() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"group": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
},
"role": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
},
"site": {
Type: schema.TypeInt,
Expand All @@ -45,6 +51,7 @@ func dataSourceNetboxVlan() *schema.Resource {
"tenant": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
},
},
}
Expand All @@ -61,6 +68,15 @@ func dataSourceNetboxVlanRead(d *schema.ResourceData, m interface{}) error {
if vid, ok := d.Get("vid").(int); ok && vid != 0 {
params.Vid = strToPtr(strconv.Itoa(vid))
}
if groupID, ok := d.Get("group").(int); ok && groupID != 0 {
params.GroupID = strToPtr(strconv.Itoa(groupID))
}
if roleID, ok := d.Get("role").(int); ok && roleID != 0 {
params.RoleID = strToPtr(strconv.Itoa(roleID))
}
if tenantID, ok := d.Get("tenant").(int); ok && tenantID != 0 {
params.TenantID = strToPtr(strconv.Itoa(tenantID))
}

res, err := api.Ipam.IpamVlansList(params, nil)
if err != nil {
Expand All @@ -78,6 +94,9 @@ func dataSourceNetboxVlanRead(d *schema.ResourceData, m interface{}) error {
d.Set("status", vlan.Status.Value)
d.Set("description", vlan.Description)

if vlan.Group != nil {
d.Set("group", vlan.Group.ID)
}
if vlan.Role != nil {
d.Set("role", vlan.Role.ID)
}
Expand Down
72 changes: 71 additions & 1 deletion netbox/data_source_netbox_vlan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func TestAccNetboxVlanDataSource_basic(t *testing.T) {
testVid := 4092
testName := testAccGetTestName("vlan")
setUp := testAccNetboxVlanSetUp(testVid, testName)
extendedSetUp := testAccNetboxVlanSetUpMore(testVid, testVid-1, testName)
resource.ParallelTest(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
Expand All @@ -30,29 +31,82 @@ func TestAccNetboxVlanDataSource_basic(t *testing.T) {
},
{
Config: setUp + testAccNetboxVlanDataByVid(testVid),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair("data.netbox_vlan.test", "id", "netbox_vlan.test", "id"),
),
},

{
Config: setUp + extendedSetUp + testAccNetboxVlanDataByNameAndRole(testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair("data.netbox_vlan.test", "id", "netbox_vlan.test", "id"),
),
},
{
Config: setUp + extendedSetUp + testAccNetboxVlanDataByVidAndTenant(testVid),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair("data.netbox_vlan.test", "id", "netbox_vlan.test", "id"),
resource.TestCheckResourceAttr("data.netbox_vlan.test", "name", testName),
resource.TestCheckResourceAttr("data.netbox_vlan.test", "status", "active"),
resource.TestCheckResourceAttr("data.netbox_vlan.test", "description", "Test"),
resource.TestCheckResourceAttrPair("data.netbox_vlan.test", "role", "netbox_ipam_role.test", "id"),
resource.TestCheckResourceAttrPair("data.netbox_vlan.test", "site", "netbox_site.test", "id"),
resource.TestCheckResourceAttrPair("data.netbox_vlan.test", "tenant", "netbox_tenant.test", "id"),
),
},
{
Config: setUp + extendedSetUp + testAccNetboxVlanDataByName(testName),
ExpectError: regexp.MustCompile("expected one device type, but got 2"),
},
{
Config: setUp + extendedSetUp + testAccNetboxVlanDataByVid(testVid),
ExpectError: regexp.MustCompile("expected one device type, but got 2"),
},
},
})
}

func testAccNetboxVlanSetUp(testVid int, testName string) string {
return fmt.Sprintf(`
resource "netbox_ipam_role" "test" {
name = "%[2]s"
}
resource "netbox_site" "test" {
name = "%[2]s"
}
resource "netbox_tenant" "test" {
name = "%[2]s"
}
resource "netbox_vlan" "test" {
vid = %[1]d
name = "%[2]s"
status = "active"
description = "Test"
role_id = netbox_ipam_role.test.id
site_id = netbox_site.test.id
status = "active"
tags = []
tenant_id = netbox_tenant.test.id
}
`, testVid, testName)
}

func testAccNetboxVlanSetUpMore(testVid int, anotherVid int, testName string) string {
return fmt.Sprintf(`
resource "netbox_vlan" "same_name" {
vid = %[1]d
name = "%[3]s"
}
resource "netbox_vlan" "not_same" {
vid = %[2]d
name = "%[3]s_unique"
}
`, testVid, anotherVid, testName)
}

const testAccNetboxVlanDataNoResult = `
data "netbox_vlan" "no_result" {
name = "_no_result_"
Expand All @@ -71,3 +125,19 @@ data "netbox_vlan" "test" {
vid = "%[1]d"
}`, testVid)
}

func testAccNetboxVlanDataByNameAndRole(testName string) string {
return fmt.Sprintf(`
data "netbox_vlan" "test" {
name = "%[1]s"
role = netbox_ipam_role.test.id
}`, testName)
}

func testAccNetboxVlanDataByVidAndTenant(testVid int) string {
return fmt.Sprintf(`
data "netbox_vlan" "test" {
vid = "%[1]d"
tenant = netbox_tenant.test.id
}`, testVid)
}

0 comments on commit 574e292

Please sign in to comment.