Skip to content

Commit

Permalink
Expose instance_id as a computed field on compute_instance (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
selmanj authored Sep 13, 2017
1 parent f836186 commit 4124823
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions google/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions google/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/compute_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 4124823

Please sign in to comment.