Skip to content

Commit

Permalink
feat: Add VRF object
Browse files Browse the repository at this point in the history
  • Loading branch information
smutel committed Aug 31, 2023
1 parent 82cc364 commit ab11014
Show file tree
Hide file tree
Showing 8 changed files with 695 additions and 5 deletions.
3 changes: 3 additions & 0 deletions examples/data-sources/netbox_ipam_vrf/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "netbox_ipam_vrf" "vrf_test" {
vrf_id = 15
}
2 changes: 2 additions & 0 deletions examples/resources/netbox_ipam_vrf/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# VRFs can be imported by id
terraform import netbox_ipam_vrf.vrf_test 1
88 changes: 88 additions & 0 deletions examples/resources/netbox_ipam_vrf/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
resource "netbox_ipam_vrf" "vrf_test" {
name = "Test VRF"
enforce_unique = false
export_targets = [ netbox_ipam_route_targets.rt_export_test.id ]
import_targets = [ netbox_ipam_route_targets.rt_import_test.id ]
rd = "test-vrf"
description = "Test VRF"
comments = <<-EOT
Test Vrf
EOT

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

custom_field {
name = "cf_boolean"
type = "boolean"
value = "true"
}

custom_field {
name = "cf_date"
type = "date"
value = "2020-12-25"
}

custom_field {
name = "cf_text"
type = "text"
value = "some text"
}

custom_field {
name = "cf_integer"
type = "integer"
value = "10"
}

custom_field {
name = "cf_selection"
type = "select"
value = "1"
}

custom_field {
name = "cf_url"
type = "url"
value = "https://github.com"
}

custom_field {
name = "cf_multi_selection"
type = "multiselect"
value = jsonencode([
"0",
"1"
])
}

custom_field {
name = "cf_json"
type = "json"
value = jsonencode({
stringvalue = "string"
boolvalue = false
dictionary = {
numbervalue = 5
}
})
}

custom_field {
name = "cf_object"
type = "object"
value = 1
}

custom_field {
name = "cf_multi_object"
type = "multiobject"
value = jsonencode([
1,
2
])
}
}
62 changes: 62 additions & 0 deletions netbox/ipam/data_netbox_ipam_vrf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ipam

import (
"context"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
netboxclient "github.com/smutel/go-netbox/v3/netbox/client"
"github.com/smutel/go-netbox/v3/netbox/client/ipam"
"github.com/smutel/terraform-provider-netbox/v7/netbox/internal/util"
)

func DataNetboxIpamVrf() *schema.Resource {
return &schema.Resource{
Description: "Get info about vrf (ipam module) from netbox.",
ReadContext: dataNetboxIpamVrfRead,

Schema: map[string]*schema.Schema{
"content_type": {
Type: schema.TypeString,
Computed: true,
Description: "The content type of this vrf (ipam module).",
},
"vrf_id": {
Type: schema.TypeInt,
Required: true,
Description: "The ID of the vrf (ipam module).",
},
},
}
}

func dataNetboxIpamVrfRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*netboxclient.NetBoxAPI)

id := int64(d.Get("vrf_id").(int))
idStr := strconv.FormatInt(id, 10)

p := ipam.NewIpamVrfsListParams().WithID(&idStr)

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

if *list.Payload.Count < 1 {
return diag.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
} else if *list.Payload.Count > 1 {
return diag.Errorf("Your query returned more than one result. " +
"Please try a more specific search criteria.")
}

r := list.Payload.Results[0]
d.SetId(strconv.FormatInt(r.ID, 10))
if err = d.Set("content_type", util.ConvertURIContentType(r.URL)); err != nil {
return diag.FromErr(err)
}

return nil
}
9 changes: 4 additions & 5 deletions netbox/ipam/resource_netbox_ipam_ip_addresses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ func testAccCheckNetboxIpamIPAddressConfig(nameSuffix string, resourceFull, extr
address = "${cidrhost("10.0.0.0/8", {{ .ipnum }} + 2 )}/24"
}
#resource "netbox_ipam_vrf" "test" {
# name = "test-{{ .namesuffix }}"
#}
resource "netbox_ipam_vrf" "test" {
name = "test-{{ .namesuffix }}"
}
{{ end }}
resource "netbox_ipam_ip_addresses" "test" {
Expand All @@ -143,8 +143,7 @@ func testAccCheckNetboxIpamIPAddressConfig(nameSuffix string, resourceFull, extr
dns_name = "test.example.local"
role = "vip"
status = "reserved"
# vrf_id = netbox_ipam_vrf.test.id
vrf_id = 1
vrf_id = netbox_ipam_vrf.test.id
tenant_id = netbox_tenancy_tenant.test.id
nat_inside_id = netbox_ipam_ip_addresses.nat.id
Expand Down
Loading

0 comments on commit ab11014

Please sign in to comment.