Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform import bulk #23926

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
251 changes: 136 additions & 115 deletions command/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,39 +53,9 @@ func (c *ImportCommand) Run(args []string) int {
}

args = cmdFlags.Args()
if len(args) != 2 {
c.Ui.Error("The import command expects two arguments.")
cmdFlags.Usage()
return 1
}

var diags tfdiags.Diagnostics

// Parse the provided resource address.
traversalSrc := []byte(args[0])
traversal, travDiags := hclsyntax.ParseTraversalAbs(traversalSrc, "<import-address>", hcl.Pos{Line: 1, Column: 1})
diags = diags.Append(travDiags)
if travDiags.HasErrors() {
c.registerSynthConfigSource("<import-address>", traversalSrc) // so we can include a source snippet
c.showDiagnostics(diags)
c.Ui.Info(importCommandInvalidAddressReference)
return 1
}
addr, addrDiags := addrs.ParseAbsResourceInstance(traversal)
diags = diags.Append(addrDiags)
if addrDiags.HasErrors() {
c.registerSynthConfigSource("<import-address>", traversalSrc) // so we can include a source snippet
c.showDiagnostics(diags)
c.Ui.Info(importCommandInvalidAddressReference)
return 1
}

if addr.Resource.Resource.Mode != addrs.ManagedResourceMode {
diags = diags.Append(errors.New("A managed resource address is required. Importing into a data resource is not allowed."))
c.showDiagnostics(diags)
return 1
}

