Skip to content

Commit

Permalink
Add tag attributes for data_source_netbox_prefix (#284)
Browse files Browse the repository at this point in the history
* Add tag attributes for data_source_netbox_prefix

* change tag fitler attribute names
  • Loading branch information
kyle-burnett authored Nov 7, 2022
1 parent b364e39 commit 5640648
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
15 changes: 10 additions & 5 deletions docs/data-sources/prefix.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@ description: |-

### Optional

- `cidr` (String, Deprecated) At least one of `prefix`, `vlan_vid`, `vrf_id`, `vlan_id` or `cidr` must be given. Conflicts with `prefix`.
- `prefix` (String) At least one of `prefix`, `vlan_vid`, `vrf_id`, `vlan_id` or `cidr` must be given. Conflicts with `cidr`.
- `vlan_id` (Number) At least one of `prefix`, `vlan_vid`, `vrf_id`, `vlan_id` or `cidr` must be given.
- `vlan_vid` (Number) At least one of `prefix`, `vlan_vid`, `vrf_id`, `vlan_id` or `cidr` must be given.
- `vrf_id` (Number) At least one of `prefix`, `vlan_vid`, `vrf_id`, `vlan_id` or `cidr` must be given.
- `cidr` (String, Deprecated) At least one of `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `cidr` or `tag` must be given. Conflicts with `prefix`.
- `prefix` (String) At least one of `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `cidr` or `tag` must be given. Conflicts with `cidr`.
- `tag` (String) Tag to include in the data source filter (must match the tag's slug). At least one of `prefix`, `vlan_vid`, `vrf_id`, `vlan_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 `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `cidr` or `tag` must be given.
- `vlan_vid` (Number) At least one of `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `cidr` or `tag` must be given.
- `vrf_id` (Number) At least one of `prefix`, `vlan_vid`, `vrf_id`, `vlan_id`, `cidr` or `tag` must be given.

### Read-Only

- `description` (String)
- `id` (Number) The ID of this resource.
- `status` (String)
- `tags` (Set of String)


31 changes: 26 additions & 5 deletions netbox/data_source_netbox_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,43 @@ 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{"prefix", "vlan_vid", "vrf_id", "vlan_id", "cidr"},
AtLeastOneOf: []string{"prefix", "vlan_vid", "vrf_id", "vlan_id", "cidr", "tag"},
},
"prefix": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsCIDR,
ConflictsWith: []string{"cidr"},
AtLeastOneOf: []string{"prefix", "vlan_vid", "vrf_id", "vlan_id", "cidr"},
AtLeastOneOf: []string{"prefix", "vlan_vid", "vrf_id", "vlan_id", "cidr", "tag"},
},
"vlan_vid": {
Type: schema.TypeFloat,
Optional: true,
AtLeastOneOf: []string{"prefix", "vlan_vid", "vrf_id", "vlan_id", "cidr"},
AtLeastOneOf: []string{"prefix", "vlan_vid", "vrf_id", "vlan_id", "cidr", "tag"},
ValidateFunc: validation.FloatBetween(1, 4094),
},
"vrf_id": {
Type: schema.TypeInt,
Optional: true,
AtLeastOneOf: []string{"prefix", "vlan_vid", "vrf_id", "vlan_id", "cidr"},
AtLeastOneOf: []string{"prefix", "vlan_vid", "vrf_id", "vlan_id", "cidr", "tag"},
},
"vlan_id": {
Type: schema.TypeInt,
Optional: true,
AtLeastOneOf: []string{"prefix", "vlan_vid", "vrf_id", "vlan_id", "cidr"},
AtLeastOneOf: []string{"prefix", "vlan_vid", "vrf_id", "vlan_id", "cidr", "tag"},
},
"tag": {
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: []string{"prefix", "vlan_vid", "vrf_id", "vlan_id", "cidr", "tag"},
Description: "Tag to include in the data source filter (must match the tag's slug).",
},
"tag__n": {
Type: schema.TypeString,
Optional: true,
Description: `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.`,
},
"status": {
Type: schema.TypeString,
Expand All @@ -58,6 +71,7 @@ func dataSourceNetboxPrefix() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchemaRead,
},
}
}
Expand Down Expand Up @@ -92,6 +106,12 @@ func dataSourceNetboxPrefixRead(d *schema.ResourceData, m interface{}) error {
if vlanVid, ok := d.Get("vlan_vid").(float64); ok && vlanVid != 0 {
params.VlanVid = &vlanVid
}
if tag, ok := d.Get("tag").(string); ok && tag != "" {
params.Tag = &tag
}
if tagn, ok := d.Get("tag__n").(string); ok && tagn != "" {
params.Tagn = &tagn
}

res, err := api.Ipam.IpamPrefixesList(params, nil)
if err != nil {
Expand All @@ -108,6 +128,7 @@ func dataSourceNetboxPrefixRead(d *schema.ResourceData, m interface{}) error {
d.Set("prefix", result.Prefix)
d.Set("status", result.Status.Value)
d.Set("description", result.Description)
d.Set("tags", getTagListFromNestedTagList(result.Tags))

if result.Vrf != nil {
d.Set("vrf_id", result.Vrf.ID)
Expand Down

0 comments on commit 5640648

Please sign in to comment.