Skip to content

Commit

Permalink
feat: Add custom fields to interface resource
Browse files Browse the repository at this point in the history
  • Loading branch information
smutel committed Oct 26, 2021
1 parent 983c6bd commit dc34ae5
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
47 changes: 47 additions & 0 deletions docs/resources/virtualization_interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,48 @@ resource "netbox_virtualization_interface" "interface_test" {
mac_address = "AA:AA:AA:AA:AA:AA"
mtu = 1500
description = "Interface de test"
custom_field {
name = "cf_boolean"
type = "boolean"
value = "true"
}
custom_field {
name = "cf_date"
type = "date"
value = "2020-12-25"
}
custom_field {
name = "cf_text"
type = "text"
value = "some text"
}
custom_field {
name = "cf_integer"
type = "integer"
value = "10"
}
custom_field {
name = "cf_selection"
type = "selection"
value = "1"
}
custom_field {
name = "cf_url"
type = "url"
value = "https://github.com"
}
custom_field {
name = "cf_multiple_selection"
type = "multiple"
value = "0,1"
}
}
```

Expand All @@ -31,6 +73,11 @@ The ``tag`` block supports:
* ``name`` - (Required) Name of the existing tag to associate with this resource.
* ``slug`` - (Required) Slug of the existing tag to associate with this resource.

The ``custom_field`` block (optional) supports:
* ``name`` - (Required) Name of the existing custom resource to associate with this resource.
* ``type`` - (Required) Type of the existing custom resource to associate with this resource (text, integer, boolean, url, selection, multiple).
* ``value`` - (Required) Value of the existing custom resource to associate with this resource.

## Attributes Reference

In addition to the above arguments, the following attributes are exported:
Expand Down
1 change: 1 addition & 0 deletions docs/resources/virtualization_vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Manage a virtual machine resource within Netbox.
resource "netbox_virtualization_vm" "vm_test" {
name = "TestVm"
comments = "VM created by terraform"
vcpus = "2.00"
disk = 50
memory = 16
cluster_id = 1
Expand Down
38 changes: 38 additions & 0 deletions netbox/resource_netbox_virtualization_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@ func resourceNetboxVirtualizationInterface() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"custom_field": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"text", "integer", "boolean",
"date", "url", "selection", "multiple"}, false),
},
"value": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"description": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -104,6 +126,8 @@ func resourceNetboxVirtualizationInterfaceCreate(d *schema.ResourceData,
m interface{}) error {
client := m.(*netboxclient.NetBoxAPI)

resourceCustomFields := d.Get("custom_field").(*schema.Set).List()
customFields := convertCustomFieldsFromTerraformToAPI(nil, resourceCustomFields)
description := d.Get("description").(string)
enabled := d.Get("enabled").(bool)
macAddress := d.Get("mac_address").(string)
Expand All @@ -116,6 +140,7 @@ func resourceNetboxVirtualizationInterfaceCreate(d *schema.ResourceData,
virtualmachineID := int64(d.Get("virtualmachine_id").(int))

newResource := &models.WritableVMInterface{
CustomFields: &customFields,
Description: description,
Enabled: enabled,
Mode: mode,
Expand Down Expand Up @@ -167,6 +192,13 @@ func resourceNetboxVirtualizationInterfaceRead(d *schema.ResourceData,

for _, resource := range resources.Payload.Results {
if strconv.FormatInt(resource.ID, 10) == d.Id() {
resourceCustomFields := d.Get("custom_field").(*schema.Set).List()
customFields := updateCustomFieldsFromAPI(resourceCustomFields, resource.CustomFields)

if err = d.Set("custom_field", customFields); err != nil {
return err
}

var description string

if resource.Description == "" {
Expand Down Expand Up @@ -254,6 +286,12 @@ func resourceNetboxVirtualizationInterfaceUpdate(d *schema.ResourceData,
taggedVlans := d.Get("tagged_vlans").(*schema.Set).List()
params.TaggedVlans = expandToInt64Slice(taggedVlans)

if d.HasChange("custom_field") {
stateCustomFields, resourceCustomFields := d.GetChange("custom_field")
customFields := convertCustomFieldsFromTerraformToAPI(stateCustomFields.(*schema.Set).List(), resourceCustomFields.(*schema.Set).List())
params.CustomFields = &customFields
}

if d.HasChange("description") {
description := d.Get("description").(string)
params.Description = description
Expand Down

0 comments on commit dc34ae5

Please sign in to comment.