Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()

Expand Down
Loading