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

Prefixes filter improvements #428

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/go-openapi/strfmt v0.21.7
github.com/goware/urlx v0.3.2
github.com/hashicorp/terraform-plugin-sdk/v2 v2.27.0
github.com/iancoleman/strcase v0.2.0
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20221217163422-3c43f8badb15
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq
github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/huandu/xstrings v1.4.0 h1:D17IlohoQq4UcpqD7fDk80P7l+lwAmlFaBHgOipl2FU=
github.com/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
Expand Down
44 changes: 24 additions & 20 deletions netbox/data_source_netbox_prefixes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package netbox

import (
"fmt"
"reflect"
"strconv"
"strings"

"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"
"github.com/iancoleman/strcase"
)

func dataSourceNetboxPrefixes() *schema.Resource {
Expand Down Expand Up @@ -98,27 +101,28 @@ func dataSourceNetboxPrefixesRead(d *schema.ResourceData, m interface{}) error {
k := f.(map[string]interface{})["name"]
v := f.(map[string]interface{})["value"]
vString := v.(string)
switch k {
case "prefix":
params.Prefix = &vString
case "vlan_vid":
float, err := strconv.ParseFloat(vString, 64)
if err != nil {
return err
paramName := strcase.ToCamel(strings.Replace(k.(string), "__n", "n", -1))
paramName = strings.Replace(paramName, "Id", "ID", -1)

params_reflect := reflect.ValueOf(params).Elem()
field := params_reflect.FieldByName(paramName)

if !(field.IsValid()) {
return fmt.Errorf("'%s' is not a supported filter parameter. Netbox go SDK does not have the associated parameter [(%s)]", k, paramName)
}

if field.Kind() == reflect.Slice {
//Param is an array/slice
field.Set(reflect.Append(field, reflect.ValueOf(vString)))
} else if (reflect.PtrTo(field.Type().Elem()).Elem().Kind()) == reflect.Float64 {
// ^ This CANT be the best way to do this, but it works
vFloat, err := strconv.ParseFloat(vString, 64)
if field.Set(reflect.ValueOf(&vFloat)); err != nil {
return fmt.Errorf("Failed to set parameter [(%s)] with error [(%s)]", paramName, err)
}
params.VlanVid = &float
case "vrf_id":
params.VrfID = &vString
case "vlan_id":
params.VlanID = &vString
case "status":
params.Status = &vString
case "site_id":
params.SiteID = &vString
case "tag":
params.Tag = []string{vString}
default:
return fmt.Errorf("'%s' is not a supported filter parameter", k)
} else {
//Param is a scalar
field.Set(reflect.ValueOf(&vString))
}
}
}
Expand Down
65 changes: 59 additions & 6 deletions netbox/data_source_netbox_prefixes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestAccNetboxPrefixesDataSource_basic(t *testing.T) {

testPrefixes := []string{"10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24", "10.0.7.0/24"}
testPrefixes := []string{"10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24", "10.0.7.0/24", "10.0.8.0/25"}
testSlug := "prefixes_ds_basic"
testVlanVids := []int{4093, 4094}
testName := testAccGetTestName(testSlug)
Expand All @@ -34,8 +34,15 @@ resource "netbox_prefix" "test_prefix2" {
vlan_id = netbox_vlan.test_vlan2.id
}

resource "netbox_prefix" "test_prefix_two_tags_and_length_25" {
prefix = "%[6]s"
status = "active"
description = "multiple-tag-prefix"
tags = [netbox_tag.test_tag3.slug, netbox_tag.test_tag4.slug]
}

resource "netbox_prefix" "without_vrf_and_vlan" {
prefix = "%[4]s"
prefix = "%[5]s"
status = "active"
}

Expand All @@ -56,12 +63,12 @@ resource "netbox_vrf" "test_vrf" {

resource "netbox_vlan" "test_vlan1" {
name = "%[1]s_vlan1"
vid = %[6]d
vid = %[7]d
}

resource "netbox_vlan" "test_vlan2" {
name = "%[1]s_vlan2"
vid = %[7]d
vid = %[8]d
}

resource "netbox_tag" "test_tag1" {
Expand All @@ -72,6 +79,14 @@ resource "netbox_tag" "test_tag2" {
name = "tag-with-no-associtions"
}

resource "netbox_tag" "test_tag3" {
name = "%[1]s-tag-3"
}

resource "netbox_tag" "test_tag4" {
name = "%[1]s-tag-4"
}

data "netbox_prefixes" "by_vrf" {
depends_on = [netbox_prefix.test_prefix1, netbox_prefix.test_prefix2]
filter {
Expand All @@ -84,7 +99,7 @@ data "netbox_prefixes" "by_vid" {
depends_on = [netbox_prefix.test_prefix1, netbox_prefix.test_prefix2]
filter {
name = "vlan_vid"
value = "%[6]d"
value = "%[7]d"
}
}

Expand All @@ -96,6 +111,38 @@ data "netbox_prefixes" "by_tag" {
}
}

data "netbox_prefixes" "by_mask_length" {
depends_on = [netbox_prefix.test_prefix_two_tags_and_length_25]
filter {
name = "mask_length"
value = "25"
}
}

data "netbox_prefixes" "by_mask_length_and_tag" {
depends_on = [netbox_prefix.test_prefix1]
filter {
name = "mask_length"
value = "24"
}
filter {
name = "tag"
value = "%[1]s"
}
}

data "netbox_prefixes" "by_multiple_tags" {
depends_on = [netbox_prefix.test_prefix_two_tags_and_length_25]
filter {
name = "tag"
value = netbox_tag.test_tag3.slug
}
filter {
name = "tag"
value = netbox_tag.test_tag4.slug
}
}

data "netbox_prefixes" "no_results" {
depends_on = [netbox_prefix.test_prefix1]
filter {
Expand All @@ -118,7 +165,7 @@ data "netbox_prefixes" "find_prefix_with_site_id" {
value = netbox_site.test.id
}
}
`, testName, testPrefixes[0], testPrefixes[1], testPrefixes[2], testPrefixes[3], testVlanVids[0], testVlanVids[1]),
`, testName, testPrefixes[0], testPrefixes[1], testPrefixes[2], testPrefixes[3], testPrefixes[4], testVlanVids[0], testVlanVids[1]),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.netbox_prefixes.by_vrf", "prefixes.#", "2"),
resource.TestCheckResourceAttrPair("data.netbox_prefixes.by_vrf", "prefixes.1.vlan_vid", "netbox_vlan.test_vlan2", "vid"),
Expand All @@ -128,6 +175,12 @@ data "netbox_prefixes" "find_prefix_with_site_id" {
resource.TestCheckResourceAttr("data.netbox_prefixes.no_results", "prefixes.#", "0"),
resource.TestCheckResourceAttr("data.netbox_prefixes.find_prefix_with_site_id", "prefixes.#", "1"),
resource.TestCheckResourceAttr("data.netbox_prefixes.find_prefix_with_site_id", "prefixes.0.prefix", "10.0.7.0/24"),
resource.TestCheckResourceAttr("data.netbox_prefixes.by_mask_length", "prefixes.#", "1"),
resource.TestCheckResourceAttr("data.netbox_prefixes.by_mask_length", "prefixes.0.prefix", "10.0.8.0/25"),
resource.TestCheckResourceAttr("data.netbox_prefixes.by_multiple_tags", "prefixes.#", "1"),
resource.TestCheckResourceAttr("data.netbox_prefixes.by_multiple_tags", "prefixes.0.prefix", "10.0.8.0/25"),
resource.TestCheckResourceAttr("data.netbox_prefixes.by_mask_length_and_tag", "prefixes.#", "1"),
resource.TestCheckResourceAttr("data.netbox_prefixes.by_mask_length_and_tag", "prefixes.0.prefix", "10.0.4.0/24"),
),
},
},
Expand Down
Loading