mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-15 10:03:44 -06:00
The work integrated in hashicorp/terraform#6322 silently broke the ability to use remote state correctly. This commit adds a fix for that, making use of the work integrated in hashicorp/terraform#7124. In order to deal with outputs which are complex structures, we use a forked version of the flatmap package - the difference in the version this commit vs the github.com/hashicorp/terraform/flatmap package is that we add in an additional key for map counts which state requires. Because we bypass the normal helper/schema mechanism, this is not set for us. Because of the HIL type checking of maps, values must be of a homogenous type. This is unfortunate, as it means we can no longer refer to outputs as: ${terraform_remote_state.foo.output.outputname} Instead we had to bring them to the top level namespace: ${terraform_remote_state.foo.outputname} This actually does lead to better overall usability - and the BC breakage is made better by the fact that indexing would have broken the original syntax anyway. We also add a real-world test and assert against specific values. Tests which were previously acceptance tests are now run as unit tests, so regression should be identified at a much earlier stage.
69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
package terraform
|
|
|
|
import (
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
"github.com/hashicorp/terraform/state/remote"
|
|
)
|
|
|
|
func dataSourceRemoteState() *schema.Resource {
|
|
return &schema.Resource{
|
|
Read: dataSourceRemoteStateRead,
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
"backend": {
|
|
Type: schema.TypeString,
|
|
Required: true,
|
|
},
|
|
|
|
"config": {
|
|
Type: schema.TypeMap,
|
|
Optional: true,
|
|
},
|
|
|
|
"__has_dynamic_attributes": {
|
|
Type: schema.TypeString,
|
|
Optional: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func dataSourceRemoteStateRead(d *schema.ResourceData, meta interface{}) error {
|
|
backend := d.Get("backend").(string)
|
|
config := make(map[string]string)
|
|
for k, v := range d.Get("config").(map[string]interface{}) {
|
|
config[k] = v.(string)
|
|
}
|
|
|
|
// Create the client to access our remote state
|
|
log.Printf("[DEBUG] Initializing remote state client: %s", backend)
|
|
client, err := remote.NewClient(backend, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Create the remote state itself and refresh it in order to load the state
|
|
log.Printf("[DEBUG] Loading remote state...")
|
|
state := &remote.State{Client: client}
|
|
if err := state.RefreshState(); err != nil {
|
|
return err
|
|
}
|
|
|
|
d.SetId(time.Now().UTC().String())
|
|
|
|
outputMap := make(map[string]interface{})
|
|
for key, val := range state.State().RootModule().Outputs {
|
|
outputMap[key] = val.Value
|
|
}
|
|
|
|
mappedOutputs := remoteStateFlatten(outputMap)
|
|
|
|
for key, val := range mappedOutputs {
|
|
d.UnsafeSetFieldRaw(key, val)
|
|
}
|
|
return nil
|
|
}
|