Skip to content

Commit

Permalink
Merge pull request #33 from smutel/AddCustomFieldsVlan
Browse files Browse the repository at this point in the history
feat: Add custom_fields to vlan resource
  • Loading branch information
smutel committed Dec 13, 2020
2 parents c5c21ac + 1315546 commit 540f901
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
16 changes: 16 additions & 0 deletions docs/resources/ipam_vlan.md
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions examples/main.tf
Expand Up @@ -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" {
Expand Down
43 changes: 38 additions & 5 deletions netbox/resource_netbox_ipam_vlan.go
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 540f901

Please sign in to comment.