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: add tenant_id filter to netbox_prefix and netbox_prefixes datasources #666

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 11 additions & 10 deletions docs/data-sources/prefix.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@ description: |-

### Optional

- `cidr` (String, Deprecated) At least one of `description`, `family`, `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `site_id`, `role_id`, `cidr` or `tag` must be given. Conflicts with `prefix`.
- `cidr` (String, Deprecated) At least one of `description`, `family`, `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `tenant_id`, `site_id`, `role_id`, `cidr` or `tag` must be given. Conflicts with `prefix`.
- `custom_fields` (Map of String)
- `description` (String) Description to include in the data source filter. At least one of `description`, `family`, `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `site_id`, `role_id`, `cidr` or `tag` must be given.
- `family` (Number) The IP family of the prefix. One of 4 or 6. At least one of `description`, `family`, `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `site_id`, `role_id`, `cidr` or `tag` must be given.
- `prefix` (String) At least one of `description`, `family`, `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `site_id`, `role_id`, `cidr` or `tag` must be given. Conflicts with `cidr`.
- `role_id` (Number) At least one of `description`, `family`, `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `site_id`, `role_id`, `cidr` or `tag` must be given.
- `site_id` (Number) At least one of `description`, `family`, `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `site_id`, `role_id`, `cidr` or `tag` must be given.
- `tag` (String) Tag to include in the data source filter (must match the tag's slug). At least one of `description`, `family`, `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `site_id`, `role_id`, `cidr` or `tag` must be given.
- `description` (String) Description to include in the data source filter. At least one of `description`, `family`, `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `tenant_id`, `site_id`, `role_id`, `cidr` or `tag` must be given.
- `family` (Number) The IP family of the prefix. One of 4 or 6. At least one of `description`, `family`, `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `tenant_id`, `site_id`, `role_id`, `cidr` or `tag` must be given.
- `prefix` (String) At least one of `description`, `family`, `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `tenant_id`, `site_id`, `role_id`, `cidr` or `tag` must be given. Conflicts with `cidr`.
- `role_id` (Number) At least one of `description`, `family`, `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `tenant_id`, `site_id`, `role_id`, `cidr` or `tag` must be given.
- `tenant_id` (Number) At least one of `description`, `family`, `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `tenant_id`, `site_id`, `role_id`, `cidr` or `tag` must be given.
- `site_id` (Number) At least one of `description`, `family`, `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `tenant_id`, `site_id`, `role_id`, `cidr` or `tag` must be given.
- `tag` (String) Tag to include in the data source filter (must match the tag's slug). At least one of `description`, `family`, `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `tenant_id`, `site_id`, `role_id`, `cidr` or `tag` must be given.
- `tag__n` (String) Tag to exclude from the data source filter (must match the tag's slug).
Refer to [Netbox's documentation](https://demo.netbox.dev/static/docs/rest-api/filtering/#lookup-expressions)
for more information on available lookup expressions.
- `vlan_id` (Number) At least one of `description`, `family`, `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `site_id`, `role_id`, `cidr` or `tag` must be given.
- `vlan_vid` (Number) At least one of `description`, `family`, `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `site_id`, `role_id`, `cidr` or `tag` must be given.
- `vrf_id` (Number) At least one of `description`, `family`, `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `site_id`, `role_id`, `cidr` or `tag` must be given.
- `vlan_id` (Number) At least one of `description`, `family`, `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `tenant_id`, `site_id`, `role_id`, `cidr` or `tag` must be given.
- `vlan_vid` (Number) At least one of `description`, `family`, `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `tenant_id`, `site_id`, `role_id`, `cidr` or `tag` must be given.
- `vrf_id` (Number) At least one of `description`, `family`, `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `tenant_id`, `site_id`, `role_id`, `cidr` or `tag` must be given.

### Read-Only

Expand Down
3 changes: 2 additions & 1 deletion docs/data-sources/prefixes.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ description: |-

Required:

- `name` (String) The name of the field to filter on. Supported fields are: `prefix`, `contains`, `vlan_vid`, `vrf_id`, `vlan_id`, `status`, `site_id`, & `tag`.
- `name` (String) The name of the field to filter on. Supported fields are: `prefix`, `contains`, `vlan_vid`, `vrf_id`, `vlan_id`, `status`, `tenant_id`, `site_id`, & `tag`.
- `value` (String) The value to pass to the specified filter.


Expand All @@ -42,6 +42,7 @@ Read-Only:
- `description` (String)
- `id` (Number)
- `prefix` (String)
- `tenant_id` (Number)
- `site_id` (Number)
- `status` (String)
- `tags` (Set of String)
Expand Down
33 changes: 23 additions & 10 deletions netbox/data_source_netbox_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,62 +25,67 @@ func dataSourceNetboxPrefix() *schema.Resource {
Deprecated: "The `cidr` parameter is deprecated in favor of the canonical `prefix` attribute.",
ConflictsWith: []string{"prefix"},
ValidateFunc: validation.IsCIDR,
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "site_id", "role_id", "cidr", "tag"},
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag"},
},
customFieldsKey: customFieldsSchema,
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "site_id", "role_id", "cidr", "tag"},
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag"},
Description: "Description to include in the data source filter.",
},
"family": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "site_id", "role_id", "cidr", "tag"},
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag"},
ValidateFunc: validation.IntInSlice([]int{4, 6}),
Description: "The IP family of the prefix. One of 4 or 6",
},
"role_id": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "site_id", "role_id", "cidr", "tag"},
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag"},
},
"prefix": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsCIDR,
ConflictsWith: []string{"cidr"},
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "site_id", "role_id", "cidr", "tag"},
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag"},
},
"vlan_vid": {
Type: schema.TypeFloat,
Optional: true,
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "site_id", "role_id", "cidr", "tag"},
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag"},
ValidateFunc: validation.FloatBetween(1, 4094),
},
"vrf_id": {
Type: schema.TypeInt,
Optional: true,
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "site_id", "role_id", "cidr", "tag"},
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag"},
},
"vlan_id": {
Type: schema.TypeInt,
Optional: true,
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "site_id", "role_id", "cidr", "tag"},
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag"},
},
"tenant_id": {
Type: schema.TypeInt,
Optional: true,
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag"},
},
"site_id": {
Type: schema.TypeInt,
Optional: true,
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "site_id", "role_id", "cidr", "tag"},
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag"},
},
"tag": {
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "site_id", "role_id", "cidr", "tag"},
AtLeastOneOf: []string{"description", "family", "prefix", "vlan_vid", "vrf_id", "vlan_id", "tenant_id", "site_id", "role_id", "cidr", "tag"},
Description: "Tag to include in the data source filter (must match the tag's slug).",
},
"tag__n": {
Expand Down Expand Up @@ -143,6 +148,11 @@ func dataSourceNetboxPrefixRead(d *schema.ResourceData, m interface{}) error {
params.VlanVid = &vlanVid
}

if tenantID, ok := d.Get("tenant_id").(int); ok && tenantID != 0 {
// Note that tenant_id is a string pointer in the netbox filter, but we use a number in the provider
params.TenantID = strToPtr(strconv.Itoa(tenantID))
}

if siteID, ok := d.Get("site_id").(int); ok && siteID != 0 {
// Note that site_id is a string pointer in the netbox filter, but we use a number in the provider
params.SiteID = strToPtr(strconv.Itoa(siteID))
Expand Down Expand Up @@ -190,6 +200,9 @@ func dataSourceNetboxPrefixRead(d *schema.ResourceData, m interface{}) error {
d.Set("vlan_vid", result.Vlan.Vid)
d.Set("vlan_id", result.Vlan.ID)
}
if result.Tenant != nil {
d.Set("tenant_id", result.Tenant.ID)
}
if result.Site != nil {
d.Set("site_id", result.Site.ID)
}
Expand Down
61 changes: 37 additions & 24 deletions netbox/data_source_netbox_prefix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,36 @@ resource "netbox_vlan" "test" {
vid = %[4]d
}

resource "netbox_tenant" "test" {
name = "%[1]s_tenant"
}

resource "netbox_site" "test" {
name = "%[1]s_site"
}

resource "netbox_ipam_role" "test" {
name = "%[1]s_role"
name = "%[1]s_role"
}

resource "netbox_prefix" "testv4" {
prefix = "%[2]s"
status = "active"
vrf_id = netbox_vrf.test.id
vlan_id = netbox_vlan.test.id
site_id = netbox_site.test.id
role_id = netbox_ipam_role.test.id
prefix = "%[2]s"
status = "active"
vrf_id = netbox_vrf.test.id
vlan_id = netbox_vlan.test.id
tenant_id = netbox_tenant.test.id
site_id = netbox_site.test.id
role_id = netbox_ipam_role.test.id
description = "%[1]s_description_test_idv4"
}

resource "netbox_prefix" "testv6" {
prefix = "%[3]s"
status = "active"
vrf_id = netbox_vrf.test.id
vlan_id = netbox_vlan.test.id
site_id = netbox_site.test.id
prefix = "%[3]s"
status = "active"
vrf_id = netbox_vrf.test.id
vlan_id = netbox_vlan.test.id
tenant_id = netbox_tenant.test.id
site_id = netbox_site.test.id
description = "%[1]s_description_test_idv6"
}

Expand All @@ -61,46 +67,52 @@ data "netbox_prefix" "by_description" {

data "netbox_prefix" "by_cidr" {
depends_on = [netbox_prefix.testv4]
cidr = "%[2]s"
cidr = "%[2]s"
}

data "netbox_prefix" "by_vrf_id" {
depends_on = [netbox_prefix.testv4]
vrf_id = netbox_vrf.test.id
family = 4
vrf_id = netbox_vrf.test.id
family = 4
}

data "netbox_prefix" "by_vlan_id" {
depends_on = [netbox_prefix.testv4]
vlan_id = netbox_vlan.test.id
family = 4
vlan_id = netbox_vlan.test.id
family = 4
}

data "netbox_prefix" "by_vlan_vid" {
depends_on = [netbox_prefix.testv4]
vlan_vid = %[4]d
family = 4
vlan_vid = %[4]d
family = 4
}

data "netbox_prefix" "by_prefix" {
depends_on = [netbox_prefix.testv4]
prefix = "%[2]s"
prefix = "%[2]s"
}

data "netbox_prefix" "by_tenant_id" {
depends_on = [netbox_prefix.testv4]
tenant_id = netbox_tenant.test.id
family = 4
}

data "netbox_prefix" "by_site_id" {
depends_on = [netbox_prefix.testv4]
site_id = netbox_site.test.id
family = 4
site_id = netbox_site.test.id
family = 4
}

data "netbox_prefix" "by_role_id" {
depends_on = [netbox_prefix.testv4]
role_id = netbox_ipam_role.test.id
role_id = netbox_ipam_role.test.id
}

data "netbox_prefix" "by_family" {
depends_on = [netbox_prefix.testv6]
family = 6
family = 6
}

`, testName, testv4Prefix, testv6Prefix, testVlanVid),
Expand All @@ -111,6 +123,7 @@ data "netbox_prefix" "by_family" {
resource.TestCheckResourceAttrPair("data.netbox_prefix.by_vrf_id", "id", "netbox_prefix.testv4", "id"),
resource.TestCheckResourceAttrPair("data.netbox_prefix.by_vlan_id", "id", "netbox_prefix.testv4", "id"),
resource.TestCheckResourceAttrPair("data.netbox_prefix.by_vlan_vid", "id", "netbox_prefix.testv4", "id"),
resource.TestCheckResourceAttrPair("data.netbox_prefix.by_tenant_id", "id", "netbox_prefix.testv4", "id"),
resource.TestCheckResourceAttrPair("data.netbox_prefix.by_site_id", "id", "netbox_prefix.testv4", "id"),
resource.TestCheckResourceAttrPair("data.netbox_prefix.by_role_id", "id", "netbox_prefix.testv4", "id"),
resource.TestCheckResourceAttrPair("data.netbox_prefix.by_family", "id", "netbox_prefix.testv6", "id"),
Expand Down
11 changes: 10 additions & 1 deletion netbox/data_source_netbox_prefixes.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func dataSourceNetboxPrefixes() *schema.Resource {
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the field to filter on. Supported fields are: `prefix`, `contains`, `vlan_vid`, `vrf_id`, `vlan_id`, `status`, `site_id`, & `tag`.",
Description: "The name of the field to filter on. Supported fields are: `prefix`, `contains`, `vlan_vid`, `vrf_id`, `vlan_id`, `status`, `tenant_id`, `site_id`, & `tag`.",
},
"value": {
Type: schema.TypeString,
Expand Down Expand Up @@ -59,6 +59,10 @@ func dataSourceNetboxPrefixes() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tenant_id": {
Type: schema.TypeInt,
Computed: true,
},
"site_id": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -119,6 +123,8 @@ func dataSourceNetboxPrefixesRead(d *schema.ResourceData, m interface{}) error {
params.VlanID = &vString
case "status":
params.Status = &vString
case "tenant_id":
params.TenantID = &vString
case "site_id":
params.SiteID = &vString
case "tag":
Expand Down Expand Up @@ -150,6 +156,9 @@ func dataSourceNetboxPrefixesRead(d *schema.ResourceData, m interface{}) error {
if v.Vrf != nil {
mapping["vrf_id"] = v.Vrf.ID
}
if v.Tenant != nil {
mapping["tenant_id"] = v.Tenant.ID
}
if v.Site != nil {
mapping["site_id"] = v.Site.ID
}
Expand Down
Loading