From d0577fda02351c73ce3d3e1e6f5e5499791865b6 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 2 Jul 2014 20:35:03 -0700 Subject: [PATCH] terraform: add ValidateResource API to ResourceProvider --- terraform/resource_provider.go | 17 ++++++++ terraform/resource_provider_mock.go | 64 +++++++++++++++++------------ 2 files changed, 55 insertions(+), 26 deletions(-) diff --git a/terraform/resource_provider.go b/terraform/resource_provider.go index 75410c2ea..554d31c4f 100644 --- a/terraform/resource_provider.go +++ b/terraform/resource_provider.go @@ -17,14 +17,31 @@ type ResourceProvider interface { // (no interpolation done) and can return a list of warnings and/or // errors. // + // This is called once with the provider configuration only. It may not + // be called at all if no provider configuration is given. + // // This should not assume that any values of the configurations are valid. // The primary use case of this call is to check that required keys are // set. Validate(*ResourceConfig) ([]string, []error) + // ValidateResource is called once at the beginning with the raw + // configuration (no interpolation done) and can return a list of warnings + // and/or errors. + // + // This is called once per resource. + // + // This should not assume any of the values in the resource configuration + // are valid since it is possible they have to be interpolated still. + // The primary use case of this call is to check that the required keys + // are set and that the general structure is correct. + ValidateResource(string, *ResourceConfig) ([]string, []error) + // Configure configures the provider itself with the configuration // given. This is useful for setting things like access keys. // + // This won't be called at all if no provider configuration is given. + // // Configure returns an error if it occurred. Configure(*ResourceConfig) error diff --git a/terraform/resource_provider_mock.go b/terraform/resource_provider_mock.go index 9532b4efb..595e20f47 100644 --- a/terraform/resource_provider_mock.go +++ b/terraform/resource_provider_mock.go @@ -6,32 +6,37 @@ type MockResourceProvider struct { // Anything you want, in case you need to store extra data with the mock. Meta interface{} - ApplyCalled bool - ApplyState *ResourceState - ApplyDiff *ResourceDiff - ApplyFn func(*ResourceState, *ResourceDiff) (*ResourceState, error) - ApplyReturn *ResourceState - ApplyReturnError error - ConfigureCalled bool - ConfigureConfig *ResourceConfig - ConfigureReturnError error - DiffCalled bool - DiffState *ResourceState - DiffDesired *ResourceConfig - DiffFn func(*ResourceState, *ResourceConfig) (*ResourceDiff, error) - DiffReturn *ResourceDiff - DiffReturnError error - RefreshCalled bool - RefreshState *ResourceState - RefreshFn func(*ResourceState) (*ResourceState, error) - RefreshReturn *ResourceState - RefreshReturnError error - ResourcesCalled bool - ResourcesReturn []ResourceType - ValidateCalled bool - ValidateConfig *ResourceConfig - ValidateReturnWarns []string - ValidateReturnErrors []error + ApplyCalled bool + ApplyState *ResourceState + ApplyDiff *ResourceDiff + ApplyFn func(*ResourceState, *ResourceDiff) (*ResourceState, error) + ApplyReturn *ResourceState + ApplyReturnError error + ConfigureCalled bool + ConfigureConfig *ResourceConfig + ConfigureReturnError error + DiffCalled bool + DiffState *ResourceState + DiffDesired *ResourceConfig + DiffFn func(*ResourceState, *ResourceConfig) (*ResourceDiff, error) + DiffReturn *ResourceDiff + DiffReturnError error + RefreshCalled bool + RefreshState *ResourceState + RefreshFn func(*ResourceState) (*ResourceState, error) + RefreshReturn *ResourceState + RefreshReturnError error + ResourcesCalled bool + ResourcesReturn []ResourceType + ValidateCalled bool + ValidateConfig *ResourceConfig + ValidateReturnWarns []string + ValidateReturnErrors []error + ValidateResourceCalled bool + ValidateResourceType string + ValidateResourceConfig *ResourceConfig + ValidateResourceReturnWarns []string + ValidateResourceReturnErrors []error } func (p *MockResourceProvider) Validate(c *ResourceConfig) ([]string, []error) { @@ -40,6 +45,13 @@ func (p *MockResourceProvider) Validate(c *ResourceConfig) ([]string, []error) { return p.ValidateReturnWarns, p.ValidateReturnErrors } +func (p *MockResourceProvider) ValidateResource(t string, c *ResourceConfig) ([]string, []error) { + p.ValidateResourceCalled = true + p.ValidateResourceType = t + p.ValidateResourceConfig = c + return p.ValidateResourceReturnWarns, p.ValidateResourceReturnErrors +} + func (p *MockResourceProvider) Configure(c *ResourceConfig) error { p.ConfigureCalled = true p.ConfigureConfig = c