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

Add check for vsphere_virtual_machine power state #1407

Merged
merged 4 commits into from
Mar 3, 2022
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
15 changes: 15 additions & 0 deletions vsphere/resource_vsphere_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ func resourceVSphereVirtualMachine() *schema.Resource {
Computed: true,
Description: "The machine object ID from VMware vSphere.",
},
"power_state": {
Type: schema.TypeString,
Computed: true,
Description: "The power state of the virtual machine.",
},
vSphereTagAttributeKey: tagsSchema(),
customattribute.ConfigKey: customattribute.ConfigSchema(),
}
Expand Down Expand Up @@ -555,6 +560,16 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{})
}
}

// Get the power state for the virtual machine.
switch vprops.Runtime.PowerState {
case types.VirtualMachinePowerStatePoweredOn:
d.Set("power_state", "on")
case types.VirtualMachinePowerStatePoweredOff:
d.Set("power_state", "off")
case types.VirtualMachinePowerStateSuspended:
d.Set("power_state", "suspended")
}

log.Printf("[DEBUG] %s: Read complete", resourceVSphereVirtualMachineIDString(d))
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/virtual_machine.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,8 @@ The following attributes are exported on the base level of this resource:

* `vapp_transport` - Computed value which is only valid for cloned virtual machines. A list of vApp transport methods supported by the source virtual machine or template.

* `power_state` - A computed value for the current power state of the virtual machine. One of `on`, `off`, or `suspended`.

[docs-about-morefs]: https://registry.terraform.io/providers/hashicorp/vsphere/latest/docs#use-of-managed-object-references-by-the-vsphere-provider

## Importing
Expand Down