Skip to content

Commit f8ec7da

Browse files
committed
command/import: allow the -config flag to specify a config location
1 parent ec0ef95 commit f8ec7da

4 files changed

Lines changed: 105 additions & 15 deletions

File tree

‎command/import.go‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,22 @@ type ImportCommand struct {
1616
}
1717

1818
func (c *ImportCommand) Run(args []string) int {
19+
// Get the pwd since its our default -config flag value
20+
pwd, err := os.Getwd()
21+
if err != nil {
22+
c.Ui.Error(fmt.Sprintf("Error getting pwd: %s", err))
23+
return 1
24+
}
25+
26+
var configPath string
1927
args = c.Meta.process(args, true)
2028

2129
cmdFlags := c.Meta.flagSet("import")
2230
cmdFlags.IntVar(&c.Meta.parallelism, "parallelism", 0, "parallelism")
2331
cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
2432
cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path")
2533
cmdFlags.StringVar(&c.Meta.backupPath, "backup", "", "path")
34+
cmdFlags.StringVar(&configPath, "config", pwd, "path")
2635
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
2736
if err := cmdFlags.Parse(args); err != nil {
2837
return 1
@@ -35,15 +44,9 @@ func (c *ImportCommand) Run(args []string) int {
3544
return 1
3645
}
3746

38-
pwd, err := os.Getwd()
39-
if err != nil {
40-
c.Ui.Error(fmt.Sprintf("Error getting pwd: %s", err))
41-
return 1
42-
}
43-
4447
// Build the context based on the arguments given
4548
ctx, _, err := c.Context(contextOpts{
46-
Path: pwd,
49+
Path: configPath,
4750
PathEmptyOk: true,
4851
StatePath: c.Meta.statePath,
4952
Parallelism: c.Meta.parallelism,
@@ -120,6 +123,11 @@ Options:
120123
modifying. Defaults to the "-state-out" path with
121124
".backup" extension. Set to "-" to disable backup.
122125
126+
-config=path Path to a directory of Terraform configuration files
127+
to use to configure the provider. Defaults to pwd.
128+
If no config files are present, they must be provided
129+
via the input prompts or env vars.
130+
123131
-input=true Ask for input for variables if not directly set.
124132
125133
-no-color If specified, output won't contain any color.

‎command/import_test.go‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,63 @@ func TestImport_providerConfig(t *testing.T) {
102102
testStateOutput(t, statePath, testImportStr)
103103
}
104104

105+
func TestImport_providerConfigDisable(t *testing.T) {
106+
defer testChdir(t, testFixturePath("import-provider"))()
107+
108+
statePath := testTempFile(t)
109+
110+
p := testProvider()
111+
ui := new(cli.MockUi)
112+
c := &ImportCommand{
113+
Meta: Meta{
114+
ContextOpts: testCtxConfig(p),
115+
Ui: ui,
116+
},
117+
}
118+
119+
p.ImportStateFn = nil
120+
p.ImportStateReturn = []*terraform.InstanceState{
121+
&terraform.InstanceState{
122+
ID: "yay",
123+
Ephemeral: terraform.EphemeralState{
124+
Type: "test_instance",
125+
},
126+
},
127+
}
128+
129+
configured := false
130+
p.ConfigureFn = func(c *terraform.ResourceConfig) error {
131+
configured = true
132+
133+
if v, ok := c.Get("foo"); ok {
134+
return fmt.Errorf("bad value: %#v", v)
135+
}
136+
137+
return nil
138+
}
139+
140+
args := []string{
141+
"-state", statePath,
142+
"-config", "",
143+
"test_instance.foo",
144+
"bar",
145+
}
146+
if code := c.Run(args); code != 0 {
147+
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
148+
}
149+
150+
// Verify that we were called
151+
if !configured {
152+
t.Fatal("Configure should be called")
153+
}
154+
155+
if !p.ImportStateCalled {
156+
t.Fatal("ImportState should be called")
157+
}
158+
159+
testStateOutput(t, statePath, testImportStr)
160+
}
161+
105162
/*
106163
func TestRefresh_badState(t *testing.T) {
107164
p := testProvider()

‎website/source/docs/commands/import.html.md‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ The command-line flags are all optional. The list of available flags are:
3535
the `-state-out` path with the ".backup" extension. Set to "-" to disable
3636
backups.
3737

38+
* `-config=path` - Path to directory of Terraform configuration files that
39+
configure the provider for import. This defaults to your working directory.
40+
If this directory contains no Terraform configuration files, the provider
41+
must be configured via manual input or environmental variables.
42+
3843
* `-input=true` - Whether to ask for input for provider configuration.
3944

4045
* `-state=path` - The path to read and save state files (unless state-out is
@@ -46,12 +51,35 @@ The command-line flags are all optional. The list of available flags are:
4651

4752
## Provider Configuration
4853

49-
To access the provider that the resource is being imported from, Terraform
50-
will ask you for access credentials. If you don't want to be asked for input,
51-
verify that all environment variables for your provider are set.
54+
Terraform will attempt to load configuration files that configure the
55+
provider being used for import. If no configuration files are present or
56+
no configuration for that specific provider is present, Terraform will
57+
prompt you for access credentials. You may also specify environmental variables
58+
to configure the provider.
59+
60+
The only limitation Terraform has when reading the configuration files
61+
is that the import provider configurations must not depend on non-variable
62+
inputs. For example, a provider configuration cannot depend on a data
63+
source.
64+
65+
As a working example, if you're importing AWS resources and you have a
66+
configuration file with the contents below, then Terraform will configure
67+
the AWS provider with this file.
68+
69+
```
70+
variable "access_key" {}
71+
variable "secret_key" {}
72+
73+
provider "aws" {
74+
access_key = "${var.access_key}"
75+
secret_key = "${var.secret_key}"
76+
}
77+
```
5278

53-
The import command cannot read provider configuration from a Terraform
54-
configuration file.
79+
You can force Terraform to explicitly not load your configuration by
80+
specifying `-config=""` (empty string). This is useful in situations where
81+
you want to manually configure the provider because your configuration
82+
may not be valid.
5583

5684
## Example: AWS Instance
5785

‎website/source/docs/import/usage.html.md‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ $ terraform import aws_instance.bar i-abcd1234
2222
...
2323
```
2424

25-
~> **Note:** In order to import resources, the provider should be configured with environment variables.
26-
We currently do not support passing credentials directly to the provider.
27-
2825
The above command imports an AWS instance with the given ID to the
2926
address `aws_instance.bar`. You can also import resources into modules.
3027
See the [resource addressing](/docs/internals/resource-addressing.html)

0 commit comments

Comments
 (0)