Skip to content

google_compute_instance reads scheduling fields from GCP. #237

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

Merged

Conversation

rosbo
Copy link
Contributor

@rosbo rosbo commented Jul 24, 2017

issue #211

Marked preemptible as ForceNew because this field can only be set at the creation of an instance.
Reads scheduling fields from GCP. Make sure this does not introduce a diff.
There should be at most one scheduling block. Enforcing it with MaxItems=1

cc/ @selmanj @danawillow

@rosbo rosbo requested review from selmanj and danawillow July 24, 2017 23:26
@rosbo
Copy link
Contributor Author

rosbo commented Jul 25, 2017

I realized that the default value for automatic_restart depends also on whether an instance is preemptible or not. Please wait before reviewing.

@rosbo rosbo changed the title google_compute_instance reads automatic_restart and preemptible scheduling fields from GCP. [WIP] google_compute_instance reads automatic_restart and preemptible scheduling fields from GCP. Jul 25, 2017
@grubernaut grubernaut added the bug label Jul 25, 2017
@rosbo rosbo force-pushed the compute_instance_read_scheduling_field branch from 1cefa21 to f44c8d2 Compare July 25, 2017 16:49
@rosbo rosbo changed the title [WIP] google_compute_instance reads automatic_restart and preemptible scheduling fields from GCP. google_compute_instance reads scheduling fields from GCP. Jul 25, 2017
@rosbo
Copy link
Contributor Author

rosbo commented Jul 25, 2017

This PR is now ready for review. I am now handling all the scheduling fields.

// of 1 with the default values for `automatic_restart` and `preemptible`.
// This suppress the diff `scheduling.#: "1" => "0"` when the `scheduling` block
// is not specified by the user.
if k == "scheduling.#" {
Copy link
Contributor

Choose a reason for hiding this comment

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

So, I think I understand why this is needed, but would appreciate your guidance. Correct me if this is wrong; because the scheduling section is currently not read before this change, any users with a state file will see a diff showing a change to scheduling.

However, shouldn't this block check the value rather than the key? Also, if we always read the scheduling section, this would only need to be suppressed once, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not exactly,

If the user don't add the scheduling block to the config because they are fine with the default values. Then, the desired state as scheduling.# = 0.

But the current state gets refreshed (calls the read method), the populates the values for the current state scheduling. Hence, scheduling.# will always be 0 for the current state.

This means a user gets a perpetual diff if they don't specify the scheduling block (which is an okay thing to do if you are fine with the default values):
scheduling.#: "1" => "0"

Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a possibility of this over-suppressing? Like if an element is added/removed/changed but scheduling.# is still 1? Why not Computed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Computed true on the TypeList itself effectively does the same thing. This is the elegant solution I was looking for.

Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"on_host_maintenance": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"automatic_restart": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not clear to me that this should be Computed (in fact, I'm not sure if we should or shouldn't use Computed for optional values... would love some guidance here).

Because this field is of type bool, there's no way to determine if it's specified or not in the config (Get and GetOk will both always give you a boolean value). This means that you'll always be reading a value for automatic_restart and therefore never really letting GCP compute it, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

automatic_restart default value depends on the preemptible field value. DefaultFunc cannot look at the value of another field to determine its default value.

The Optional & Computed means that unless the user specify it in the config, take the GCP value.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, optional+computed is generally fine but booleans get weird since we can't tell the difference between false and unset. We need the user to be able to set automatic_restart to false, so we ForceSendFields it, which means that we'll always be providing a value to GCP and the computedness of it doesn't really end up mattering.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

scheduling.Preemptible = val.(bool)
}

// The Preemptible field cannot be updated. The instance must be recreated.
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, so I think the following scenario would fail:

  • preemptible is true
  • I want to change one of the other attributes
    because I think this would try to update preemptible to false.

However, we can't actually change either of the other attributes when preemptible is true- it would fail server-side. So two options for fixing this:

  1. put scheduling.Preemptible back in the request- we know that we won't hit this if it changes since it's marked ForceNew, so this is just to propagate the current value
  2. throw an error when trying to update other parts of scheduling if preemptible is true

I think #1 is clearer in the code and more future-proof, so I recommend that one- up to you though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did suggestion #1

@rosbo rosbo merged commit ec21efb into hashicorp:master Aug 3, 2017
@rosbo rosbo deleted the compute_instance_read_scheduling_field branch August 3, 2017 20:51
z1nkum pushed a commit to z1nkum/terraform-provider-google that referenced this pull request Aug 15, 2017
negz pushed a commit to negz/terraform-provider-google that referenced this pull request Oct 17, 2017
luis-silva pushed a commit to luis-silva/terraform-provider-google that referenced this pull request May 21, 2019
@ghost
Copy link

ghost commented Mar 31, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks!

@ghost ghost locked and limited conversation to collaborators Mar 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants