Skip to content

Commit

Permalink
feat: Add data source IPAM aggregate
Browse files Browse the repository at this point in the history
  • Loading branch information
smutel committed Dec 21, 2020
1 parent cdaa29f commit f33b8d9
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
23 changes: 23 additions & 0 deletions docs/data-sources/ipam_aggregate.md
Original file line number Diff line number Diff line change
@@ -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.
56 changes: 56 additions & 0 deletions netbox/data_netbox_ipam_aggregate.go
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 3 additions & 2 deletions netbox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down

0 comments on commit f33b8d9

Please sign in to comment.