Skip to content

Commit

Permalink
feat: add 'rd' and 'enforce_unique' fields to vrf (#585)
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultbustarret-ovhcloud authored and fbreckle committed May 17, 2024
1 parent 704c7fc commit b352246
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/resources/vrf.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ resource "netbox_vrf" "cust_a_prod" {
### Optional

- `description` (String)
- `enforce_unique` (Boolean) Defaults to `true`.
- `rd` (String)
- `tags` (Set of String)
- `tenant_id` (Number)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
toolchain go1.21.3

require (
github.com/fbreckle/go-netbox v0.0.0-20240308101138-0b0a4b03021a
github.com/fbreckle/go-netbox v0.0.0-20240517155930-7b96c5ef0e3f
github.com/fbreckle/terraform-plugin-docs v0.0.0-20220812121758-a828466500d3
github.com/go-openapi/runtime v0.28.0
github.com/go-openapi/strfmt v0.23.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/fbreckle/go-netbox v0.0.0-20240308101138-0b0a4b03021a h1:FaRCEleNa/MINB2wOyv/ym79nhX46zKvEU7dVXYxyyc=
github.com/fbreckle/go-netbox v0.0.0-20240308101138-0b0a4b03021a/go.mod h1:3U3/m/hna9Ntd3sbHBYwZ1IqbP2+coRzoXw3mCfu3kM=
github.com/fbreckle/go-netbox v0.0.0-20240517155930-7b96c5ef0e3f h1:KFREao9Ah6YYYkCnQELzUGP7da0+ecGnkVlkzU097s4=
github.com/fbreckle/go-netbox v0.0.0-20240517155930-7b96c5ef0e3f/go.mod h1:3U3/m/hna9Ntd3sbHBYwZ1IqbP2+coRzoXw3mCfu3kM=
github.com/fbreckle/terraform-plugin-docs v0.0.0-20220812121758-a828466500d3 h1:DMSpM0btVedE2Tt1vfDHWQhf2obzjAe1F0/j8/CyfW4=
github.com/fbreckle/terraform-plugin-docs v0.0.0-20220812121758-a828466500d3/go.mod h1:j3HmJySEjx6hOAOPDjGzmzpVNDQq9SNnnF+Vm22d2rs=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
Expand Down
30 changes: 30 additions & 0 deletions netbox/resource_netbox_vrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/fbreckle/go-netbox/netbox/client/ipam"
"github.com/fbreckle/go-netbox/netbox/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func resourceNetboxVrf() *schema.Resource {
Expand All @@ -33,6 +34,17 @@ func resourceNetboxVrf() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
},
"enforce_unique": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"rd": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 21),
},

tagsKey: tagsSchema,
},
Importer: &schema.ResourceImporter{
Expand All @@ -47,13 +59,19 @@ func resourceNetboxVrfCreate(d *schema.ResourceData, m interface{}) error {

name := d.Get("name").(string)
tenantID := int64(d.Get("tenant_id").(int))
enforceUnique := d.Get("enforce_unique").(bool)
rd := d.Get("rd").(string)

data.Name = &name
if tenantID != 0 {
data.Tenant = &tenantID
}

data.Description = getOptionalStr(d, "description", true)
data.EnforceUnique = enforceUnique
if rd != "" {
data.Rd = &rd
}

data.Tags, _ = getNestedTagListFromResourceDataSet(api, d.Get(tagsKey))

Expand Down Expand Up @@ -93,6 +111,12 @@ func resourceNetboxVrfRead(d *schema.ResourceData, m interface{}) error {
vrf := res.GetPayload()
d.Set("name", vrf.Name)
d.Set("description", vrf.Description)
d.Set("enforce_unique", vrf.EnforceUnique)
if vrf.Rd != nil {
d.Set("rd", *vrf.Rd)
} else {
d.Set("rd", nil)
}
if vrf.Tenant != nil {
d.Set("tenant_id", vrf.Tenant.ID)
} else {
Expand All @@ -108,6 +132,7 @@ func resourceNetboxVrfUpdate(d *schema.ResourceData, m interface{}) error {
data := models.WritableVRF{}

name := d.Get("name").(string)
enforceUnique := d.Get("enforce_unique").(bool)

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

Expand All @@ -116,6 +141,11 @@ func resourceNetboxVrfUpdate(d *schema.ResourceData, m interface{}) error {
data.ExportTargets = []int64{}
data.ImportTargets = []int64{}
data.Description = getOptionalStr(d, "description", true)
data.EnforceUnique = enforceUnique

if rd, ok := d.GetOk("rd"); ok {
data.Rd = strToPtr(rd.(string))
}

if tenantID, ok := d.GetOk("tenant_id"); ok {
data.Tenant = int64ToPtr(int64(tenantID.(int)))
Expand Down
60 changes: 60 additions & 0 deletions netbox/resource_netbox_vrf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,66 @@ resource "netbox_vrf" "test_tenant" {
})
}

func TestAccNetboxVrf_rd(t *testing.T) {
testSlug := "vrf_rd"
testName := testAccGetTestName(testSlug)
resource.ParallelTest(t, resource.TestCase{
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "netbox_vrf" "test_rd" {
name = "%s"
rd = "123:456"
}`, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_vrf.test_rd", "name", testName),
resource.TestCheckResourceAttr("netbox_vrf.test_rd", "rd", "123:456"),
),
},
{
ResourceName: "netbox_vrf.test_rd",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccNetboxVrf_enforceUnique(t *testing.T) {
testSlug := "vrf_enforce_unique"
testName := testAccGetTestName(testSlug)
resource.ParallelTest(t, resource.TestCase{
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "netbox_vrf" "test_enforce_unique" {
name = "%s-true"
enforce_unique = true
}`, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_vrf.test_enforce_unique", "name", testName+"-true"),
resource.TestCheckResourceAttr("netbox_vrf.test_enforce_unique", "enforce_unique", "true"),
),
},
{
Config: fmt.Sprintf(`
resource "netbox_vrf" "test_enforce_unique_false" {
name = "%s-false"
enforce_unique = false
}`, testName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_vrf.test_enforce_unique_false", "name", testName+"-false"),
resource.TestCheckResourceAttr("netbox_vrf.test_enforce_unique_false", "enforce_unique", "false"),
),
},
},
})
}

func init() {
resource.AddTestSweepers("netbox_vrf", &resource.Sweeper{
Name: "netbox_vrf",
Expand Down

0 comments on commit b352246

Please sign in to comment.