Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add VRF data source and support for vrf_id in netbox_ip_address res… #26

Merged
merged 2 commits into from Dec 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions netbox/data_source_netbox_vrf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package netbox

import (
"errors"
"github.com/fbreckle/go-netbox/netbox/client"
"github.com/fbreckle/go-netbox/netbox/client/ipam"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"strconv"
)

func dataSourceNetboxVrf() *schema.Resource {
return &schema.Resource{
Read: dataSourceNetboxVrfRead,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}

func dataSourceNetboxVrfRead(d *schema.ResourceData, m interface{}) error {
api := m.(*client.NetBoxAPI)

name := d.Get("name").(string)
params := ipam.NewIpamVrfsListParams()
params.Name = &name
limit := int64(2) // Limit of 2 is enough
params.Limit = &limit

res, err := api.Ipam.IpamVrfsList(params, nil)
if err != nil {
return err
}

if *res.GetPayload().Count > int64(1) {
return errors.New("More than one result. Specify a more narrow filter")
}
if *res.GetPayload().Count == int64(0) {
return errors.New("No result")
}
result := res.GetPayload().Results[0]
d.SetId(strconv.FormatInt(result.ID, 10))
d.Set("name", result.Name)
return nil
}
33 changes: 33 additions & 0 deletions netbox/data_source_netbox_vrf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package netbox

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"testing"
)

func TestAccNetboxVrfDataSource_basic(t *testing.T) {

testSlug := "tnt_ds_basic"
testName := testAccGetTestName(testSlug)
resource.ParallelTest(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource"netbox_vrf" "test" {
name = "%[1]s"
}

data "netbox_vrf" "test" {
depends_on = [netbox_vrf.test]
name = "%[1]s"
}`, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair("data.netbox_vrf.test", "id", "netbox_vrf.test", "id"),
),
ExpectNonEmptyPlan: true,
},
},
})
}
2 changes: 2 additions & 0 deletions netbox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func Provider() *schema.Provider {
"netbox_cluster_type": resourceNetboxClusterType(),
"netbox_cluster": resourceNetboxCluster(),
"netbox_tenant": resourceNetboxTenant(),
"netbox_vrf": resourceNetboxVrf(),
"netbox_ip_address": resourceNetboxIPAddress(),
"netbox_interface": resourceNetboxInterface(),
"netbox_service": resourceNetboxService(),
Expand All @@ -23,6 +24,7 @@ func Provider() *schema.Provider {
DataSourcesMap: map[string]*schema.Resource{
"netbox_cluster": dataSourceNetboxCluster(),
"netbox_tenant": dataSourceNetboxTenant(),
"netbox_vrf": dataSourceNetboxVrf(),
"netbox_platform": dataSourceNetboxPlatform(),
"netbox_device_role": dataSourceNetboxDeviceRole(),
"netbox_tag": dataSourceNetboxTag(),
Expand Down
14 changes: 14 additions & 0 deletions netbox/resource_netbox_ip_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func resourceNetboxIPAddress() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
},
"vrf_id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"tenant_id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -108,6 +112,12 @@ func resourceNetboxIPAddressRead(d *schema.ResourceData, m interface{}) error {
d.Set("interface_id", nil)
}

if res.GetPayload().Vrf != nil {
d.Set("vrf_id", res.GetPayload().Vrf.ID)
} else {
d.Set("vrf_id", nil)
}

if res.GetPayload().Tenant != nil {
d.Set("tenant_id", res.GetPayload().Tenant.ID)
} else {
Expand Down Expand Up @@ -151,6 +161,10 @@ func resourceNetboxIPAddressUpdate(d *schema.ResourceData, m interface{}) error
data.AssignedObjectID = int64ToPtr(int64(interfaceID.(int)))
}

if vrfID, ok := d.GetOk("vrf_id"); ok {
data.Vrf = int64ToPtr(int64(vrfID.(int)))
}

if tenantID, ok := d.GetOk("tenant_id"); ok {
data.Tenant = int64ToPtr(int64(tenantID.(int)))
}
Expand Down
8 changes: 8 additions & 0 deletions netbox/resource_netbox_ip_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ resource "netbox_tenant" "test" {
name = "%[1]s"
}

resource "netbox_vrf" "test" {
name = "%[1]s"
}

resource "netbox_cluster_type" "test" {
name = "%[1]s"
}
Expand Down Expand Up @@ -64,6 +68,7 @@ resource "netbox_ip_address" "test" {
resource.TestCheckResourceAttr("netbox_ip_address.test", "tags.#", "1"),
resource.TestCheckResourceAttr("netbox_ip_address.test", "tags.0", testName),
resource.TestCheckResourceAttr("netbox_ip_address.test", "tenant_id", "0"),
resource.TestCheckResourceAttr("netbox_ip_address.test", "vrf_id", "0"),
),
},
{
Expand All @@ -73,12 +78,14 @@ resource "netbox_ip_address" "test" {
interface_id = netbox_interface.test.id
status = "reserved"
tenant_id = netbox_tenant.test.id
vrf_id = netbox_vrf.test.id
tags = [netbox_tag.test.name]
}`, testIP),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_ip_address.test", "ip_address", testIP),
resource.TestCheckResourceAttr("netbox_ip_address.test", "status", "reserved"),
resource.TestCheckResourceAttrPair("netbox_ip_address.test", "tenant_id", "netbox_tenant.test", "id"),
resource.TestCheckResourceAttrPair("netbox_ip_address.test", "vrf_id", "netbox_vrf.test", "id"),
),
},
{
Expand All @@ -93,6 +100,7 @@ resource "netbox_ip_address" "test" {
resource.TestCheckResourceAttr("netbox_ip_address.test", "ip_address", testIP),
resource.TestCheckResourceAttr("netbox_ip_address.test", "status", "dhcp"),
resource.TestCheckResourceAttr("netbox_ip_address.test", "tenant_id", "0"),
resource.TestCheckResourceAttr("netbox_ip_address.test", "vrf_id", "0"),
),
},
{
Expand Down
117 changes: 117 additions & 0 deletions netbox/resource_netbox_vrf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package netbox

import (
"github.com/fbreckle/go-netbox/netbox/client"
"github.com/fbreckle/go-netbox/netbox/client/ipam"
"github.com/fbreckle/go-netbox/netbox/models"
"github.com/go-openapi/runtime"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"strconv"
)

func resourceNetboxVrf() *schema.Resource {
return &schema.Resource{
Create: resourceNetboxVrfCreate,
Read: resourceNetboxVrfRead,
Update: resourceNetboxVrfUpdate,
Delete: resourceNetboxVrfDelete,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"tags": &schema.Schema{
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Set: schema.HashString,
},
},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
}
}

