Skip to content

Commit

Permalink
terraform_remote_state resource becomes a data source
Browse files Browse the repository at this point in the history
As a first example of a real-world data source, the pre-existing
terraform_remote_state resource is adapted to be a data source. The
original resource is shimmed to wrap the data source for backward
compatibility.
  • Loading branch information
apparentlymart committed Apr 17, 2016
1 parent 842d2ac commit 8d2b959
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,19 @@ import (
"github.com/hashicorp/terraform/state/remote"
)

func resourceRemoteState() *schema.Resource {
func dataSourceRemoteState() *schema.Resource {
return &schema.Resource{
Create: resourceRemoteStateCreate,
Read: resourceRemoteStateRead,
Delete: resourceRemoteStateDelete,
Read: dataSourceRemoteStateRead,

Schema: map[string]*schema.Schema{
"backend": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"config": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},

"output": &schema.Schema{
Expand All @@ -35,11 +31,7 @@ func resourceRemoteState() *schema.Resource {
}
}

func resourceRemoteStateCreate(d *schema.ResourceData, meta interface{}) error {
return resourceRemoteStateRead(d, meta)
}

func resourceRemoteStateRead(d *schema.ResourceData, meta interface{}) error {
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{}) {
Expand Down Expand Up @@ -69,8 +61,3 @@ func resourceRemoteStateRead(d *schema.ResourceData, meta interface{}) error {
d.Set("output", outputs)
return nil
}

func resourceRemoteStateDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}
7 changes: 6 additions & 1 deletion builtin/providers/terraform/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import (
func Provider() terraform.ResourceProvider {
return &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
"terraform_remote_state": resourceRemoteState(),
"terraform_remote_state": schema.DataSourceResourceShim(
dataSourceRemoteState(),
),
},
DataSourcesMap: map[string]*schema.Resource{
"terraform_remote_state": dataSourceRemoteState(),
},
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: "terraform"
page_title: "Terraform: terraform_remote_state"
sidebar_current: "docs-terraform-resource-remote-state"
sidebar_current: "docs-terraform-datasource-remote-state"
description: |-
Accesses state meta data from a remote backend.
---
Expand All @@ -13,7 +13,7 @@ Retrieves state meta data from a remote backend
## Example Usage

```
resource "terraform_remote_state" "vpc" {
data "terraform_remote_state" "vpc" {
backend = "atlas"
config {
name = "hashicorp/vpc-prod"
Expand All @@ -22,7 +22,7 @@ resource "terraform_remote_state" "vpc" {
resource "aws_instance" "foo" {
# ...
subnet_id = "${terraform_remote_state.vpc.output.subnet_id}"
subnet_id = "${data.terraform_remote_state.vpc.output.subnet_id}"
}
```

Expand Down
17 changes: 5 additions & 12 deletions website/source/docs/providers/terraform/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,16 @@ description: |-

# Terraform Provider

The terraform provider exposes resources to access state meta data
for Terraform outputs from shared infrastructure.
The terraform provider provides access to outputs from the Terraform state
of shared infrastructure.

The terraform provider is what we call a _logical provider_. This has no
impact on how it behaves, but conceptually it is important to understand.
The terraform provider doesn't manage any _physical_ resources; it isn't
creating servers, writing files, etc. It is used to access the outputs
of other Terraform states to be used as inputs for resources.
Examples will explain this best.

Use the navigation to the left to read about the available resources.
Use the navigation to the left to read about the available data sources.

## Example Usage

```
# Shared infrastructure state stored in Atlas
resource "terraform_remote_state" "vpc" {
data "terraform_remote_state" "vpc" {
backend = "atlas"
config {
path = "hashicorp/vpc-prod"
Expand All @@ -33,6 +26,6 @@ resource "terraform_remote_state" "vpc" {
resource "aws_instance" "foo" {
# ...
subnet_id = "${terraform_remote_state.vpc.output.subnet_id}"
subnet_id = "${data.terraform_remote_state.vpc.output.subnet_id}"
}
```
8 changes: 4 additions & 4 deletions website/source/layouts/terraform.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
<a href="/docs/providers/terraform/index.html">Terraform Provider</a>
</li>

<li<%= sidebar_current(/^docs-terraform-resource/) %>>
<a href="#">Resources</a>
<li<%= sidebar_current(/^docs-terraform-datasource/) %>>
<a href="#">Data Sources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-terraform-resource-remote-state") %>>
<a href="/docs/providers/terraform/r/remote_state.html">terraform_remote_state</a>
<li<%= sidebar_current("docs-terraform-datasource-remote-state") %>>
<a href="/docs/providers/terraform/d/remote_state.html">terraform_remote_state</a>
</li>
</ul>
</li>
Expand Down

0 comments on commit 8d2b959

Please sign in to comment.