Skip to content

Commit

Permalink
Support mark_utilized in available_prefix resource
Browse files Browse the repository at this point in the history
  • Loading branch information
cova-fe committed Jan 14, 2022
1 parent 61f7005 commit 344432a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
4 changes: 2 additions & 2 deletions netbox/data_source_netbox_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ func dataSourceNetboxPrefix() *schema.Resource {
return &schema.Resource{
Read: dataSourceNetboxPrefixRead,
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"cidr": &schema.Schema{
"cidr": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.IsCIDR,
Expand Down
10 changes: 7 additions & 3 deletions netbox/resource_netbox_available_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func resourceNetboxAvailablePrefix() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
},
"mark_utilized": {
Type: schema.TypeBool,
Optional: true,
},
"vrf_id": {
Type: schema.TypeInt,
Optional: true,
Expand All @@ -57,15 +61,15 @@ func resourceNetboxAvailablePrefix() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
},
"site_id": &schema.Schema{
"site_id": {
Type: schema.TypeInt,
Optional: true,
},
"vlan_id": &schema.Schema{
"vlan_id": {
Type: schema.TypeInt,
Optional: true,
},
"role_id": &schema.Schema{
"role_id": {
Type: schema.TypeInt,
Optional: true,
},
Expand Down
2 changes: 2 additions & 0 deletions netbox/resource_netbox_available_prefix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ resource "netbox_available_prefix" "test" {
description = "%s"
status = "active"
tags = [netbox_tag.test.name]
mark_utilized = true
}`, testPrefixLength, testDesc),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "prefix", expectedPrefix),
resource.TestCheckResourceAttr(resourceName, "status", "active"),
resource.TestCheckResourceAttr(resourceName, "description", testDesc),
resource.TestCheckResourceAttr(resourceName, "tags.#", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.0", testName),
resource.TestCheckResourceAttr(resourceName, "mark_utilized", "true"),
),
},
{
Expand Down
25 changes: 16 additions & 9 deletions netbox/resource_netbox_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,49 @@ func resourceNetboxPrefix() *schema.Resource {
Delete: resourceNetboxPrefixDelete,

Schema: map[string]*schema.Schema{
"prefix": &schema.Schema{
"prefix": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.IsCIDR,
},
"status": &schema.Schema{
"status": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"active", "reserved", "deprecated", "container"}, false),
},
"description": &schema.Schema{
"description": {
Type: schema.TypeString,
Optional: true,
},
"is_pool": {
Type: schema.TypeBool,
Optional: true,
},
"vrf_id": &schema.Schema{
"mark_utilized": {
Type: schema.TypeBool,
Optional: true,
},
"vrf_id": {
Type: schema.TypeInt,
Optional: true,
},
"tenant_id": &schema.Schema{
"tenant_id": {
Type: schema.TypeInt,
Optional: true,
},
"site_id": &schema.Schema{
"site_id": {
Type: schema.TypeInt,
Optional: true,
},
"vlan_id": &schema.Schema{
"vlan_id": {
Type: schema.TypeInt,
Optional: true,
},
"role_id": &schema.Schema{
"role_id": {
Type: schema.TypeInt,
Optional: true,
},
"tags": &schema.Schema{
"tags": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -115,6 +119,7 @@ func resourceNetboxPrefixRead(d *schema.ResourceData, m interface{}) error {

d.Set("description", res.GetPayload().Description)
d.Set("is_pool", res.GetPayload().IsPool)
d.Set("mark_utilized", res.GetPayload().MarkUtilized)
if res.GetPayload().Status != nil {
d.Set("status", res.GetPayload().Status.Value)
}
Expand Down Expand Up @@ -166,12 +171,14 @@ func resourceNetboxPrefixUpdate(d *schema.ResourceData, m interface{}) error {
status := d.Get("status").(string)
description := d.Get("description").(string)
is_pool := d.Get("is_pool").(bool)
mark_utilized := d.Get("mark_utilized").(bool)

data.Prefix = &prefix
data.Status = status

data.Description = description
data.IsPool = is_pool
data.MarkUtilized = mark_utilized

if vrfID, ok := d.GetOk("vrf_id"); ok {
data.Vrf = int64ToPtr(int64(vrfID.(int)))
Expand Down
11 changes: 11 additions & 0 deletions netbox/resource_netbox_prefix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@ resource "netbox_prefix" "test" {
description = "%s"
status = "active"
tags = [netbox_tag.test.name]
mark_utilized = true
}`, testPrefix, testDesc),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_prefix.test", "prefix", testPrefix),
resource.TestCheckResourceAttr("netbox_prefix.test", "status", "active"),
resource.TestCheckResourceAttr("netbox_prefix.test", "description", testDesc),
resource.TestCheckResourceAttr("netbox_prefix.test", "tags.#", "1"),
resource.TestCheckResourceAttr("netbox_prefix.test", "tags.0", testName),
resource.TestCheckResourceAttr("netbox_prefix.test", "mark_utilized", "true"),
),
},
{
Expand All @@ -80,6 +82,7 @@ resource "netbox_prefix" "test" {
description = "%s"
status = "provoke_error"
tags = [netbox_tag.test.name]
mark_utilized = true
}`, testPrefix, testDesc),
ExpectError: regexp.MustCompile("expected status to be one of .*"),
},
Expand All @@ -90,6 +93,7 @@ resource "netbox_prefix" "test" {
description = "%s"
status = "deprecated"
tags = [netbox_tag.test.name]
mark_utilized = true
}`, testPrefix, testDesc),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_prefix.test", "prefix", testPrefix),
Expand All @@ -103,6 +107,7 @@ resource "netbox_prefix" "test" {
description = "%s"
status = "container"
tags = [netbox_tag.test.name]
mark_utilized = true
}`, testPrefix, testDesc),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_prefix.test", "prefix", testPrefix),
Expand All @@ -116,6 +121,7 @@ resource "netbox_prefix" "test" {
description = "%s 2"
status = "active"
tags = [netbox_tag.test.name]
mark_utilized = true
}`, testPrefix, testDesc),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_prefix.test", "prefix", testPrefix),
Expand All @@ -126,6 +132,7 @@ resource "netbox_prefix" "test" {
resource.TestCheckResourceAttr("netbox_prefix.test", "site_id", "0"),
resource.TestCheckResourceAttr("netbox_prefix.test", "tags.#", "1"),
resource.TestCheckResourceAttr("netbox_prefix.test", "tags.0", testName),
resource.TestCheckResourceAttr("netbox_prefix.test", "mark_utilized", "true"),
),
},
{
Expand All @@ -137,6 +144,7 @@ resource "netbox_prefix" "test" {
vrf_id = netbox_vrf.test.id
tenant_id = netbox_tenant.test.id
tags = [netbox_tag.test.name]
mark_utilized = true
}`, testPrefix, testDesc),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_prefix.test", "prefix", testPrefix),
Expand All @@ -146,6 +154,7 @@ resource "netbox_prefix" "test" {
resource.TestCheckResourceAttrPair("netbox_prefix.test", "tenant_id", "netbox_tenant.test", "id"),
resource.TestCheckResourceAttr("netbox_prefix.test", "tags.#", "1"),
resource.TestCheckResourceAttr("netbox_prefix.test", "tags.0", testName),
resource.TestCheckResourceAttr("netbox_prefix.test", "mark_utilized", "true"),
),
},
{
Expand All @@ -160,6 +169,7 @@ resource "netbox_prefix" "test" {
vlan_id = netbox_vlan.test.id
role_id = netbox_ipam_role.test.id
tags = [netbox_tag.test.name]
mark_utilized = true
}`, testPrefix, testDesc),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("netbox_prefix.test", "prefix", testPrefix),
Expand All @@ -172,6 +182,7 @@ resource "netbox_prefix" "test" {
resource.TestCheckResourceAttrPair("netbox_prefix.test", "role_id", "netbox_ipam_role.test", "id"),
resource.TestCheckResourceAttr("netbox_prefix.test", "tags.#", "1"),
resource.TestCheckResourceAttr("netbox_prefix.test", "tags.0", testName),
resource.TestCheckResourceAttr("netbox_prefix.test", "mark_utilized", "true"),
),
},
{
Expand Down

0 comments on commit 344432a

Please sign in to comment.