Commit Graph

139 Commits

Author SHA1 Message Date
James Bardin 2747d964cb Merge pull request #7877 from hashicorp/jbardin/races
core: Fix race conditions, mostly around diffs
2016-07-29 18:42:31 -04:00
James Nugent 0e4e94a86f core: Fix -module for terraform output command
The behaviour whereby outputs for a particular nested module can be
output was broken by the changes for lists and maps. This commit
restores the previous behaviour by passing the module path into the
outputsAsString function.

We also add a new test of this since the code path for indivdual output
vs all outputs for a module has diverged.
2016-07-29 16:39:59 -05:00
James Bardin 5802f76eaa Make all terraform package tests pass under -race
This isn't a pretty refactor, but fixes the race issues in this package
for now.

Fix race on RawConfig.Config()

fix command package races
2016-07-29 16:12:21 -04:00
Paul Hinze 40fbb8d2e8 Merge pull request #7370 from tpounds/show-tf-ver-on-state-mismatch
Show Terraform version on state version mismatch.
2016-06-29 10:48:13 -05:00
James Bardin 94f1899f4e increment the state serial whenever we upgrade
Always increment the state serial whenever we upgrade the state version.
This prevents possible version conflicts between local and remote state
when one has been upgraded, but the serial numbers match.
2016-06-28 16:32:36 -04:00
Trevor Pounds 5932214f0d Show Terraform version on state version mismatch. 2016-06-27 11:52:43 -07:00
James Nugent 17f4777039 core: Fix Stringer on OutputState for types
The implementation of Stringer on OutputState previously assumed outputs
may only be strings - we now no longer cast to string, instead using the
built in formatting directives.
2016-06-23 21:15:33 +01:00
James Nugent d60365af02 core: Correctly ensure that State() is a copy
The previous mechanism for testing state threw away the mutation made on
the state by calling State() twice - this commit corrects the test to
match the comment.

In addition, we replace the custom copying logic with the copystructure
library to simplify the code.
2016-06-22 17:21:27 +03:00
Martin Atkins 985fa371dc core: State "Lineage" concept
The lineage of a state is an identifier shared by a set of states whose
serials are meaningfully comparable because they are produced by
progressive Refresh/Apply operations from the same initial empty state.

This is initialized as a type-4 (random) UUID when a new state is
initialized and then preserved on all other changes.

Since states before this change will not have lineage but users may wish
to set a lineage for an existing state in order to get the safety
benefits it will grow to imply, an empty lineage is considered to be
compatible with all lineages.
2016-06-10 07:31:30 -07:00
James Nugent 706ccb7dfe core: Introduce state v3 and upgrade process
This commit makes the current Terraform state version 3 (previously 2),
and a migration process as part of reading v2 state. For the most part
this is unnecessary: helper/schema will deal with upgrading state for
providers written with that framework. However, for providers which
implemented the resource model directly, this gives a best-efforts
attempt at lossless upgrade.

The heuristics used to change the count of a map from the .# key to the
.% key are as follows:

    - if the flat map contains any non-numeric keys, we treat it as a
      map
    - if the map is empty it must be computed or optional, so we remove
      it from state

There is a known edge condition: maps with all-numeric keys are
indistinguishable from sets without access to the schema. They will need
manual conversion or may result in spurious diffs.
2016-06-09 10:49:49 +01:00
James Nugent 49995428fd core: Remove support for V0 state
This removes support for the V0 binary state format which was present in
Terraform prior to 0.3. We still check for the file type and present an
error message explaining to the user that they can upgrade it using a
prior version of Terraform.
2016-06-08 18:38:41 +01:00
Sander van Harmelen d97b24e3c1
Add tests and fix last issues 2016-05-26 19:56:03 -05:00
Sander van Harmelen 8560f50cbc
Change taint behaviour to act as a normal resource
This means it’s shown correctly in a plan and takes into account any
actions that are dependant on the tainted resource and, vice verse, any
actions that the tainted resource depends on.

So this changes the behaviour from saying this resource is tainted so
just forget about it and make sure it gets deleted in the background,
to saying I want that resource to be recreated (taking into account the
existing resource and it’s place in the graph).
2016-05-26 19:55:26 -05:00
James Nugent 3ea3c657b5 core: Use OutputState in JSON instead of map
This commit forward ports the changes made for 0.6.17, in order to store
the type and sensitive flag against outputs.

It also refactors the logic of the import for V0 to V1 state, and
fixes up the call sites of the new format for outputs in V2 state.