if !c.dirIsConfigPath(configPath) {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Expand All @@ -108,83 +78,13 @@ func (c *ImportCommand) Run(args []string) int {
return 1
}

// Verify that the given address points to something that exists in config.
// This is to reduce the risk that a typo in the resource address will
// import something that Terraform will want to immediately destroy on
// the next plan, and generally acts as a reassurance of user intent.
targetConfig := config.DescendentForInstance(addr.Module)
if targetConfig == nil {
modulePath := addr.Module.String()
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Import to non-existent module",
Detail: fmt.Sprintf(
"%s is not defined in the configuration. Please add configuration for this module before importing into it.",
modulePath,
),
})
c.showDiagnostics(diags)
return 1
}
targetMod := targetConfig.Module
rcs := targetMod.ManagedResources
var rc *configs.Resource
resourceRelAddr := addr.Resource.Resource
for _, thisRc := range rcs {
if resourceRelAddr.Type == thisRc.Type && resourceRelAddr.Name == thisRc.Name {
rc = thisRc
break
}
}
if !c.Meta.allowMissingConfig && rc == nil {
modulePath := addr.Module.String()
if modulePath == "" {
modulePath = "the root module"
}

targets, newResources, targetDiags := c.argsToTargets(config, cmdFlags.Args())
diags = diags.Append(targetDiags)
if diags.HasErrors() {
c.showDiagnostics(diags)

// This is not a diagnostic because currently our diagnostics printer
// doesn't support having a code example in the detail, and there's
// a code example in this message.
// TODO: Improve the diagnostics printer so we can use it for this
// message.
c.Ui.Error(fmt.Sprintf(
importCommandMissingResourceFmt,
addr, modulePath, resourceRelAddr.Type, resourceRelAddr.Name,
))
return 1
}

// Also parse the user-provided provider address, if any.
var providerAddr addrs.AbsProviderConfig
if c.Meta.provider != "" {
traversal, travDiags := hclsyntax.ParseTraversalAbs([]byte(c.Meta.provider), `-provider=...`, hcl.Pos{Line: 1, Column: 1})
diags = diags.Append(travDiags)
if travDiags.HasErrors() {
c.showDiagnostics(diags)
c.Ui.Info(importCommandInvalidAddressReference)
return 1
}
relAddr, addrDiags := configs.ParseProviderConfigCompact(traversal)
diags = diags.Append(addrDiags)
if addrDiags.HasErrors() {
c.showDiagnostics(diags)
return 1
}
providerAddr = relAddr.Absolute(addrs.RootModuleInstance)
} else {
// Use a default address inferred from the resource type.
// We assume the same module as the resource address here, which
// may get resolved to an inherited provider when we construct the
// import graph inside ctx.Import, called below.
if rc != nil && rc.ProviderConfigRef != nil {
providerAddr = rc.ProviderConfigAddr().Absolute(addr.Module)
} else {
providerAddr = resourceRelAddr.DefaultProviderConfig().Absolute(addr.Module)
}
}

// Check for user-supplied plugin path
if c.pluginPath, err = c.loadPluginPath(); err != nil {
c.Ui.Error(fmt.Sprintf("Error loading plugin path: %s", err))
Expand Down Expand Up @@ -247,17 +147,8 @@ 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 := ctx.Import(&terraform.ImportOpts{
Targets: []*terraform.ImportTarget{
&terraform.ImportTarget{
Addr: addr,
ID: args[1],
ProviderAddr: providerAddr,
},
},
Targets: targets,
})
diags = diags.Append(importDiags)
if diags.HasErrors() {
Expand All @@ -278,7 +169,7 @@ func (c *ImportCommand) Run(args []string) int {

c.Ui.Output(c.Colorize().Color("[reset][green]\n" + importCommandSuccessMsg))

if c.Meta.allowMissingConfig && rc == nil {
if c.Meta.allowMissingConfig && newResources {
c.Ui.Output(c.Colorize().Color("[reset][yellow]\n" + importCommandAllowMissingResourceMsg))
}

Expand All @@ -290,9 +181,139 @@ func (c *ImportCommand) Run(args []string) int {
return 0
}

func (c *ImportCommand) argsToTargets(config *configs.Config, args []string) ([]*terraform.ImportTarget, bool, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
if (len(args) % 2) != 0 {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"the import command expects an even number of arguments",
c.Help(),
))
return nil, false, diags
}
var targets []*terraform.ImportTarget
newResources := false
for i := 0; i < len(args); i += 2 {
t, nr, targetDiags := c.argToTarget(config, args[i], args[i+1])
diags = diags.Append(targetDiags)
if t != nil {
targets = append(targets, t)
}
newResources = newResources || nr
}
return targets, newResources, diags
}

func (c *ImportCommand) argToTarget(config *configs.Config, addrstr, id string) (*terraform.ImportTarget, bool, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
// Parse the provided resource address.
traversalSrc := []byte(addrstr)
traversal, travDiags := hclsyntax.ParseTraversalAbs(traversalSrc, "<import-address>", hcl.Pos{Line: 1, Column: 1})
diags = diags.Append(travDiags)
if diags.HasErrors() {
c.registerSynthConfigSource("<import-address>", traversalSrc) // so we can include a source snippet
c.Ui.Info(importCommandInvalidAddressReference)
return nil, false, diags
}
addr, addrDiags := addrs.ParseAbsResourceInstance(traversal)
diags = diags.Append(addrDiags)
if diags.HasErrors() {
c.registerSynthConfigSource("<import-address>", traversalSrc) // so we can include a source snippet
c.Ui.Info(importCommandInvalidAddressReference)
return nil, false, diags
}

if addr.Resource.Resource.Mode != addrs.ManagedResourceMode {
diags = diags.Append(errors.New("A managed resource address is required. Importing into a data resource is not allowed."))
return nil, false, diags
}

// Verify that the given address points to something that exists in config.
// This is to reduce the risk that a typo in the resource address will
// import something that Terraform will want to immediately destroy on
// the next plan, and generally acts as a reassurance of user intent.
targetConfig := config.DescendentForInstance(addr.Module)
if targetConfig == nil {
modulePath := addr.Module.String()
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Import to non-existent module",
Detail: fmt.Sprintf(
"%s is not defined in the configuration. Please add configuration for this module before importing into it.",
modulePath,
),
})
return nil, false, diags
}
targetMod := targetConfig.Module
rcs := targetMod.ManagedResources
var rc *configs.Resource
resourceRelAddr := addr.Resource.Resource
for _, thisRc := range rcs {
if resourceRelAddr.Type == thisRc.Type && resourceRelAddr.Name == thisRc.Name {
rc = thisRc
break
}
}
if !c.Meta.allowMissingConfig && rc == nil {
modulePath := addr.Module.String()
if modulePath == "" {
modulePath = "the root module"
}
// This is not a diagnostic because currently our diagnostics printer
// doesn't support having a code example in the detail, and there's
// a code example in this message.
// TODO: Improve the diagnostics printer so we can use it for this
// message.
c.Ui.Error(fmt.Sprintf(
importCommandMissingResourceFmt,
addr, modulePath, resourceRelAddr.Type, resourceRelAddr.Name,
))

diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
fmt.Sprintf("resource address %q does not exist in the configuration.", addr),
"",
))
return nil, true, diags
}

