Skip to content

Add custom_fields support for available_prefix #679

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions docs/resources/available_prefix.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ resource "netbox_available_prefix" "test" {

### Optional

- `custom_fields` (Map of String)
- `description` (String)
- `is_pool` (Boolean)
- `mark_utilized` (Boolean)
Expand Down
6 changes: 5 additions & 1 deletion netbox/resource_netbox_available_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ func resourceNetboxAvailablePrefix() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
},
tagsKey: tagsSchema,
customFieldsKey: customFieldsSchema,
tagsKey: tagsSchema,
},
Importer: &schema.ResourceImporter{
StateContext: func(c context.Context, rd *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
Expand Down Expand Up @@ -122,6 +123,9 @@ func resourceNetboxAvailablePrefixCreate(d *schema.ResourceData, m interface{})
data := models.PrefixLength{
PrefixLength: &prefixLength,
}
if cf, ok := d.GetOk(customFieldsKey); ok {
data.CustomFields = cf
}
params := ipam.NewIpamPrefixesAvailablePrefixesCreateParams().WithID(parentPrefixID).WithData(&data)

res, err := api.Ipam.IpamPrefixesAvailablePrefixesCreate(params, nil)
Expand Down
63 changes: 62 additions & 1 deletion netbox/resource_netbox_available_prefix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ resource "netbox_prefix" "parent" {
description = "%[1]s"
status = "container"
tags = [netbox_tag.test.name]
lifecycle {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this required?

ignore_changes = [%[3]s]
}
}
`, testName, parentPrefix)
`, testName, parentPrefix, customFieldsKey)
}

func TestAccNetboxAvailablePrefix_basic(t *testing.T) {
Expand Down Expand Up @@ -100,6 +103,64 @@ resource "netbox_available_prefix" "test" {
})
}

func TestAccNetboxAvailablePrefix_cf(t *testing.T) {
testParentPrefix := "1.1.0.0/24"
testPrefixLength := 25
expectedPrefix := "1.1.0.0/25"
testSlug := "prefix_cf"
testName := testAccGetTestName(testSlug)

parentResourceName := "netbox_prefix.parent"
resourceName := "netbox_available_prefix.test"

resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccNetboxAvailablePrefixFullDependencies(testName, testParentPrefix) + fmt.Sprintf(`
resource "netbox_custom_field" "test" {
name = "%s"
type = "text"
weight = 100
content_types = ["ipam.prefix"]
}

resource "netbox_available_prefix" "test" {
parent_prefix_id = netbox_prefix.parent.id
prefix_length = %d
status = "active"

custom_fields = {
"${netbox_custom_field.test.name}" = "test-field"
}
}`, testSlug, testPrefixLength),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "prefix", expectedPrefix),
resource.TestCheckResourceAttr(resourceName, "status", "active"),
resource.TestCheckResourceAttr(resourceName, fmt.Sprintf("custom_fields.%s", testSlug), "test-field"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: func(s *terraform.State) (string, error) {
parent, ok := s.RootModule().Resources[parentResourceName]
if !ok {
return "", fmt.Errorf("Not found: %s", parentResourceName)
}
resource, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("Not found: %s", resourceName)
}

return fmt.Sprintf("%s %s %d", parent.Primary.ID, resource.Primary.ID, testPrefixLength), nil
},
},
},
})
}

func TestAccNetboxAvailablePrefix_multiplePrefixesSerial(t *testing.T) {
testParentPrefix := "1.1.0.0/24"
testPrefixLength := 25
Expand Down