Commit Graph

3254 Commits

Author SHA1 Message Date
James Bardin
5fe848b642 change mock return values to pointers
This allows up to detect an unset value from the zero value so that
defaults can be implemented, while still allowing tests to return
specific values of their choosing.
2021-01-12 15:16:48 -05:00
James Bardin
fb2208a6d9 refactor context plan
get rid of the mutation of the plans.Plan and return values.
2021-01-08 13:29:54 -05:00
James Bardin
0b3b84acc1 refresh state during a destroy plan
Because the destroy plan only creates the necessary changes for apply to
remove all the resources, it does no reading of resources or data
sources, leading to stale data in the state. In most cases this is not a
problem, but when a provider configuration is using resource values, the
provider may not be able to run correctly during apply. In prior
versions of terraform, the implicit refresh that happened during
`terraform destroy` would update the data sources and remove missing
resources from state as required.

The destroy plan graph has a minimal amount of information, so it is not
feasible to work the reading of resources into the operation without
completely replicating the normal plan graph, and updating the plan
graph and all destroy node implementation is also a considerable amount
of refactoring. Instead, we can run a normal plan which is used to
refresh the state before creating the destroy plan. This brings back
similar behavior to core versions prior to 0.14, and the refresh can
still be skipped using the `-refresh=false` cli flag.
2021-01-08 13:29:54 -05:00
James Bardin
3e224df379 destroy plan with provider config from data 2021-01-08 13:29:54 -05:00
Pam Selle
d175e67bc9
Merge pull request #27429 from hashicorp/pselle/staticcheck
Staticcheck fixes in terraform package
2021-01-08 11:10:40 -05:00
Pam Selle
c9f372a62b
Merge pull request #24896 from bendrucker/validate-ignore-empty-provider
validate: ignore providers with no configuration
2021-01-07 15:30:57 -05:00
James Bardin
9b3c4159be remove unused testdata files
these are no longer referenced in any tests
2021-01-07 12:13:25 -05:00
Pam Selle
fb5f7b9a59 Staticcheck fixes
Fixes within the terraform package to remove staticcheck errors
2021-01-07 10:28:40 -05:00
Alisdair McDiarmid
f96c193060
Merge pull request #27412 from hashicorp/alisdair/fix-variable-validation-sensitive-value
core: Fix sensitive value variable validation
2021-01-06 09:57:34 -05:00
Alisdair McDiarmid
1fa65bdd91 core: Fix sensitive value variable validation
Binding a sensitive value to a variable with custom validation rules
would cause a panic, as the validation expression carries the sensitive
mark when it is evaluated for truthiness. This commit drops the marks
before testing, which fixes the issue.
2021-01-05 13:52:33 -05:00
Pam Selle
dbde1be363 Update validate test to add case for marked count value
The diff here is largely a refactor to allow the test to run multiple cases
2021-01-05 11:49:35 -05:00
Pam Selle
33c31dce2b Use evaluateCountExpressionValue in validate step
There was a special, duplicate function that could be replaced by
calling evaluateCountExpressionValue, and removes duplicate code
2021-01-05 11:26:18 -05:00
Alisdair McDiarmid
f770f03620
Merge pull request #27335 from hashicorp/alisdair/fix-sensitive-data-source-arguments
core: Fix for sensitive data source arguments
2021-01-04 13:53:35 -05:00
Pam Selle
87b576da7a Roll back test schema expansion and isolate
Isolate the test schema expansion, because having NestingSet
in the schema actually necessitates [] values in the AttrsJson.
While this didn't fail any tests on its addition, that
is scary and so isolate this to the one test using it.
2020-12-18 15:08:44 -05:00
Alisdair McDiarmid
577b1ea2af core: Fix for sensitive data source arguments 2020-12-18 11:22:00 -05:00
James Bardin
1309b36b83 plan context test for mysterious changes
This plan would occasionally show changes when there weren't any due to
the sensitive marks being compared incorrectly.
2020-12-17 12:55:58 -05:00
James Bardin
79a31f627b compare unordered sets of PathMarkValues
When comparing marks for values during plan and apply, we need to ensure
the order of the marked paths is consistent.
2020-12-17 12:55:58 -05:00
Pam Selle
428d404d92
Allow for_each arguments containing sensitive values if they aren't keys (#27247)
* Add test for existing behavior, when a value contains a marked value

* Allow some marked values as for_each arguments

