Commit Graph

1065 Commits

Author SHA1 Message Date
Brandon Croft
c33c8b013f
fix: have terraform output adhere to authorization w/ cloud
Normally, `terraform output` refreshes and reads the entire state in the command package before pulling output values out of it. This doesn't give Terraform Cloud the opportunity to apply the read state outputs org permission and instead applies the read state versions permission.

I decided to expand the state manager interface to provide a separate GetRootOutputValues function in order to give the cloud backend a more nuanced opportunity to fetch just the outputs. This required moving state Refresh/Read code that was previously in the command into the shared backend state as well as the filesystem state packages.
2022-07-25 10:04:43 -06:00
Martin Atkins
72dd14ca5c core: Do everything except the actual action for plans.NoOp
Previously we tried to early-exit before doing anything at all for any
no-op changes, but that means we also skip some ancillary steps like
evaluating any preconditions/postconditions.

Now we'll skip only the main action itself for plans.NoOp, and still run
through all of the other side-steps.

Since one of those other steps is emitting events through the hooks
interface, this means that now no-op actions are visible to hooks, whereas
before we always filtered them out before calling. I therefore added some
additional logic to the hooks to filter them out at the UI layer instead;
the decision for whether or not to report that we visited a particular
object and found no action required seems defensible as a UI-level concern
anyway.
2022-07-22 15:27:15 -07:00
Martin Atkins
9e277033bc core: Create apply graph nodes even for no-op "changes"
We previously would optimize away the graph nodes for any resource
instance without a real change pending, but that means we don't get an
opportunity to re-check any invariants associated with the instance, such
as preconditions and postconditions.

Other upstream changes during apply can potentially decide the outcome of
a condition even if the instance itself isn't being changed, so we do
still need to revisit these during apply or else we might skip running
certain checks altogether, if they yielded unknown results during planning
and then don't get run during apply.
2022-07-22 15:27:15 -07:00
Martin Atkins
3cd069900b core: Use DynamicExpand even for root module outputs
We previously had a special case in the graph transformer for output
values where it would directly create an individual output value node
instead of an "expand" node as we would do for output values in nested
modules.

While it's true that we do always know that expanding a root module
output value will always produce exactly one instance, treating this case
as special creates the risk of those two codepaths diverging in other
ways.

Instead, we'll let the expand node also deal with root modules and
minimize the special case only to how we look up any changes for the
output values, since the design of plans.Changes is a bit awkward and
requires us to ask the question differently for root module output values.
Otherwise, the behavior will now be consistent across all output values
regardless of module.
2022-07-22 15:25:22 -07:00
Brandon Croft
74ee19b864
Merge pull request #31466 from hashicorp/aw/send-jsonstateoutputs
Include JSONStateOutputs
2022-07-22 14:49:09 -06:00
James Bardin
8b1b05f50f
Merge pull request #31499 from hashicorp/jbardin/dag-walks
topological sorting for refactoring
2022-07-22 15:41:37 -04:00
Brandon Croft
7892cb6b5a
reformat imports using goimports 2022-07-22 12:03:03 -06:00
James Bardin
e3ca4be4b9 goimports 2022-07-22 13:57:16 -04:00
James Bardin
dc9d7108dd use TopologicalOrder for evaluating moves
The dag package did not previously provide a topological walk of a given
graph. While the existing combination of a transitive reduction with a
depth-first walk appeared to accomplish this, depth-first is only
equivalent with a simple tree. If there are multiple paths to a node, a
depth-first approach will skip dependencies from alternate paths.
2022-07-22 13:57:16 -04:00
James Bardin
ca272b2107 Add methods for topological sorts
A topological walk was previously only done in Terraform via the
concurrent method used for walking the primary dependency graph in core.
Sometime however we want a dependency ordering without the overhead of
instantiating the concurrent walk with the channel-based edges.

