cli: terraform import -ignore-missing-config
This new option allows importing without configuration present. Configuration is required by default as a confirmation that the provided resource name is correct, but it can be useful to override this in tools that wrap Terraform to do more involved operations.
This commit is contained in:
parent
5afe1d39d1
commit
3f2136d7ee
|
@ -41,6 +41,7 @@ func (c *ImportCommand) Run(args []string) int {
|
||||||
cmdFlags.StringVar(&c.Meta.provider, "provider", "", "provider")
|
cmdFlags.StringVar(&c.Meta.provider, "provider", "", "provider")
|
||||||
cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state")
|
cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state")
|
||||||
cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout")
|
cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout")
|
||||||
|
cmdFlags.BoolVar(&c.Meta.allowMissingConfig, "allow-missing-config", false, "allow missing config")
|
||||||
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
||||||
if err := cmdFlags.Parse(args); err != nil {
|
if err := cmdFlags.Parse(args); err != nil {
|
||||||
return 1
|
return 1
|
||||||
|
@ -103,7 +104,7 @@ func (c *ImportCommand) Run(args []string) int {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if rc == nil {
|
if !c.Meta.allowMissingConfig && rc == nil {
|
||||||
modulePath := addr.WholeModuleAddress().String()
|
modulePath := addr.WholeModuleAddress().String()
|
||||||
if modulePath == "" {
|
if modulePath == "" {
|
||||||
modulePath = "the root module"
|
modulePath = "the root module"
|
||||||
|
@ -182,6 +183,10 @@ func (c *ImportCommand) Run(args []string) int {
|
||||||
|
|
||||||
c.Ui.Output(c.Colorize().Color("[reset][green]\n" + importCommandSuccessMsg))
|
c.Ui.Output(c.Colorize().Color("[reset][green]\n" + importCommandSuccessMsg))
|
||||||
|
|
||||||
|
if c.Meta.allowMissingConfig && rc == nil {
|
||||||
|
c.Ui.Output(c.Colorize().Color("[reset][yellow]\n" + importCommandAllowMissingResourceMsg))
|
||||||
|
}
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -202,11 +207,13 @@ Usage: terraform import [options] ADDR ID
|
||||||
determine the ID syntax to use. It typically matches directly to the ID
|
determine the ID syntax to use. It typically matches directly to the ID
|
||||||
that the provider uses.
|
that the provider uses.
|
||||||
|
|
||||||
In the current state of Terraform import, the resource is only imported
|
The current implementation of Terraform import can only import resources
|
||||||
into your state file. Once it is imported, you must manually write
|
into the state. It does not generate configuration. A future version of
|
||||||
configuration for the new resource or Terraform will mark it for destruction.
|
Terraform will also generate configuration.
|
||||||
Future versions of Terraform will expand the functionality of Terraform
|
|
||||||
import.
|
Because of this, prior to running terraform import it is necessary to write
|
||||||
|
a resource configuration block for the resource manually, to which the
|
||||||
|
imported object will be attached.
|
||||||
|
|
||||||
This command will not modify your infrastructure, but it will make
|
This command will not modify your infrastructure, but it will make
|
||||||
network requests to inspect parts of your infrastructure relevant to
|
network requests to inspect parts of your infrastructure relevant to
|
||||||
|
@ -223,6 +230,8 @@ Options:
|
||||||
If no config files are present, they must be provided
|
If no config files are present, they must be provided
|
||||||
via the input prompts or env vars.
|
via the input prompts or env vars.
|
||||||
|
|
||||||
|
-allow-missing-config Allow import when no resource configuration block exists.
|
||||||
|
|
||||||
-input=true Ask for input for variables if not directly set.
|
-input=true Ask for input for variables if not directly set.
|
||||||
|
|
||||||
-lock=true Lock the state file when locking is supported.
|
-lock=true Lock the state file when locking is supported.
|
||||||
|
@ -300,9 +309,13 @@ const importCommandSuccessMsg = `Import successful!
|
||||||
|
|
||||||
The resources that were imported are shown above. These resources are now in
|
The resources that were imported are shown above. These resources are now in
|
||||||
your Terraform state and will henceforth be managed by Terraform.
|
your Terraform state and will henceforth be managed by Terraform.
|
||||||
|
`
|
||||||
Import does not generate configuration, so the next step is to ensure that
|
|
||||||
the resource configurations match the current (or desired) state of the
|
const importCommandAllowMissingResourceMsg = `Import does not generate resource configuration, you must create a resource
|
||||||
imported resources. You can use the output from "terraform plan" to verify that
|
configuration block that matches the current or desired state manually.
|
||||||
the configuration is correct and complete.
|
|
||||||
|
If there is no matching resource configuration block for the imported
|
||||||
|
resource, Terraform will delete the resource on the next "terraform apply".
|
||||||
|
It is recommended that you run "terraform plan" to verify that the
|
||||||
|
configuration is correct and complete.
|
||||||
`
|
`
|
||||||
|
|
|
@ -403,6 +403,47 @@ func TestImport_customProvider(t *testing.T) {
|
||||||
testStateOutput(t, statePath, testImportCustomProviderStr)
|
testStateOutput(t, statePath, testImportCustomProviderStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestImport_allowMissingResourceConfig(t *testing.T) {
|
||||||
|
defer testChdir(t, testFixturePath("import-missing-resource-config"))()
|
||||||
|
|
||||||
|
statePath := testTempFile(t)
|
||||||
|
|
||||||
|
p := testProvider()
|
||||||
|
ui := new(cli.MockUi)
|
||||||
|
c := &ImportCommand{
|
||||||
|
Meta: Meta{
|
||||||
|
testingOverrides: metaOverridesForProvider(p),
|
||||||
|
Ui: ui,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
p.ImportStateFn = nil
|
||||||
|
p.ImportStateReturn = []*terraform.InstanceState{
|
||||||
|
{
|
||||||
|
ID: "yay",
|
||||||
|
Ephemeral: terraform.EphemeralState{
|
||||||
|
Type: "test_instance",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
args := []string{
|
||||||
|
"-state", statePath,
|
||||||
|
"-allow-missing-config",
|
||||||
|
"test_instance.foo",
|
||||||
|
"bar",
|
||||||
|
}
|
||||||
|
if code := c.Run(args); code != 0 {
|
||||||
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if !p.ImportStateCalled {
|
||||||
|
t.Fatal("ImportState should be called")
|
||||||
|
}
|
||||||
|
|
||||||
|
testStateOutput(t, statePath, testImportStr)
|
||||||
|
}
|
||||||
|
|
||||||
func TestImport_missingResourceConfig(t *testing.T) {
|
func TestImport_missingResourceConfig(t *testing.T) {
|
||||||
defer testChdir(t, testFixturePath("import-missing-resource-config"))()
|
defer testChdir(t, testFixturePath("import-missing-resource-config"))()
|
||||||
|
|
||||||
|
|
|
@ -142,6 +142,9 @@ type Meta struct {
|
||||||
errWriter *io.PipeWriter
|
errWriter *io.PipeWriter
|
||||||
// done chan to wait for the scanner goroutine
|
// done chan to wait for the scanner goroutine
|
||||||
errScannerDone chan struct{}
|
errScannerDone chan struct{}
|
||||||
|
|
||||||
|
// Used with the import command to allow import of state when no matching config exists.
|
||||||
|
allowMissingConfig bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type PluginOverrides struct {
|
type PluginOverrides struct {
|
||||||
|
|
Loading…
Reference in New Issue