Skip to content

Commit

Permalink
Merge pull request #35 from smutel/AddAggregate
Browse files Browse the repository at this point in the history
feat: Add resource and data for IPAM aggregate
  • Loading branch information
smutel authored Jan 1, 2021
2 parents 540f901 + f33b8d9 commit f2fd13a
Show file tree
Hide file tree
Showing 7 changed files with 483 additions and 19 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,6 @@ $ terraform init & terraform apply
```
## Known bugs in external project which can impact this provider

* [Issue 85 in project go-netbox](https://github.com/netbox-community/go-netbox/issues/85)
* [Issue 54 in project go-netbox](https://github.com/netbox-community/go-netbox/issues/54)
* *Closed* - [Issue 85 in project go-netbox](https://github.com/netbox-community/go-netbox/issues/85)
* *Closed* - [Issue 54 in project go-netbox](https://github.com/netbox-community/go-netbox/issues/54)
* *Open* - [Issue 115 in project go-netbox](https://github.com/netbox-community/go-netbox/issues/115)
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.
51 changes: 51 additions & 0 deletions docs/resources/ipam_aggregate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# netbox\_ipam\_aggregate Resource

Manage an aggregate within Netbox.

## Example Usage

```hcl
resource "netbox_ipam_aggregate" "aggregate_test" {
prefix = "192.168.56.0/24"
rir_id = 1
date_created = "2020-12-21"
tag {
name = "tag1"
slug = "tag1"
}
custom_fields = {
cf_boolean = "true"
cf_date = "2020-12-25"
cf_integer = "10"
cf_selection = "1"
cf_text = "Some text"
cf_url = "https://github.com"
}
}
```

## Argument Reference

The following arguments are supported:
* ``custom_fields`` - (Optional) Custom Field Keys and Values for this object
* For boolean, use the string value "true" or "false"
* For data, use the string format "YYYY-MM-DD"
* For integer, use the value between double quote "10"
* For selection, use the level id
* For text, use the string value
* For URL, use the URL as string
* ``date_added`` - (Optional) Date when this aggregate was added. Format *YYYY-MM-DD*.
* ``description`` - (Optional) The description of this object.
* ``prefix`` - (Required) The prefix (with mask) used for this object.
* ``rir_id`` - (Required) The RIR id linked to this object.

The ``tag`` block supports:
* ``name`` - (Required) Name of the existing tag to associate with this resource.
* ``slug`` - (Required) Slug of the existing tag to associate with this resource.

## Attributes Reference

In addition to the above arguments, the following attributes are exported:
* ``id`` - The id (ref in Netbox) of this object.
56 changes: 41 additions & 15 deletions examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ resource "netbox_tenancy_tenant" "tenant_test" {
}

custom_fields = {
cf_boolean = "true"
cf_date = "2020-12-25"
cf_integer = "10"
cf_boolean = "true"
cf_date = "2020-12-25"
cf_integer = "10"
cf_selection = "1"
cf_text = "Some text"
cf_url = "https://github.com"
cf_text = "Some text"
cf_url = "https://github.com"
}
}

Expand Down Expand Up @@ -67,12 +67,12 @@ resource "netbox_ipam_vlan" "vlan_test" {
}

custom_fields = {
cf_boolean = "true"
cf_date = "2020-12-25"
cf_integer = "10"
cf_boolean = "true"
cf_date = "2020-12-25"
cf_integer = "10"
cf_selection = "1"
cf_text = "Some text"
cf_url = "https://github.com"
cf_text = "Some text"
cf_url = "https://github.com"
}
}

Expand Down Expand Up @@ -136,12 +136,12 @@ resource "netbox_virtualization_vm" "vm_test" {
}

custom_fields = {
cf_boolean = "true"
cf_date = "2020-12-25"
cf_integer = "10"
cf_boolean = "true"
cf_date = "2020-12-25"
cf_integer = "10"
cf_selection = "1"
cf_text = "Some text"
cf_url = "https://github.com"
cf_text = "Some text"
cf_url = "https://github.com"
}
}

Expand All @@ -151,3 +151,29 @@ resource "netbox_virtualization_interface" "interface_test" {
mac_address = "AA:AA:AA:AA:AA:AA"
description = "Interface de test"
}

resource "netbox_ipam_aggregate" "aggregate_test" {
prefix = "192.167.0.0/24"
rir_id = 1
date_added = "2020-12-21"
description = "Aggregate created by terraform"

tag {
name = "tag1"
slug = "tag1"
}

tag {
name = "tag2"
slug = "tag2"
}

custom_fields = {
cf_boolean = "true"
cf_date = "2020-12-25"
cf_integer = "10"
cf_selection = "1"
cf_text = "Some text"
cf_url = "https://github.com"
}
}
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
}
6 changes: 4 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 @@ -47,8 +48,9 @@ func Provider() *schema.Provider {
"netbox_virtualization_cluster": dataNetboxVirtualizationCluster(),
},
ResourcesMap: map[string]*schema.Resource{
"netbox_ipam_prefix": resourceNetboxIpamPrefix(),
"netbox_ipam_aggregate": resourceNetboxIpamAggregate(),
"netbox_ipam_ip_addresses": resourceNetboxIpamIPAddresses(),
"netbox_ipam_prefix": resourceNetboxIpamPrefix(),
"netbox_ipam_vlan": resourceNetboxIpamVlan(),
"netbox_ipam_vlan_group": resourceNetboxIpamVlanGroup(),
"netbox_tenancy_tenant": resourceNetboxTenancyTenant(),
Expand Down
Loading

0 comments on commit f2fd13a

Please sign in to comment.