From 9018beda81f641b553bf5eaf40807b5dba1707fa Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 5 Jun 2014 11:30:48 -0700 Subject: [PATCH] terraform: placeholder key for computed values --- terraform/resource_provider.go | 13 +++++++++++++ terraform/resource_provider_mock.go | 10 ++++++++++ terraform/terraform_test.go | 20 +++++++++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/terraform/resource_provider.go b/terraform/resource_provider.go index 879c75ea2..544437869 100644 --- a/terraform/resource_provider.go +++ b/terraform/resource_provider.go @@ -1,5 +1,10 @@ package terraform +// ComputedPlaceholderKey is the configuration key given to Configure +// in a ResourceProvider that contains the value for the computed value +// placeholder when diffs are being done. +const ComputedPlaceholderKey = "tf_computed_placeholder" + // ResourceProvider is an interface that must be implemented by any // resource provider: the thing that creates and manages the resources in // a Terraform configuration. @@ -28,6 +33,14 @@ type ResourceProvider interface { map[string]interface{}) (ResourceDiff, error) } +// ResourceProviderCommonConfig contains the common configuration +// keys that are sent with every resource provider configuration. +// This can be used with something like mapstructure to extract +// the proper value. +type ResourceProviderCommonConfig struct { + TFComputedPlaceholder string `mapstructure:"tf_computed_placeholder"` +} + // ResourceType is a type of resource that a resource provider can manage. type ResourceType struct { Name string diff --git a/terraform/resource_provider_mock.go b/terraform/resource_provider_mock.go index 6854dc732..bce4051ac 100644 --- a/terraform/resource_provider_mock.go +++ b/terraform/resource_provider_mock.go @@ -1,5 +1,9 @@ package terraform +import ( + "github.com/mitchellh/mapstructure" +) + // MockResourceProvider implements ResourceProvider but mocks out all the // calls for testing purposes. type MockResourceProvider struct { @@ -7,6 +11,7 @@ type MockResourceProvider struct { Meta interface{} ConfigureCalled bool + ConfigureCommonConfig ResourceProviderCommonConfig ConfigureConfig map[string]interface{} ConfigureReturnWarnings []string ConfigureReturnError error @@ -23,6 +28,11 @@ type MockResourceProvider struct { func (p *MockResourceProvider) Configure(c map[string]interface{}) ([]string, error) { p.ConfigureCalled = true p.ConfigureConfig = c + + if err := mapstructure.Decode(&p.ConfigureCommonConfig, c); err != nil { + return nil, err + } + return p.ConfigureReturnWarnings, p.ConfigureReturnError } diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 09f157484..513d8a215 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -39,6 +39,20 @@ func TestNew(t *testing.T) { if testProviderName(mapping["do_droplet.bar"]) != "do" { t.Fatalf("bad: %#v", mapping) } + + /* + val := testProviderMock(mapping["aws_instance.foo"]). + ConfigureCommonConfig.TFComputedPlaceholder + if val == "" { + t.Fatal("should have computed placeholder") + } + + val = testProviderMock(mapping["aws_instance.bar"]). + ConfigureCommonConfig.TFComputedPlaceholder + if val == "" { + t.Fatal("should have computed placeholder") + } + */ } func TestNew_graphCycle(t *testing.T) { @@ -169,8 +183,12 @@ func testProviderFunc(n string, rs []string) ResourceProviderFactory { } } +func testProviderMock(p ResourceProvider) *MockResourceProvider { + return p.(*MockResourceProvider) +} + func testProviderName(p ResourceProvider) string { - return p.(*MockResourceProvider).Meta.(string) + return testProviderMock(p).Meta.(string) } func testResourceMapping(tf *Terraform) map[string]ResourceProvider {