2015-03-12 00:17:47 +01:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2017-02-22 20:37:56 +01:00
|
|
|
"fmt"
|
2015-03-12 00:17:47 +01:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2017-02-28 22:25:42 +01:00
|
|
|
"github.com/hashicorp/terraform/backend"
|
2017-02-22 20:37:56 +01:00
|
|
|
backendinit "github.com/hashicorp/terraform/backend/init"
|
|
|
|
"github.com/hashicorp/terraform/config"
|
2015-03-12 00:17:47 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2017-02-22 20:37:56 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-03-12 00:17:47 +01:00
|
|
|
)
|
|
|
|
|
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{
|
2016-06-09 14:45:55 +02:00
|
|
|
"backend": {
|
2015-03-12 00:17:47 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
2016-09-04 00:38:43 +02:00
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
if vStr, ok := v.(string); ok && vStr == "_local" {
|
|
|
|
ws = append(ws, "Use of the %q backend is now officially "+
|
|
|
|
"supported as %q. Please update your configuration to ensure "+
|
|
|
|
"compatibility with future versions of Terraform.",
|
|
|
|
"_local", "local")
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
},
|
2015-03-12 00:17:47 +01:00
|
|
|
},
|
|
|
|
|
2016-06-09 14:45:55 +02:00
|
|
|
"config": {
|
2015-03-12 00:17:47 +01:00
|
|
|
Type: schema.TypeMap,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
2017-02-28 22:25:42 +01:00
|
|
|
"environment": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Default: backend.DefaultStateName,
|
|
|
|
},
|
|
|
|
|
2016-06-09 14:45:55 +02:00
|
|
|
"__has_dynamic_attributes": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-03-12 00:17:47 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2017-02-22 20:37:56 +01:00
|
|
|
|
|
|
|
// Get the configuration in a type we want.
|
|
|
|
rawConfig, err := config.NewRawConfig(d.Get("config").(map[string]interface{}))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error initializing backend: %s", err)
|
2015-03-12 00:17:47 +01:00
|
|
|
}
|
|
|
|
|
2016-09-04 00:38:43 +02:00
|
|
|
// Don't break people using the old _local syntax - but note warning above
|
|
|
|
if backend == "_local" {
|
|
|
|
log.Println(`[INFO] Switching old (unsupported) backend "_local" to "local"`)
|
|
|
|
backend = "local"
|
|
|
|
}
|
|
|
|
|
2015-03-12 00:17:47 +01:00
|
|
|
// Create the client to access our remote state
|
2017-02-22 20:37:56 +01:00
|
|
|
log.Printf("[DEBUG] Initializing remote state backend: %s", backend)
|
|
|
|
f := backendinit.Backend(backend)
|
|
|
|
if f == nil {
|
|
|
|
return fmt.Errorf("Unknown backend type: %s", backend)
|
2015-03-12 00:17:47 +01:00
|
|
|
}
|
2017-02-22 20:37:56 +01:00
|
|
|
b := f()
|
2015-03-12 00:17:47 +01:00
|
|
|
|
2017-02-22 20:37:56 +01:00
|
|
|
// Configure the backend
|
|
|
|
if err := b.Configure(terraform.NewResourceConfig(rawConfig)); err != nil {
|
|
|
|
return fmt.Errorf("error initializing backend: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the state
|
2017-02-28 22:25:42 +01:00
|
|
|
env := d.Get("environment").(string)
|
|
|
|
state, err := b.State(env)
|
2017-02-22 20:37:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error loading the remote state: %s", err)
|
|
|
|
}
|
2015-03-12 00:17:47 +01:00
|
|
|
if err := state.RefreshState(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-09 14:45:55 +02:00
|
|
|
d.SetId(time.Now().UTC().String())
|
|
|
|
|
|
|
|
outputMap := make(map[string]interface{})
|
2016-07-07 21:37:57 +02:00
|
|
|
|
|
|
|
remoteState := state.State()
|
|
|
|
if remoteState.Empty() {
|
|
|
|
log.Println("[DEBUG] empty remote state")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range remoteState.RootModule().Outputs {
|
2016-06-09 14:45:55 +02:00
|
|
|
outputMap[key] = val.Value
|
2015-04-02 07:49:05 +02:00
|
|
|
}
|
|
|
|
|
2016-06-09 14:45:55 +02:00
|
|
|
mappedOutputs := remoteStateFlatten(outputMap)
|
|
|
|
|
|
|
|
for key, val := range mappedOutputs {
|
|
|
|
d.UnsafeSetFieldRaw(key, val)
|
|
|
|
}
|
2015-03-12 00:17:47 +01:00
|
|
|
return nil
|
|
|
|
}
|