diff --git a/google/resource_compute_instance.go b/google/resource_compute_instance.go index c9a99a6b370..9633c0b6a58 100644 --- a/google/resource_compute_instance.go +++ b/google/resource_compute_instance.go @@ -250,6 +250,11 @@ func resourceComputeInstance() *schema.Resource { ForceNew: true, }, + "instance_id": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "zone": &schema.Schema{ Type: schema.TypeString, Required: true, @@ -1175,6 +1180,7 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error d.Set("cpu_platform", instance.CpuPlatform) d.Set("min_cpu_platform", instance.MinCpuPlatform) d.Set("self_link", ConvertSelfLinkToV1(instance.SelfLink)) + d.Set("instance_id", fmt.Sprintf("%d", instance.Id)) d.SetId(instance.Name) return nil diff --git a/google/resource_compute_instance_test.go b/google/resource_compute_instance_test.go index 94614a9a526..0d201032116 100644 --- a/google/resource_compute_instance_test.go +++ b/google/resource_compute_instance_test.go @@ -52,6 +52,7 @@ func TestAccComputeInstance_basic1(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckComputeInstanceExists( "google_compute_instance.foobar", &instance), + testAccCheckComputeInstanceHasInstanceId(&instance, "google_compute_instance.foobar"), testAccCheckComputeInstanceTag(&instance, "foo"), testAccCheckComputeInstanceLabel(&instance, "my_key", "my_value"), testAccCheckComputeInstanceMetadata(&instance, "foo", "bar"), @@ -972,6 +973,25 @@ func testAccCheckComputeInstanceDisk(instance *compute.Instance, source string, } } +func testAccCheckComputeInstanceHasInstanceId(instance *compute.Instance, n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + remote := fmt.Sprintf("%d", instance.Id) + local := rs.Primary.Attributes["instance_id"] + + if remote != local { + return fmt.Errorf("Instance id stored does not match: remote has %#v but local has %#v", remote, + local) + } + + return nil + } +} + func testAccCheckComputeInstanceBootDisk(instance *compute.Instance, source string) resource.TestCheckFunc { return func(s *terraform.State) error { if instance.Disks == nil { diff --git a/website/docs/r/compute_instance.html.markdown b/website/docs/r/compute_instance.html.markdown index e082eaa90bd..f79ec5ff09d 100644 --- a/website/docs/r/compute_instance.html.markdown +++ b/website/docs/r/compute_instance.html.markdown @@ -293,6 +293,8 @@ The `guest_accelerator` block supports: In addition to the arguments listed above, the following computed attributes are exported: +* `instance_id` - The server-assigned unique identifier of this instance. + * `metadata_fingerprint` - The unique fingerprint of the metadata. * `self_link` - The URI of the created resource.