terraform: call Configure on the provider

This commit is contained in:
Mitchell Hashimoto 2014-06-06 00:20:23 -07:00
parent 967d4a61c1
commit ce0867fa09
3 changed files with 49 additions and 5 deletions

View File

@ -21,11 +21,10 @@ type Terraform struct {
// terraformProvider contains internal state information about a resource
// provider for Terraform.
type terraformProvider struct {
Provider ResourceProvider
Config *config.ProviderConfig
Configured bool
Provider ResourceProvider
Config *config.ProviderConfig
sync.Mutex
sync.Once
}
// Config is the configuration that must be given to instantiate
@ -134,7 +133,8 @@ func (t *Terraform) diffWalkFn(
panic(fmt.Sprintf("No provider for resource: %s", r.Id()))
}
// TODO(mitchellh): initialize provider if we haven't
// Initialize the provider if we haven't already
p.init(vars)
l.RLock()
var rs *ResourceState
@ -172,3 +172,18 @@ func (t *Terraform) diffWalkFn(
return nil
}
}
func (t *terraformProvider) init(vars map[string]string) error {
var err error
t.Once.Do(func() {
var c map[string]interface{}
if t.Config != nil {
c = t.Config.ReplaceVariables(vars).Config
}
_, err = t.Provider.Configure(c)
})
return err
}

View File

@ -221,6 +221,26 @@ func TestTerraformDiff_computed(t *testing.T) {
}
}
func TestTerraformDiff_providerInit(t *testing.T) {
tf := testTerraform(t, "diff-provider-init")
_, err := tf.Diff(nil)
if err != nil {
t.Fatalf("err: %s", err)
}
p := testProviderMock(testProvider(tf, "do_droplet.bar"))
if p == nil {
t.Fatal("should have provider")
}
if !p.ConfigureCalled {
t.Fatal("configure should be called")
}
if p.ConfigureConfig["foo"].(string) != "2" {
t.Fatalf("bad: %#v", p.ConfigureConfig)
}
}
func testConfig(t *testing.T, name string) *config.Config {
c, err := config.Load(filepath.Join(fixtureDir, name, "main.tf"))
if err != nil {

View File

@ -0,0 +1,9 @@
provider "do" {
foo = "${aws_instance.foo.num}"
}
resource "aws_instance" "foo" {
num = "2"
}
resource "do_droplet" "bar" {}