github.com/ottenhoff/terraform@v0.7.0-rc1.0.20160607213102-ac2d195cc560/builtin/providers/terraform/data_source_state.go (about) 1 package terraform 2 3 import ( 4 "log" 5 "time" 6 7 "github.com/hashicorp/terraform/helper/schema" 8 "github.com/hashicorp/terraform/state/remote" 9 ) 10 11 func dataSourceRemoteState() *schema.Resource { 12 return &schema.Resource{ 13 Read: dataSourceRemoteStateRead, 14 15 Schema: map[string]*schema.Schema{ 16 "backend": &schema.Schema{ 17 Type: schema.TypeString, 18 Required: true, 19 }, 20 21 "config": &schema.Schema{ 22 Type: schema.TypeMap, 23 Optional: true, 24 }, 25 26 "output": &schema.Schema{ 27 Type: schema.TypeMap, 28 Computed: true, 29 }, 30 }, 31 } 32 } 33 34 func dataSourceRemoteStateRead(d *schema.ResourceData, meta interface{}) error { 35 backend := d.Get("backend").(string) 36 config := make(map[string]string) 37 for k, v := range d.Get("config").(map[string]interface{}) { 38 config[k] = v.(string) 39 } 40 41 // Create the client to access our remote state 42 log.Printf("[DEBUG] Initializing remote state client: %s", backend) 43 client, err := remote.NewClient(backend, config) 44 if err != nil { 45 return err 46 } 47 48 // Create the remote state itself and refresh it in order to load the state 49 log.Printf("[DEBUG] Loading remote state...") 50 state := &remote.State{Client: client} 51 if err := state.RefreshState(); err != nil { 52 return err 53 } 54 55 var outputs map[string]interface{} 56 if !state.State().Empty() { 57 outputValueMap := make(map[string]string) 58 for key, output := range state.State().RootModule().Outputs { 59 //This is ok for 0.6.17 as outputs will have been strings 60 outputValueMap[key] = output.Value.(string) 61 } 62 } 63 64 d.SetId(time.Now().UTC().String()) 65 d.Set("output", outputs) 66 return nil 67 }