github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/terraform/resource_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 resourceRemoteState() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourceRemoteStateCreate, 14 Read: resourceRemoteStateRead, 15 Delete: resourceRemoteStateDelete, 16 17 Schema: map[string]*schema.Schema{ 18 "backend": &schema.Schema{ 19 Type: schema.TypeString, 20 Required: true, 21 ForceNew: true, 22 }, 23 24 "config": &schema.Schema{ 25 Type: schema.TypeMap, 26 Optional: true, 27 ForceNew: true, 28 }, 29 30 "output": &schema.Schema{ 31 Type: schema.TypeMap, 32 Computed: true, 33 }, 34 }, 35 } 36 } 37 38 func resourceRemoteStateCreate(d *schema.ResourceData, meta interface{}) error { 39 return resourceRemoteStateRead(d, meta) 40 } 41 42 func resourceRemoteStateRead(d *schema.ResourceData, meta interface{}) error { 43 backend := d.Get("backend").(string) 44 config := make(map[string]string) 45 for k, v := range d.Get("config").(map[string]interface{}) { 46 config[k] = v.(string) 47 } 48 49 // Create the client to access our remote state 50 log.Printf("[DEBUG] Initializing remote state client: %s", backend) 51 client, err := remote.NewClient(backend, config) 52 if err != nil { 53 return err 54 } 55 56 // Create the remote state itself and refresh it in order to load the state 57 log.Printf("[DEBUG] Loading remote state...") 58 state := &remote.State{Client: client} 59 if err := state.RefreshState(); err != nil { 60 return err 61 } 62 63 var outputs map[string]string 64 if !state.State().Empty() { 65 outputs = state.State().RootModule().Outputs 66 } 67 68 d.SetId(time.Now().UTC().String()) 69 d.Set("output", outputs) 70 return nil 71 } 72 73 func resourceRemoteStateDelete(d *schema.ResourceData, meta interface{}) error { 74 d.SetId("") 75 return nil 76 }