diff --git a/examples/data-sources/netbox_ipam_asn/data-source.tf b/examples/data-sources/netbox_ipam_asn/data-source.tf new file mode 100644 index 000000000..f2debe25e --- /dev/null +++ b/examples/data-sources/netbox_ipam_asn/data-source.tf @@ -0,0 +1,4 @@ +data "netbox_ipam_asn" "asn_test" { + asn = 15 + rir_id = 15 +} diff --git a/netbox/ipam/data_netbox_ipam_asn.go b/netbox/ipam/data_netbox_ipam_asn.go new file mode 100644 index 000000000..e98b3d8f3 --- /dev/null +++ b/netbox/ipam/data_netbox_ipam_asn.go @@ -0,0 +1,72 @@ +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 DataNetboxIpamAsn() *schema.Resource { + return &schema.Resource{ + Description: "Get info about aggregate (ipam module) from Netbox.", + ReadContext: dataNetboxIpamAsnRead, + + Schema: map[string]*schema.Schema{ + "asn": { + Type: schema.TypeInt, + Required: true, + ValidateFunc: validation.IntAtLeast(1), + Description: "The asn number of this asn (ipam module).", + }, + "content_type": { + Type: schema.TypeString, + Computed: true, + Description: "The content type of this aggregate (ipam module).", + }, + "rir_id": { + Type: schema.TypeInt, + Required: true, + Description: "The rir for this asn (ipam module).", + }, + }, + } +} + +func dataNetboxIpamAsnRead(ctx context.Context, d *schema.ResourceData, + m interface{}) diag.Diagnostics { + client := m.(*netboxclient.NetBoxAPI) + + asn := d.Get("asn").(int) + asnStr := strconv.Itoa(asn) + rirID := d.Get("rir_id").(int) + rirIDStr := strconv.Itoa(rirID) + + p := ipam.NewIpamAsnsListParams().WithAsn(&asnStr).WithRirID(&rirIDStr) + + list, err := client.Ipam.IpamAsnsList(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 e3dfeafae..cb3766221 100644 --- a/netbox/provider.go +++ b/netbox/provider.go @@ -159,6 +159,7 @@ func Provider() *schema.Provider { "netbox_dcim_region": dcim.DataNetboxDcimRegion(), "netbox_dcim_site": dcim.DataNetboxDcimSite(), "netbox_ipam_aggregate": ipam.DataNetboxIpamAggregate(), + "netbox_ipam_asn": ipam.DataNetboxIpamAsn(), "netbox_ipam_ip_addresses": ipam.DataNetboxIpamIPAddresses(), "netbox_ipam_role": ipam.DataNetboxIpamRole(), "netbox_ipam_route_targets": ipam.DataNetboxIpamRouteTargets(),