terraform: prefix the Id for configuring providers

This commit is contained in:
Mitchell Hashimoto 2014-09-29 10:36:49 -07:00
parent 459ffe9d2a
commit 133a28e363
4 changed files with 100 additions and 1 deletions

View File

@ -569,11 +569,17 @@ func (c *walkContext) inputWalkFn() depgraph.WalkFunc {
}
rc := NewResourceConfig(raw)
// Wrap the input into a namespace
input := &PrefixUIInput{
IdPrefix: fmt.Sprintf("provider.%s", rn.ID),
UIInput: c.Context.uiInput,
}
// Go through each provider and capture the input necessary
// to satisfy it.
configs := make(map[string]map[string]interface{})
for k, p := range sharedProvider.Providers {
newc, err := p.Input(c.Context.uiInput, rc)
newc, err := p.Input(input, rc)
if err != nil {
return fmt.Errorf(
"Error configuring %s: %s", k, err)

View File

@ -499,6 +499,56 @@ func TestContextInput_provider(t *testing.T) {
}
}
func TestContextInput_providerId(t *testing.T) {
input := new(MockUIInput)
m := testModule(t, "input-provider")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
UIInput: input,
})
var actual interface{}
p.InputFn = func(i UIInput, c *ResourceConfig) (*ResourceConfig, error) {
v, err := i.Input(&InputOpts{Id: "foo"})
if err != nil {
return nil, err
}
c.Raw["foo"] = v
return c, nil
}
p.ConfigureFn = func(c *ResourceConfig) error {
actual = c.Raw["foo"]
return nil
}
input.InputReturnMap = map[string]string{
"provider.aws.foo": "bar",
}
if err := ctx.Input(); err != nil {
t.Fatalf("err: %s", err)
}
if _, err := ctx.Plan(nil); err != nil {
t.Fatalf("err: %s", err)
}
if _, err := ctx.Apply(); err != nil {
t.Fatalf("err: %s", err)
}
if !reflect.DeepEqual(actual, "bar") {
t.Fatalf("bad: %#v", actual)
}
}
func TestContextApply(t *testing.T) {
m := testModule(t, "apply-good")
p := testProvider("aws")

View File

@ -0,0 +1,17 @@
package terraform
import (
"fmt"
)
// PrefixUIInput is an implementation of UIInput that prefixes the ID
// with a string, allowing queries to be namespaced.
type PrefixUIInput struct {
IdPrefix string
UIInput UIInput
}
func (i *PrefixUIInput) Input(opts *InputOpts) (string, error) {
opts.Id = fmt.Sprintf("%s.%s", i.IdPrefix, opts.Id)
return i.UIInput.Input(opts)
}

View File

@ -0,0 +1,26 @@
package terraform
import (
"testing"
)
func TestPrefixUIInput_impl(t *testing.T) {
var _ UIInput = new(PrefixUIInput)
}
func testPrefixUIInput(t *testing.T) {
input := new(MockUIInput)
prefix := &PrefixUIInput{
IdPrefix: "foo",
UIInput: input,
}
_, err := prefix.Input(&InputOpts{Id: "bar"})
if err != nil {
t.Fatalf("err: %s", err)
}
if input.InputOpts.Id != "foo.bar" {
t.Fatalf("bad: %#v", input.InputOpts)
}
}