terraform/helper
Paul Hinze b4df304b47
helper/schema: Normalize bools to "true"/"false" in diffs
For a long time now, the diff logic has relied on the behavior of
`mapstructure.WeakDecode` to determine how various primitives are
converted into strings.  The `schema.DiffString` function is used for
all primitive field types: TypeBool, TypeInt, TypeFloat, and TypeString.

The `mapstructure` library's string representation of booleans is "0"
and "1", which differs from `strconv.FormatBool`'s "false" and "true"
(which is used in writing out boolean fields to the state).

Because of this difference, diffs have long had the potential for
cosmetically odd but semantically neutral output like:

    "true" => "1"
    "false" => "0"

So long as `mapstructure.Decode` or `strconv.ParseBool` are used to
interpret these strings, there's no functional problem.

We had our first clear functional problem with #6005 and friends, where
users noticed diffs like the above showing up unexpectedly and causing
troubles when `ignore_changes` was in play.

This particular bug occurs down in Terraform core's EvalIgnoreChanges.
There, the diff is modified to account for ignored attributes, and
special logic attempts to handle properly the situation where the
ignored attribute was going to trigger a resource replacement. That
logic relies on the string representations of the Old and New fields in
the diff to be the same so that it filters properly.

So therefore, we now get a bug when a diff includes `Old: "0", New:
"false"` since the strings do not match, and `ignore_changes` is not
properly handled.

Here, we introduce `TypeBool`-specific normalizing into `finalizeDiff`.
I spiked out a full `diffBool` function, but figuring out which pieces
of `diffString` to duplicate there got hairy. This seemed like a simpler
and more direct solution.

Fixes #6005 (and potentially others!)
2016-05-05 09:00:58 -05:00
..
acctest tests: allow opt-out of remote tests via env var 2016-01-21 15:44:18 -06:00
config Fix failing tests 2014-10-09 21:29:21 -07:00
diff helper/diff: replace ocnfig/lang 2016-02-03 13:24:04 -05:00
hashcode formatting, cleanups 2015-02-11 11:40:49 -08:00
logging provider/aws: log HTTP req/resp at DEBUG level 2016-03-14 12:26:37 -05:00
mutexkv provider/aws: serialize SG rule access to fix race condition 2015-11-18 12:39:59 -06:00
pathorcontents ssh: accept private key contents instead of path 2015-11-12 14:59:14 -06:00
resource providers/aws: aws_instance id-only 2016-04-22 09:37:41 -07:00
schema helper/schema: Normalize bools to "true"/"false" in diffs 2016-05-05 09:00:58 -05:00
README.md dos2unix 2014-07-28 10:43:00 -07:00

README.md

Helper Libraries

This folder contains helper libraries for Terraform plugins. A running joke is that this is "Terraform standard library" for plugins. The goal of the packages in this directory are to provide high-level helpers to make it easier to implement the various aspects of writing a plugin for Terraform.