Ignore changes in tfstate

Hello,

In my provider, I have the definition below:

"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_selection"}, false),                               
            },                                                                                           
            "value": {                                                                                   
              Type:     schema.TypeString,                                                               
              Required: true,                                                                            
            },                                                                                           
          },                                                                                             
        },                                                                                               
      },

After doing an terraform apply, I can see in the tfstate:

"custom_field": [                                                                       
              {                                                                                     
                "name": "cf_integer",                                                               
                "type": "integer",                                                                  
                "value": "10"                                                                       
              },                                                                                    
              {                                                                                     
                "name": "cf_text",                                                                  
                "type": "text",                                                                     
                "value": "some text"                                                                
              }                                                                                     
            ],  

Is-it possible in the read function to not care about the field type ?
My concern is than the API is returning the name and value but not the type explicitly…

Thanks.

I’m reading this as you have an attribute named type, and you’re asking if you can omit that, not ignore the type of the attribute. If that’s wrong, please correct me!

Any attribute you don’t explicitly override with ResourceData.Set during Read should retain its existing value, I believe. Note that if the user imports your resource, that may result in your type attribute not being set!

Your are right.

So I changed my code and the result of the read is not as expected:

      - custom_field {                              
          - name  = "cf_integer"
          - value = "10"
        }             
      + custom_field {                              
          + name  = "cf_integer"     
          + type  = "integer"          
          + value = "10"
        }             
      - custom_field {                              
          - name  = "cf_text"
          - value = "some text"
        }
      + custom_field {
          + name  = "cf_text"
          + type  = "text"
          + value = "some text"
        }

Terraform is detecting a change because the field type in not retrieved by the read function.

What’s the code you’re using in Read to set the values?

You’re replacing the set items entirely, not just updating specific fields within each set item, so you’d need to read the value you’re looking for out of the set to persist it through. Right now, you’re saying “replace this element entirely”, not “modify this element”, so you don’t get existing values passed through.

1 Like