2015-03-12 00:17:47 +01:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/hashicorp/terraform/state/remote"
|
|
|
|
)
|
|
|
|
|
2016-05-02 01:05:54 +02:00
|
|
|
func dataSourceRemoteState() *schema.Resource {
|
2015-03-12 00:17:47 +01:00
|
|
|
return &schema.Resource{
|
2016-05-02 01:05:54 +02:00
|
|
|
Read: dataSourceRemoteStateRead,
|
2015-03-12 00:17:47 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"backend": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"config": &schema.Schema{
|
|
|
|
Type: schema.TypeMap,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"output": &schema.Schema{
|
|
|
|
Type: schema.TypeMap,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 01:05:54 +02:00
|
|
|
func dataSourceRemoteStateRead(d *schema.ResourceData, meta interface{}) error {
|
2015-03-12 00:17:47 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-03-22 15:22:33 +01:00
|
|
|
var outputs map[string]interface{}
|
2015-04-02 07:49:05 +02:00
|
|
|
if !state.State().Empty() {
|
2016-05-12 02:05:02 +02:00
|
|
|
outputValueMap := make(map[string]string)
|
|
|
|
for key, output := range state.State().RootModule().Outputs {
|
|
|
|
//This is ok for 0.6.17 as outputs will have been strings
|
|
|
|
outputValueMap[key] = output.Value.(string)
|
|
|
|
}
|
2015-04-02 07:49:05 +02:00
|
|
|
}
|
|
|
|
|
2015-03-12 00:17:47 +01:00
|
|
|
d.SetId(time.Now().UTC().String())
|
2015-04-02 07:49:05 +02:00
|
|
|
d.Set("output", outputs)
|
2015-03-12 00:17:47 +01:00
|
|
|
return nil
|
|
|
|
}
|