From dc34ae5e43c16190795db5c88559deb5cc45f2e4 Mon Sep 17 00:00:00 2001 From: smutel <12967891+smutel@users.noreply.github.com> Date: Tue, 26 Oct 2021 15:53:32 +0200 Subject: [PATCH] feat: Add custom fields to interface resource --- docs/resources/virtualization_interface.md | 47 +++++++++++++++++++ docs/resources/virtualization_vm.md | 1 + ...esource_netbox_virtualization_interface.go | 38 +++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/docs/resources/virtualization_interface.md b/docs/resources/virtualization_interface.md index 0e1c45732..db613a427 100644 --- a/docs/resources/virtualization_interface.md +++ b/docs/resources/virtualization_interface.md @@ -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" + } } ``` @@ -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: diff --git a/docs/resources/virtualization_vm.md b/docs/resources/virtualization_vm.md index be1866385..91d990baf 100644 --- a/docs/resources/virtualization_vm.md +++ b/docs/resources/virtualization_vm.md @@ -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 diff --git a/netbox/resource_netbox_virtualization_interface.go b/netbox/resource_netbox_virtualization_interface.go index 16080b010..729adb1e7 100644 --- a/netbox/resource_netbox_virtualization_interface.go +++ b/netbox/resource_netbox_virtualization_interface.go @@ -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, @@ -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) @@ -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, @@ -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 == "" { @@ -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