terraform: add ValidateResource API to ResourceProvider

This commit is contained in:
Mitchell Hashimoto 2014-07-02 20:35:03 -07:00
parent 770d4e1e71
commit d0577fda02
2 changed files with 55 additions and 26 deletions

View File

@ -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

View File

@ -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