I am writing a custom terraform provider for an internal API.
For any resource that are created by the provider I would like to add a tag to the resource of the form: Created-By-Terraform: True along with other tags that the user providers.
return &schema.Resource{
.....
"tags": {
Type: schema.TypeMap,
Required: false,
Optional: true,
Computed: true,
ForceNew: false,
Description: "Tags (key = value pairs)",
},
....
func GetTfTags(d *schema.ResourceData) []string {
tfTags := d.Get("tags").(map[string]interface{})
tags := []string{"Created-By-Terraform:true"}
for k, v := range tfTags {
tags = append(tags, k+":"+v.(string))
}
return tags
}
func resourceMyResourceCreate(d *schema.ResourceData, m interface{}) error {
tags := GetTfTags(d)
// create the resource on the backend
}
func resourceMyResourceUpdate(d *schema.ResourceData, m interface{}) error {
tags := GetTfTags(d)
// Update the resource on the backend
}
This works perfectly fine as expected. However, acceptance tests keeps failing when I try to provide a user-defined tag “foo”: “bar”
Step 0 error: After applying this step, the plan was not empty:
DIFF:
UPDATE: my_resource.my_resource
tags.Created-By-Terraform: "true" => ""
tags.foo: "bar" => "bar"
STATE:
tags.Created-By-Terraform = true
tags.foo = bar
Any idea why this is the case? Do I need to use customdiff
functionality?
I don’t want users to set lifecycle
at provider
level or at each resource
level when they write HCL.