Finally we fix up tests which did not previously set a state version
where one is required.
2016-05-18 13:25:20 -05:00
Martin Atkins 844e1abdd3 core: ResourceStateKey understands resource modes
Once a data resource gets into the state, the state system needs to be
able to parse its id to match it with resources in the configuration.
Since data resources live in a separate namespace than managed resources,
the extra "mode" discriminator is required to specify which namespace
we're talking about, just like we do in the resource configuration.
2016-05-14 08:26:36 -07:00
Mitchell Hashimoto 1d3f11f0ba
terraform: working on fixes for imports in modules 2016-05-11 13:02:32 -07:00
Mitchell Hashimoto 45c3fd58d8
terraform: InstanceState.DeepCopy is exported 2016-05-11 13:02:31 -07:00
Mitchell Hashimoto 1040cb7cdb
terraform: add type to Ephemeral 2016-05-11 13:02:31 -07:00
Mitchell Hashimoto af3c3e4c60
terraform: Module copy copies outputs and dependencies 2016-05-10 13:25:02 -07:00
Mitchell Hashimoto 25098f20c9
terraform: State.Add works for module to module (new) 2016-05-10 13:25:02 -07:00
James Nugent e57a399d71 core: Use native HIL maps instead of flatmaps
This changes the representation of maps in the interpolator from the
dotted flatmap form of a string variable named "var.variablename.key"
per map element to use native HIL maps instead.

This involves porting some of the interpolation functions in order to
keep the tests green, and adding support for map outputs.

There is one backwards incompatibility: as a result of an implementation
detail of maps, one could access an indexed map variable using the
syntax "${var.variablename.key}".

This is no longer possible - instead HIL native syntax -
"${var.variablename["key"]}" must be used. This was previously
documented, (though not heavily used) so it must be noted as a backward
compatibility issue for Terraform 0.7.
2016-05-10 14:49:13 -04:00
James Nugent 6aac79e194 state: Add support for outputs of multiple types
This commit adds the groundwork for supporting module outputs of types
other than string. In order to do so, the state version is increased
from 1 to 2 (though the "public-facing" state version is actually as the
first state file was binary).

Tests are added to ensure that V2 (1) state is upgraded to V3 (2) state,
though no separate read path is required since the V2 JSON will
unmarshal correctly into the V3 structure.

Outputs in a ModuleState are now of type map[string]interface{}, and a
test covers round-tripping string, []string and map[string]string, which
should cover all of the types in question.

Type switches have been added where necessary to deal with the
interface{} value, but they currently default to panicking when the input
is not a string.
2016-05-10 14:40:12 -04:00
James Nugent 3393492033 Renumber original binary state as V0
This commit rectifies the fact that the original binary state is
referred to as V1 in the source code, but the first version of the JSON
state uses StateVersion: 1. We instead make the code refer to V0 as the
binary state, and V1 as the first version of JSON state.
2016-05-10 14:40:12 -04:00
Mitchell Hashimoto 35c87836b4 core: Add terraform_version to state
This adds a field terraform_version to the state that represents the
Terraform version that wrote that state. If Terraform encounters a state
written by a future version, it will error. You must use at least the
version that wrote that state.

Internally we have fields to override this behavior (StateFutureAllowed),
but I chose not to expose them as CLI flags, since the user can just
modify the state directly. This is tricky, but should be tricky to
represent the horrible disaster that can happen by enabling it.

We didn't have to bump the state format version since the absense of the
field means it was written by version "0.0.0" which will always be
older. In effect though this change will always apply to version 2 of
the state since it appears in 0.7 which bumped the version for other
purposes.
2016-05-10 14:40:11 -04:00
Mitchell Hashimoto a94b9fdc92 terraform: Internals for `state rm` command
I decided to split this up from the terraform state rm command to make the diff easier to see. These changes will also be used for terraform state mv.

This adds a `Remove` method to the `*terraform.State` struct. It takes a list of addresses and removes the items matching that list. This leverages the `StateFilter` committed last week to make the view of the world consistent across address lookups.

There is a lot of test duplication here with StateFilter, but in Terraform style: we like it that way.
2016-05-10 14:14:48 -04:00
Paul Hinze c7f5450a96 command: Add `terraform untaint`
- [x] Docs
 - [x] Command Unit Tests
 - [x] State Unit Tests

Closes #4820
2016-03-11 12:38:57 -06:00
Paul Hinze e76fdb92e7 core: fix bug detecting deeply nested module orphans
Context:

As part of building up a Plan, Terraform needs to detect "orphaned"
resources--resources which are present in the state but not in the
config. This happens when config for those resources is removed by the
user, making it Terraform's responsibility to destroy them.

Both state and config are organized by Module into a logical tree, so
the process of finding orphans involves checking for orphaned Resources
in the current module and for orphaned Modules, which themselves will
have all their Resources marked as orphans.

Bug:

In #3114 a problem was exposed where, given a module tree that looked
like this:

```
root
 |
 +-- parent (empty, except for sub-modules)
       |
       +-- child1 (1 resource)
       |
       +-- child2 (1 resource)
```

If `parent` was removed, a bunch of error messages would occur during
the plan. The root cause of this was duplicate orphans appearing for the
resources in child1 and child2.

Fix:

This turned out to be a bug in orphaned module detection. When looking
for deeply nested orphaned modules, root.parent was getting added twice
as an orphaned module to the graph.

Here, we add an additional check to prevent a double add, which
addresses this scenario properly.

