diff --git a/docs/resources/ipam_vlan.md b/docs/resources/ipam_vlan.md index 0a702e933..5ab4c2c58 100644 --- a/docs/resources/ipam_vlan.md +++ b/docs/resources/ipam_vlan.md @@ -18,12 +18,28 @@ resource "netbox_ipam_vlan" "vlan_test" { name = "tag1" slug = "tag1" } + + custom_fields = { + cf_boolean = "true" + cf_date = "2020-12-25" + cf_integer = "10" + cf_selection = "1" + cf_text = "Some text" + cf_url = "https://github.com" + } } ``` ## Argument Reference The following arguments are supported: +* ``custom_fields`` - (Optional) Custom Field Keys and Values for this object + * For boolean, use the string value "true" or "false" + * For data, use the string format "YYYY-MM-DD" + * For integer, use the value between double quote "10" + * For selection, use the level id + * For text, use the string value + * For URL, use the URL as string * ``description`` - (Optional) The description of this object. * ``vlan_group_id`` - (Optional) ID of the group where this object belongs to. * ``name`` - (Required) The name for this object. diff --git a/examples/main.tf b/examples/main.tf index 1d05e1463..f9d89ee81 100644 --- a/examples/main.tf +++ b/examples/main.tf @@ -65,6 +65,15 @@ resource "netbox_ipam_vlan" "vlan_test" { name = "tag2" slug = "tag2" } + + custom_fields = { + cf_boolean = "true" + cf_date = "2020-12-25" + cf_integer = "10" + cf_selection = "1" + cf_text = "Some text" + cf_url = "https://github.com" + } } resource "netbox_ipam_prefix" "prefix_test" { diff --git a/netbox/resource_netbox_ipam_vlan.go b/netbox/resource_netbox_ipam_vlan.go index 28c581c51..0c0e59ea1 100644 --- a/netbox/resource_netbox_ipam_vlan.go +++ b/netbox/resource_netbox_ipam_vlan.go @@ -20,6 +20,24 @@ func resourceNetboxIpamVlan() *schema.Resource { Exists: resourceNetboxIpamVlanExists, Schema: map[string]*schema.Schema{ + "custom_fields": { + Type: schema.TypeMap, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + // terraform default behavior sees a difference between null and an empty string + // therefore we override the default, because null in terraform results in empty string in netbox + DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { + // function is called for each member of map + // including additional call on the amount of entries + // we ignore the count, because the actual state always returns the amount of existing custom_fields and all are optional in terraform + if k == CustomFieldsRegex { + return true + } + return old == new + }, + }, "description": { Type: schema.TypeString, Optional: true, @@ -82,6 +100,8 @@ func resourceNetboxIpamVlanCreate(d *schema.ResourceData, m interface{}) error { client := m.(*netboxclient.NetBoxAPI) + resourceCustomFields := d.Get("custom_fields").(map[string]interface{}) + customFields := convertCustomFieldsFromTerraformToAPICreate(resourceCustomFields) description := d.Get("description").(string) groupID := int64(d.Get("vlan_group_id").(int)) name := d.Get("name").(string) @@ -93,11 +113,12 @@ func resourceNetboxIpamVlanCreate(d *schema.ResourceData, vid := int64(d.Get("vlan_id").(int)) newResource := &models.WritableVLAN{ - Description: description, - Name: &name, - Status: status, - Tags: convertTagsToNestedTags(tags), - Vid: &vid, + CustomFields: &customFields, + Description: description, + Name: &name, + Status: status, + Tags: convertTagsToNestedTags(tags), + Vid: &vid, } if groupID != 0 { @@ -140,6 +161,12 @@ func resourceNetboxIpamVlanRead(d *schema.ResourceData, for _, resource := range resources.Payload.Results { if strconv.FormatInt(resource.ID, 10) == d.Id() { + customFields := convertCustomFieldsFromAPIToTerraform(resource.CustomFields) + + if err = d.Set("custom_fields", customFields); err != nil { + return err + } + var description string if resource.Description == "" { @@ -239,6 +266,12 @@ func resourceNetboxIpamVlanUpdate(d *schema.ResourceData, params.Description = description } + if d.HasChange("custom_fields") { + stateCustomFields, resourceCustomFields := d.GetChange("custom_fields") + customFields := convertCustomFieldsFromTerraformToAPIUpdate(stateCustomFields, resourceCustomFields) + params.CustomFields = &customFields + } + if d.HasChange("vlan_group_id") { groupID := int64(d.Get("vlan_group_id").(int)) if groupID != 0 {