Optional + Computed attribute in embedded resource

Hello Community!

I have a question that is bugging me for some time. I will be thankful for explaining how it works under the hood and tips for solving it or using alternative approach.

Is it possible to use both, Optional and Computed, on attribute that is embedded resource ? Lets assume schema:

return map[string]*schema.Schema{
    "name": {
        Type:     schema.TypeString,
        Required: true,
    },
    "embedded": {
        Type:     schema.TypeSet,
        Optional: true,
        Elem: &schema.Resource{
           Schema: map[string]*schema.Schema{
               "name": {
                   Type:     schema.TypeString,
                   Required: true,
               },
               "color": {
                   Type:     schema.TypeString,
                   Optional: true,
                   Computed: true,
               },
           },
        },
    },
}

embedded is a Set with elements that have Resource schema.
This schema requires to configure name but leaves color as optional - if not provided, it might be computed by remote.

Such configuration ends in plan that is not clean every time when remote calculates color attribute. Plan indicates that embedded needs to be replaced. Having color as Computed only works well.

I’ve experimented with many functions like ConfigMode, Set orDiffSuppressFuncbut did not find suitable solution yet.

Thanks in advance.

Hello , did you have any luck?, I tried to workaround the behavior.
But it seems one option would be to set the DiffSuppressFunc
at the level of the schema.TypeSet object, but it trasverse all the fields in the object, so it’s not that handy or reliable.

@leofigy1 finally I resigned from using set in favor of a list, as it was possible in my case (I had max 1 element in a collection). List elements work well with Optional + Computed attributes (there are no hashes).
However, in cases where it is not possible (and wise) to use list, the best option is probably to avoid such arguments.

Computed attributes inside a Set are notoriously tricky, because the behavior around them is a bit undefined, yet needs to be maintained for compatibility purposes. :confused: My recommendation is generally to avoid it if possible.