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

helper/schema: change GetOk semantics to mean non-zero #993

Merged
merged 2 commits into from
Feb 18, 2015
Merged

Conversation

mitchellh
Copy link
Contributor

This changes the GetOk semantics to mean that a value is non-zero. The prior semantics were: "it is set in the configuration." But that used the assumption that a diff would only contain values that are non-zero IF they were in the configuration. With some bug fixes today, this assumption is no longer true: the diff can contain changes that aren't necessarily from the configuration, but could be.

Before this commit, GetOk was then totally broken: it always returned true, basically, because the diff contains a lot more information about zero values being set. This was required for #952 and friends.

This makes the requirement slightly more strict which is "it must exist AND it must be non-zero". I took a look around the builtins and this looks to be what everyone is assuming.

If the provider author wants to check that its set and is zero, then they can just use Get, which returns a zero value (never nil) for the proper type. And if they want to set a default that is non-zero, then the Default field is still available.

@@ -828,7 +828,7 @@ func TestResourceDataGetOk(t *testing.T) {

Key: "availability_zone",
Value: "",
Ok: true,
Ok: false,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the behavior change mentioned in the description.

@mitchellh
Copy link
Contributor Author

I'm wrong, Get only gives you a zero-value back if its not set, but you can't tell if something is set or not.

I looked around, and I don't think anything wanted this behavior (in builtins). If we want to support that in the future I think it'll require core changes to annotate the Diff about where the data came from.

}
}
// NOTE: ValueType has more functions defined on it in schema.go. We can't
// put them here because we reference other files.
Copy link
Contributor

Choose a reason for hiding this comment

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

<3 for the note

@phinze
Copy link
Contributor

phinze commented Feb 18, 2015

LGTM, this will make providers more sane

mitchellh added a commit that referenced this pull request Feb 18, 2015
helper/schema: change GetOk semantics to mean non-zero
@mitchellh mitchellh merged commit 2edd7a4 into master Feb 18, 2015
@mitchellh mitchellh deleted the b-getok branch February 18, 2015 01:46
@jefferai
Copy link
Member

In my limited testing this fixes #950.

phinze added a commit that referenced this pull request Feb 21, 2015
The `SourceDestCheck` attribute can only be changed via
`ModifyInstance`, so the AWS instance resource's `Create` function calls
out to `Update` before it returns to take care of applying
`source_dest_check` properly.

The `Update` function originally guarded against unnecessary API calls
with `GetOk`, which worked fine until #993 when we changed the `GetOk`
semantics to no longer distinguish between "configured and zero-value"
and "not configured".

I attempted in #1003 to fix this by switching to `HasChange` for the
guard, but this does not work in the `Create` case.

I played around with a few different ideas, none of which worked:

(a) Setting `Default: true` on `source_dest_check' has no effect

(b) Setting `Computed: true` on `source_dest_check' and adding a `d.Set`
    call in the `Read` function (which will initially set the value to `true`
    after instance creation). I really thought I could get this to work,
    but it results in the following:

```go
d.Get('source_dest_check')       // true
d.HasChange('source_dest_check') // false
d.GetChange('source_dest_check') // old: false, new: false
```

I couldn't figure out a way of coherently dealing with that result, so I
ended up throwing up my hands and giving up on the guard altogether.
We'll call `ModifyInstance` more than we have to, but this at least
yields expected behavior for both Creates and Updates.

Fixes #1020
yahyapo pushed a commit to yahyapo/terraform that referenced this pull request Mar 13, 2015
The `SourceDestCheck` attribute can only be changed via
`ModifyInstance`, so the AWS instance resource's `Create` function calls
out to `Update` before it returns to take care of applying
`source_dest_check` properly.

The `Update` function originally guarded against unnecessary API calls
with `GetOk`, which worked fine until hashicorp#993 when we changed the `GetOk`
semantics to no longer distinguish between "configured and zero-value"
and "not configured".

I attempted in hashicorp#1003 to fix this by switching to `HasChange` for the
guard, but this does not work in the `Create` case.

I played around with a few different ideas, none of which worked:

(a) Setting `Default: true` on `source_dest_check' has no effect

(b) Setting `Computed: true` on `source_dest_check' and adding a `d.Set`
    call in the `Read` function (which will initially set the value to `true`
    after instance creation). I really thought I could get this to work,
    but it results in the following:

```go
d.Get('source_dest_check')       // true
d.HasChange('source_dest_check') // false
d.GetChange('source_dest_check') // old: false, new: false
```

I couldn't figure out a way of coherently dealing with that result, so I
ended up throwing up my hands and giving up on the guard altogether.
We'll call `ModifyInstance` more than we have to, but this at least
yields expected behavior for both Creates and Updates.

Fixes hashicorp#1020
@ghost
Copy link

ghost commented May 4, 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 have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators May 4, 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.

4 participants