providers/terraform: remote state resource

This commit is contained in:
Mitchell Hashimoto 2015-03-11 18:17:47 -05:00
commit 693f688b49
4 changed files with 171 additions and 0 deletions

15
provider.go Normal file
View File

@ -0,0 +1,15 @@
package terraform
import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
// Provider returns a terraform.ResourceProvider.
func Provider() terraform.ResourceProvider {
return &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
"terraform_state": resourceState(),
},
}
}

31
provider_test.go Normal file
View File

@ -0,0 +1,31 @@
package terraform
import (
"testing"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
var testAccProviders map[string]terraform.ResourceProvider
var testAccProvider *schema.Provider
func init() {
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
"terraform": testAccProvider,
}
}
func TestProvider(t *testing.T) {
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
}
func testAccPreCheck(t *testing.T) {
}

71
resource_state.go Normal file
View File

@ -0,0 +1,71 @@
package terraform
import (
"log"
"time"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/state/remote"
)
func resourceState() *schema.Resource {
return &schema.Resource{
Create: resourceStateCreate,
Read: resourceStateRead,
Delete: resourceStateDelete,
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{
Type: schema.TypeMap,
Computed: true,
},
},
}
}
func resourceStateCreate(d *schema.ResourceData, meta interface{}) error {
return resourceStateRead(d, meta)
}
func resourceStateRead(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{}) {
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
}
d.SetId(time.Now().UTC().String())
d.Set("output", state.State().RootModule().Outputs)
return nil
}
func resourceStateDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}

54
resource_state_test.go Normal file
View File

@ -0,0 +1,54 @@
package terraform
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccState_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccState_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckStateValue(
"terraform_state.foo", "foo", "bar"),
),
},
},
})
}
func testAccCheckStateValue(id, name, value string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[id]
if !ok {
return fmt.Errorf("Not found: %s", id)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}
v := rs.Primary.Attributes["output."+name]
if v != value {
return fmt.Errorf(
"Value for %s is %s, not %s", name, v, value)
}
return nil
}
}
const testAccState_basic = `
resource "terraform_state" "foo" {
backend = "_local"
config {
path = "./test-fixtures/basic.tfstate"
}
}`