Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose instance_id as a computed field on compute_instance #427

Merged
merged 1 commit into from
Sep 13, 2017
Merged
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
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))
Copy link
Contributor

@rosbo rosbo Sep 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered using https://golang.org/pkg/strconv/#Itoa?

Copy link
Contributor Author

@selmanj selmanj Sep 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I almost did that, but Itoa takes an int and instance.Id is of type int64.

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