From f33b8d9908b0639918632203fe878ee092fa333d Mon Sep 17 00:00:00 2001 From: smutel Date: Mon, 21 Dec 2020 20:59:41 +0100 Subject: [PATCH] feat: Add data source IPAM aggregate --- docs/data-sources/ipam_aggregate.md | 23 ++++++++++++ netbox/data_netbox_ipam_aggregate.go | 56 ++++++++++++++++++++++++++++ netbox/provider.go | 5 ++- 3 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 docs/data-sources/ipam_aggregate.md create mode 100644 netbox/data_netbox_ipam_aggregate.go diff --git a/docs/data-sources/ipam_aggregate.md b/docs/data-sources/ipam_aggregate.md new file mode 100644 index 000000000..f3d85a374 --- /dev/null +++ b/docs/data-sources/ipam_aggregate.md @@ -0,0 +1,23 @@ +# netbox\_ipam\_aggregate Data Source + +Get info about aggregate from Netbox. + +## Example Usage + +```hcl +data "netbox_ipam_aggregate" "aggregate_test" { + prefix = "192.168.56.0/24" + rir_id = 1 +} +``` + +## Argument Reference + +The following arguments are supported: +* ``prefix`` - (Required) The prefix (with mask) used for this object. +* ``rir_id`` - (Required) The RIR id linked to this object. + +## Attributes Reference + +In addition to the above arguments, the following attributes are exported: +* ``id`` - The id (ref in Netbox) of this object. diff --git a/netbox/data_netbox_ipam_aggregate.go b/netbox/data_netbox_ipam_aggregate.go new file mode 100644 index 000000000..dfe6906c8 --- /dev/null +++ b/netbox/data_netbox_ipam_aggregate.go @@ -0,0 +1,56 @@ +package netbox + +import ( + "fmt" + "strconv" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" + netboxclient "github.com/netbox-community/go-netbox/netbox/client" + "github.com/netbox-community/go-netbox/netbox/client/ipam" +) + +func dataNetboxIpamAggregate() *schema.Resource { + return &schema.Resource{ + Read: dataNetboxIpamAggregateRead, + + Schema: map[string]*schema.Schema{ + "prefix": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.IsCIDRNetwork(0, 256), + }, + "rir_id": { + Type: schema.TypeInt, + Required: true, + }, + }, + } +} + +func dataNetboxIpamAggregateRead(d *schema.ResourceData, + m interface{}) error { + client := m.(*netboxclient.NetBoxAPI) + + prefix := d.Get("prefix").(string) + rirID := d.Get("rir_id").(string) + + p := ipam.NewIpamAggregatesListParams().WithPrefix(&prefix).WithRirID(&rirID) + + list, err := client.Ipam.IpamAggregatesList(p, nil) + if err != nil { + return err + } + + if *list.Payload.Count < 1 { + return fmt.Errorf("Your query returned no results. " + + "Please change your search criteria and try again.") + } else if *list.Payload.Count > 1 { + return fmt.Errorf("Your query returned more than one result. " + + "Please try a more specific search criteria.") + } + + d.SetId(strconv.FormatInt(list.Payload.Results[0].ID, 10)) + + return nil +} diff --git a/netbox/provider.go b/netbox/provider.go index c4e79a447..9ef94f9e1 100644 --- a/netbox/provider.go +++ b/netbox/provider.go @@ -36,8 +36,9 @@ func Provider() *schema.Provider { }, }, DataSourcesMap: map[string]*schema.Resource{ - "netbox_dcim_site": dataNetboxDcimSite(), "netbox_dcim_platform": dataNetboxDcimPlatform(), + "netbox_dcim_site": dataNetboxDcimSite(), + "netbox_ipam_aggregate": dataNetboxIpamAggregate(), "netbox_ipam_ip_addresses": dataNetboxIpamIPAddresses(), "netbox_ipam_role": dataNetboxIpamRole(), "netbox_ipam_vlan": dataNetboxIpamVlan(), @@ -48,8 +49,8 @@ func Provider() *schema.Provider { }, ResourcesMap: map[string]*schema.Resource{ "netbox_ipam_aggregate": resourceNetboxIpamAggregate(), - "netbox_ipam_prefix": resourceNetboxIpamPrefix(), "netbox_ipam_ip_addresses": resourceNetboxIpamIPAddresses(), + "netbox_ipam_prefix": resourceNetboxIpamPrefix(), "netbox_ipam_vlan": resourceNetboxIpamVlan(), "netbox_ipam_vlan_group": resourceNetboxIpamVlanGroup(), "netbox_tenancy_tenant": resourceNetboxTenancyTenant(),