Terraform Plugin Framework: Creating a resource with a null field

Hi,

Is there a way to differentiate whether a field was not included in the resource body or if a field was set to null?

I.e

case 1:
resource “foo” “foo” {
field1 = “val1”
field2 = null
}

versus

case 2:
resource “foo” “foo” {
field1 = “val1”
}

When I set field2’s schema as Optional: true, then in both cases, when I get the plan, field2.IsNull() == true. When I set field2 to Optional: true and Computed: true in the schema, then for both cases field2.IsUnknown == true.

I was wondering if it is at all possible to differentiate knowing if a field was explicitly included in the resource declaration or not (even if the value was set to null).

If not, what would be the best practice to achieve this functionality? I want to be able to pass a null value to fields to explicitly nullify them on resource creation / update.

relevant

If you set an argument of a resource to null , Terraform behaves as though you had completely omitted it

1 Like

It’s better for consumer experience has no difference between value2 = null and value2 ommited, because in some cases like loops, the user can set the value with null to keep contract with the map.

this is an one example …

@hQnVyLRx @claytonsilva So for the case where I want to explicitly update a field to null using the terraform provider, what would be the best practice implementation?

In my opnion, the default use case, when does not explicity define Required in the schema,
is the best practice.

You don’t need do add other validation

schema.StringAttribute{
                Required: false,  // Or dont explicity this option
            },

@claytonsilva Sorry I don’t think I quite understand. What I’m trying to achieve is the following:

field1 schema:

schema.StringAttribute{
                Optional: true
            },

Step 1. Set the optional field (“field1”) value to a string.

Step 2. Update field1 to a null value.

The problem I’m having is that if setting a value to null is treated as omitting the value entirely, how would I know whether to pass that field to my API with a null value or to omit it from the API request body?

I need it so I can achieve these two functionalities:

// this means I want to create a req body to my api to set field1 to null (and field2 to “val2”)

resource "test" "test" {
  field1: null
  field2: "val2"
}

and

// This means just set field2 to “val2” (do not include “field1” in the req body)

resource "test" "test" {
  field2: "val2"
}

If there’s no way of determining whether a field was included or set to null, how can I achieve both these functionalities? And what would be the best practice to implement this functionality?

1 Like

I apologize for the misunderstanding regarding the problem. I believe I now understand what you’re trying to solve.

It seems like you need the “field1” field explicitly declared when calling your API. I think you could solve this implicitly by declaring the field during execution and populating it with null if it’s either null or omitted.

While there’s no standard best practice for populating optional fields (at least not that I’m aware of), I would personally prefer consuming your API with fewer required variables.