diff --git a/mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl b/mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl index 75f30166ecf8..da1dd74b3d39 100644 --- a/mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl +++ b/mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl @@ -245,6 +245,26 @@ func ResourceComputeInstance() *schema.Resource { Delete: schema.DefaultTimeout(20 * time.Minute), }, + Identity: &schema.ResourceIdentity{ + Version: 1, + SchemaFunc: func() map[string]*schema.Schema { + return map[string]*schema.Schema{ + "project": { + Type: schema.TypeString, + OptionalForImport: true, + }, + "zone": { + Type: schema.TypeString, + OptionalForImport: true, + }, + "name": { + Type: schema.TypeString, + RequiredForImport: true, + }, + } + }, + }, + // A compute instance is more or less a superset of a compute instance // template. Please attempt to maintain consistency with the // resource_compute_instance_template schema when updating this one. @@ -2655,7 +2675,11 @@ func populateComputeInstanceResourceData(d *schema.ResourceData, instance *compu } {{- end }} - return nil + return tpgresource.SetResourceIdentityAttributes(d, map[string]interface{}{ + "project": project, + "zone": zone, + "name": instance.Name, + }) } func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) error { diff --git a/mmv1/third_party/terraform/services/compute/resource_compute_instance_test.go.tmpl b/mmv1/third_party/terraform/services/compute/resource_compute_instance_test.go.tmpl index fff4f3534793..b53bc5f5aeee 100644 --- a/mmv1/third_party/terraform/services/compute/resource_compute_instance_test.go.tmpl +++ b/mmv1/third_party/terraform/services/compute/resource_compute_instance_test.go.tmpl @@ -13,6 +13,7 @@ import ( "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/plancheck" "github.com/hashicorp/terraform-plugin-testing/terraform" + "github.com/hashicorp/terraform-plugin-testing/tfversion" "github.com/stretchr/testify/assert" "github.com/hashicorp/terraform-provider-google/google/acctest" @@ -385,6 +386,39 @@ func TestAccComputeInstance_basic5(t *testing.T) { }) } +func TestAccComputeInstance_importBlockWithResourceIdentity(t *testing.T) { + t.Parallel() + + instanceName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10)) + project := envvar.GetTestProjectFromEnv() + zone := "us-central1-a" + + acctest.VcrTest(t, resource.TestCase{ + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipBelow(tfversion.Version1_12_0), + }, + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccComputeInstance_basic(instanceName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("google_compute_instance.foobar", "name", instanceName), + resource.TestCheckResourceAttr("google_compute_instance.foobar", "zone", zone), + resource.TestCheckResourceAttr("google_compute_instance.foobar", "project", project), + ), + }, + { + ResourceName: "google_compute_instance.foobar", + RefreshState: true, + ExpectNonEmptyPlan: true, + ImportStateKind: resource.ImportBlockWithResourceIdentity, + }, + }, + }) +} + func TestAccComputeInstance_metadataGceContainerDeclaration(t *testing.T) { t.Parallel()