Skip to content

Commit

Permalink
fix: Custom fields default values not null
Browse files Browse the repository at this point in the history
Fixes: 180
- Fix field default.
- Add group_name and ui_visibility to customfield
  • Loading branch information
amhn authored and smutel committed Mar 14, 2023
1 parent 2144830 commit 53382b9
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 87 deletions.
2 changes: 2 additions & 0 deletions examples/resources/netbox_extras_custom_field/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Custom fields can be imported by id
terraform import netbox_extras_custom_field.cf_text 1
67 changes: 59 additions & 8 deletions netbox/extras/resource_netbox_extras_custom_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package extras

import (
"context"
"encoding/json"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -63,7 +64,7 @@ func ResourceNetboxExtrasCustomField() *schema.Resource {
"default": {
Type: schema.TypeString,
Optional: true,
Description: "The default value for this custom field. Encoded as JSON.",
Description: "The default value for this custom field. This value must be valid Json. Strings, List and Dicts should be wrapped in jsonencode()",
},
"description": {
Type: schema.TypeString,
Expand All @@ -79,6 +80,13 @@ func ResourceNetboxExtrasCustomField() *schema.Resource {
ValidateFunc: validation.StringInSlice([]string{"disabled", "loose", "exact"}, false),
Description: "The filter logic for this custom field. Allowed values: \"loose\" (default), \"exact\", \"disabled\"",
},
"group_name": {
Type: schema.TypeString,
Optional: true,
Default: nil,
ValidateFunc: validation.StringLenBetween(1, 50),
Description: "Custom fields within the same group will be displayed together.",
},
"label": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -123,6 +131,13 @@ func ResourceNetboxExtrasCustomField() *schema.Resource {
}, false),
Description: "Type of the custom field (text, longtext, integer, boolean, url, json, select, multiselect, object, multiobject).",
},
"ui_visibility": {
Type: schema.TypeString,
Optional: true,
Default: "read-write",
ValidateFunc: validation.StringInSlice([]string{"read-write", "read-only", "hidden"}, false),
Description: "The filter logic for this custom field. Allowed values: \"read-write\" (default), \"read-only\", \"hidden\"",
},
"url": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -167,7 +182,6 @@ func resourceNetboxExtrasCustomFieldCreate(ctx context.Context, d *schema.Resour
client := m.(*netboxclient.NetBoxAPI)

name := d.Get("name").(string)
defaultstring := d.Get("default").(string)
validationMaximum := int64(d.Get("validation_maximum").(int))
var validationMaximumPtr *int64
if validationMaximum != 0 {
Expand All @@ -187,20 +201,30 @@ func resourceNetboxExtrasCustomFieldCreate(ctx context.Context, d *schema.Resour
newResource := &models.WritableCustomField{
Choices: util.ToListofStrings(d.Get("choices").(*schema.Set).List()),
ContentTypes: util.ToListofStrings(d.Get("content_types").(*schema.Set).List()),
Default: &defaultstring,
Description: d.Get("description").(string),
FilterLogic: d.Get("filter_logic").(string),
GroupName: d.Get("group_name").(string),
Label: d.Get("label").(string),
Name: &name,
ObjectType: d.Get("object_type").(string),
Required: d.Get("required").(bool),
Type: d.Get("type").(string),
UIVisibility: d.Get("ui_visibility").(string),
ValidationMaximum: validationMaximumPtr,
ValidationMinimum: validationMinimumPtr,
ValidationRegex: d.Get("validation_regex").(string),
Weight: &weight,
}

if defaultstring := d.Get("default").(string); defaultstring != "" {
var jsonMap interface{}
err := json.Unmarshal([]byte(defaultstring), &jsonMap)
if err != nil {
return diag.FromErr(err)
}
newResource.Default = jsonMap
}

resource := extras.NewExtrasCustomFieldsCreateParams().WithData(newResource)

resourceCreated, err := client.Extras.ExtrasCustomFieldsCreate(resource, nil)
Expand Down Expand Up @@ -246,15 +270,25 @@ func resourceNetboxExtrasCustomFieldRead(ctx context.Context, d *schema.Resource
if err = d.Set("data_type", resource.DataType); err != nil {
return diag.FromErr(err)
}
if err = d.Set("default", resource.Default); err != nil {
return diag.FromErr(err)
if resource.Default != nil {
jsonValue, _ := json.Marshal(resource.Default)
if err = d.Set("default", string(jsonValue)); err != nil {
return diag.FromErr(err)
}
} else {
if err = d.Set("default", nil); err != nil {
return diag.FromErr(err)
}
}
if err = d.Set("description", resource.Description); err != nil {
return diag.FromErr(err)
}
if err = d.Set("filter_logic", resource.FilterLogic.Value); err != nil {
return diag.FromErr(err)
}
if err = d.Set("group_name", resource.GroupName); err != nil {
return diag.FromErr(err)
}
if err = d.Set("label", resource.Label); err != nil {
return diag.FromErr(err)
}
Expand All @@ -273,6 +307,9 @@ func resourceNetboxExtrasCustomFieldRead(ctx context.Context, d *schema.Resource
if err = d.Set("type", resource.Type.Value); err != nil {
return diag.FromErr(err)
}
if err = d.Set("ui_visibility", util.GetCustomFieldUIVisibilityValue(resource.UIVisibility)); err != nil {
return diag.FromErr(err)
}
if err = d.Set("url", resource.URL); err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -310,9 +347,16 @@ func resourceNetboxExtrasCustomFieldUpdate(ctx context.Context, d *schema.Resour
params.ContentTypes = util.ToListofStrings(d.Get("content_types").(*schema.Set).List())
}
if d.HasChange("default") {
defaultvalue := d.Get("default").(string)
params.Default = &defaultvalue
modifiedFields["default"] = defaultvalue
if defaultstring := d.Get("default").(string); defaultstring != "" {
var jsonMap interface{}
err := json.Unmarshal([]byte(defaultstring), &jsonMap)
if err != nil {
return diag.FromErr(err)
}
params.Default = jsonMap
} else {
modifiedFields["default"] = nil
}
}
if d.HasChange("description") {
params.Description = d.Get("description").(string)
Expand All @@ -321,6 +365,10 @@ func resourceNetboxExtrasCustomFieldUpdate(ctx context.Context, d *schema.Resour
if d.HasChange("filter_logic") {
params.FilterLogic = d.Get("filter_logic").(string)
}
if d.HasChange("group_name") {
params.GroupName = d.Get("group_name").(string)
modifiedFields["group_name"] = params.GroupName
}
if d.HasChange("label") {
params.Label = d.Get("label").(string)
modifiedFields["label"] = params.Label
Expand All @@ -339,6 +387,9 @@ func resourceNetboxExtrasCustomFieldUpdate(ctx context.Context, d *schema.Resour
if d.HasChange("type") {
params.Type = d.Get("type").(string)
}
if d.HasChange("ui_visibility") {
params.UIVisibility = d.Get("ui_visibility").(string)
}
if d.HasChange("validation_maximum") {
validationMaximum := int64(d.Get("validation_maximum").(int))
params.ValidationMaximum = &validationMaximum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,16 @@ func testAccCheckNetboxExtrasCustomFieldBooleanConfig(nameSuffix string, resourc
"dcim.site",
]
type = "boolean"
type = "boolean"
{{ if eq .resourcefull "true" }}
description = "Test custom field"
label = "Test Label for CF"
weight = 50
#required = true
filter_logic = "disabled"
# Fixed in Netbox 3.3
#default = true
description = "Test custom field"
group_name = "testgroup"
ui_visibility = "hidden"
label = "Test Label for CF"
weight = 50
#required = true
filter_logic = "disabled"
default = true
{{ end }}
}
`
Expand Down
17 changes: 9 additions & 8 deletions netbox/extras/resource_netbox_extras_custom_field_date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,16 @@ func testAccCheckNetboxExtrasCustomFieldDateConfig(nameSuffix string, resourceFu
"dcim.site",
]
type = "date"
type = "date"
{{ if eq .resourcefull "true" }}
description = "Test custom field"
label = "Test Label for CF"
weight = 50
#required = true
filter_logic = "disabled"
# Fixed in Netbox 3.3
#default = "2022-01-01"
description = "Test custom field"
group_name = "testgroup"
ui_visibility = "hidden"
label = "Test Label for CF"
weight = 50
#required = true
filter_logic = "disabled"
default = jsonencode("2022-01-01")
{{ end }}
}
`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@ func testAccCheckNetboxExtrasCustomFieldIntegerConfig(nameSuffix string, resourc
type = "integer"
{{ if eq .resourcefull "true" }}
description = "Test custom field"
group_name = "testgroup"
ui_visibility = "hidden"
label = "Test Label for CF"
weight = 50
#required = true
filter_logic = "disabled"
# Fixed in Netbox 3.3
#default = 50
default = 50
validation_minimum = 1
validation_maximum = 500
{{ end }}
Expand Down
29 changes: 15 additions & 14 deletions netbox/extras/resource_netbox_extras_custom_field_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,22 @@ func testAccCheckNetboxExtrasCustomFieldJSONConfig(nameSuffix string, resourceFu
"dcim.site",
]
type = "json"
type = "json"
{{ if eq .resourcefull "true" }}
description = "Test custom field"
label = "Test Label for CF"
weight = 50
#required = true
filter_logic = "disabled"
# Fixed in Netbox 3.3
#default = jsonencode({
# bool = false
# number = 1.5
# dict = {
# text = "Some text"}
# }
#})
description = "Test custom field"
group_name = "testgroup"
ui_visibility = "hidden"
label = "Test Label for CF"
weight = 50
#required = true
filter_logic = "disabled"
default = jsonencode({
bool = false
number = 1.5
dict = {
text = "Some text"
}
})
{{ end }}
}
`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@ func testAccCheckNetboxExtrasCustomFieldLongtextConfig(nameSuffix string, resour
type = "longtext"
{{ if eq .resourcefull "true" }}
description = "Test custom field"
group_name = "testgroup"
ui_visibility = "hidden"
label = "Test Label for CF"
weight = 50
#required = true
filter_logic = "disabled"
default = "Default text"
default = jsonencode("Default text")
validation_regex = "^.*$"
{{ end }}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,19 @@ func testAccCheckNetboxExtrasCustomFieldMultiObjectConfig(nameSuffix string, res
"dcim.site",
]
type = "multiobject"
object_type = "dcim.platform"
type = "multiobject"
object_type = "dcim.platform"
{{ if eq .resourcefull "true" }}
description = "Test custom field"
label = "Test Label for CF"
weight = 50
#required = true
filter_logic = "disabled"
# Fixed in netbox 3.3
#default = jsonencode([
# netbox_dcim_platform.test.id
#])
description = "Test custom field"
label = "Test Label for CF"
group_name = "testgroup"
ui_visibility = "hidden"
weight = 50
#required = true
filter_logic = "disabled"
default = jsonencode([
netbox_dcim_platform.test.id
])
{{ end }}
}
`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,20 @@ func testAccCheckNetboxExtrasCustomFieldMultiSelectConfig(nameSuffix string, res
"dcim.site",
]
type = "multiselect"
type = "multiselect"
{{ if eq .resourcefull "true" }}
description = "Test custom field"
description = "Test custom field"
choices = [
"test",
"test2"
]
label = "Test Label for CF"
weight = 50
#required = true
filter_logic = "disabled"
# Fixed in netbox 3.3
#default = "test"
label = "Test Label for CF"
group_name = "testgroup"
ui_visibility = "hidden"
weight = 50
#required = true
filter_logic = "disabled"
default = jsonencode(["test"])
{{ end }}
}
`
Expand Down
23 changes: 12 additions & 11 deletions netbox/extras/resource_netbox_extras_custom_field_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,19 @@ func testAccCheckNetboxExtrasCustomFieldObjectConfig(nameSuffix string, resource
"dcim.site",
]
type = "object"
object_type = "dcim.platform"
type = "object"
object_type = "dcim.platform"
{{ if eq .resourcefull "true" }}
description = "Test custom field"
label = "Test Label for CF"
weight = 50
#required = true
filter_logic = "disabled"
# Fixed in netbox 3.3
#default = jsonencode([
# netbox_dcim_platform.test.id
#])
description = "Test custom field"
group_name = "testgroup"
ui_visibility = "hidden"
label = "Test Label for CF"
weight = 50
#required = true
filter_logic = "disabled"
default = jsonencode([
netbox_dcim_platform.test.id
])
{{ end }}
}
`
Expand Down
Loading

0 comments on commit 53382b9

Please sign in to comment.