Add TopologicalOrder and ReverseTopologicalOrder to obtain a list of
nodes which can be used to visit each while ensuring that all
dependencies are satisfied.
2022-07-22 13:57:16 -04:00
Brandon Croft
0b9bd2fc36
make cloud backend_state error messages more conventional 2022-07-22 11:31:48 -06:00
Brandon Croft
01d510f3cb
test(cloud): ensure state version is created when saving state 2022-07-22 11:26:28 -06:00
Anna Winkler
d72911a640 Save detailed type outputs to JSONStateOutputs 2022-07-22 10:28:20 -06:00
James Bardin
95019e3d02 Implement breadth-first walks and add tests
Make DAG walks test-able, and add tests for more complex graph ordering.
We also add breadth-first for comparison, though it's not used currently
in Terraform.
2022-07-22 11:01:29 -04:00
Martin Atkins
69aa0a2b1f states/remote: use t.Run in table-based tests
These tests were originally written long before Go supported subtests
explicitly, but now that we have t.Run we can avoid the prior problem
that one test failing would mask all of the others that followed it.

Now we'll always run all of them, potentially collecting more errors in
a single run so we can have more context to debug with and potentially
fix them all in a single step rather than one by one.
2022-07-21 13:40:56 -07:00
Liam Cervante
ad5ac89461
Fix tests failing on main (#31481) 2022-07-20 14:10:48 +01:00
Liam Cervante
9f0d1d0630
terraform init: add link to documentation when a checksum is missing from the lock file (#31408)
* terraform init: add suggested fix for when a checksum is missing from the lock file

* improve error message

* add link to the documentation

* cleanup leftovers from previous attempt

* fix tests

* s/,/;

* fix imports
2022-07-20 13:28:16 +01:00
Liam Cervante
83e84e5477
terraform init: add warning and guidance when lock file is incomplete (#31399)
* terraform init: add warning and guidance when lock file is incomplete

* make the provider list in the warning deterministic

* create installer event for tracking provider lock hashes (#31406)

* create installer event for tracking provider lock hashes

* address comments

* fix tests

* improve error message

* Update internal/command/init.go

Co-authored-by: Martin Atkins <mart@degeneration.co.uk>

Co-authored-by: Martin Atkins <mart@degeneration.co.uk>
2022-07-20 13:28:04 +01:00
Liam Cervante
224728879d
Ignore existing package hashes for providers lock command (#31389)
* Ignore existing package hashes for  command

* missing new line

* Fix incorrect logic when deciding change message

* fix imports
2022-07-20 13:27:24 +01:00
kmoe
7b4a5513a9
command: fix panic on show when state file is invalid or unavailable (#31444) 2022-07-15 17:31:56 +01:00
hellertang
0dbf0711a0
cos backend support accelerate (#31425) 2022-07-15 17:07:41 +01:00
Alisdair McDiarmid
d3284bdc4e
Merge pull request #31220 from FooBartn/string-starts-ends-with-funcs
feat: add startswith and endswith funcs
2022-07-14 13:13:04 -04:00
Jeff Bonhag
f30738d965
Add golden reference test for JSON plan (#31362)
* Add golden JSON test for Terraform plan

* Add data source to golden JSON plan

* Move output comparison code into shared helper function

* Add note for maintainer to contact TFC when UI changes

UI changes may potentially impact the behavior of structured run output
on TFC.

* Add test_data_source to other mock providers
2022-07-12 17:00:36 -04:00
Tom Bamford
dc1f5bccf8
Merge pull request #31070 from hashicorp/f/removing-adal-auth
backend/azurerm: exclusively using Microsoft Graph/MSAL and removing Azure Active Directory Graph/ADAL
2022-07-12 12:18:29 +01:00
Brian Sidebotham
a6aa75931b Update internal/command/fmt.go
Co-authored-by: Laura Pacilio <83350965+laurapacilio@users.noreply.github.com>
2022-07-08 09:22:22 -07:00
Brian Sidebotham
7dd199cbd0 Fixes #30072 - Update documentation to show that the old DIR option can target a single file instead 2022-07-08 09:19:50 -07:00
Radek Simko
7feef1c4aa
Use hashicorp/terraform-registry-address as a decoupled library (#28338)
* refactor: Use tfaddr for provider address parsing

* refactor: Use tfaddr for module address parsing

* deps: introduce hashicorp/terraform-registry-address
2022-07-08 14:46:29 +01:00
Martin Atkins
de8eef1da5 addrs: Format string instance keys in an HCL-compatible way
So far we've only ever needed to re-parse address strings that happen not
to contain instance keys and so we've gotten away with our serialization
of these not being quite right, but given how liberally we've expected to
be able to use address strings from this package for wire format
interchange it seems likely that this is going to surprise us eventually.

Now we'll use an escaping scheme compatible with HCL's parser rather
than Go's parser, and so we can safely rely on hclsyntax.ParseTraversal
as part of reversing this operation to transform an address string back
into an address equivalent to the value it was created from.
2022-07-07 14:05:48 -07:00
James Bardin
26c569e384 s/Capabilities/ServerCapabilities/ 2022-07-06 13:47:35 -04:00
James Bardin
fd742cd544 add e2e test with provider schema capabilities
enable destroy planning for the simple providers used in the e2e tests
2022-07-06 13:47:35 -04:00
James Bardin
6706d52832 check PlanDestroy capability in plugins
This is most easily handled in the plugin code, without involving
Terraform core.

The biggest change here other than checking the PlanDestroy capability,
is the removal of the schema helper methods in the plugins. With the
addition of the capabilities field, combined with the necessity of
checking diagnostics from the schema, the helpers have outlived their
usefulness. Perhaps there's a better pattern for these repetitive calls,
but for now there isn't too extra verbosity involved.
2022-07-06 13:47:35 -04:00
James Bardin
b9f1a5ac57 add Schema Capabilities to protocol 2022-07-06 13:47:35 -04:00
James Bardin
9487cfba28 add test for planned private data in destroy 2022-07-06 13:47:35 -04:00
James Bardin
96c720517f fixup broken test fixtures
some of the minimal test provider implementations didn't check for null
values.
2022-07-06 13:47:35 -04:00
James Bardin
acba1159e4 add provider metas to destroy plan 2022-07-06 13:47:35 -04:00
James Bardin
ccd2c7820a plan resource destroy
Call PlanResourceDestroy during a destroy plan.

This allows providers two new abilities:
- They can evaluate if the plan is valid, notifying users of any
  potential errors before an apply is started, which may not be able to
  complete.
- They can inspect and modify their private data during a destroy plan
  just like they can with an other plan operation.
2022-07-06 13:47:35 -04:00
James Bardin
e95bfe6d19 fix test mocks to behave when planning destroys 2022-07-06 13:47:35 -04:00
James Bardin
1214a16a63
Merge pull request #31347 from hashicorp/jbardin/remove-etcdv3
remove deprecated etcdv3 backend
2022-07-05 11:50:02 -04:00
Martin Atkins
b88e29e473 configs: Show tailored message for optional attrs experiment
Since we've both concluded the module_variables_optional_attrs experiment
and made experiments available only in alpha releases in the same minor
release, we accidentally made the more general message about experiments
not being available mask the specific message about the experiment being
concluded.

In order to give better feedback to those who were participating in the
experiment in earlier Terraform releases, we'll retain a minimal exception
to our checks to allow the "experiment has concluded" error message to
shine through if and only if that is the only selected experiment.
2022-07-01 09:08:12 -07:00
Joshua Barton
fd3798b276 tests: add test cases around empty values 2022-06-30 18:30:32 -05:00
Joshua Barton
c73c0ad63c feat: add startswith and endswith funcs 2022-06-30 18:30:32 -05:00
James Bardin
d61d3e9fd8 remove deprecated etcdv3 backend 2022-06-30 15:32:43 -04:00
Liam Cervante
acb79a7545
Standardise how blank lines are added to plan for unchanged blocks (#31330)
* Standardise blank lines added to plan for unchanged blocks

* Fix copy-paste error inside NestingList logic
2022-06-30 13:29:47 +01:00
Liam Cervante
d876e68e2d
Fail global required_version check if it contains any prerelease fields (#31331)
* Fail global required_version check if it contains any prerelease fields

* go mod tidy

* Improve required_version prerelease not supported error string

* Add prerelease version constraint unit tests

* Fix side-effects by populating global diags too soon
2022-06-30 09:58:28 +01:00
James Bardin
953c448f9a add simple error indicating backend removal
There are no good options for inserting diagnostics into the backend
lookup, or creating a backend which reports it's removal because none of
the init or GetSchema functions return any errors.

Keep a registry of the removed backend so that we can at least notify
users that a backend was removed vs an invalid name.
2022-06-28 13:58:22 -04:00
James Bardin
799ab6c951 remove deprecated etcd backend
This allows us to remove the manual replace directives
github.com/dgrijalva/jwt-go and google.golang.org/grpc, so that we can
remove the CVE warnings and update the grpc packages.

While the etcdv3 backend is also marked as deprecated, the changes here
are done in a manner to keep that backend working for the time being.
2022-06-27 15:01:21 -04:00
Martin Atkins
90ea7b0bc5 tfdiags: Treat unknown-related or sensitive-related messages differently
By observing the sorts of questions people ask in the community, and the
ways they ask them, we've inferred that various different people have been
confused by Terraform reporting that a value won't be known until apply
or that a value is sensitive as part of an error message when that message
doesn't actually relate to the known-ness and sensitivity of any value.

Quite reasonably, someone who sees Terraform discussing an unfamiliar
concept like unknown values can assume that it must be somehow relevant to
the problem being discussed, and so in that sense Terraform's current
error messages are giving "too much information": information that isn't
actually helpful in understanding the problem being described, and in the
worst case is a distraction from understanding the problem being described.

With that in mind then, here we introduce an explicit annotation on
diagnostic objects that are directly talking about unknown values or
sensitive values, and then the diagnostic renderer will react to that to
avoid using the terminology "known only after apply" or "sensitive" in the
generated diagnostic annotations unless we're rendering a message that is
explicitly related to one of those topics.

This ends up being a bit of a cross-cutting concern because the code that
generates these diagnostics and the code that renders them are in separate
packages and are not directly aware of each other. With that in mind, the
logic for actually deciding for a particular diagnostic whether it's
flagged in one of these special ways lives inside the tfdiags package as
an intermediation point, which both the diagnostic generator (in the core
package) and the diagnostic renderer can both depend on.
2022-06-23 13:52:23 -07:00
Martin Atkins
31aee9650e command/format: Include function call information in diagnostics
When an error occurs in a function call, the error message text often
includes references to particular parameters in the function signature.

This commit improves that reporting by also including a summary of the
full function signature as part of the diagnostic context in that case,
so a reader can see which parameter is which given that function
arguments are always assigned positionally and so the parameter names
do not appear in the caller's source code.
2022-06-23 13:52:23 -07:00
Martin Atkins
8405f46bc5 tfdiags: Expose the "extra information" concept from HCL
HCL's diagnostic model now includes the idea of "extra information" which
works by attaching an initially-opaque interface value to each diagnostic
and then asking callers to type-assert against that value to sniff for
particular interfaces in order to discover additional machine-readable
context about a certain diagnostic message.

This commit echoes that idea into our tfdiags API, for now only for
diagnostics that are backed by an hcl.Diagnostic. All other implementations
of the diagnostic interface just always return nil, which means they never
carry any "extra information".

As is typical for our wrapping abstraction, we have here also a modified
copy of HCL's helper function for conveniently probing a diagnostic for
information of a particular type, designed to work with our diagnostic
interface instead of HCL's concrete diagnostic type.
2022-06-23 13:52:23 -07:00
James Bardin
77e6b622f8
Merge pull request #31283 from hashicorp/jbardin/plan-import
Use plan graph for importing resources
2022-06-23 13:26:59 -04:00