Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for bulk vrfs data source #441

Merged
merged 2 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions docs/data-sources/vrfs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
# generated by https://github.com/fbreckle/terraform-plugin-docs
page_title: "netbox_vrfs Data Source - terraform-provider-netbox"
subcategory: "IP Address Management (IPAM)"
description: |-

---

# netbox_vrfs (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.
- `vrfs` (List of Object) (see [below for nested schema](#nestedatt--vrfs))

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

Required:

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


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

Read-Only:

- `description` (String)
- `id` (Number)
- `name` (String)
- `rd` (String)
- `tenant` (Number)


149 changes: 149 additions & 0 deletions netbox/data_source_netbox_vrfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
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 dataSourceNetboxVrfs() *schema.Resource {
return &schema.Resource{
Read: dataSourceNetboxVrfsRead,
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)),
fbreckle marked this conversation as resolved.
Show resolved Hide resolved
Default: 0,
},
"vrfs": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"rd": {
Type: schema.TypeString,
Computed: true,
},
"tenant": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
}
}

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

params := ipam.NewIpamVrfsListParams()

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 "id":
params.ID = &vString
case "name":
params.Name = &vString
case "description":
params.Description = &vString
case "rd":
params.Rd = &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.IpamVrfsList(params, nil)
if err != nil {
return err
}

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

filteredVrfs := res.GetPayload().Results

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

mapping["id"] = v.ID
mapping["name"] = v.Name
mapping["description"] = v.Description
if v.Rd != nil {
mapping["rd"] = v.Rd
}
if v.Tenant != nil {
mapping["tenant"] = v.Tenant.ID
}

s = append(s, mapping)
}

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

import (
"testing"

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

func testAccNetboxVrfsSetUp() string {
return `
resource "netbox_vrf" "test_1" {
name = "VRF1"
}

resource "netbox_vrf" "test_2" {
name = "VRF2"
}

resource "netbox_vrf" "test_3" {
name = "VRF3"
}`
}

func testAccNetboxVrfsByName() string {
return `
data "netbox_vrfs" "test" {
filter {
name = "name"
value = "VRF1"
}
}`
}

func TestAccNetboxVrfsDataSource_basic(t *testing.T) {
setUp := testAccNetboxVrfsSetUp()
resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: setUp,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_vrf.test_1", "name", "VRF1"),
resource.TestCheckResourceAttr("netbox_vrf.test_2", "name", "VRF2"),
resource.TestCheckResourceAttr("netbox_vrf.test_3", "name", "VRF3"),
),
},
{
Config: setUp + testAccNetboxVrfsByName(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.netbox_vrfs.test", "vrfs.#", "1"),
resource.TestCheckResourceAttrPair("data.netbox_vrfs.test", "vrfs.0.name", "netbox_vrf.test_1", "name"),
),
},
},
})
}
1 change: 1 addition & 0 deletions netbox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func Provider() *schema.Provider {
"netbox_tenants": dataSourceNetboxTenants(),
"netbox_tenant_group": dataSourceNetboxTenantGroup(),
"netbox_vrf": dataSourceNetboxVrf(),
"netbox_vrfs": dataSourceNetboxVrfs(),
"netbox_platform": dataSourceNetboxPlatform(),
"netbox_prefix": dataSourceNetboxPrefix(),
"netbox_prefixes": dataSourceNetboxPrefixes(),
Expand Down