From 1c7167a9a105545b0fe9a622850da8994554d22e Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Wed, 12 Feb 2020 14:45:41 -0500 Subject: [PATCH] command/import: remove -provider command line argument (#24090) Now that #22862 has been merged, terraform will properly pick up the resource provider configuration from state. We can remove the deprecated `-provider` flag. --- command/import.go | 41 +---------- command/import_test.go | 151 ----------------------------------------- 2 files changed, 2 insertions(+), 190 deletions(-) diff --git a/command/import.go b/command/import.go index c818b1777..e8d0f32ce 100644 --- a/command/import.go +++ b/command/import.go @@ -43,7 +43,6 @@ func (c *ImportCommand) Run(args []string) int { cmdFlags.StringVar(&c.Meta.stateOutPath, "state-out", "", "path") cmdFlags.StringVar(&c.Meta.backupPath, "backup", "", "path") cmdFlags.StringVar(&configPath, "config", pwd, "path") - cmdFlags.StringVar(&c.Meta.provider, "provider", "", "provider") cmdFlags.BoolVar(&c.Meta.stateLock, "lock", true, "lock state") cmdFlags.DurationVar(&c.Meta.stateLockTimeout, "lock-timeout", 0, "lock timeout") cmdFlags.BoolVar(&c.Meta.allowMissingConfig, "allow-missing-config", false, "allow missing config") @@ -156,36 +155,6 @@ func (c *ImportCommand) Run(args []string) int { 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 { - providerType := resourceRelAddr.DefaultProvider() - providerAddr = addrs.NewDefaultLocalProviderConfig(providerType.LegacyString()).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)) @@ -254,9 +223,8 @@ func (c *ImportCommand) Run(args []string) int { newState, importDiags := ctx.Import(&terraform.ImportOpts{ Targets: []*terraform.ImportTarget{ &terraform.ImportTarget{ - Addr: addr, - ID: args[1], - ProviderAddr: providerAddr, + Addr: addr, + ID: args[1], }, }, }) @@ -341,11 +309,6 @@ Options: -no-color If specified, output won't contain any color. - -provider=provider Deprecated: Override the provider configuration to use - when importing the object. By default, Terraform uses the - provider specified in the configuration for the target - resource, and that is the best behavior in most cases. - -state=PATH Path to the source state file. Defaults to the configured backend, or "terraform.tfstate" diff --git a/command/import_test.go b/command/import_test.go index 43d54e874..6c6cf08f6 100644 --- a/command/import_test.go +++ b/command/import_test.go @@ -12,7 +12,6 @@ import ( "github.com/mitchellh/cli" "github.com/zclconf/go-cty/cty" - "github.com/hashicorp/terraform/addrs" "github.com/hashicorp/terraform/configs/configschema" "github.com/hashicorp/terraform/helper/copy" "github.com/hashicorp/terraform/plugin/discovery" @@ -537,156 +536,6 @@ func TestImport_providerConfigWithVarFile(t *testing.T) { testStateOutput(t, statePath, testImportStr) } -func TestImport_customProvider(t *testing.T) { - defer testChdir(t, testFixturePath("import-provider-aliased"))() - - statePath := testTempFile(t) - - p := testProvider() - ui := new(cli.MockUi) - c := &ImportCommand{ - Meta: Meta{ - testingOverrides: metaOverridesForProvider(p), - Ui: ui, - }, - } - - p.ImportResourceStateFn = nil - p.ImportResourceStateResponse = providers.ImportResourceStateResponse{ - ImportedResources: []providers.ImportedResource{ - { - TypeName: "test_instance", - State: cty.ObjectVal(map[string]cty.Value{ - "id": cty.StringVal("yay"), - }), - }, - }, - } - p.GetSchemaReturn = &terraform.ProviderSchema{ - Provider: &configschema.Block{ - Attributes: map[string]*configschema.Attribute{ - "foo": {Type: cty.String, Optional: true}, - }, - }, - ResourceTypes: map[string]*configschema.Block{ - "test_instance": { - Attributes: map[string]*configschema.Attribute{ - "id": {Type: cty.String, Optional: true, Computed: true}, - }, - }, - }, - } - - args := []string{ - "-provider", "test.alias", - "-state", statePath, - "test_instance.foo", - "bar", - } - if code := c.Run(args); code != 0 { - t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) - } - - if !p.ImportResourceStateCalled { - t.Fatal("ImportResourceState should be called") - } - - testStateOutput(t, statePath, testImportCustomProviderStr) -} - -// This tests behavior when the provider name does not match the implied -// provider name -func TestImport_providerNameMismatch(t *testing.T) { - defer testChdir(t, testFixturePath("import-provider-mismatch"))() - - statePath := testTempFile(t) - - p := testProvider() - ui := new(cli.MockUi) - c := &ImportCommand{ - Meta: Meta{ - testingOverrides: &testingOverrides{ - ProviderResolver: providers.ResolverFixed( - map[addrs.Provider]providers.Factory{ - addrs.NewLegacyProvider("test-beta"): providers.FactoryFixed(p), - }, - ), - }, - Ui: ui, - }, - } - - configured := false - p.ConfigureNewFn = func(req providers.ConfigureRequest) providers.ConfigureResponse { - configured = true - - cfg := req.Config - if !cfg.Type().HasAttribute("foo") { - return providers.ConfigureResponse{ - Diagnostics: tfdiags.Diagnostics{}.Append(fmt.Errorf("configuration has no foo argument")), - } - } - if got, want := cfg.GetAttr("foo"), cty.StringVal("baz"); !want.RawEquals(got) { - return providers.ConfigureResponse{ - Diagnostics: tfdiags.Diagnostics{}.Append(fmt.Errorf("foo argument is %#v, but want %#v", got, want)), - } - } - - return providers.ConfigureResponse{} - } - - p.ImportResourceStateFn = nil - p.ImportResourceStateResponse = providers.ImportResourceStateResponse{ - ImportedResources: []providers.ImportedResource{ - { - TypeName: "test_instance", - State: cty.ObjectVal(map[string]cty.Value{ - "id": cty.StringVal("yay"), - }), - }, - }, - } - p.GetSchemaReturn = &terraform.ProviderSchema{ - Provider: &configschema.Block{ - Attributes: map[string]*configschema.Attribute{ - "foo": {Type: cty.String, Optional: true}, - }, - }, - ResourceTypes: map[string]*configschema.Block{ - "test_instance": { - Attributes: map[string]*configschema.Attribute{ - "id": {Type: cty.String, Optional: true, Computed: true}, - }, - }, - }, - } - - args := []string{ - "-provider", "test-beta", - "-state", statePath, - "test_instance.foo", - "bar", - } - - if code := c.Run(args); code != 0 { - t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) - } - - // Verify that the test-beta provider was configured - if !configured { - t.Fatal("Configure should be called") - } - - if !p.ImportResourceStateCalled { - t.Fatal("ImportResourceState (provider 'test-beta') should be called") - } - - if !p.ReadResourceCalled { - t.Fatal("ReadResource (provider 'test-beta' should be called") - } - - testStateOutput(t, statePath, testImportProviderMismatchStr) -} func TestImport_allowMissingResourceConfig(t *testing.T) { defer testChdir(t, testFixturePath("import-missing-resource-config"))()