diff --git a/examples/data-sources/netbox_ipam_rir/data-source.tf b/examples/data-sources/netbox_ipam_rir/data-source.tf new file mode 100644 index 000000000..716e2effe --- /dev/null +++ b/examples/data-sources/netbox_ipam_rir/data-source.tf @@ -0,0 +1,3 @@ +data "netbox_ipam_rir" "rir_test" { + slug = "test-rir" +} diff --git a/netbox/ipam/data_netbox_ipam_rir.go b/netbox/ipam/data_netbox_ipam_rir.go new file mode 100644 index 000000000..7c1aaee68 --- /dev/null +++ b/netbox/ipam/data_netbox_ipam_rir.go @@ -0,0 +1,63 @@ +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 DataNetboxIpamRir() *schema.Resource { + return &schema.Resource{ + Description: "Get info about prefix (ipam module) from netbox.", + ReadContext: dataNetboxIpamRirRead, + + Schema: map[string]*schema.Schema{ + "content_type": { + Type: schema.TypeString, + Computed: true, + Description: "The content type of this rir (ipam module).", + }, + "slug": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringLenBetween(1, 100), + Description: "The slug of this rir (ipam module).", + }, + }, + } +} + +func dataNetboxIpamRirRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + client := m.(*netboxclient.NetBoxAPI) + + slug := d.Get("slug").(string) + + p := ipam.NewIpamRirsListParams().WithSlug(&slug) + + list, err := client.Ipam.IpamRirsList(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 80a106f26..6c31dda7b 100644 --- a/netbox/provider.go +++ b/netbox/provider.go @@ -163,6 +163,7 @@ func Provider() *schema.Provider { "netbox_ipam_ip_addresses": ipam.DataNetboxIpamIPAddresses(), "netbox_ipam_ip_range": ipam.DataNetboxIpamIPRange(), "netbox_ipam_prefix": ipam.DataNetboxIpamPrefix(), + "netbox_ipam_rir": ipam.DataNetboxIpamRir(), "netbox_ipam_role": ipam.DataNetboxIpamRole(), "netbox_ipam_route_targets": ipam.DataNetboxIpamRouteTargets(), "netbox_ipam_service": ipam.DataNetboxIpamService(),