Fixes #3114 (the Provisioner side of it was fixed in #4877)
2016-02-09 10:35:46 -06:00
Paul Hinze a0d3245ee3 core: Orphan addressing / targeting
Instead of trying to skip non-targeted orphans as they are added to
the graph in OrphanTransformer, remove knowledge of targeting from
OrphanTransformer and instead make the orphan resource nodes properly
addressable.

That allows us to use existing logic in TargetTransformer to filter out
the nodes appropriately. This does require adding TargetTransformer to the
list of transforms that run during DynamicExpand so that targeting can
be applied to nodes with expanded counts.

Fixes #4515
Fixes #2538
Fixes #4462
2016-01-19 17:48:44 -06:00
Paul Hinze 05c0998d2d core: store deeply nested modules in a consistent order in the state
We were only comparing the last element of the module, which meant that
deeply nested modules with the same name but different ancestry had an
undefined sort order, which could cause inconsistencies in state
storage and potentially break remote state MD5 checksumming.
2015-10-20 14:35:57 -05:00
Paul Hinze fca44bdec3 core: state metadata difference should bump serial
Remote state includes MD5-based checksumming to protect against State
conflicts. This can generate improper conflicts with states that differ
only in their Schema version.

We began to see this issue with
https://github.com/hashicorp/terraform/pull/3470 which changes the
"schema_version" of aws_key_pairs.
2015-10-20 12:28:12 -05:00
Anthony Scalisi 198e1a5186 remove various typos 2015-09-11 11:56:20 -07:00
Mitchell Hashimoto 955bbbba41 terraform: state module orphans should only return direct 2015-07-20 08:41:24 -07:00
Mitchell Hashimoto edd05f2aa2 terraform: fix some test failures 2015-07-19 14:14:05 -07:00
Mitchell Hashimoto 96a04c16f6 terraform: state ModuleOrphans should return grandchild orphans 2015-07-19 13:41:57 -07:00
Matt Good 21b0a03d70 Support for multiple providers of the same type
Adds an "alias" field to the provider which allows creating multiple instances
of a provider under different names. This provides support for configurations
such as multiple AWS providers for different regions. In each resource, the
provider can be set with the "provider" field.

(thanks to Cisco Cloud for their support)
2015-04-20 14:14:34 -07:00
Paul Hinze f2968b045c Merge pull request #1316 from hashicorp/b-empty-instancestate-state-migrate-crash
providers/aws: handle empty instancestate in state migration
2015-03-29 08:53:04 -05:00
Seth Vargo 1adb3fbfa0 command: when setting up state, only write back if local is newer 2015-03-26 17:16:54 -07:00
Paul Hinze f51fb5e127 providers/aws: handle empty instancestate in state migration
fixes #1309
2015-03-26 13:07:04 -05:00
Mitchell Hashimoto 9c6e2cdc21 terraform: make sure our serial is always higher 2015-03-25 15:40:53 -07:00
Mitchell Hashimoto 0d4c7887c5 terraform: don't increment state if one is nil 2015-03-25 15:38:24 -07:00
Paul Hinze 3ba8ed536b helper/schema: record schema version on apply
We were previously only recording the schema version on refresh. This
caused the state to be incorrectly written after a `terraform apply`
causing subsequent commands to run the state through an unnecessary
migration.
2015-03-18 19:08:48 -05:00
Paul Hinze 3d4b55e557 helper/schema: schema versioning & migration
Providers get a per-resource SchemaVersion integer that they can bump
when a resource's schema changes format. Each InstanceState with an
older recorded SchemaVersion than the cureent one is yielded to a
`MigrateSchema` function to be transformed such that it can be addressed
by the current version of the resource's Schema.
2015-03-06 16:26:11 -06:00
Paul Hinze 6e13aacefa core: [refactor] DRY up EvalWriteState nodes
Also some final comment cleanup
2015-03-04 18:22:18 -06:00
Paul Hinze 6c93fbb85d core: [refactor] store Deposed resource instances as a list
Deposed instances need to be stored as a list for certain pathological
cases where destroys fail for some reason (e.g. upstream API failure,
Terraform interrupted mid-run). Terraform needs to be able to remember
all Deposed nodes so that it can clean them up properly in subsequent
runs.

Deposed instances will now never touch the Tainted list - they're fully
managed from within their own list.

Added a "multiDepose" test case that walks through a scenario to
exercise this.
2015-03-04 12:25:59 -06:00
Paul Hinze 596e891b80 core: [refactor] pull Deposed out of Tainted list 2015-03-04 12:25:47 -06:00
Mitchell Hashimoto 4ec31ecb95 command/taint: new command 2015-02-26 10:29:23 -08:00
Mitchell Hashimoto b3cd1bd5bc terraform: add ResourceState.Taint 2015-02-26 09:58:56 -08:00
Mitchell Hashimoto 57f7507ebd terraform: more state tests, fix a bug 2015-02-23 21:43:54 -08:00
Mitchell Hashimoto f3af221866 terraform: make DeepCopy public 2015-02-23 21:32:27 -08:00
Mitchell Hashimoto c2bf600603 state: only change serial if changed 2015-02-23 21:26:33 -08:00