Skip to content

Commit

Permalink
Add check for vsphere_virtual_machine power state (#1407)
Browse files Browse the repository at this point in the history
Adds support for each `vprops.Runtime.PowerState`
- `VirtualMachinePowerStatePoweredOn`
- `VirtualMachinePowerStatePoweredOff`
- `VirtualMachinePowerStateSuspended`

Keeps the terminology simple for the computed attribute.

Signed-off-by: Ryan Johnson <[email protected]>

Co-authored-by: Ryan Johnson <[email protected]>
  • Loading branch information
sumitmaggo and Ryan Johnson authored Mar 3, 2022
1 parent 82c743a commit e56edec
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
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

0 comments on commit e56edec

Please sign in to comment.