// Also parse the user-provided provider address, if any.
var providerAddr addrs.AbsProviderConfig
if c.Meta.provider != "" {
traversal, travDiags := hclsyntax.ParseTraversalAbs([]byte(c.Meta.provider), `-provider=...`, hcl.Pos{Line: 1, Column: 1})
diags = diags.Append(travDiags)
if diags.HasErrors() {
c.Ui.Info(importCommandInvalidAddressReference)
return nil, false, diags
}
relAddr, addrDiags := configs.ParseProviderConfigCompact(traversal)
diags = diags.Append(addrDiags)
if addrDiags.HasErrors() {
return nil, false, diags
}
providerAddr = relAddr.Absolute(addrs.RootModuleInstance)
} else {
// Use a default address inferred from the resource type.
// We assume the same module as the resource address here, which
// may get resolved to an inherited provider when we construct the
// import graph inside ctx.Import, called below.
if rc != nil && rc.ProviderConfigRef != nil {
providerAddr = rc.ProviderConfigAddr().Absolute(addr.Module)
} else {
providerAddr = resourceRelAddr.DefaultProviderConfig().Absolute(addr.Module)
}
}
return &terraform.ImportTarget{
Addr: addr,
ID: id,
ProviderAddr: providerAddr,
}, rc == nil, diags
}

func (c *ImportCommand) Help() string {
helpText := `
Usage: terraform import [options] ADDR ID
Usage: terraform import [options] ADDR ID [ADDR ID]...

Import existing infrastructure into your Terraform state.

Expand Down
32 changes: 27 additions & 5 deletions command/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,28 @@ func TestImport(t *testing.T) {
},
}

p.ImportResourceStateFn = nil
p.ImportResourceStateResponse = providers.ImportResourceStateResponse{
ImportedResources: []providers.ImportedResource{
importMap := map[providers.ImportResourceStateRequest]providers.ImportResourceStateResponse{
{"test_instance", "bar"}: providers.ImportResourceStateResponse{ImportedResources: []providers.ImportedResource{
{
TypeName: "test_instance",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("yay"),
}),
},
},
},
{"test_instance", "morebar"}: providers.ImportResourceStateResponse{ImportedResources: []providers.ImportedResource{
{
TypeName: "test_instance",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("moreyay"),
}),
},
},
},
}
p.ImportResourceStateFn = func(r providers.ImportResourceStateRequest) providers.ImportResourceStateResponse {
return importMap[r]
}
p.GetSchemaReturn = &terraform.ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
Expand All @@ -60,6 +72,8 @@ func TestImport(t *testing.T) {
"-state", statePath,
"test_instance.foo",
"bar",
"test_instance.morefoo",
"morebar",
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
Expand All @@ -69,7 +83,7 @@ func TestImport(t *testing.T) {
t.Fatal("ImportResourceState should be called")
}

testStateOutput(t, statePath, testImportStr)
testStateOutput(t, statePath, testImportTwo)
}

func TestImport_providerConfig(t *testing.T) {
Expand Down Expand Up @@ -733,7 +747,7 @@ func TestImport_missingResourceConfig(t *testing.T) {
}
code := c.Run(args)
if code != 1 {
t.Fatalf("import succeeded; expected failure")
t.Errorf("import succeeded; expected failure")
}

msg := ui.ErrorWriter.String()
Expand Down Expand Up @@ -934,6 +948,14 @@ test_instance.foo:
ID = yay
provider = provider.test
`
const testImportTwo = `
test_instance.foo:
ID = yay
provider = provider.test
test_instance.morefoo:
ID = moreyay
provider = provider.test
`

const testImportCustomProviderStr = `
test_instance.foo:
Expand Down
3 changes: 3 additions & 0 deletions command/testdata/import-provider-implicit/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
# "test" provider, making it available for import.
resource "test_instance" "foo" {
}

resource "test_instance" "morefoo" {
}