'How can I get the resource name from main.tf file programatically?
Context: I'm developing a TF Provider and I'd like to query for a full resource name, e.g. given the following TF config:
resource "foo" "example-name" {
...
}
I'd like to retrieve foo.example-name or just example-name at least from fooRead() method:
func fooRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
I tried to look at the properties of schema.ResourceData (see its definition) but didn't find anything useful, e.g. here's the result of executing fmt.Sprintf("%#v", d):
&schema.ResourceData{schema:map[string]*schema.Schema{"property_0":(*schema.Schema)(0xc0001df040), "property_1":(*schema.Schema)(0xc0001df400), "property_2":(*schema.Schema)(0xc0001df2c0), "property_3":(*schema.Schema)(0xc0001df180)}, config:(*terraform.ResourceConfig)(nil), state:(*terraform.InstanceState)(0xc000532410), diff:(*terraform.InstanceDiff)(0xc000167900), meta:map[string]interface {}(nil), timeouts:(*schema.ResourceTimeout)(0xc000537da0), providerMeta:cty.Value{ty:cty.Type{typeImpl:cty.typeObject{typeImplSigil:cty.typeImplSigil{}, AttrTypes:map[string]cty.Type{}}}, v:interface {}(nil)}, multiReader:(*schema.MultiLevelFieldReader)(0xc000818660), setWriter:(*schema.MapFieldWriter)(0xc0001544f8), newState:(*terraform.InstanceState)(0xc000533380), partial:false, once:sync.Once{done:0x1, m:sync.Mutex{state:0, sema:0x0}}, isNew:true, panicOnError:true}
i.e. there's no "example-name" anywhere.
I also tried printing out d.State() but it didn't help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
