diff --git a/internal/providers/testing/provider_mock.go b/internal/providers/testing/provider_mock.go index 1e9ac5c161..fdde33d9ff 100644 --- a/internal/providers/testing/provider_mock.go +++ b/internal/providers/testing/provider_mock.go @@ -152,12 +152,12 @@ type MockProvider struct { DeleteStateFn func(providers.DeleteStateRequest) providers.DeleteStateResponse PlanActionCalled bool - PlanActionResponse providers.PlanActionResponse + PlanActionResponse *providers.PlanActionResponse PlanActionRequest providers.PlanActionRequest PlanActionFn func(providers.PlanActionRequest) providers.PlanActionResponse InvokeActionCalled bool - InvokeActionResponse providers.InvokeActionResponse + InvokeActionResponse *providers.InvokeActionResponse InvokeActionRequest providers.InvokeActionRequest InvokeActionFn func(providers.InvokeActionRequest) providers.InvokeActionResponse @@ -1025,7 +1025,11 @@ func (p *MockProvider) PlanAction(r providers.PlanActionRequest) (resp providers return p.PlanActionFn(r) } - return p.PlanActionResponse + if p.PlanActionResponse != nil { + return *p.PlanActionResponse + } + + return resp } func (p *MockProvider) InvokeAction(r providers.InvokeActionRequest) (resp providers.InvokeActionResponse) { @@ -1039,7 +1043,11 @@ func (p *MockProvider) InvokeAction(r providers.InvokeActionRequest) (resp provi return p.InvokeActionFn(r) } - return p.InvokeActionResponse + if p.InvokeActionResponse != nil { + return *p.InvokeActionResponse + } + + return resp } func (p *MockProvider) Close() error { diff --git a/internal/terraform/context_validate_test.go b/internal/terraform/context_validate_test.go index 48cf132602..67beadb889 100644 --- a/internal/terraform/context_validate_test.go +++ b/internal/terraform/context_validate_test.go @@ -3667,7 +3667,7 @@ resource "test_instance" "foo" { }, }, }, - Actions: map[string]providers.ActionSchema{ + Actions: map[string]*providers.ActionSchema{ "test_register": { ConfigSchema: &configschema.Block{ Attributes: map[string]*configschema.Attribute{ diff --git a/internal/terraform/node_action_trigger_apply.go b/internal/terraform/node_action_trigger_apply.go index 13c8dcadc3..f0e6ec8b32 100644 --- a/internal/terraform/node_action_trigger_apply.go +++ b/internal/terraform/node_action_trigger_apply.go @@ -148,24 +148,33 @@ func (n *nodeActionTriggerApply) Execute(ctx EvalContext, wo walkOperation) tfdi return diags } - for event := range resp.Events { - switch ev := event.(type) { - case providers.InvokeActionEvent_Progress: - ctx.Hook(func(h Hook) (HookAction, error) { - return h.ProgressAction(hookIdentity, ev.Message) - }) - case providers.InvokeActionEvent_Completed: - // Enhance the diagnostics - diags = diags.Append(n.AddSubjectToDiagnostics(ev.Diagnostics)) - ctx.Hook(func(h Hook) (HookAction, error) { - return h.CompleteAction(hookIdentity, ev.Diagnostics.Err()) - }) - if ev.Diagnostics.HasErrors() { - return diags + if resp.Events != nil { // should only occur in misconfigured tests + for event := range resp.Events { + switch ev := event.(type) { + case providers.InvokeActionEvent_Progress: + ctx.Hook(func(h Hook) (HookAction, error) { + return h.ProgressAction(hookIdentity, ev.Message) + }) + case providers.InvokeActionEvent_Completed: + // Enhance the diagnostics + diags = diags.Append(n.AddSubjectToDiagnostics(ev.Diagnostics)) + ctx.Hook(func(h Hook) (HookAction, error) { + return h.CompleteAction(hookIdentity, ev.Diagnostics.Err()) + }) + if ev.Diagnostics.HasErrors() { + return diags + } + default: + panic(fmt.Sprintf("unexpected action event type %T", ev)) } - default: - panic(fmt.Sprintf("unexpected action event type %T", ev)) } + } else { + diags = diags.Append(&hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Provider return invalid response", + Detail: "Provider response did not include any events", + Subject: n.ActionTriggerRange, + }) } return diags @@ -202,12 +211,12 @@ func (n *nodeActionTriggerApply) References() []*addrs.Reference { return refs } -// GraphNodeModulePath +// GraphNodeReferencer func (n *nodeActionTriggerApply) ModulePath() addrs.Module { return n.ActionInvocation.Addr.Module.Module() } -// GraphNodeModuleInstance +// GraphNodeExecutable func (n *nodeActionTriggerApply) Path() addrs.ModuleInstance { return n.ActionInvocation.Addr.Module } diff --git a/internal/terraform/resource_provider_mock_test.go b/internal/terraform/resource_provider_mock_test.go index 693d53e37d..ceaea28951 100644 --- a/internal/terraform/resource_provider_mock_test.go +++ b/internal/terraform/resource_provider_mock_test.go @@ -51,21 +51,21 @@ func mockProviderWithResourceTypeSchema(name string, schema *configschema.Block) } } -// simpleMockProvider returns a MockProvider that is pre-configured -// with schema for its own config, for a resource type called "test_object" and -// for a data source also called "test_object". +// simpleMockProvider returns a MockProvider that is pre-configured with schema +// for its own config, a resource type called "test_object", a data source also +// called "test_object", and an unlinked (generic) action called "test_action". // -// All three schemas have the same content as returned by function +// All four schemas have the same content as returned by function // simpleTestSchema. // // For most reasonable uses the returned provider must be registered in a -// componentFactory under the name "test". Use simpleMockComponentFactory -// to obtain a pre-configured componentFactory containing the result of -// this function along with simpleMockProvisioner, both registered as "test". +// componentFactory under the name "test". Use simpleMockComponentFactory to +// obtain a pre-configured componentFactory containing the result of this +// function along with simpleMockProvisioner, both registered as "test". // // The returned provider has no other behaviors by default, but the caller may -// modify it in order to stub any other required functionality, or modify -// the default schema stored in the field GetSchemaReturn. Each new call to +// modify it in order to stub any other required functionality, or modify the +// default schema stored in the field GetSchemaReturn. Each new call to // simpleTestProvider produces entirely new instances of all of the nested // objects so that callers can mutate without affecting mock objects. func simpleMockProvider() *testing_provider.MockProvider { @@ -73,10 +73,13 @@ func simpleMockProvider() *testing_provider.MockProvider { GetProviderSchemaResponse: &providers.GetProviderSchemaResponse{ Provider: providers.Schema{Body: simpleTestSchema()}, ResourceTypes: map[string]providers.Schema{ - "test_object": providers.Schema{Body: simpleTestSchema()}, + "test_object": {Body: simpleTestSchema()}, }, DataSources: map[string]providers.Schema{ - "test_object": providers.Schema{Body: simpleTestSchema()}, + "test_object": {Body: simpleTestSchema()}, + }, + Actions: map[string]providers.ActionSchema{ + "test_action": {ConfigSchema: simpleTestSchema(), Unlinked: &providers.UnlinkedAction{}}, }, }, } @@ -91,6 +94,7 @@ func getProviderSchema(p *testing_provider.MockProvider) *providerSchema { ResourceTypes: make(map[string]*configschema.Block), ResourceTypeSchemaVersions: make(map[string]uint64), DataSources: make(map[string]*configschema.Block), + Actions: make(map[string]*providers.ActionSchema), } } @@ -102,6 +106,7 @@ func getProviderSchema(p *testing_provider.MockProvider) *providerSchema { ResourceTypes: map[string]*configschema.Block{}, DataSources: map[string]*configschema.Block{}, ResourceTypeSchemaVersions: map[string]uint64{}, + Actions: map[string]*providers.ActionSchema{}, } for resType, s := range resp.ResourceTypes { @@ -113,6 +118,10 @@ func getProviderSchema(p *testing_provider.MockProvider) *providerSchema { schema.DataSources[dataSource] = s.Body } + for action, s := range resp.Actions { + schema.Actions[action] = &s + } + return schema } @@ -128,7 +137,7 @@ type providerSchema struct { IdentityTypeSchemaVersions map[string]uint64 ListResourceTypes map[string]*configschema.Block ListResourceTypeSchemaVersions map[string]uint64 - Actions map[string]providers.ActionSchema + Actions map[string]*providers.ActionSchema } // getProviderSchemaResponseFromProviderSchema is a test helper to convert a @@ -171,7 +180,7 @@ func getProviderSchemaResponseFromProviderSchema(providerSchema *providerSchema) } for name, schema := range providerSchema.Actions { - resp.Actions[name] = schema + resp.Actions[name] = *schema } return resp