Skip to content

Commit

Permalink
feat: Add code to manage IP addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
smutel committed Jun 25, 2020
1 parent 1291bb7 commit 9db82c0
Show file tree
Hide file tree
Showing 4 changed files with 447 additions and 2 deletions.
6 changes: 6 additions & 0 deletions examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ resource "netbox_ipam_prefix" "prefix_test" {
tags = ["tag1"]
status = "container"
}

resource "netbox_ipam_ip_addresses" "ip_test" {
address = "192.168.56.1/24"
status = "active"
tenant_id = netbox_tenancy_tenant.tenant_test.id
}
51 changes: 51 additions & 0 deletions netbox/data_netbox_ipam_ip_addresses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package netbox

import (
"regexp"
"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"
pkgerrors "github.com/pkg/errors"
)

func dataNetboxIpamIPAddresses() *schema.Resource {
return &schema.Resource{
Read: dataNetboxIpamIPAddressesRead,

Schema: map[string]*schema.Schema{
"address": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/"+
"[0-9]{1,2}$"), "Must be like 192.168.56.1/24"),
},
},
}
}

func dataNetboxIpamIPAddressesRead(d *schema.ResourceData,
m interface{}) error {
client := m.(*netboxclient.NetBox)

address := d.Get("address").(string)

p := ipam.NewIpamIPAddressesListParams().WithAddress(&address)

list, err := client.Ipam.IpamIPAddressesList(p, nil)
if err != nil {
return err
}

if *list.Payload.Count == 1 {
d.SetId(strconv.FormatInt(list.Payload.Results[0].ID, 10))
} else {
return pkgerrors.New("Data results for netbox_ipam_ip_addresses returns 0 or " +
"more than one result.")
}

return nil
}
6 changes: 4 additions & 2 deletions netbox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ func Provider() *schema.Provider {
},
DataSourcesMap: map[string]*schema.Resource{
"netbox_dcim_site": dataNetboxDcimSite(),
"netbox_ipam_ip_addresses": dataNetboxIpamIPAddresses(),
"netbox_ipam_role": dataNetboxIpamRole(),
"netbox_ipam_vlan_group": dataNetboxIpamVlanGroup(),
"netbox_ipam_vlan": dataNetboxIpamVlan(),
"netbox_tenancy_tenant_group": dataNetboxTenancyTenantGroup(),
"netbox_ipam_vlan_group": dataNetboxIpamVlanGroup(),
"netbox_tenancy_tenant": dataNetboxTenancyTenant(),
"netbox_tenancy_tenant_group": dataNetboxTenancyTenantGroup(),
},
ResourcesMap: map[string]*schema.Resource{
"netbox_ipam_prefix": resourceNetboxIpamPrefix(),
"netbox_ipam_ip_addresses": resourceNetboxIpamIPAddresses(),
"netbox_ipam_vlan": resourceNetboxIpamVlan(),
"netbox_ipam_vlan_group": resourceNetboxIpamVlanGroup(),
"netbox_tenancy_tenant": resourceNetboxTenancyTenant(),
Expand Down
Loading

0 comments on commit 9db82c0

Please sign in to comment.