Skip to content

Commit

Permalink
feat: Add netbox_ipam_prefix data
Browse files Browse the repository at this point in the history
  • Loading branch information
smutel committed Sep 20, 2023
1 parent d8f0455 commit 42a3222
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/data-sources/netbox_ipam_prefix/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "netbox_ipam_prefix" "prefix_test" {
prefix = "192.168.56.0/24"
}
64 changes: 64 additions & 0 deletions netbox/ipam/data_netbox_ipam_prefix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ipam

import (
"context"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
netboxclient "github.com/smutel/go-netbox/v3/netbox/client"
"github.com/smutel/go-netbox/v3/netbox/client/ipam"
"github.com/smutel/terraform-provider-netbox/v7/netbox/internal/util"
)

func DataNetboxIpamPrefix() *schema.Resource {
return &schema.Resource{
Description: "Get info about prefix (ipam module) from netbox.",
ReadContext: dataNetboxIpamPrefixRead,

Schema: map[string]*schema.Schema{
"content_type": {
Type: schema.TypeString,
Computed: true,
Description: "The content type of this prefix (ipam module).",
},
"prefix": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.IsCIDRNetwork(0, 256),
Description: "The prefix (IP address/mask) used for this prefix (ipam module).",
},
},
}
}

func dataNetboxIpamPrefixRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*netboxclient.NetBoxAPI)

prefix := d.Get("prefix").(string)

p := ipam.NewIpamPrefixesListParams().WithPrefix(&prefix)

list, err := client.Ipam.IpamPrefixesList(p, nil)
if err != nil {
return diag.FromErr(err)
}

if *list.Payload.Count < 1 {
return diag.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
} else if *list.Payload.Count > 1 {
return diag.Errorf("Your query returned more than one result. " +
"Please try a more specific search criteria.")
}

r := list.Payload.Results[0]
d.SetId(strconv.FormatInt(r.ID, 10))
if err = d.Set("content_type", util.ConvertURIContentType(r.URL)); err != nil {
return diag.FromErr(err)
}

return nil
}
1 change: 1 addition & 0 deletions netbox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func Provider() *schema.Provider {
"netbox_ipam_asn": ipam.DataNetboxIpamAsn(),
"netbox_ipam_ip_addresses": ipam.DataNetboxIpamIPAddresses(),
"netbox_ipam_ip_range": ipam.DataNetboxIpamIPRange(),
"netbox_ipam_prefix": ipam.DataNetboxIpamPrefix(),
"netbox_ipam_role": ipam.DataNetboxIpamRole(),
"netbox_ipam_route_targets": ipam.DataNetboxIpamRouteTargets(),
"netbox_ipam_service": ipam.DataNetboxIpamService(),
Expand Down

0 comments on commit 42a3222

Please sign in to comment.