Skip to content

Commit

Permalink
Avoid map setting in StateToTpl for the resource schema
Browse files Browse the repository at this point in the history
The resource schema should be only read, but not modified in `tfadd`. The only exception is that in `StateToTpl` we modify the schema of the `id` attribute of all resources, to convert it from O+C to be C only. This is mainly for provider schemas based on SDKv2. This modification can be done by directly setting the attribute schema, or setting a new attribute schema to the `id` attribute. The latter will effectively modify the schema.Attributes map, which if is under accessed by other Go routines, will cause Go's concurrent map access panic. So this PR changes it to using the former method.
  • Loading branch information
magodo committed Aug 10, 2024
1 parent 59efdda commit cd427db
Showing 1 changed file with 1 addition and 4 deletions.
5 changes: 1 addition & 4 deletions tfadd/internal/state_to_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ func StateToTpl(r *tfstate.StateResource, schema *tfjson.SchemaBlock, opt *Optio
c.WriteString(fmt.Sprintf("resource %q %q {\n", addr.Type, addr.Name))

// Special handling on attribute "id" to make it a Computed only attribute. This is mainly for the provider that is using the plugin sdk v2, where it is set to be O+C.
schema.Attributes["id"] = &tfjson.SchemaAttribute{
AttributeType: cty.String,
Computed: true,
}
schema.Attributes["id"].Optional = false

if err := c.AddAttributes(r.Value, schema.Attributes, 2); err != nil {
return nil, err
Expand Down

0 comments on commit cd427db

Please sign in to comment.