Add an item to the SetAttribute, if this value wasn't provided in the HCL configuration

Is it possible using PlanModifiers: []planmodifier.Set{} add a new item into the Set, if this item is not currently provided in the configuration?
I’d like to have something like this:

			"groups": schema.SetAttribute{
				ElementType:         types.StringType,
				MarkdownDescription: "List of groups this user is a part of.",
				Optional:            true,
				Computed:            true,
				PlanModifiers: []planmodifier.Set{
						myModifier.AddReaders(ctx, req, resp),
				Validators: []validator.Set{setvalidator.SizeAtLeast(1)},

The scenario is if groups = [ "test0", "test1" ] in the configuration, then the Provider should add a group “readers”, so the end result in the state will be [ "readers", "test0", "test1" ].
If I add this item in the Create function, it will break the import.
I’ve tried to write a custom plan modifier but not sure if I’m on the right way. Can someone share some ideas please?

Hi @danielmkn :wave: Thank you for raising this topic.

Terraform’s data consistency rules require that a configuration value match the stored state value. For sets, this rule applies to the entire set, meaning that it is not valid to “add” elements. If there needs to be additional data associated with a configuration value, then the expected solution is to create a second computed attribute with all the values or automatically handle the data differences within the resource Create/Read/Update logic ensuring that the saved state matches the configuration.

Please note that I have adjusted the category of this topic to be under Plugin Development - HashiCorp Discuss so it is more visible to folks asking and answering these types of questions. :+1:

1 Like