From d8f0455e9cc7ec2e1c1db017f3a80aa492152320 Mon Sep 17 00:00:00 2001 From: Samuel Mutel <12967891+smutel@users.noreply.github.com> Date: Mon, 4 Sep 2023 11:54:12 +0200 Subject: [PATCH] feat: Add netbox_ipam_ip_range data --- .../netbox_ipam_ip_range/data-source.tf | 4 ++ netbox/ipam/data_netbox_ipam_ip_range.go | 71 +++++++++++++++++++ netbox/provider.go | 1 + 3 files changed, 76 insertions(+) create mode 100644 examples/data-sources/netbox_ipam_ip_range/data-source.tf create mode 100644 netbox/ipam/data_netbox_ipam_ip_range.go diff --git a/examples/data-sources/netbox_ipam_ip_range/data-source.tf b/examples/data-sources/netbox_ipam_ip_range/data-source.tf new file mode 100644 index 000000000..f5065cf57 --- /dev/null +++ b/examples/data-sources/netbox_ipam_ip_range/data-source.tf @@ -0,0 +1,4 @@ +data "netbox_ipam_ip_range" "iprange_test" { + start_address = "192.168.56.1/24" + end_address = "192.168.56.254/24" +} diff --git a/netbox/ipam/data_netbox_ipam_ip_range.go b/netbox/ipam/data_netbox_ipam_ip_range.go new file mode 100644 index 000000000..8267ad8b8 --- /dev/null +++ b/netbox/ipam/data_netbox_ipam_ip_range.go @@ -0,0 +1,71 @@ +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 DataNetboxIpamIPRange() *schema.Resource { + return &schema.Resource{ + Description: "Get info about IP addresses (ipam module) from netbox.", + ReadContext: dataNetboxIpamIPRangeRead, + + Schema: map[string]*schema.Schema{ + "content_type": { + Type: schema.TypeString, + Computed: true, + Description: "The content type of this IP range (ipam module).", + }, + "start_address": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.IsCIDR, + Description: "The first address of this IP range", + }, + "end_address": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.IsCIDR, + Description: "The last address of this IP range (ipam module)", + }, + }, + } +} + +func dataNetboxIpamIPRangeRead(ctx context.Context, d *schema.ResourceData, + m interface{}) diag.Diagnostics { + client := m.(*netboxclient.NetBoxAPI) + + startAddress := d.Get("start_address").(string) + endAddress := d.Get("end_address").(string) + + p := ipam.NewIpamIPRangesListParams().WithStartAddress(&startAddress).WithEndAddress(&endAddress) + + list, err := client.Ipam.IpamIPRangesList(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 +} diff --git a/netbox/provider.go b/netbox/provider.go index cb3766221..3a00ec1c6 100644 --- a/netbox/provider.go +++ b/netbox/provider.go @@ -161,6 +161,7 @@ func Provider() *schema.Provider { "netbox_ipam_aggregate": ipam.DataNetboxIpamAggregate(), "netbox_ipam_asn": ipam.DataNetboxIpamAsn(), "netbox_ipam_ip_addresses": ipam.DataNetboxIpamIPAddresses(), + "netbox_ipam_ip_range": ipam.DataNetboxIpamIPRange(), "netbox_ipam_role": ipam.DataNetboxIpamRole(), "netbox_ipam_route_targets": ipam.DataNetboxIpamRouteTargets(), "netbox_ipam_service": ipam.DataNetboxIpamService(),