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

r/virtual_machine: Enforce powered on state #152

Merged
merged 2 commits into from
Sep 8, 2017

Conversation

vancluever
Copy link
Contributor

This commit adds something that we generally assume is correct in
Terraform - that an instance's power state is either one of two states:
on, or gone.

vsphere_virtual_machine has had a couple of issues that have been
attributed to power state in the last few months that have been fixed,
but the issue of ensuring that VMs are always powered on has not been
touched on yet.

In this commit, we add the power_state pseudo-computed variable,
working around the fact that the provider can't manipulate the diff
directly by using an immutable default to always indicate that a VM that
is powered off or suspended will be turned back on during the next
Terraform run. If a VM is powered off, this turns the VM back on during
the configuration modification phase at the end of the update.

Fixes #134.
Related to #103.

This commit adds something that we generally assume is correct in
Terraform - that an instance's power state is either one of two states:
on, or gone.

vsphere_virtual_machine has had a couple of issues that have been
attributed to power state in the last few months that have been fixed,
but the issue of ensuring that VMs are always powered on has not been
touched on yet.

In this commit, we add the `power_state` pseudo-computed variable,
working around the fact that the provider can't manipulate the diff
directly by using an immutable default to always indicate that a VM that
is powered off or suspended will be turned back on during the next
Terraform run. If a VM is powered off, this turns the VM back on during
the configuration modification phase at the end of the update.

Fixes #134.
Related to #103.
Copy link
Member

@mbfrahry mbfrahry left a comment

Choose a reason for hiding this comment

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

Hey Chris. Looks good. Could we add another test that makes sure a the plan is empty when a vm is powered off? Unless it's irrelevant. I'm just always a little wary of optional/computed/default value all wrapped into one.

// copyStatePtr returns a TestCheckFunc that copies the reference to the test
// run's state to t. This allows access to the state data in later steps where
// it's not normally accessible (ie: in pre-config parts in another test step).
func copyStatePtr(t **terraform.State) resource.TestCheckFunc {
Copy link
Member

Choose a reason for hiding this comment

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

Are the pointers to pointers necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Double pointer rationale here.

resource.TestStep{
Config: config,
Check: resource.ComposeTestCheckFunc(
copyStatePtr(&state),
Copy link
Member

Choose a reason for hiding this comment

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

If we didn't do a pointer to a pointer with the above comment we could just pass state instead of &state. Unless I'm missing something

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, the double pointer is necessary because a terrafrom.State instance is always passed by reference. So we need to directly modify the value of the pointer, or else it's essentially passed by value and does not get retained. Example of a failure of a pass by value function. Unless I'm missing something. :)

A different approach would be to use State.Copy, but I don't necessarily want a copy here, but instead the in-flight state.

@vancluever
Copy link
Contributor Author

Hey @mbfrahry, also, we actually want a non-empty plan here on powered down (there is actually a test in master testing this right now) because we always want to diff poweredOff => poweredOn to indicate that powered off is not a valid power state to be in.

Unfortunately with the current state of affairs with vsphere_virtrual_machine, a powered off VM creates a non-empty plan even without this update, but that's going to be a longer-term battle.

Reference to the existing test: https://github.com/terraform-providers/terraform-provider-vsphere/blob/master/vsphere/resource_vsphere_virtual_machine_test.go#L300

Copy link
Member

@mbfrahry mbfrahry left a comment

Choose a reason for hiding this comment

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

Sounds good to me!

@vancluever vancluever merged commit b865d00 into master Sep 8, 2017
@vancluever vancluever deleted the f-virtual-machine-power-state branch September 12, 2017 15:13
@ghost ghost locked and limited conversation to collaborators Apr 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Terraform crashed when powered off existing "vsphere_virtual_machine" resource then apply again
2 participants