Skip to content

Commit

Permalink
Add vlans data source
Browse files Browse the repository at this point in the history
  • Loading branch information
danischm authored and fbreckle committed Jun 27, 2023
1 parent 1702df9 commit d0e9dcc
Show file tree
Hide file tree
Showing 5 changed files with 344 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/data-sources/ip_addresses.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,20 @@ Read-Only:
- `last_updated` (String)
- `role` (String)
- `status` (String)
- `tags` (List of Object) (see [below for nested schema](#nestedobjatt--ip_addresses--tags))
- `tenant` (List of Object) (see [below for nested schema](#nestedobjatt--ip_addresses--tenant))

<a id="nestedobjatt--ip_addresses--tags"></a>
### Nested Schema for `ip_addresses.tags`

Read-Only:

- `display` (String)
- `id` (Number)
- `name` (String)
- `slug` (String)


<a id="nestedobjatt--ip_addresses--tenant"></a>
### Nested Schema for `ip_addresses.tenant`

Expand Down
51 changes: 51 additions & 0 deletions docs/data-sources/vlans.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
# generated by https://github.com/fbreckle/terraform-plugin-docs
page_title: "netbox_vlans Data Source - terraform-provider-netbox"
subcategory: "IP Address Management (IPAM)"
description: |-
---

# netbox_vlans (Data Source)





<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `filter` (Block Set) (see [below for nested schema](#nestedblock--filter))
- `limit` (Number) Defaults to `0`.

### Read-Only

- `id` (String) The ID of this resource.
- `vlans` (List of Object) (see [below for nested schema](#nestedatt--vlans))

<a id="nestedblock--filter"></a>
### Nested Schema for `filter`

Required:

- `name` (String)
- `value` (String)


<a id="nestedatt--vlans"></a>
### Nested Schema for `vlans`

Read-Only:

- `description` (String)
- `group_id` (Number)
- `name` (String)
- `role` (Number)
- `site` (Number)
- `status` (String)
- `tenant` (Number)
- `vid` (Number)


181 changes: 181 additions & 0 deletions netbox/data_source_netbox_vlans.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package netbox

import (
"errors"
"fmt"

"github.com/fbreckle/go-netbox/netbox/client"
"github.com/fbreckle/go-netbox/netbox/client/ipam"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func dataSourceNetboxVlans() *schema.Resource {
return &schema.Resource{
Read: dataSourceNetboxVlansRead,
Description: `:meta:subcategory:IP Address Management (IPAM):`,
Schema: map[string]*schema.Schema{
"filter": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"value": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"limit": {
Type: schema.TypeInt,
Optional: true,
ValidateDiagFunc: validation.ToDiagFunc(validation.IntAtLeast(1)),
Default: 0,
},
"vlans": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"vid": {
Type: schema.TypeInt,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"group_id": {
Type: schema.TypeInt,
Computed: true,
},
"role": {
Type: schema.TypeInt,
Computed: true,
},
"site": {
Type: schema.TypeInt,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"tenant": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
}
}

func dataSourceNetboxVlansRead(d *schema.ResourceData, m interface{}) error {
api := m.(*client.NetBoxAPI)

params := ipam.NewIpamVlansListParams()

if limitValue, ok := d.GetOk("limit"); ok {
params.Limit = int64ToPtr(int64(limitValue.(int)))
}

if filter, ok := d.GetOk("filter"); ok {
var filterParams = filter.(*schema.Set)
for _, f := range filterParams.List() {
k := f.(map[string]interface{})["name"]
v := f.(map[string]interface{})["value"]
vString := v.(string)
switch k {
case "vid":
params.Vid = &vString
case "vid__gt":
params.VidGt = &vString
case "vid__gte":
params.VidGte = &vString
case "vid__lt":
params.VidLt = &vString
case "vid__lte":
params.VidLte = &vString
case "vid__n":
params.Vidn = &vString
case "group":
params.Group = &vString
case "group__n":
params.Groupn = &vString
case "group_id":
params.GroupID = &vString
case "group_id__n":
params.GroupIDn = &vString
case "tenant":
params.Tenant = &vString
case "tenant__n":
params.Tenantn = &vString
case "tenant_group":
params.TenantGroup = &vString
case "tenant_group__n":
params.TenantGroupn = &vString
case "tenant_group_id":
params.TenantGroupID = &vString
case "tenant_group_id__n":
params.TenantGroupIDn = &vString
case "tenant_id":
params.TenantID = &vString
case "tenant_id__n":
params.TenantIDn = &vString
default:
return fmt.Errorf("'%s' is not a supported filter parameter", k)
}
}
}

res, err := api.Ipam.IpamVlansList(params, nil)
if err != nil {
return err
}

if *res.GetPayload().Count == int64(0) {
return errors.New("no result")
}

filteredVlans := res.GetPayload().Results

var s []map[string]interface{}
for _, v := range filteredVlans {
var mapping = make(map[string]interface{})

mapping["vid"] = v.Vid
mapping["name"] = v.Name
mapping["description"] = v.Description
if v.Group != nil {
mapping["group_id"] = v.Group.ID
}
mapping["vid"] = v.Vid
if v.Role != nil {
mapping["role"] = v.Role.ID
}
if v.Site != nil {
mapping["site"] = v.Site.ID
}
mapping["status"] = v.Status.Value
if v.Tenant != nil {
mapping["tenant"] = v.Tenant.ID
}

s = append(s, mapping)
}

d.SetId(id.UniqueId())
return d.Set("vlans", s)
}
99 changes: 99 additions & 0 deletions netbox/data_source_netbox_vlans_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package netbox

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func testAccNetboxVlansSetUp() string {
return `
resource "netbox_vlan" "test_1" {
name = "VLAN1234"
vid = 1234
}
resource "netbox_vlan" "test_2" {
name = "VLAN1235"
vid = 1235
}
resource "netbox_vlan" "test_3" {
name = "VLAN1236"
vid = 1236
}`
}

func testAccNetboxVlansByVid() string {
return `
data "netbox_vlans" "test" {
filter {
name = "vid"
value = "1234"
}
}`
}

func testAccNetboxVlansByVidN() string {
return `
data "netbox_vlans" "test" {
filter {
name = "vid__n"
value = "1234"
}
}`
}

func testAccNetboxVlansByVidRange() string {
return `
data "netbox_vlans" "test" {
filter {
name = "vid__gte"
value = "1235"
}
filter {
name = "vid__lte"
value = "1236"
}
}`
}

func TestAccNetboxVlansDataSource_basic(t *testing.T) {
setUp := testAccNetboxVlansSetUp()
// This test cannot be run in parallel with other tests, because other tests create also Vlans
// These Vlans then interfere with the __n filter test
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: setUp,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_vlan.test_1", "vid", "1234"),
),
},
{
Config: setUp + testAccNetboxVlansByVid(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.netbox_vlans.test", "vlans.#", "1"),
resource.TestCheckResourceAttrPair("data.netbox_vlans.test", "vlans.0.vid", "netbox_vlan.test_1", "vid"),
),
},
{
Config: setUp + testAccNetboxVlansByVidN(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.netbox_vlans.test", "vlans.#", "2"),
resource.TestCheckResourceAttrPair("data.netbox_vlans.test", "vlans.0.vid", "netbox_vlan.test_2", "vid"),
),
},
{
Config: setUp + testAccNetboxVlansByVidRange(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.netbox_vlans.test", "vlans.#", "2"),
resource.TestCheckResourceAttrPair("data.netbox_vlans.test", "vlans.0.vid", "netbox_vlan.test_2", "vid"),
resource.TestCheckResourceAttrPair("data.netbox_vlans.test", "vlans.1.vid", "netbox_vlan.test_3", "vid"),
),
},
},
})
}
1 change: 1 addition & 0 deletions netbox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func Provider() *schema.Provider {
"netbox_ip_range": dataSourceNetboxIpRange(),
"netbox_region": dataSourceNetboxRegion(),
"netbox_vlan": dataSourceNetboxVlan(),
"netbox_vlans": dataSourceNetboxVlans(),
"netbox_vlan_group": dataSourceNetboxVlanGroup(),
"netbox_site_group": dataSourceNetboxSiteGroup(),
"netbox_racks": dataSourceNetboxRacks(),
Expand Down

0 comments on commit d0e9dcc

Please sign in to comment.