Commit Graph

69 Commits

Author SHA1 Message Date
Clint a84aa5e914 Revert "helper/schema: Make nested Set(s) in List(s) work" (#7436) 2016-06-30 10:48:52 -05:00
Radek Simko 378b526dc3 helper/schema: Add regression tests for nested Set/List
Although DiffFieldReader was the one mostly responsible for a buggy behaviour
more tests were added throughout the debugging process most of which
would fail without the bugfix.

 - ResourceData
 - MultiLevelFieldReader
 - MapFieldReader
 - DiffFieldReader
2016-06-28 17:40:44 +01:00
James Nugent dbf725bd68 core: Allow dynamic attributes in helper/schema
The helper/schema framework for building providers previously validated
in all cases that each field being set in state was in the schema.
However, in order to support remote state in a usable fashion, the need
has arisen for the top level attributes of the resource to be created
dynamically. In order to still be able to use helper/schema, this commit
adds the capability to assign additional fields.

Though I do not forsee this being used by providers other than remote
state (and that eventually may move into Terraform Core rather than
being a provider), the usage and semantics are:

To opt into dynamic attributes, add a schema attribute named
"__has_dynamic_attributes", and make it an optional string with no
default value, in order that it does not appear in diffs:

        "__has_dynamic_attributes": {
            Type: schema.TypeString
            Optional: true
        }

In the read callback, use the d.UnsafeSetFieldRaw(key, value) function
to set the dynamic attributes.

Note that other fields in the schema _are_ copied into state, and that
the names of the schema fields cannot currently be used as dynamic
attribute names, as we check to ensure a value is not already set for a
given key.
2016-06-11 13:29:05 +01:00
James Nugent 074545e536 core: Use .% instead of .# for maps in state
The flatmapped representation of state prior to this commit encoded maps
and lists (and therefore by extension, sets) with a key corresponding to
the number of elements, or the unknown variable indicator under a .# key
and then individual items. For example, the list ["a", "b", "c"] would
have been encoded as:

    listname.# = 3
    listname.0 = "a"
    listname.1 = "b"
    listname.2 = "c"

And the map {"key1": "value1", "key2", "value2"} would have been encoded
as:

    mapname.# = 2
    mapname.key1 = "value1"
    mapname.key2 = "value2"

Sets use the hash code as the key - for example a set with a (fictional)
hashcode calculation may look like:

    setname.# = 2
    setname.12312512 = "value1"
    setname.56345233 = "value2"

Prior to the work done to extend the type system, this was sufficient
since the internal representation of these was effectively the same.
However, following the separation of maps and lists into distinct
first-class types, this encoding presents a problem: given a state file,
it is impossible to tell the encoding of an empty list and an empty map
apart. This presents problems for the type checker during interpolation,
as many interpolation functions will operate on only one of these two
structures.

This commit therefore changes the representation in state of maps to use
a "%" as the key for the number of elements. Consequently the map above
will now be encoded as:

    mapname.% = 2
    mapname.key1 = "value1"
    mapname.key2 = "value2"

This has the effect of an empty list (or set) now being encoded as:

    listname.# = 0

And an empty map now being encoded as:

    mapname.% = 0

Therefore we can eliminate some nasty guessing logic from the resource
variable supplier for interpolation, at the cost of having to migrate
state up front (to follow in a subsequent commit).

In order to reduce the number of potential situations in which resources
would be "forced new", we continue to accept "#" as the count key when
reading maps via helper/schema. There is no situation under which we can
allow "#" as an actual map key in any case, as it would not be
distinguishable from a list or set in state.
2016-06-09 10:49:42 +01:00
Mitchell Hashimoto 1685054a9a
helper/schema: cleaner way to store Ephemeral 2016-05-11 13:02:31 -07:00
Sander van Harmelen ef4726bd50 Change Set internals and make (extreme) performance improvements
Changing the Set internals makes a lot of sense as it saves doing
conversions in multiple places and gives a central place to alter
the key when a item is computed.

This will have no side effects other then that the ordering is now
based on strings instead on integers, so the order will be different.
This will however have no effect on existing configs as these will
use the individual codes/keys and not the ordering to determine if
there is a diff or not.

Lastly (but I think also most importantly) there is a fix in this PR
that makes diffing sets extremely more performand. Before a full diff
required reading the complete Set for every single parameter/attribute
you wanted to diff, while now it only gets that specific parameter.

We have a use case where we have a Set that has 18 parameters and the
set consist of about 600 items (don't ask 😉). So when doing a diff
it would take 100% CPU of all cores and stay that way for almost an
hour before being able to complete the diff.

Debugging this we learned that for retrieving every single parameter
it made over 52.000 calls to `func (c *ResourceConfig) get(..)`. In
this function a slice is created and used only for the duration of the
call, so the time needed to create all needed slices and on the other
hand the time the garbage collector needed to clean them up again caused
the system to cripple itself. Next to that there are also some expensive
reflect calls in this function which also claimed a fair amount of CPU
time.

After this fix the number of calls needed to get a single parameter
dropped from 52.000+ to only 2! 😃
2015-11-22 14:21:28 +01:00
Panagiotis Moustafellos e4845f75cc removed extra parentheses 2015-10-08 15:48:04 +03:00
Mitchell Hashimoto 6e509aedcb helper/schema: diff should include removed set items [GH-1823] 2015-06-25 21:52:49 -07:00
Paul Hinze bc9792f4c5 helper/schema: tweak test anotation 2015-05-07 10:39:17 -05:00
Paul Hinze f2368428d3 helper/schema: write "attr.#": "0" for empty maps
This fixes some perpetual diffs I saw in Atlas AccTests where an empty
map (`map[string]interface{}{}`) was being `d.Set` for "metadata_full".

Because the MapFieldWriter was not distinguishing between empty and nil,
this trigger the "map delete" logic and no count was written to the
state. This caused subsequent plans to improperly report a diff.

Here we redefine the map delete functionality to explicitly trigger only
on `nil`, so we catch the `.#` field for empty maps.
2015-05-06 10:21:22 -05:00
Paul Hinze a3101568c5 helper/schema: add clarifying GetOk test
Wrote this test to verify behavior, committing and commenting to help me
get the answer faster in the future.
2015-05-04 14:58:12 -05:00
Mitchell Hashimoto 58a8776c41 helper/schema: test real nil pointer to ResourceData.Set 2015-03-02 23:37:43 -08:00
Mitchell Hashimoto c030148259 helper/schema: allow pointer values to ResourceData.Set 2015-03-02 21:06:14 -08:00
Mitchell Hashimoto dd00001c9a helper/schema: tests that all pass as I was trying to track down a bug 2015-02-18 14:10:12 -08:00
Mitchell Hashimoto fa7f496bef helper/schema: zero value of a set should be empty 2015-02-17 16:58:47 -08:00
Mitchell Hashimoto 7d32c8946a helper/schema: GetOk now only returns true if set to non-zero value 2015-02-17 16:55:39 -08:00
Mitchell Hashimoto 66f7731995 helper/schema: GetChange shouldn't return true when no change 2015-02-17 15:43:19 -08:00
Mitchell Hashimoto 2212d6895d helper/schema: diff with set going to 0 elements removes it from state 2015-02-17 11:38:56 -08:00
Dave Cunningham aa2015ccd0 Fix failing tests 2015-01-28 16:20:14 -05:00
Dave Cunningham 3cbf1a3230 Fix missing import of math 2015-01-28 15:39:32 -05:00
Dave Cunningham 319933f551 Add some tests for TypeFloat 2015-01-28 15:22:47 -05:00
Sander van Harmelen c7550595a3 Fixing two related bugs in HasChange and GetChange
This was actually quite nasty as the first bug covered the second one…

The first bug is with HasChange. This function uses reflect.DeepEqual
to check if two instances are the same/have the same content. This
works fine for all types except for Set’s as they contain a function.
And reflect.DeepEqual will only say the functions are equal if they are
both nil (which they aren’t in a Set). So in effect it means that
currently HasChange will always say true for Set’s, even when they are
actually being equal.

As soon as you fix this problem, you will notice the second one (which
the added test is written for). Without saying you want the exact diff,
you will end up with a merged value which will (in most cases) be the
same.

Run all unit tests and a good part of the acc tests to verify this
works as expected and all look good.
2015-01-16 14:13:40 +01:00
Mitchell Hashimoto 448887f3c4 helper/schema: map counts in state 2015-01-15 14:12:24 -08:00
Mitchell Hashimoto 22436555a7 helper/schema: test setting computed value and retrieving it via state 2015-01-15 11:08:06 -08:00
Mitchell Hashimoto 4d067f4d6d helper/schema: don't put things into the state that don't exist or are
computed [GH-805]
2015-01-15 10:35:44 -08:00
Sander van Harmelen cb37e10c6f Adding a test for issue #791
Running this test on commit 47f02f80bc
from 6 days ago, is successful, but on master it now fails.
2015-01-14 20:50:58 +01:00
Mitchell Hashimoto 3c1b55a75f helper/schema: use the field reader/writer for state 2015-01-10 12:18:32 -08:00
Mitchell Hashimoto e57f3f69b1 helper/schema: empty maps, support reading objects directly 2015-01-09 15:07:02 -08:00
Mitchell Hashimoto f0af1c36f5 helper/schema: nested resource fields should be zero-valued on get 2015-01-09 11:51:29 -08:00
Mitchell Hashimoto 942a988ac2 helper/schema: zero value of a set should be a set 2015-01-08 18:48:03 -08:00
Mitchell Hashimoto b4bf813151 helper/schema: too big to fail 2015-01-08 18:02:19 -08:00
Sander van Harmelen 83c760fcb3 core: refactoring the way sets work internally v2
This is a refactored solution for PR #616. Functionally this is still
the same change, but it’s implemented a lot cleaner with less code and
less changes to existing parts of TF.
2014-12-12 23:21:20 +01:00
Mitchell Hashimoto 1792334ec4 fmt 2014-10-21 11:00:12 -07:00
Mitchell Hashimoto d0ce67a5b7 helper/schema: on destroy/create, reset state to be empty [GH-464] 2014-10-21 00:28:53 -07:00
Mitchell Hashimoto d1324678dd helper/schema: setting empty map works [GH-464] 2014-10-21 00:17:17 -07:00
Mitchell Hashimoto 5390357e45 helper/schema: sets properly take into account the diff 2014-10-20 15:32:30 -07:00
Mitchell Hashimoto 59349cca11 helper/schema: sets must be treated atomically within ResourceData
This fixes a seemingly minor issue (GH-255) around plans showing changes
when in fact there are none. But in reality this turned out to uncover a
really terrible bug.

The effect of what was happening was that multiple items in a set were
being merged. Now, they were being merged in the right order, so if you
didn't have rich types (lists in a set) then you never saw the effect
since the later value would overwrite the earlier. But with lists (such
as in security groups), you would end up with the lists merging. So, if
you had one ingress rule with CIDR blocks and one with SGs, then after
the merge both ingress rules would have BOTH CIDR and SGs, resulting in
an incorrect plan (GH-255).

This fixes the issue by introducing a `getSourceExact` bitflag to the
ResourceData source. When this is set, ALL data must come from this
level, instead of merging lower levels. In the case of sets and diffs,
this is exactly what you want: "Get me the set 'foo' from the config and
the config ONLY (not the state or diff or w/e)".

Andddddd its fixed.

GH-255
2014-10-11 10:40:54 -07:00
Mitchell Hashimoto 3a107d2e50 helper/schema: set the field to empty if it is a list and computed 2014-10-10 15:58:38 -07:00
Mitchell Hashimoto 9b2b3a963f ResourceDiff => InstanceDiff 2014-09-17 16:33:24 -07:00
Mitchell Hashimoto 81d9d70296 helper/schema: conforms to new API, tests pass 2014-09-16 17:07:13 -07:00
Mitchell Hashimoto faa4e75960 helper/schema: Get on computed diff should be empty 2014-08-27 15:45:52 -07:00
Mitchell Hashimoto 87a488092c helper/schema: support partial states 2014-08-26 20:19:44 -07:00
Mitchell Hashimoto 9ed601d541 helper/schema: store state with processed properly 2014-08-22 12:09:06 -07:00
Mitchell Hashimoto 50026a6d5c helper/schema: When having a StateFunc, make sure NewExtra contains
original
2014-08-22 08:57:44 -07:00
Mitchell Hashimoto d009ea800a helper/schema: add support for StateFunc 2014-08-22 08:45:54 -07:00
Mitchell Hashimoto 7be2f1b091 helper/schema: add GetOk 2014-08-21 23:03:04 -07:00
Mitchell Hashimoto 37cf52fa27 helper/schema: if no ID is set then return nil 2014-08-21 22:19:33 -07:00
Mitchell Hashimoto 3d3789920d helper/schema: can set conninfo 2014-08-21 22:15:47 -07:00
Mitchell Hashimoto ca18e971d1 helper/schema: can set sets 2014-08-20 21:09:06 -07:00
Mitchell Hashimoto 9fe21f0423 helper/schema: verify that sets remove duplicates 2014-08-20 21:09:06 -07:00