github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/context_import_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform-plugin-sdk/internal/addrs"
     9  	"github.com/hashicorp/terraform-plugin-sdk/internal/configs/configschema"
    10  	"github.com/hashicorp/terraform-plugin-sdk/internal/providers"
    11  	"github.com/hashicorp/terraform-plugin-sdk/internal/states"
    12  	"github.com/zclconf/go-cty/cty"
    13  )
    14  
    15  func TestContextImport_basic(t *testing.T) {
    16  	p := testProvider("aws")
    17  	m := testModule(t, "import-provider")
    18  	ctx := testContext2(t, &ContextOpts{
    19  		Config: m,
    20  		ProviderResolver: providers.ResolverFixed(
    21  			map[string]providers.Factory{
    22  				"aws": testProviderFuncFixed(p),
    23  			},
    24  		),
    25  	})
    26  
    27  	p.ImportStateReturn = []*InstanceState{
    28  		{
    29  			ID:        "foo",
    30  			Ephemeral: EphemeralState{Type: "aws_instance"},
    31  		},
    32  	}
    33  
    34  	state, diags := ctx.Import(&ImportOpts{
    35  		Targets: []*ImportTarget{
    36  			{
    37  				Addr: addrs.RootModuleInstance.ResourceInstance(
    38  					addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
    39  				),
    40  				ID: "bar",
    41  			},
    42  		},
    43  	})
    44  	if diags.HasErrors() {
    45  		t.Fatalf("unexpected errors: %s", diags.Err())
    46  	}
    47  	actual := strings.TrimSpace(state.String())
    48  	expected := strings.TrimSpace(testImportStr)
    49  	if actual != expected {
    50  		t.Fatalf("bad: \n%s", actual)
    51  	}
    52  }
    53  
    54  func TestContextImport_countIndex(t *testing.T) {
    55  	p := testProvider("aws")
    56  	m := testModule(t, "import-provider")
    57  	ctx := testContext2(t, &ContextOpts{
    58  		Config: m,
    59  		ProviderResolver: providers.ResolverFixed(
    60  			map[string]providers.Factory{
    61  				"aws": testProviderFuncFixed(p),
    62  			},
    63  		),
    64  	})
    65  
    66  	p.ImportStateReturn = []*InstanceState{
    67  		{
    68  			ID:        "foo",
    69  			Ephemeral: EphemeralState{Type: "aws_instance"},
    70  		},
    71  	}
    72  
    73  	state, diags := ctx.Import(&ImportOpts{
    74  		Targets: []*ImportTarget{
    75  			{
    76  				Addr: addrs.RootModuleInstance.ResourceInstance(
    77  					addrs.ManagedResourceMode, "aws_instance", "foo", addrs.IntKey(0),
    78  				),
    79  				ID: "bar",
    80  			},
    81  		},
    82  	})
    83  	if diags.HasErrors() {
    84  		t.Fatalf("unexpected errors: %s", diags.Err())
    85  	}
    86  
    87  	actual := strings.TrimSpace(state.String())
    88  	expected := strings.TrimSpace(testImportCountIndexStr)
    89  	if actual != expected {
    90  		t.Fatalf("bad: \n%s", actual)
    91  	}
    92  }
    93  
    94  func TestContextImport_collision(t *testing.T) {
    95  	p := testProvider("aws")
    96  	m := testModule(t, "import-provider")
    97  	ctx := testContext2(t, &ContextOpts{
    98  		Config: m,
    99  		ProviderResolver: providers.ResolverFixed(
   100  			map[string]providers.Factory{
   101  				"aws": testProviderFuncFixed(p),
   102  			},
   103  		),
   104  
   105  		State: states.BuildState(func(s *states.SyncState) {
   106  			s.SetResourceInstanceCurrent(
   107  				addrs.Resource{
   108  					Mode: addrs.ManagedResourceMode,
   109  					Type: "aws_instance",
   110  					Name: "foo",
   111  				}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
   112  				&states.ResourceInstanceObjectSrc{
   113  					AttrsFlat: map[string]string{
   114  						"id": "bar",
   115  					},
   116  					Status: states.ObjectReady,
   117  				},
   118  				addrs.ProviderConfig{Type: "aws"}.Absolute(addrs.RootModuleInstance),
   119  			)
   120  		}),
   121  	})
   122  
   123  	p.ImportStateReturn = []*InstanceState{
   124  		{
   125  			ID:        "foo",
   126  			Ephemeral: EphemeralState{Type: "aws_instance"},
   127  		},
   128  	}
   129  
   130  	state, diags := ctx.Import(&ImportOpts{
   131  		Targets: []*ImportTarget{
   132  			{
   133  				Addr: addrs.RootModuleInstance.ResourceInstance(
   134  					addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
   135  				),
   136  				ID: "bar",
   137  			},
   138  		},
   139  	})
   140  	if !diags.HasErrors() {
   141  		t.Fatalf("succeeded; want an error indicating that the resource already exists in state")
   142  	}
   143  
   144  	actual := strings.TrimSpace(state.String())
   145  	expected := `aws_instance.foo:
   146    ID = bar
   147    provider = provider.aws`
   148  
   149  	if actual != expected {
   150  		t.Fatalf("bad: \n%s", actual)
   151  	}
   152  }
   153  
   154  func TestContextImport_missingType(t *testing.T) {
   155  	p := testProvider("aws")
   156  	m := testModule(t, "import-provider")
   157  
   158  	p.ImportStateReturn = []*InstanceState{
   159  		{
   160  			ID: "foo",
   161  		},
   162  	}
   163  
   164  	ctx := testContext2(t, &ContextOpts{
   165  		Config: m,
   166  		ProviderResolver: providers.ResolverFixed(
   167  			map[string]providers.Factory{
   168  				"aws": testProviderFuncFixed(p),
   169  			},
   170  		),
   171  	})
   172  
   173  	state, diags := ctx.Import(&ImportOpts{
   174  		Targets: []*ImportTarget{
   175  			{
   176  				Addr: addrs.RootModuleInstance.ResourceInstance(
   177  					addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
   178  				),
   179  				ID: "bar",
   180  			},
   181  		},
   182  	})
   183  	if !diags.HasErrors() {
   184  		t.Fatal("should error")
   185  	}
   186  
   187  	actual := strings.TrimSpace(state.String())
   188  	expected := "<no state>"
   189  	if actual != expected {
   190  		t.Fatalf("bad: \n%s", actual)
   191  	}
   192  }
   193  
   194  func TestContextImport_moduleProvider(t *testing.T) {
   195  	p := testProvider("aws")
   196  
   197  	p.ImportStateReturn = []*InstanceState{
   198  		{
   199  			ID:        "foo",
   200  			Ephemeral: EphemeralState{Type: "aws_instance"},
   201  		},
   202  	}
   203  
   204  	configured := false
   205  	p.ConfigureFn = func(c *ResourceConfig) error {
   206  		configured = true
   207  
   208  		if v, ok := c.Get("foo"); !ok || v.(string) != "bar" {
   209  			return fmt.Errorf("bad")
   210  		}
   211  
   212  		return nil
   213  	}
   214  
   215  	m := testModule(t, "import-provider")
   216  	ctx := testContext2(t, &ContextOpts{
   217  		Config: m,
   218  		ProviderResolver: providers.ResolverFixed(
   219  			map[string]providers.Factory{
   220  				"aws": testProviderFuncFixed(p),
   221  			},
   222  		),
   223  	})
   224  
   225  	state, diags := ctx.Import(&ImportOpts{
   226  		Config: m,
   227  		Targets: []*ImportTarget{
   228  			{
   229  				Addr: addrs.RootModuleInstance.ResourceInstance(
   230  					addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
   231  				),
   232  				ID: "bar",
   233  			},
   234  		},
   235  	})
   236  	if diags.HasErrors() {
   237  		t.Fatalf("unexpected errors: %s", diags.Err())
   238  	}
   239  
   240  	if !configured {
   241  		t.Fatal("didn't configure provider")
   242  	}
   243  
   244  	actual := strings.TrimSpace(state.String())
   245  	expected := strings.TrimSpace(testImportStr)
   246  	if actual != expected {
   247  		t.Fatalf("expected:\n%s\n\ngot:\n%s", expected, actual)
   248  	}
   249  }
   250  
   251  // Importing into a module requires a provider config in that module.
   252  func TestContextImport_providerModule(t *testing.T) {
   253  	p := testProvider("aws")
   254  	m := testModule(t, "import-provider-module")
   255  	ctx := testContext2(t, &ContextOpts{
   256  		Config: m,
   257  		ProviderResolver: providers.ResolverFixed(
   258  			map[string]providers.Factory{
   259  				"aws": testProviderFuncFixed(p),
   260  			},
   261  		),
   262  	})
   263  
   264  	p.ImportStateReturn = []*InstanceState{
   265  		{
   266  			ID:        "foo",
   267  			Ephemeral: EphemeralState{Type: "aws_instance"},
   268  		},
   269  	}
   270  
   271  	configured := false
   272  	p.ConfigureFn = func(c *ResourceConfig) error {
   273  		configured = true
   274  
   275  		if v, ok := c.Get("foo"); !ok || v.(string) != "bar" {
   276  			return fmt.Errorf("bad")
   277  		}
   278  
   279  		return nil
   280  	}
   281  
   282  	_, diags := ctx.Import(&ImportOpts{
   283  		Config: m,
   284  		Targets: []*ImportTarget{
   285  			{
   286  				Addr: addrs.RootModuleInstance.Child("child", addrs.NoKey).ResourceInstance(
   287  					addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
   288  				),
   289  				ID: "bar",
   290  			},
   291  		},
   292  	})
   293  	if diags.HasErrors() {
   294  		t.Fatalf("unexpected errors: %s", diags.Err())
   295  	}
   296  
   297  	if !configured {
   298  		t.Fatal("didn't configure provider")
   299  	}
   300  }
   301  
   302  // Test that import will interpolate provider configuration and use
   303  // that configuration for import.
   304  func TestContextImport_providerVarConfig(t *testing.T) {
   305  	p := testProvider("aws")
   306  	m := testModule(t, "import-provider-vars")
   307  	ctx := testContext2(t, &ContextOpts{
   308  		Config: m,
   309  		ProviderResolver: providers.ResolverFixed(
   310  			map[string]providers.Factory{
   311  				"aws": testProviderFuncFixed(p),
   312  			},
   313  		),
   314  		Variables: InputValues{
   315  			"foo": &InputValue{
   316  				Value:      cty.StringVal("bar"),
   317  				SourceType: ValueFromCaller,
   318  			},
   319  		},
   320  	})
   321  
   322  	configured := false
   323  	p.ConfigureFn = func(c *ResourceConfig) error {
   324  		configured = true
   325  
   326  		if v, ok := c.Get("foo"); !ok || v.(string) != "bar" {
   327  			return fmt.Errorf("bad value %#v; want %#v", v, "bar")
   328  		}
   329  
   330  		return nil
   331  	}
   332  
   333  	p.ImportStateReturn = []*InstanceState{
   334  		{
   335  			ID:        "foo",
   336  			Ephemeral: EphemeralState{Type: "aws_instance"},
   337  		},
   338  	}
   339  
   340  	state, diags := ctx.Import(&ImportOpts{
   341  		Targets: []*ImportTarget{
   342  			{
   343  				Addr: addrs.RootModuleInstance.ResourceInstance(
   344  					addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
   345  				),
   346  				ID: "bar",
   347  			},
   348  		},
   349  	})
   350  	if diags.HasErrors() {
   351  		t.Fatalf("unexpected errors: %s", diags.Err())
   352  	}
   353  
   354  	if !configured {
   355  		t.Fatal("didn't configure provider")
   356  	}
   357  
   358  	actual := strings.TrimSpace(state.String())
   359  	expected := strings.TrimSpace(testImportStr)
   360  	if actual != expected {
   361  		t.Fatalf("bad: \n%s", actual)
   362  	}
   363  }
   364  
   365  // Test that provider configs can't reference resources.
   366  func TestContextImport_providerNonVarConfig(t *testing.T) {
   367  	p := testProvider("aws")
   368  	m := testModule(t, "import-provider-non-vars")
   369  	ctx := testContext2(t, &ContextOpts{
   370  		Config: m,
   371  		ProviderResolver: providers.ResolverFixed(
   372  			map[string]providers.Factory{
   373  				"aws": testProviderFuncFixed(p),
   374  			},
   375  		),
   376  	})
   377  
   378  	p.ImportStateReturn = []*InstanceState{
   379  		{
   380  			ID:        "foo",
   381  			Ephemeral: EphemeralState{Type: "aws_instance"},
   382  		},
   383  	}
   384  
   385  	_, diags := ctx.Import(&ImportOpts{
   386  		Targets: []*ImportTarget{
   387  			{
   388  				Addr: addrs.RootModuleInstance.ResourceInstance(
   389  					addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
   390  				),
   391  				ID:           "bar",
   392  				ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
   393  			},
   394  		},
   395  	})
   396  	if !diags.HasErrors() {
   397  		t.Fatal("should error")
   398  	}
   399  }
   400  
   401  func TestContextImport_refresh(t *testing.T) {
   402  	p := testProvider("aws")
   403  	m := testModule(t, "import-provider")
   404  	ctx := testContext2(t, &ContextOpts{
   405  		Config: m,
   406  		ProviderResolver: providers.ResolverFixed(
   407  			map[string]providers.Factory{
   408  				"aws": testProviderFuncFixed(p),
   409  			},
   410  		),
   411  	})
   412  
   413  	p.ImportStateReturn = []*InstanceState{
   414  		{
   415  			ID:        "foo",
   416  			Ephemeral: EphemeralState{Type: "aws_instance"},
   417  		},
   418  	}
   419  
   420  	p.ReadResourceFn = nil
   421  
   422  	p.ReadResourceResponse = providers.ReadResourceResponse{
   423  		NewState: cty.ObjectVal(map[string]cty.Value{
   424  			"id":  cty.StringVal("foo"),
   425  			"foo": cty.StringVal("bar"),
   426  		}),
   427  	}
   428  
   429  	state, diags := ctx.Import(&ImportOpts{
   430  		Targets: []*ImportTarget{
   431  			{
   432  				Addr: addrs.RootModuleInstance.ResourceInstance(
   433  					addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
   434  				),
   435  				ID:           "bar",
   436  				ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
   437  			},
   438  		},
   439  	})
   440  	if diags.HasErrors() {
   441  		t.Fatalf("unexpected errors: %s", diags.Err())
   442  	}
   443  
   444  	actual := strings.TrimSpace(state.String())
   445  	expected := strings.TrimSpace(testImportRefreshStr)
   446  	if actual != expected {
   447  		t.Fatalf("bad: \n%s", actual)
   448  	}
   449  }
   450  
   451  func TestContextImport_refreshNil(t *testing.T) {
   452  	p := testProvider("aws")
   453  	m := testModule(t, "import-provider")
   454  	ctx := testContext2(t, &ContextOpts{
   455  		Config: m,
   456  		ProviderResolver: providers.ResolverFixed(
   457  			map[string]providers.Factory{
   458  				"aws": testProviderFuncFixed(p),
   459  			},
   460  		),
   461  	})
   462  
   463  	p.ImportStateReturn = []*InstanceState{
   464  		{
   465  			ID:        "foo",
   466  			Ephemeral: EphemeralState{Type: "aws_instance"},
   467  		},
   468  	}
   469  
   470  	p.ReadResourceFn = func(req providers.ReadResourceRequest) providers.ReadResourceResponse {
   471  		return providers.ReadResourceResponse{
   472  			NewState: cty.NullVal(cty.DynamicPseudoType),
   473  		}
   474  	}
   475  
   476  	state, diags := ctx.Import(&ImportOpts{
   477  		Targets: []*ImportTarget{
   478  			{
   479  				Addr: addrs.RootModuleInstance.ResourceInstance(
   480  					addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
   481  				),
   482  				ID:           "bar",
   483  				ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
   484  			},
   485  		},
   486  	})
   487  	if !diags.HasErrors() {
   488  		t.Fatal("should error")
   489  	}
   490  
   491  	actual := strings.TrimSpace(state.String())
   492  	expected := "<no state>"
   493  	if actual != expected {
   494  		t.Fatalf("bad: \n%s", actual)
   495  	}
   496  }
   497  
   498  func TestContextImport_module(t *testing.T) {
   499  	p := testProvider("aws")
   500  	m := testModule(t, "import-provider")
   501  	ctx := testContext2(t, &ContextOpts{
   502  		Config: m,
   503  		ProviderResolver: providers.ResolverFixed(
   504  			map[string]providers.Factory{
   505  				"aws": testProviderFuncFixed(p),
   506  			},
   507  		),
   508  	})
   509  
   510  	p.ImportStateReturn = []*InstanceState{
   511  		{
   512  			ID:        "foo",
   513  			Ephemeral: EphemeralState{Type: "aws_instance"},
   514  		},
   515  	}
   516  
   517  	state, diags := ctx.Import(&ImportOpts{
   518  		Targets: []*ImportTarget{
   519  			{
   520  				Addr: addrs.RootModuleInstance.Child("foo", addrs.NoKey).ResourceInstance(
   521  					addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
   522  				),
   523  				ID:           "bar",
   524  				ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
   525  			},
   526  		},
   527  	})
   528  	if diags.HasErrors() {
   529  		t.Fatalf("unexpected errors: %s", diags.Err())
   530  	}
   531  
   532  	actual := strings.TrimSpace(state.String())
   533  	expected := strings.TrimSpace(testImportModuleStr)
   534  	if actual != expected {
   535  		t.Fatalf("bad: \n%s", actual)
   536  	}
   537  }
   538  
   539  func TestContextImport_moduleDepth2(t *testing.T) {
   540  	p := testProvider("aws")
   541  	m := testModule(t, "import-provider")
   542  	ctx := testContext2(t, &ContextOpts{
   543  		Config: m,
   544  		ProviderResolver: providers.ResolverFixed(
   545  			map[string]providers.Factory{
   546  				"aws": testProviderFuncFixed(p),
   547  			},
   548  		),
   549  	})
   550  
   551  	p.ImportStateReturn = []*InstanceState{
   552  		{
   553  			ID:        "foo",
   554  			Ephemeral: EphemeralState{Type: "aws_instance"},
   555  		},
   556  	}
   557  
   558  	state, diags := ctx.Import(&ImportOpts{
   559  		Targets: []*ImportTarget{
   560  			{
   561  				Addr: addrs.RootModuleInstance.Child("a", addrs.NoKey).Child("b", addrs.NoKey).ResourceInstance(
   562  					addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
   563  				),
   564  				ID:           "bar",
   565  				ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
   566  			},
   567  		},
   568  	})
   569  	if diags.HasErrors() {
   570  		t.Fatalf("unexpected errors: %s", diags.Err())
   571  	}
   572  
   573  	actual := strings.TrimSpace(state.String())
   574  	expected := strings.TrimSpace(testImportModuleDepth2Str)
   575  	if actual != expected {
   576  		t.Fatalf("bad: \n%s", actual)
   577  	}
   578  }
   579  
   580  func TestContextImport_moduleDiff(t *testing.T) {
   581  	p := testProvider("aws")
   582  	m := testModule(t, "import-provider")
   583  	ctx := testContext2(t, &ContextOpts{
   584  		Config: m,
   585  		ProviderResolver: providers.ResolverFixed(
   586  			map[string]providers.Factory{
   587  				"aws": testProviderFuncFixed(p),
   588  			},
   589  		),
   590  
   591  		State: states.BuildState(func(s *states.SyncState) {
   592  			s.SetResourceInstanceCurrent(
   593  				addrs.Resource{
   594  					Mode: addrs.ManagedResourceMode,
   595  					Type: "aws_instance",
   596  					Name: "bar",
   597  				}.Instance(addrs.NoKey).Absolute(addrs.Module{"bar"}.UnkeyedInstanceShim()),
   598  				&states.ResourceInstanceObjectSrc{
   599  					AttrsFlat: map[string]string{
   600  						"id": "bar",
   601  					},
   602  					Status: states.ObjectReady,
   603  				},
   604  				addrs.ProviderConfig{Type: "aws"}.Absolute(addrs.RootModuleInstance),
   605  			)
   606  		}),
   607  	})
   608  
   609  	p.ImportStateReturn = []*InstanceState{
   610  		{
   611  			ID:        "foo",
   612  			Ephemeral: EphemeralState{Type: "aws_instance"},
   613  		},
   614  	}
   615  
   616  	state, diags := ctx.Import(&ImportOpts{
   617  		Targets: []*ImportTarget{
   618  			{
   619  				Addr: addrs.RootModuleInstance.Child("foo", addrs.NoKey).ResourceInstance(
   620  					addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
   621  				),
   622  				ID:           "bar",
   623  				ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
   624  			},
   625  		},
   626  	})
   627  	if diags.HasErrors() {
   628  		t.Fatalf("unexpected errors: %s", diags.Err())
   629  	}
   630  
   631  	actual := strings.TrimSpace(state.String())
   632  	expected := strings.TrimSpace(testImportModuleDiffStr)
   633  	if actual != expected {
   634  		t.Fatalf("\nexpected: %q\ngot:      %q\n", expected, actual)
   635  	}
   636  }
   637  
   638  func TestContextImport_moduleExisting(t *testing.T) {
   639  	p := testProvider("aws")
   640  	m := testModule(t, "import-provider")
   641  	ctx := testContext2(t, &ContextOpts{
   642  		Config: m,
   643  		ProviderResolver: providers.ResolverFixed(
   644  			map[string]providers.Factory{
   645  				"aws": testProviderFuncFixed(p),
   646  			},
   647  		),
   648  
   649  		State: states.BuildState(func(s *states.SyncState) {
   650  			s.SetResourceInstanceCurrent(
   651  				addrs.Resource{
   652  					Mode: addrs.ManagedResourceMode,
   653  					Type: "aws_instance",
   654  					Name: "bar",
   655  				}.Instance(addrs.NoKey).Absolute(addrs.Module{"foo"}.UnkeyedInstanceShim()),
   656  				&states.ResourceInstanceObjectSrc{
   657  					AttrsFlat: map[string]string{
   658  						"id": "bar",
   659  					},
   660  					Status: states.ObjectReady,
   661  				},
   662  				addrs.ProviderConfig{Type: "aws"}.Absolute(addrs.RootModuleInstance),
   663  			)
   664  		}),
   665  	})
   666  
   667  	p.ImportStateReturn = []*InstanceState{
   668  		{
   669  			ID:        "foo",
   670  			Ephemeral: EphemeralState{Type: "aws_instance"},
   671  		},
   672  	}
   673  
   674  	state, diags := ctx.Import(&ImportOpts{
   675  		Targets: []*ImportTarget{
   676  			{
   677  				Addr: addrs.RootModuleInstance.Child("foo", addrs.NoKey).ResourceInstance(
   678  					addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
   679  				),
   680  				ID:           "bar",
   681  				ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
   682  			},
   683  		},
   684  	})
   685  	if diags.HasErrors() {
   686  		t.Fatalf("unexpected errors: %s", diags.Err())
   687  	}
   688  
   689  	actual := strings.TrimSpace(state.String())
   690  	expected := strings.TrimSpace(testImportModuleExistingStr)
   691  	if actual != expected {
   692  		t.Fatalf("\nexpected: %q\ngot:      %q\n", expected, actual)
   693  	}
   694  }
   695  
   696  func TestContextImport_multiState(t *testing.T) {
   697  	p := testProvider("aws")
   698  	m := testModule(t, "import-provider")
   699  
   700  	p.GetSchemaReturn = &ProviderSchema{
   701  		Provider: &configschema.Block{
   702  			Attributes: map[string]*configschema.Attribute{
   703  				"foo": {Type: cty.String, Optional: true},
   704  			},
   705  		},
   706  		ResourceTypes: map[string]*configschema.Block{
   707  			"aws_instance": {
   708  				Attributes: map[string]*configschema.Attribute{
   709  					"id": {Type: cty.String, Computed: true},
   710  				},
   711  			},
   712  			"aws_instance_thing": {
   713  				Attributes: map[string]*configschema.Attribute{
   714  					"id": {Type: cty.String, Computed: true},
   715  				},
   716  			},
   717  		},
   718  	}
   719  
   720  	p.ImportStateReturn = []*InstanceState{
   721  		{
   722  			ID:        "foo",
   723  			Ephemeral: EphemeralState{Type: "aws_instance"},
   724  		},
   725  		{
   726  			ID:        "bar",
   727  			Ephemeral: EphemeralState{Type: "aws_instance_thing"},
   728  		},
   729  	}
   730  
   731  	ctx := testContext2(t, &ContextOpts{
   732  		Config: m,
   733  		ProviderResolver: providers.ResolverFixed(
   734  			map[string]providers.Factory{
   735  				"aws": testProviderFuncFixed(p),
   736  			},
   737  		),
   738  	})
   739  
   740  	state, diags := ctx.Import(&ImportOpts{
   741  		Targets: []*ImportTarget{
   742  			{
   743  				Addr: addrs.RootModuleInstance.ResourceInstance(
   744  					addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
   745  				),
   746  				ID:           "bar",
   747  				ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
   748  			},
   749  		},
   750  	})
   751  	if diags.HasErrors() {
   752  		t.Fatalf("unexpected errors: %s", diags.Err())
   753  	}
   754  
   755  	actual := strings.TrimSpace(state.String())
   756  	expected := strings.TrimSpace(testImportMultiStr)
   757  	if actual != expected {
   758  		t.Fatalf("bad: \n%s", actual)
   759  	}
   760  }
   761  
   762  func TestContextImport_multiStateSame(t *testing.T) {
   763  	p := testProvider("aws")
   764  	m := testModule(t, "import-provider")
   765  
   766  	p.GetSchemaReturn = &ProviderSchema{
   767  		Provider: &configschema.Block{
   768  			Attributes: map[string]*configschema.Attribute{
   769  				"foo": {Type: cty.String, Optional: true},
   770  			},
   771  		},
   772  		ResourceTypes: map[string]*configschema.Block{
   773  			"aws_instance": {
   774  				Attributes: map[string]*configschema.Attribute{
   775  					"id": {Type: cty.String, Computed: true},
   776  				},
   777  			},
   778  			"aws_instance_thing": {
   779  				Attributes: map[string]*configschema.Attribute{
   780  					"id": {Type: cty.String, Computed: true},
   781  				},
   782  			},
   783  		},
   784  	}
   785  
   786  	p.ImportStateReturn = []*InstanceState{
   787  		{
   788  			ID:        "foo",
   789  			Ephemeral: EphemeralState{Type: "aws_instance"},
   790  		},
   791  		{
   792  			ID:        "bar",
   793  			Ephemeral: EphemeralState{Type: "aws_instance_thing"},
   794  		},
   795  		{
   796  			ID:        "qux",
   797  			Ephemeral: EphemeralState{Type: "aws_instance_thing"},
   798  		},
   799  	}
   800  
   801  	ctx := testContext2(t, &ContextOpts{
   802  		Config: m,
   803  		ProviderResolver: providers.ResolverFixed(
   804  			map[string]providers.Factory{
   805  				"aws": testProviderFuncFixed(p),
   806  			},
   807  		),
   808  	})
   809  
   810  	state, diags := ctx.Import(&ImportOpts{
   811  		Targets: []*ImportTarget{
   812  			{
   813  				Addr: addrs.RootModuleInstance.ResourceInstance(
   814  					addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
   815  				),
   816  				ID:           "bar",
   817  				ProviderAddr: addrs.RootModuleInstance.ProviderConfigDefault("aws"),
   818  			},
   819  		},
   820  	})
   821  	if diags.HasErrors() {
   822  		t.Fatalf("unexpected errors: %s", diags.Err())
   823  	}
   824  
   825  	actual := strings.TrimSpace(state.String())
   826  	expected := strings.TrimSpace(testImportMultiSameStr)
   827  	if actual != expected {
   828  		t.Fatalf("bad: \n%s", actual)
   829  	}
   830  }
   831  
   832  // import missing a provider alias should fail
   833  func TestContextImport_customProviderMissing(t *testing.T) {
   834  	p := testProvider("aws")
   835  	m := testModule(t, "import-provider")
   836  	ctx := testContext2(t, &ContextOpts{
   837  		Config: m,
   838  		ProviderResolver: providers.ResolverFixed(
   839  			map[string]providers.Factory{
   840  				"aws": testProviderFuncFixed(p),
   841  			},
   842  		),
   843  	})
   844  
   845  	p.ImportStateReturn = []*InstanceState{
   846  		{
   847  			ID:        "foo",
   848  			Ephemeral: EphemeralState{Type: "aws_instance"},
   849  		},
   850  	}
   851  
   852  	_, diags := ctx.Import(&ImportOpts{
   853  		Targets: []*ImportTarget{
   854  			{
   855  				Addr: addrs.RootModuleInstance.ResourceInstance(
   856  					addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
   857  				),
   858  				ID:           "bar",
   859  				ProviderAddr: addrs.RootModuleInstance.ProviderConfigAliased("aws", "alias"),
   860  			},
   861  		},
   862  	})
   863  	if !diags.HasErrors() {
   864  		t.Fatal("expected error")
   865  	}
   866  }
   867  
   868  func TestContextImport_customProvider(t *testing.T) {
   869  	p := testProvider("aws")
   870  	m := testModule(t, "import-provider-alias")
   871  	ctx := testContext2(t, &ContextOpts{
   872  		Config: m,
   873  		ProviderResolver: providers.ResolverFixed(
   874  			map[string]providers.Factory{
   875  				"aws": testProviderFuncFixed(p),
   876  			},
   877  		),
   878  	})
   879  
   880  	p.ImportStateReturn = []*InstanceState{
   881  		{
   882  			ID:        "foo",
   883  			Ephemeral: EphemeralState{Type: "aws_instance"},
   884  		},
   885  	}
   886  
   887  	state, diags := ctx.Import(&ImportOpts{
   888  		Targets: []*ImportTarget{
   889  			{
   890  				Addr: addrs.RootModuleInstance.ResourceInstance(
   891  					addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
   892  				),
   893  				ID:           "bar",
   894  				ProviderAddr: addrs.RootModuleInstance.ProviderConfigAliased("aws", "alias"),
   895  			},
   896  		},
   897  	})
   898  	if diags.HasErrors() {
   899  		t.Fatalf("unexpected errors: %s", diags.Err())
   900  	}
   901  
   902  	actual := strings.TrimSpace(state.String())
   903  	expected := strings.TrimSpace(testImportCustomProviderStr)
   904  	if actual != expected {
   905  		t.Fatalf("bad: \n%s", actual)
   906  	}
   907  }
   908  
   909  const testImportStr = `
   910  aws_instance.foo:
   911    ID = foo
   912    provider = provider.aws
   913  `
   914  
   915  const testImportCountIndexStr = `
   916  aws_instance.foo.0:
   917    ID = foo
   918    provider = provider.aws
   919  `
   920  
   921  const testImportModuleStr = `
   922  <no state>
   923  module.foo:
   924    aws_instance.foo:
   925      ID = foo
   926      provider = provider.aws
   927  `
   928  
   929  const testImportModuleDepth2Str = `
   930  <no state>
   931  module.a.b:
   932    aws_instance.foo:
   933      ID = foo
   934      provider = provider.aws
   935  `
   936  
   937  const testImportModuleDiffStr = `
   938  <no state>
   939  module.bar:
   940    aws_instance.bar:
   941      ID = bar
   942      provider = provider.aws
   943  module.foo:
   944    aws_instance.foo:
   945      ID = foo
   946      provider = provider.aws
   947  `
   948  
   949  const testImportModuleExistingStr = `
   950  <no state>
   951  module.foo:
   952    aws_instance.bar:
   953      ID = bar
   954      provider = provider.aws
   955    aws_instance.foo:
   956      ID = foo
   957      provider = provider.aws
   958  `
   959  
   960  const testImportMultiStr = `
   961  aws_instance.foo:
   962    ID = foo
   963    provider = provider.aws
   964  aws_instance_thing.foo:
   965    ID = bar
   966    provider = provider.aws
   967  `
   968  
   969  const testImportMultiSameStr = `
   970  aws_instance.foo:
   971    ID = foo
   972    provider = provider.aws
   973  aws_instance_thing.foo:
   974    ID = bar
   975    provider = provider.aws
   976  aws_instance_thing.foo-1:
   977    ID = qux
   978    provider = provider.aws
   979  `
   980  
   981  const testImportRefreshStr = `
   982  aws_instance.foo:
   983    ID = foo
   984    provider = provider.aws
   985    foo = bar
   986  `
   987  
   988  const testImportCustomProviderStr = `
   989  aws_instance.foo:
   990    ID = foo
   991    provider = provider.aws.alias
   992  `