Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul Custom Fields for Devices to support any underlying type #443

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Commits on Nov 17, 2023

  1. feat: Add funcs for custom fields of any type

    Create functions for overahauling custom fields to support multiple
    underlying types.
    
    Create function for the schema with using TypeString instead of
    TypeMap. This allows for complex json types using jsonencode/jsondecode.
    
    Create handleCustomFieldUpdate function and tests for it. This function
    takes in the results of a `d.GetChange` funtion `old` & `new`, which
    allows us to compare old and new values, and update the `custom_fields`
    field in the api. Add unit test to validate functionality.
    
    handleCustomFieldRead and tests were added to handle the reading of
    custom fields and setting the field to "" if all the custom fields
    are nil. Adds tests to validate the functionality.
    tagur87 committed Nov 17, 2023
    Configuration menu
    Copy the full SHA
    b8a88b3 View commit details
    Browse the repository at this point in the history
  2. feat: allow any custom field type in netbox_devices data source

    Update the data source `netbox_devices` to use the string based
    schema for the `custom_fields` attribute.
    
    This allows for any underlying type to be accessed by using the
    `jsondecode()` terraform function.
    
    For example, to access custom fields, the following code can be used.
    
    ```terraform
    data "netbox_devices" "test" {
    	filter {
    		name  = "name"
    		value = "device1"
    	}
    }
    
    output "device_field" {
    	value = jsondecode(data.netbox_devices.test[0].custom_fields).field1
    }
    ```
    tagur87 committed Nov 17, 2023
    Configuration menu
    Copy the full SHA
    eedbd47 View commit details
    Browse the repository at this point in the history
  3. feat: allow any custom field type in netbox_device

    Updates the `netbox_device` resource to allow for any custom field
    type to be set. This is done by using a string field with the
    `jsonencode()` and `jsondecode()` terraform functions.
    
    This allows for any complex custom fields or data types that are needed,
    since the underlying types don't matter and are unmarshalled using
    map[string]interface{}.
    
    To set custom fields using this new method, use the following syntax.
    
    ```terraform
    resource "netbox_device" "test" {
      name = "device1"
      tenant_id = netbox_tenant.test.id
      role_id = netbox_device_role.test.id
      device_type_id = netbox_device_type.test.id
      site_id = netbox_site.test.id
    	custom_fields = jsonencode({
    		"text_field" = "81"
    		"bool_field" = true
    	})
    }
    ```
    
    NOTE: This may be a breaking change for some users.
    tagur87 committed Nov 17, 2023
    Configuration menu
    Copy the full SHA
    df26a39 View commit details
    Browse the repository at this point in the history