tofu: Context.Import now takes a context.Context

This continues our ongoing effort to get a coherent chain of
context.Context all the way from "package main" to all of our calls to
external components.

Context.Import doesn't yet do anything with its new context, but we'll
plumb this deeper in future.

OpenTofu has some historical situational private uses of context.Context
to handle the graceful shutdown behaviors. Those use context.Context as
a private implementation detail rather than public API, and so this commit
leaves them as-is and adds a new "primary context" alongside. Hopefully
in future refactoring we can simplify this to use the primary context also
as the primary cancellation signal, but that's too risky a change to bundle
in with this otherwise-mostly-harmless context plumbing.

All of the _test.go file updates here are purely mechanical additions of
the extra argument. No test is materially modified by this change, which
is intentional to get some assurance that isn't a breaking change.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
This commit is contained in:
Martin Atkins 2024-11-12 15:38:03 -08:00
parent 3b79efa834
commit 8ae790ca06
3 changed files with 26 additions and 22 deletions

View File

@ -30,6 +30,8 @@ type ImportCommand struct {
}
func (c *ImportCommand) Run(args []string) int {
ctx := c.CommandContext()
// Get the pwd since its our default -config flag value
pwd, err := os.Getwd()
if err != nil {
@ -243,7 +245,7 @@ func (c *ImportCommand) Run(args []string) int {
// Perform the import. Note that as you can see it is possible for this
// API to import more than one resource at once. For now, we only allow
// one while we stabilize this feature.
newState, importDiags := lr.Core.Import(lr.Config, lr.InputState, &tofu.ImportOpts{
newState, importDiags := lr.Core.Import(ctx, lr.Config, lr.InputState, &tofu.ImportOpts{
Targets: []*tofu.ImportTarget{
{
CommandLineImportTarget: &tofu.CommandLineImportTarget{

View File

@ -6,6 +6,7 @@
package tofu
import (
"context"
"fmt"
"log"
"sync"
@ -238,7 +239,7 @@ func (ri *ImportResolver) GetImport(address addrs.AbsResourceInstance) *Evaluate
// Further, this operation also gracefully handles partial state. If during
// an import there is a failure, all previously imported resources remain
// imported.
func (c *Context) Import(config *configs.Config, prevRunState *states.State, opts *ImportOpts) (*states.State, tfdiags.Diagnostics) {
func (c *Context) Import(ctx context.Context, config *configs.Config, prevRunState *states.State, opts *ImportOpts) (*states.State, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
// Hold a lock since we can modify our own state here

View File

@ -6,6 +6,7 @@
package tofu
import (
"context"
"errors"
"fmt"
"log"
@ -43,7 +44,7 @@ func TestContextImport_basic(t *testing.T) {
},
}
state, diags := ctx.Import(m, states.NewState(), &ImportOpts{
state, diags := ctx.Import(context.Background(), m, states.NewState(), &ImportOpts{
Targets: []*ImportTarget{
{
CommandLineImportTarget: &CommandLineImportTarget{
@ -96,7 +97,7 @@ resource "aws_instance" "foo" {
},
}
state, diags := ctx.Import(m, states.NewState(), &ImportOpts{
state, diags := ctx.Import(context.Background(), m, states.NewState(), &ImportOpts{
Targets: []*ImportTarget{
{
CommandLineImportTarget: &CommandLineImportTarget{
@ -253,7 +254,7 @@ func TestContextImport_multiInstanceProviderConfig(t *testing.T) {
)
t.Logf("importing into %s, which should succeed because it's configured", existingInstanceAddr)
log.Printf("[TRACE] TestContextImport_multiInstanceProviderConfig: importing into %s, which should succeed because it's configured", existingInstanceAddr)
state, diags := ctx.Import(m, states.NewState(), &ImportOpts{
state, diags := ctx.Import(context.Background(), m, states.NewState(), &ImportOpts{
Targets: []*ImportTarget{
{
CommandLineImportTarget: &CommandLineImportTarget{
@ -349,7 +350,7 @@ resource "aws_instance" "foo" {
}),
}
state, diags := ctx.Import(m, states.NewState(), &ImportOpts{
state, diags := ctx.Import(context.Background(), m, states.NewState(), &ImportOpts{
Targets: []*ImportTarget{
{
CommandLineImportTarget: &CommandLineImportTarget{
@ -418,7 +419,7 @@ func TestContextImport_collision(t *testing.T) {
},
}
state, diags := ctx.Import(m, state, &ImportOpts{
state, diags := ctx.Import(context.Background(), m, state, &ImportOpts{
Targets: []*ImportTarget{
{
CommandLineImportTarget: &CommandLineImportTarget{
@ -464,7 +465,7 @@ func TestContextImport_missingType(t *testing.T) {
},
})
state, diags := ctx.Import(m, states.NewState(), &ImportOpts{
state, diags := ctx.Import(context.Background(), m, states.NewState(), &ImportOpts{
Targets: []*ImportTarget{
{
CommandLineImportTarget: &CommandLineImportTarget{
@ -517,7 +518,7 @@ func TestContextImport_moduleProvider(t *testing.T) {
},
})
state, diags := ctx.Import(m, states.NewState(), &ImportOpts{
state, diags := ctx.Import(context.Background(), m, states.NewState(), &ImportOpts{
Targets: []*ImportTarget{
{
CommandLineImportTarget: &CommandLineImportTarget{
@ -574,7 +575,7 @@ func TestContextImport_providerModule(t *testing.T) {
return
}
_, diags := ctx.Import(m, states.NewState(), &ImportOpts{
_, diags := ctx.Import(context.Background(), m, states.NewState(), &ImportOpts{
Targets: []*ImportTarget{
{
CommandLineImportTarget: &CommandLineImportTarget{
@ -632,7 +633,7 @@ func TestContextImport_providerConfig(t *testing.T) {
},
}
state, diags := ctx.Import(m, states.NewState(), &ImportOpts{
state, diags := ctx.Import(context.Background(), m, states.NewState(), &ImportOpts{
Targets: []*ImportTarget{
{
CommandLineImportTarget: &CommandLineImportTarget{
@ -694,7 +695,7 @@ func TestContextImport_providerConfigResources(t *testing.T) {
},
}
_, diags := ctx.Import(m, states.NewState(), &ImportOpts{
_, diags := ctx.Import(context.Background(), m, states.NewState(), &ImportOpts{
Targets: []*ImportTarget{
{
CommandLineImportTarget: &CommandLineImportTarget{
@ -767,7 +768,7 @@ data "aws_data_source" "bar" {
}),
}
state, diags := ctx.Import(m, states.NewState(), &ImportOpts{
state, diags := ctx.Import(context.Background(), m, states.NewState(), &ImportOpts{
Targets: []*ImportTarget{
{
CommandLineImportTarget: &CommandLineImportTarget{
@ -820,7 +821,7 @@ func TestContextImport_refreshNil(t *testing.T) {
}
}
state, diags := ctx.Import(m, states.NewState(), &ImportOpts{
state, diags := ctx.Import(context.Background(), m, states.NewState(), &ImportOpts{
Targets: []*ImportTarget{
{
CommandLineImportTarget: &CommandLineImportTarget{
@ -863,7 +864,7 @@ func TestContextImport_module(t *testing.T) {
},
}
state, diags := ctx.Import(m, states.NewState(), &ImportOpts{
state, diags := ctx.Import(context.Background(), m, states.NewState(), &ImportOpts{
Targets: []*ImportTarget{
{
CommandLineImportTarget: &CommandLineImportTarget{
@ -906,7 +907,7 @@ func TestContextImport_moduleDepth2(t *testing.T) {
},
}
state, diags := ctx.Import(m, states.NewState(), &ImportOpts{
state, diags := ctx.Import(context.Background(), m, states.NewState(), &ImportOpts{
Targets: []*ImportTarget{
{
CommandLineImportTarget: &CommandLineImportTarget{
@ -949,7 +950,7 @@ func TestContextImport_moduleDiff(t *testing.T) {
},
}
state, diags := ctx.Import(m, states.NewState(), &ImportOpts{
state, diags := ctx.Import(context.Background(), m, states.NewState(), &ImportOpts{
Targets: []*ImportTarget{
{
CommandLineImportTarget: &CommandLineImportTarget{
@ -1019,7 +1020,7 @@ func TestContextImport_multiState(t *testing.T) {
},
})
state, diags := ctx.Import(m, states.NewState(), &ImportOpts{
state, diags := ctx.Import(context.Background(), m, states.NewState(), &ImportOpts{
Targets: []*ImportTarget{
{
CommandLineImportTarget: &CommandLineImportTarget{
@ -1095,7 +1096,7 @@ func TestContextImport_multiStateSame(t *testing.T) {
},
})
state, diags := ctx.Import(m, states.NewState(), &ImportOpts{
state, diags := ctx.Import(context.Background(), m, states.NewState(), &ImportOpts{
Targets: []*ImportTarget{
{
CommandLineImportTarget: &CommandLineImportTarget{
@ -1191,7 +1192,7 @@ resource "test_resource" "unused" {
},
})
state, diags := ctx.Import(m, states.NewState(), &ImportOpts{
state, diags := ctx.Import(context.Background(), m, states.NewState(), &ImportOpts{
Targets: []*ImportTarget{
{
CommandLineImportTarget: &CommandLineImportTarget{
@ -1263,7 +1264,7 @@ resource "test_resource" "test" {
},
})
state, diags := ctx.Import(m, states.NewState(), &ImportOpts{
state, diags := ctx.Import(context.Background(), m, states.NewState(), &ImportOpts{
Targets: []*ImportTarget{
{
CommandLineImportTarget: &CommandLineImportTarget{
@ -1310,7 +1311,7 @@ func TestContextImport_33572(t *testing.T) {
},
}
state, diags := ctx.Import(m, states.NewState(), &ImportOpts{
state, diags := ctx.Import(context.Background(), m, states.NewState(), &ImportOpts{
Targets: []*ImportTarget{
{
CommandLineImportTarget: &CommandLineImportTarget{