2014-05-29 00:07:47 +02:00
|
|
|
package terraform
|
|
|
|
|
2014-12-12 23:21:20 +01:00
|
|
|
import "sync"
|
2014-07-10 22:38:04 +02:00
|
|
|
|
2014-05-29 00:07:47 +02:00
|
|
|
// MockResourceProvider implements ResourceProvider but mocks out all the
|
|
|
|
// calls for testing purposes.
|
|
|
|
type MockResourceProvider struct {
|
2014-07-10 22:38:04 +02:00
|
|
|
sync.Mutex
|
|
|
|
|
2014-06-04 00:08:00 +02:00
|
|
|
// Anything you want, in case you need to store extra data with the mock.
|
|
|
|
Meta interface{}
|
|
|
|
|
core: New ResourceProvider methods for data resources
This is a breaking change to the ResourceProvider interface that adds the
new operations relating to data sources.
DataSources, ValidateDataSource, ReadDataDiff and ReadDataApply are the
data source equivalents of Resources, Validate, Diff and Apply (respectively)
for managed resources.
The diff/apply model seems at first glance a rather strange workflow for
read-only resources, but implementing data resources in this way allows them
to fit cleanly into the standard plan/apply lifecycle in cases where the
configuration contains computed arguments and thus the read must be deferred
until apply time.
Along with breaking the interface, we also fix up the plugin client/server
and helper/schema implementations of it, which are all of the callers
used when provider plugins use helper/schema. This would be a breaking
change for any provider plugin that directly implements the provider
interface, but no known plugins do this and it is not recommended.
At the helper/schema layer the implementer sees ReadDataApply as a "Read",
as opposed to "Create" or "Update" as in the managed resource Apply
implementation. The planning mechanics are handled entirely within
helper/schema, so that complexity is hidden from the provider implementation
itself.
2016-05-08 06:55:32 +02:00
|
|
|
CloseCalled bool
|
|
|
|
CloseError error
|
|
|
|
InputCalled bool
|
|
|
|
InputInput UIInput
|
|
|
|
InputConfig *ResourceConfig
|
|
|
|
InputReturnConfig *ResourceConfig
|
|
|
|
InputReturnError error
|
|
|
|
InputFn func(UIInput, *ResourceConfig) (*ResourceConfig, error)
|
|
|
|
ApplyCalled bool
|
|
|
|
ApplyInfo *InstanceInfo
|
|
|
|
ApplyState *InstanceState
|
|
|
|
ApplyDiff *InstanceDiff
|
|
|
|
ApplyFn func(*InstanceInfo, *InstanceState, *InstanceDiff) (*InstanceState, error)
|
|
|
|
ApplyReturn *InstanceState
|
|
|
|
ApplyReturnError error
|
|
|
|
ConfigureCalled bool
|
|
|
|
ConfigureConfig *ResourceConfig
|
|
|
|
ConfigureFn func(*ResourceConfig) error
|
|
|
|
ConfigureReturnError error
|
|
|
|
DiffCalled bool
|
|
|
|
DiffInfo *InstanceInfo
|
|
|
|
DiffState *InstanceState
|
|
|
|
DiffDesired *ResourceConfig
|
|
|
|
DiffFn func(*InstanceInfo, *InstanceState, *ResourceConfig) (*InstanceDiff, error)
|
|
|
|
DiffReturn *InstanceDiff
|
|
|
|
DiffReturnError error
|
|
|
|
RefreshCalled bool
|
|
|
|
RefreshInfo *InstanceInfo
|
|
|
|
RefreshState *InstanceState
|
|
|
|
RefreshFn func(*InstanceInfo, *InstanceState) (*InstanceState, error)
|
|
|
|
RefreshReturn *InstanceState
|
|
|
|
RefreshReturnError error
|
|
|
|
ResourcesCalled bool
|
|
|
|
ResourcesReturn []ResourceType
|
|
|
|
ReadDataApplyCalled bool
|
|
|
|
ReadDataApplyInfo *InstanceInfo
|
|
|
|
ReadDataApplyDiff *InstanceDiff
|
|
|
|
ReadDataApplyFn func(*InstanceInfo, *InstanceDiff) (*InstanceState, error)
|
|
|
|
ReadDataApplyReturn *InstanceState
|
|
|
|
ReadDataApplyReturnError error
|
|
|
|
ReadDataDiffCalled bool
|
|
|
|
ReadDataDiffInfo *InstanceInfo
|
|
|
|
ReadDataDiffDesired *ResourceConfig
|
|
|
|
ReadDataDiffFn func(*InstanceInfo, *ResourceConfig) (*InstanceDiff, error)
|
|
|
|
ReadDataDiffReturn *InstanceDiff
|
|
|
|
ReadDataDiffReturnError error
|
2016-10-24 02:33:11 +02:00
|
|
|
StopCalled bool
|
|
|
|
StopFn func() error
|
|
|
|
StopReturnError error
|
core: New ResourceProvider methods for data resources
This is a breaking change to the ResourceProvider interface that adds the
new operations relating to data sources.
DataSources, ValidateDataSource, ReadDataDiff and ReadDataApply are the
data source equivalents of Resources, Validate, Diff and Apply (respectively)
for managed resources.
The diff/apply model seems at first glance a rather strange workflow for
read-only resources, but implementing data resources in this way allows them
to fit cleanly into the standard plan/apply lifecycle in cases where the
configuration contains computed arguments and thus the read must be deferred
until apply time.
Along with breaking the interface, we also fix up the plugin client/server
and helper/schema implementations of it, which are all of the callers
used when provider plugins use helper/schema. This would be a breaking
change for any provider plugin that directly implements the provider
interface, but no known plugins do this and it is not recommended.
At the helper/schema layer the implementer sees ReadDataApply as a "Read",
as opposed to "Create" or "Update" as in the managed resource Apply
implementation. The planning mechanics are handled entirely within
helper/schema, so that complexity is hidden from the provider implementation
itself.
2016-05-08 06:55:32 +02:00
|
|
|
DataSourcesCalled bool
|
|
|
|
DataSourcesReturn []DataSource
|
|
|
|
ValidateCalled bool
|
|
|
|
ValidateConfig *ResourceConfig
|
|
|
|
ValidateFn func(*ResourceConfig) ([]string, []error)
|
|
|
|
ValidateReturnWarns []string
|
|
|
|
ValidateReturnErrors []error
|
|
|
|
ValidateResourceFn func(string, *ResourceConfig) ([]string, []error)
|
|
|
|
ValidateResourceCalled bool
|
|
|
|
ValidateResourceType string
|
|
|
|
ValidateResourceConfig *ResourceConfig
|
|
|
|
ValidateResourceReturnWarns []string
|
|
|
|
ValidateResourceReturnErrors []error
|
|
|
|
ValidateDataSourceFn func(string, *ResourceConfig) ([]string, []error)
|
|
|
|
ValidateDataSourceCalled bool
|
|
|
|
ValidateDataSourceType string
|
|
|
|
ValidateDataSourceConfig *ResourceConfig
|
|
|
|
ValidateDataSourceReturnWarns []string
|
|
|
|
ValidateDataSourceReturnErrors []error
|
2016-04-26 19:56:41 +02:00
|
|
|
|
|
|
|
ImportStateCalled bool
|
|
|
|
ImportStateInfo *InstanceInfo
|
2016-05-04 21:40:45 +02:00
|
|
|
ImportStateID string
|
2016-04-26 19:56:41 +02:00
|
|
|
ImportStateReturn []*InstanceState
|
|
|
|
ImportStateReturnError error
|
2016-05-10 05:46:16 +02:00
|
|
|
ImportStateFn func(*InstanceInfo, string) ([]*InstanceState, error)
|
2014-06-13 07:30:09 +02:00
|
|
|
}
|
|
|
|
|
2015-06-29 19:33:37 +02:00
|
|
|
func (p *MockResourceProvider) Close() error {
|
|
|
|
p.CloseCalled = true
|
|
|
|
return p.CloseError
|
|
|
|
}
|
|
|
|
|
2014-09-29 08:50:37 +02:00
|
|
|
func (p *MockResourceProvider) Input(
|
|
|
|
input UIInput, c *ResourceConfig) (*ResourceConfig, error) {
|
|
|
|
p.InputCalled = true
|
|
|
|
p.InputInput = input
|
|
|
|
p.InputConfig = c
|
2014-09-29 18:13:15 +02:00
|
|
|
if p.InputFn != nil {
|
|
|
|
return p.InputFn(input, c)
|
|
|
|
}
|
2014-09-29 08:50:37 +02:00
|
|
|
return p.InputReturnConfig, p.InputReturnError
|
|
|
|
}
|
|
|
|
|
2014-06-13 07:30:09 +02:00
|
|
|
func (p *MockResourceProvider) Validate(c *ResourceConfig) ([]string, []error) {
|
2014-07-10 22:38:04 +02:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2014-06-13 07:30:09 +02:00
|
|
|
p.ValidateCalled = true
|
|
|
|
p.ValidateConfig = c
|
2014-09-25 06:38:23 +02:00
|
|
|
if p.ValidateFn != nil {
|
|
|
|
return p.ValidateFn(c)
|
|
|
|
}
|
2014-06-13 07:30:09 +02:00
|
|
|
return p.ValidateReturnWarns, p.ValidateReturnErrors
|
2014-05-29 00:07:47 +02:00
|
|
|
}
|
|
|
|
|
2014-07-03 05:35:03 +02:00
|
|
|
func (p *MockResourceProvider) ValidateResource(t string, c *ResourceConfig) ([]string, []error) {
|
2014-07-10 22:38:04 +02:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2014-07-03 05:35:03 +02:00
|
|
|
p.ValidateResourceCalled = true
|
|
|
|
p.ValidateResourceType = t
|
|
|
|
p.ValidateResourceConfig = c
|
2014-07-11 20:09:19 +02:00
|
|
|
|
|
|
|
if p.ValidateResourceFn != nil {
|
|
|
|
return p.ValidateResourceFn(t, c)
|
|
|
|
}
|
|
|
|
|
2014-07-03 05:35:03 +02:00
|
|
|
return p.ValidateResourceReturnWarns, p.ValidateResourceReturnErrors
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:59:59 +02:00
|
|
|
func (p *MockResourceProvider) Configure(c *ResourceConfig) error {
|
2014-07-10 22:38:04 +02:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2014-05-29 00:07:47 +02:00
|
|
|
p.ConfigureCalled = true
|
|
|
|
p.ConfigureConfig = c
|
2014-09-24 22:31:35 +02:00
|
|
|
|
|
|
|
if p.ConfigureFn != nil {
|
|
|
|
return p.ConfigureFn(c)
|
|
|
|
}
|
|
|
|
|
2014-06-06 09:28:57 +02:00
|
|
|
return p.ConfigureReturnError
|
2014-05-29 00:07:47 +02:00
|
|
|
}
|
2014-06-03 23:26:31 +02:00
|
|
|
|
2016-10-24 02:33:11 +02:00
|
|
|
func (p *MockResourceProvider) Stop() error {
|
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
|
|
|
p.StopCalled = true
|
|
|
|
if p.StopFn != nil {
|
|
|
|
return p.StopFn()
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.StopReturnError
|
|
|
|
}
|
|
|
|
|
2014-06-19 00:35:03 +02:00
|
|
|
func (p *MockResourceProvider) Apply(
|
2014-09-17 01:20:11 +02:00
|
|
|
info *InstanceInfo,
|
|
|
|
state *InstanceState,
|
2014-09-18 01:33:24 +02:00
|
|
|
diff *InstanceDiff) (*InstanceState, error) {
|
2015-10-14 19:43:51 +02:00
|
|
|
// We only lock while writing data. Reading is fine
|
2014-07-10 22:38:04 +02:00
|
|
|
p.Lock()
|
2014-06-19 00:35:03 +02:00
|
|
|
p.ApplyCalled = true
|
2014-09-17 01:20:11 +02:00
|
|
|
p.ApplyInfo = info
|
2014-06-19 00:35:03 +02:00
|
|
|
p.ApplyState = state
|
|
|
|
p.ApplyDiff = diff
|
2015-10-14 19:43:51 +02:00
|
|
|
p.Unlock()
|
|
|
|
|
2014-06-19 00:35:03 +02:00
|
|
|
if p.ApplyFn != nil {
|
2014-09-17 01:20:11 +02:00
|
|
|
return p.ApplyFn(info, state, diff)
|
2014-06-19 00:35:03 +02:00
|
|
|
}
|
|
|
|
|
2016-10-18 01:29:24 +02:00
|
|
|
return p.ApplyReturn.DeepCopy(), p.ApplyReturnError
|
2014-06-19 00:35:03 +02:00
|
|
|
}
|
|
|
|
|
2014-06-04 01:42:21 +02:00
|
|
|
func (p *MockResourceProvider) Diff(
|
2014-09-17 01:20:11 +02:00
|
|
|
info *InstanceInfo,
|
|
|
|
state *InstanceState,
|
2014-09-18 01:33:24 +02:00
|
|
|
desired *ResourceConfig) (*InstanceDiff, error) {
|
2014-07-10 22:38:04 +02:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2014-06-05 11:32:10 +02:00
|
|
|
p.DiffCalled = true
|
2014-09-17 01:20:11 +02:00
|
|
|
p.DiffInfo = info
|
2014-06-05 11:32:10 +02:00
|
|
|
p.DiffState = state
|
|
|
|
p.DiffDesired = desired
|
2014-06-05 15:57:06 +02:00
|
|
|
if p.DiffFn != nil {
|
2014-09-17 01:20:11 +02:00
|
|
|
return p.DiffFn(info, state, desired)
|
2014-06-05 15:57:06 +02:00
|
|
|
}
|
|
|
|
|
2016-10-18 01:29:24 +02:00
|
|
|
return p.DiffReturn.DeepCopy(), p.DiffReturnError
|
2014-06-04 01:42:21 +02:00
|
|
|
}
|
|
|
|
|
2014-06-20 06:22:07 +02:00
|
|
|
func (p *MockResourceProvider) Refresh(
|
2014-09-17 01:20:11 +02:00
|
|
|
info *InstanceInfo,
|
|
|
|
s *InstanceState) (*InstanceState, error) {
|
2014-07-10 22:38:04 +02:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2014-06-20 06:22:07 +02:00
|
|
|
p.RefreshCalled = true
|
2014-09-17 01:20:11 +02:00
|
|
|
p.RefreshInfo = info
|
2014-06-20 06:22:07 +02:00
|
|
|
p.RefreshState = s
|
|
|
|
|
|
|
|
if p.RefreshFn != nil {
|
2014-09-17 01:20:11 +02:00
|
|
|
return p.RefreshFn(info, s)
|
2014-06-20 06:22:07 +02:00
|
|
|
}
|
|
|
|
|
2016-10-18 01:29:24 +02:00
|
|
|
return p.RefreshReturn.DeepCopy(), p.RefreshReturnError
|
2014-06-20 06:22:07 +02:00
|
|
|
}
|
|
|
|
|
2014-06-03 23:26:31 +02:00
|
|
|
func (p *MockResourceProvider) Resources() []ResourceType {
|
2014-07-10 22:38:04 +02:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
2014-06-03 23:26:31 +02:00
|
|
|
p.ResourcesCalled = true
|
|
|
|
return p.ResourcesReturn
|
|
|
|
}
|
2016-04-26 19:56:41 +02:00
|
|
|
|
2016-05-04 21:40:45 +02:00
|
|
|
func (p *MockResourceProvider) ImportState(info *InstanceInfo, id string) ([]*InstanceState, error) {
|
2016-04-26 19:56:41 +02:00
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
|
|
|
p.ImportStateCalled = true
|
|
|
|
p.ImportStateInfo = info
|
2016-05-04 21:40:45 +02:00
|
|
|
p.ImportStateID = id
|
2016-04-26 19:56:41 +02:00
|
|
|
if p.ImportStateFn != nil {
|
2016-05-10 05:46:16 +02:00
|
|
|
return p.ImportStateFn(info, id)
|
2016-04-26 19:56:41 +02:00
|
|
|
}
|
|
|
|
|
2016-10-18 01:29:24 +02:00
|
|
|
var result []*InstanceState
|
|
|
|
if p.ImportStateReturn != nil {
|
|
|
|
result = make([]*InstanceState, len(p.ImportStateReturn))
|
|
|
|
for i, v := range p.ImportStateReturn {
|
|
|
|
result[i] = v.DeepCopy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, p.ImportStateReturnError
|
2016-04-26 19:56:41 +02:00
|
|
|
}
|
core: New ResourceProvider methods for data resources
This is a breaking change to the ResourceProvider interface that adds the
new operations relating to data sources.
DataSources, ValidateDataSource, ReadDataDiff and ReadDataApply are the
data source equivalents of Resources, Validate, Diff and Apply (respectively)
for managed resources.
The diff/apply model seems at first glance a rather strange workflow for
read-only resources, but implementing data resources in this way allows them
to fit cleanly into the standard plan/apply lifecycle in cases where the
configuration contains computed arguments and thus the read must be deferred
until apply time.
Along with breaking the interface, we also fix up the plugin client/server
and helper/schema implementations of it, which are all of the callers
used when provider plugins use helper/schema. This would be a breaking
change for any provider plugin that directly implements the provider
interface, but no known plugins do this and it is not recommended.
At the helper/schema layer the implementer sees ReadDataApply as a "Read",
as opposed to "Create" or "Update" as in the managed resource Apply
implementation. The planning mechanics are handled entirely within
helper/schema, so that complexity is hidden from the provider implementation
itself.
2016-05-08 06:55:32 +02:00
|
|
|
|
|
|
|
func (p *MockResourceProvider) ValidateDataSource(t string, c *ResourceConfig) ([]string, []error) {
|
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
|
|
|
p.ValidateDataSourceCalled = true
|
|
|
|
p.ValidateDataSourceType = t
|
|
|
|
p.ValidateDataSourceConfig = c
|
|
|
|
|
|
|
|
if p.ValidateDataSourceFn != nil {
|
|
|
|
return p.ValidateDataSourceFn(t, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.ValidateDataSourceReturnWarns, p.ValidateDataSourceReturnErrors
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MockResourceProvider) ReadDataDiff(
|
|
|
|
info *InstanceInfo,
|
|
|
|
desired *ResourceConfig) (*InstanceDiff, error) {
|
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
|
|
|
p.ReadDataDiffCalled = true
|
|
|
|
p.ReadDataDiffInfo = info
|
|
|
|
p.ReadDataDiffDesired = desired
|
|
|
|
if p.ReadDataDiffFn != nil {
|
|
|
|
return p.ReadDataDiffFn(info, desired)
|
|
|
|
}
|
|
|
|
|
2016-10-18 01:29:24 +02:00
|
|
|
return p.ReadDataDiffReturn.DeepCopy(), p.ReadDataDiffReturnError
|
core: New ResourceProvider methods for data resources
This is a breaking change to the ResourceProvider interface that adds the
new operations relating to data sources.
DataSources, ValidateDataSource, ReadDataDiff and ReadDataApply are the
data source equivalents of Resources, Validate, Diff and Apply (respectively)
for managed resources.
The diff/apply model seems at first glance a rather strange workflow for
read-only resources, but implementing data resources in this way allows them
to fit cleanly into the standard plan/apply lifecycle in cases where the
configuration contains computed arguments and thus the read must be deferred
until apply time.
Along with breaking the interface, we also fix up the plugin client/server
and helper/schema implementations of it, which are all of the callers
used when provider plugins use helper/schema. This would be a breaking
change for any provider plugin that directly implements the provider
interface, but no known plugins do this and it is not recommended.
At the helper/schema layer the implementer sees ReadDataApply as a "Read",
as opposed to "Create" or "Update" as in the managed resource Apply
implementation. The planning mechanics are handled entirely within
helper/schema, so that complexity is hidden from the provider implementation
itself.
2016-05-08 06:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MockResourceProvider) ReadDataApply(
|
|
|
|
info *InstanceInfo,
|
|
|
|
d *InstanceDiff) (*InstanceState, error) {
|
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
|
|
|
p.ReadDataApplyCalled = true
|
|
|
|
p.ReadDataApplyInfo = info
|
|
|
|
p.ReadDataApplyDiff = d
|
|
|
|
|
|
|
|
if p.ReadDataApplyFn != nil {
|
|
|
|
return p.ReadDataApplyFn(info, d)
|
|
|
|
}
|
|
|
|
|
2016-10-18 01:29:24 +02:00
|
|
|
return p.ReadDataApplyReturn.DeepCopy(), p.ReadDataApplyReturnError
|
core: New ResourceProvider methods for data resources
This is a breaking change to the ResourceProvider interface that adds the
new operations relating to data sources.
DataSources, ValidateDataSource, ReadDataDiff and ReadDataApply are the
data source equivalents of Resources, Validate, Diff and Apply (respectively)
for managed resources.
The diff/apply model seems at first glance a rather strange workflow for
read-only resources, but implementing data resources in this way allows them
to fit cleanly into the standard plan/apply lifecycle in cases where the
configuration contains computed arguments and thus the read must be deferred
until apply time.
Along with breaking the interface, we also fix up the plugin client/server
and helper/schema implementations of it, which are all of the callers
used when provider plugins use helper/schema. This would be a breaking
change for any provider plugin that directly implements the provider
interface, but no known plugins do this and it is not recommended.
At the helper/schema layer the implementer sees ReadDataApply as a "Read",
as opposed to "Create" or "Update" as in the managed resource Apply
implementation. The planning mechanics are handled entirely within
helper/schema, so that complexity is hidden from the provider implementation
itself.
2016-05-08 06:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MockResourceProvider) DataSources() []DataSource {
|
|
|
|
p.Lock()
|
|
|
|
defer p.Unlock()
|
|
|
|
|
|
|
|
p.DataSourcesCalled = true
|
|
|
|
return p.DataSourcesReturn
|
|
|
|
}
|