Rather than disallow values that have any marks
as for_each arguments, this makes the check more
nuanced to disallow cases where the whole value
is marked (a whole map, or any set). This allows
cases where a user may pass a map that has marked
values, but the keys are not sensitive
2020-12-17 11:27:12 -05:00
James Bardin
c85adf191a modify ProvidedBy to indicate no provider needed
Because of the composition pattern used within core, we can't easily
remove a behavior from an embedded type. Rather than trying to
re-implement all necessary methods on the
NodePlannableResourceInstnaceOrphan to exclude orphaned data resources
from GraphNodeProviderConsumer, we can modify ProvidedBy to indicate
when there is no provider required.
2020-12-17 09:01:20 -05:00
Ben Drucker
a39273cfa3 Merge remote-tracking branch 'origin/master' into validate-ignore-empty-provider 2020-12-14 14:39:48 -08:00
Pam Selle
fa74710aef Guard against double marking in other locations in evaluate 2020-12-14 15:58:06 -05:00
Kristin Laemmert
e938b02337
terraform: improve provider config related error messages (#27261)
* terraform: improve provider config related error messages with nil
config

If there is no provider configuration present in the config at all,
errors related to missing required arguments lack source information or
even a reference to the provider in question. This PR adds more specific
error messages in three of these situations:
- ValidateProvider
- ConfigureProvider: provider.PrepareProviderConfig
- ConfigureProvider: ctx.ConfigureProvider

To test the last case I added a ConfigureProviderFn to the MockContext.

* remove newlines and let the diagnost renderer handle fit
2020-12-11 13:18:49 -05:00
Pam Selle
6e1017f247
Merge pull request #27238 from hashicorp/pselle/count-sensitive
Unmark values in count before go conversion
2020-12-10 13:36:59 -05:00
Pam Selle
0d586fd056 Unmark values in count before go conversion
When evaluating count values, we need to unmark
the cty value before passing the value for conversion
into a go int value.
2020-12-10 11:38:10 -05:00
Kristin Laemmert
ff27841b50
terraform: final eval-related cleanup (#27232)
This is a purely mechanical refactor PR: I de-exported a few more
functions which did not need to be exported in the first place, and
fixed a few outdated log outputs.
2020-12-10 09:55:50 -05:00
Kristin Laemmert
c7bf43154f
Mildwonkey/eval apply (#27222)
* rename files for consistency with contents

* terraform: refactor EvalValidateSelfref

The EvalValidateSelfref eval node implementation was removed in favor of a regular function.

* terraform: refactor EvalValidateProvisioner

EvalValidateProvisioner is now a method on NodeValidatableResource.

* terraform: refactor EvalValidateResource

EvalValidateResource is now a method on NodeValidatableResource, and the
functions called by (the new) validateResource are now standalone
functions.

This particular refactor gets the prize for "most complicated test
refactoring".

* terraform: refactor EvalMaybeTainted

EvalMaybeTainted was a relatively simple operation which never returned
an error, so I've refactored it into a plain function and moved it into
the only file its called from.

* terraform: eval-related cleanup

De-exported preApplyHook, which got missed in my general cleanup sweeps.

Removed resourceHasUserVisibleApply in favor of moving the logic inline
- it was a single-line check so calling the function was (nearly) as
much code as just checking if the resource was managed.

* terraform: refactor EvalApplyProvisioners

EvalApplyProvisioners.Eval is now a method on
NodeResourceAbstractInstance. There were two "apply"ish functions, so I
named the first "evalApplyProvisioners" since it mainly determined if
provisioners should be run before passing off execution to
applyProvisioners.

* terraform: refactor EvalApply

EvalApply is now a method on NodeAbstractResourceInstance. This was one
of the trickier Eval()s to refactor, and my goal was to change as little
as possible to avoid unintended side effects.

One notable change: there was a createNew boolean that was only used in
NodeApplyableResourceInstance.managedResourceExecute, and that boolean
was populated from the change (which was available from
managedResourceExecute), so I removed it from apply entirely. Out of an
abundance of caution I assigned the value to createNew in (roughtly) the same spot,
in case I was missing some place where the change might get modified.

TODO: Destroy nodes passed nil configs into apply, and I am curious if
we can get the same functionality by checking if the planned change is a
destroy, instead of passing a config into apply. That felt too risky for
this refactor but it is something I would like to explore at a future
point.

There are also a few updates to log output in this PR, since I spent
some time staring at logs and noticed various spots I missed.
2020-12-10 08:05:53 -05:00
Kristin Laemmert
d50dc9cf16 terraform: refactor EvalValidateResource
EvalValidateResource is now a method on NodeValidatableResource, and the
functions called by (the new) validateResource are now standalone
functions.

This particular refactor gets the prize for "most complicated test
refactoring".
2020-12-10 08:05:00 -05:00
Kristin Laemmert
fbe3219fbe terraform: refactor EvalValidateProvisioner
EvalValidateProvisioner is now a method on NodeValidatableResource.
2020-12-10 08:05:00 -05:00
Kristin Laemmert
a5cb780c87 terraform: refactor EvalValidateSelfref
The EvalValidateSelfref eval node implementation was removed in favor of a regular function.
2020-12-10 08:05:00 -05:00
Kristin Laemmert
468999b9b0 rename files for consistency with contents 2020-12-10 08:05:00 -05:00
Kristin Laemmert
93c36e67b1
terraform: refactor ReadData (#27189)
I took some lessons learned during yesterday's marathon refactoring and
re-refactored the dataSource plan and apply to be functions on
NodeResourceAbstractInstance. Includes mild renaming to differentiate
between plan and planDataSource.
2020-12-08 10:43:04 -05:00
Kristin Laemmert
e7aaf9e39f
Eval() Refactor: Plan Edition (#27177)
* terraforn: refactor EvalRefresh

EvalRefresh.Eval(ctx) is now Refresh(evalRefreshReqest, ctx). While none
of the inner logic of the function has changed, it now returns a
states.ResourceInstanceObject instead of updating a pointer. This is a
human-centric change, meant to make the logic flow (in the calling
functions) easier to follow.

* terraform: refactor EvalReadDataPlan and Apply

This is a very minor refactor that removes the (currently) redundant
types EvalReadDataPlan and EvalReadDataApply in favor of using
EvalReadData with a Plan and Apply functions.

This is in effect an aesthetic change; since there is no longer an
Eval() abstraction we can rename functions to make their functionality
as obvious as possible.

* terraform: refactor EvalCheckPlannedChange

EvalCheckPlannedChange was only used by NodeApplyableResourceInstance
and has been refactored into a method on that type called
checkPlannedChange.

* terraform: refactor EvalDiff.Eval

EvalDiff.Eval is now a method on NodeResourceAbstracted called Plan
which takes as a parameter an EvalPlanRequest. Instead of updating
pointers it returns a new plan and state.

I removed as many redundant fields from the original EvalDiff struct as
possible.

* terraform: refactor EvalReduceDiff

EvalReduceDiff is now reducePlan, a regular function (without a method)
that returns a value.

* terraform: refactor EvalDiffDestroy

EvalDiffDestroy.Eval is now NodeAbstractResourceInstance.PlanDestroy
which takes ctx, state and optional DeposedKey and returns a change.
I've removed the state return value since it was only ever returning a
nil state.

* terraform: refactor EvalWriteDiff

EvalWriteDiff.Eval is now NodeAbstractResourceInstance.WriteChange.

* rename files to something more logical

* terrafrom: refresh refactor, continued!

I had originally made Refresh a stand-alone function since it was
(obnoxiously) called from a graphNodeImportStateSub, but after some
(greatly appreciated) prompting in the PR I instead made it a method on
the NodeAbstractResourceInstance, in keeping with the other refactored
eval nodes, and then built a NodeAbstractResourceInstance inside import.

Since I did that I could also remove my duplicated 'writeState' code
inside graphNodeImportStateSub and use n.writeResourceInstanceState, so
double thanks!

* unexport eval methods

* re-refactor Plan, it made more sense on NodeAbstractResourceInstance. Sorry

* Remove uninformative `Eval`s from EvalReadData, consolidate to a single
file, and rename file to match function names.

* manual rebase
2020-12-08 08:50:30 -05:00
James Bardin
ee52f26647 ignore_changes can ignore unknowns too
The logic for handling unknown equality was incorrect, and would miss
changes where the configuration is still not known during plan.
2020-12-07 14:01:44 -05:00
Kristin Laemmert
bedc08f5eb
terraform: refactor EvalWriteStateDeposed (#27149)
* terraform: refactor EvalWriteStateDeposed

EvalWriteStateDeposed is now
NodeDestroyDeposedResourceInstanceObject.writeResourceInstanceState.
Since that's the only caller I considered putting the logic directly
inline, but things are clunky enough right now that I think this is good
enough for this refactor.
2020-12-07 08:39:20 -05:00
Ben Drucker
7e11b97923 Merge remote-tracking branch 'origin/master' into validate-ignore-empty-provider 2020-12-06 09:47:24 -08:00
Ben Drucker
a2c31088f4 call configBody.Content 2020-12-06 09:38:43 -08:00
Ben Drucker
186eff96b8 expand early return comment 2020-12-06 09:36:53 -08:00
Ben Drucker
eacf8b5c55 rename empty provider config test 2020-12-06 09:35:35 -08:00
Pam Selle
ae025248cc
Merge pull request #27131 from hashicorp/pselle/double-marks
Avoid double-marking variables
2020-12-04 13:21:54 -05:00
Pam Selle
12b5d437da Avoid double-marking variables
It is possible, say with multiple layers of
sensitive variables, to "double-mark" a variable.
Add a check to ensure this does not happen.
2020-12-04 13:10:02 -05:00
James Bardin
3b4079d451
Merge pull request #27141 from hashicorp/jbardin/ignore_changes
Fixes for ignore_changes with unintended provider behavior
2020-12-04 12:55:26 -05:00
Kristin Laemmert
29d89c4a15
Eval(): refactor EvalWriteState() (#27145)
* fix inaccurate log

* terraform: refactor EvalWriteState

EvalWriteState is refactored into a method on
NodeAbstractResourceInstance and renamed writeResourceInstanceState.

Import, my nemesis, gave me pause. I did not expect to find
EvalWriteState in an transform node, and so I decided to copy the
function inline rather than rethink my entire refactor for one function
that's likely to be (heavily) refactored in the future.
2020-12-04 12:44:40 -05:00
Kristin Laemmert
7370a98ab7
Eval() Refactor (#27087)
* terraform: refactor EvalPreApply and EvalPostApply

EvalPreApply and EvalPostApply have been refactored as methods on
NodeAbstractResourceInstance.

* terraform: remove EvalReadState and EvalReadStateDeposed

These two functions had already been re-implemented as functions on
NodeAbstractResource, so this commit finished the process of removing
the Evals and refactoring the tests.

* terraform: remove EvalRefreshLifecycle

EvalRefreshLifecycle was only used in one node,
NodePlannableResourceInstance, so the functionality has been moved
directly inline.

* terraform: remove EvalDeposeState

EvalDeposeState was only used in one function, so it has been removed
and the logic placed in-line in
NodeApplyableResourceInstance.managedResourceExecute.

* terraform: remove EvalMaybeRestoreDeposedObject

EvalMaybeRestoreDeposedObject was only used in one place, so I've
removed it in favor of in-line code.
2020-12-04 09:16:26 -05:00
James Bardin
4f4e8c17e0 validate the configuration before ignore_changes
The ignore_changes option `all` can cause computed attributes to show up
in the validation configuration, which will be rejected by the provider
SDK. Validate the config before applying the ignore_changes options.

In the future it we should probably have a way for processIgnoreChanges
to skip computed values based on the schema. Since we also want a way to
more easily query the schema for "computed-ness" to validate the
ignore_changes arguments for computed values, we can fix these at the
same time with a future change to configschema. This will most likely
require some sort of method to retrieve info from the configschema.Block
via cty.Path, which we cannot easily do right now.
2020-12-03 17:59:03 -05:00
James Bardin
02e7efab9e re-apply ignore_changes after plan for legacy
Because we allow legacy providers to depart from the contract and return
changes to non-computed values, the plan response may have altered
values that were already suppressed with ignore_changes. A prime example
of this is where providers attempt to obfuscate config data by turning
the config value into a hash and storing the hash value in the state.
There are enough cases of this in existing providers that we must
accommodate the behavior for now, so for ignore_changes to work at all
on these values, we will revert the ignored values once more on the
planned state.
2020-12-03 17:44:35 -05:00
James Bardin
aa5c8add2e fix bug in ignore_changes Transform
The cty.Transform for ignore_changes could return early when building a
map that had multiple ignored keys.

Refactor the function to try and separate the fast-path a little better,
and hopefully make it easier to follow.
2020-12-03 16:09:12 -05:00
Alisdair McDiarmid
50f4d79867 terraform: Write state if sensitivity changes
When applying, we return early if only sensitivity changed between the
before and after values of the changeset. This avoids unnecessarily
invoking the provider.

Previously, we did not write the new value for a resource to the state
when this happened. The result was a permanent diff for resource updates
which only change sensitivity, as the apply step is skipped and the
state is unchanged.

This commit adds a state write to this shortcut return path, and fixes a
test for this exact case which was accidentally relying on a value diff
caused by an incorrect manual state value.
2020-12-03 15:58:54 -05:00
James Bardin
dcf0dba6f4
Merge pull request #27081 from hashicorp/jbardin/staticcheck
Fixes to pass static analysis
2020-12-02 15:43:10 -05:00
Kristin Laemmert
3fa063b8dc
command/format: concise diff is now the default (#27079)
* command/format: concise diff is no longer an experiment

Since state formatting goes through the "diff" printer, I have
repurposed the concise flag as a verbose flag, used only when printing
state. It's silly but it works!

* remove helper/experiment
With this experiment concluded, we no longer need helper/experiment. The
shadow experiment had not been touched in many years, so I removed all
references, and removed the package entirely. Any new experiments are
expected to be configuration experiments handled by our (other)
experiments package.

* check for the verbose flag consistently, in case we end up using it in plans in the future
2020-12-02 15:42:41 -05:00
James Bardin
3a6c32cb1c terraform: staticcheck 2020-12-02 13:59:19 -05:00