func resourceNetboxVrfCreate(d *schema.ResourceData, m interface{}) error {
api := m.(*client.NetBoxAPI)

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

tags, _ := getNestedTagListFromResourceDataSet(api, d.Get("tags"))

params := ipam.NewIpamVrfsCreateParams().WithData(
&models.WritableVRF{
Name: &name,
Tags: tags,
},
)

res, err := api.Ipam.IpamVrfsCreate(params, nil)
if err != nil {
return err
}

d.SetId(strconv.FormatInt(res.GetPayload().ID, 10))

return resourceNetboxVrfRead(d, m)
}

func resourceNetboxVrfRead(d *schema.ResourceData, m interface{}) error {
api := m.(*client.NetBoxAPI)
id, _ := strconv.ParseInt(d.Id(), 10, 64)
params := ipam.NewIpamVrfsReadParams().WithID(id)

res, err := api.Ipam.IpamVrfsRead(params, nil)
if err != nil {
errorcode := err.(*runtime.APIError).Response.(runtime.ClientResponse).Code()
if errorcode == 404 {
// If the ID is updated to blank, this tells Terraform the resource no longer exists (maybe it was destroyed out of band). Just like the destroy callback, the Read function should gracefully handle this case. https://www.terraform.io/docs/extend/writing-custom-providers.html
d.SetId("")
return nil
}
return err
}

d.Set("name", res.GetPayload().Name)
return nil
}

func resourceNetboxVrfUpdate(d *schema.ResourceData, m interface{}) error {
api := m.(*client.NetBoxAPI)

id, _ := strconv.ParseInt(d.Id(), 10, 64)
data := models.WritableVRF{}

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

tags, _ := getNestedTagListFromResourceDataSet(api, d.Get("tags"))

data.Name = &name
data.Tags = tags

params := ipam.NewIpamVrfsPartialUpdateParams().WithID(id).WithData(&data)

_, err := api.Ipam.IpamVrfsPartialUpdate(params, nil)
if err != nil {
return err
}

return resourceNetboxVrfRead(d, m)
}

func resourceNetboxVrfDelete(d *schema.ResourceData, m interface{}) error {
api := m.(*client.NetBoxAPI)

id, _ := strconv.ParseInt(d.Id(), 10, 64)
params := ipam.NewIpamVrfsDeleteParams().WithID(id)

_, err := api.Ipam.IpamVrfsDelete(params, nil)
if err != nil {
return err
}
return nil
}
Loading