Commit Graph

254 Commits

Author SHA1 Message Date
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
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
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
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
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
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
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
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
James Bardin
4981942191 remove -allow-missing-config from import 2022-06-21 11:43:04 -04:00
James Bardin
03fc2b09df remove errant fmt.Println 2022-06-21 11:32:07 -04:00
Martin Atkins
fda0579537 Experiments supported only in alpha/dev builds
We originally introduced the idea of language experiments as a way to get
early feedback on not-yet-proven feature ideas, ideally as part of the
initial exploration of the solution space rather than only after a
solution has become relatively clear.

Unfortunately, our tradeoff of making them available in normal releases
behind an explicit opt-in in order to make it easier to participate in the
feedback process had the unintended side-effect of making it feel okay
to use experiments in production and endure the warnings they generate.
This in turn has made us reluctant to make use of the experiments feature
lest experiments become de-facto production features which we then feel
compelled to preserve even though we aren't yet ready to graduate them
to stable features.

In an attempt to tweak that compromise, here we make the availability of
experiments _at all_ a build-time flag which will not be set by default,
and therefore experiments will not be available in most release builds.

The intent (not yet implemented in this PR) is for our release process to
set this flag only when it knows it's building an alpha release or a
development snapshot not destined for release at all, which will therefore
allow us to still use the alpha releases as a vehicle for giving feedback
participants access to a feature (without needing to install a Go
toolchain) but will not encourage pretending that these features are
production-ready before they graduate from experimental.

Only language experiments have an explicit framework for dealing with them
which outlives any particular experiment, so most of the changes here are
to that generalized mechanism. However, the intent is that non-language
experiments, such as experimental CLI commands, would also in future
check Meta.AllowExperimentalFeatures and gate the use of those experiments
too, so that we can be consistent that experimental features will never
be available unless you explicitly choose to use an alpha release or
a custom build from source code.

Since there are already some experiments active at the time of this commit
which were not previously subject to this restriction, we'll pragmatically
leave those as exceptions that will remain generally available for now,
and so this new approach will apply only to new experiments started in the
future. Once those experiments have all concluded, we will be left with
no more exceptions unless we explicitly choose to make an exception for
some reason we've not imagined yet.

It's important that we be able to write tests that rely on experiments
either being available or not being available, so here we're using our
typical approach of making "package main" deal with the global setting
that applies to Terraform CLI executables while making the layers below
all support fine-grain selection of this behavior so that tests with
different needs can run concurrently without trampling on one another.

As a compromise, the integration tests in the terraform package will
run with experiments enabled _by default_ since we commonly need to
exercise experiments in those tests, but they can selectively opt-out
if they need to by overriding the loader setting back to false again.
2022-06-17 14:46:07 -07:00
Alisdair McDiarmid
c7bc82bd87
Merge pull request #31235 from hashicorp/alisdair/json-plan-unknown-outputs
json-output: Extended detail for unknown outputs
2022-06-17 11:50:21 -04:00
Alisdair McDiarmid
9497b2cd6f json-output: Fix unknowns for tuples and sets
The JSON output for sequences previously omitted unknown values for
tuples and sets, which made it impossible to interpret the corresponding
unknown marks. For example, consider this resource:

    resource "example_resource" "example" {
      tags = toset(["alpha", timestamp(), "charlie"])
    }

This would previously be encoded in JSON as:

    "after": {
        "tags": ["alpha", "charlie"]
    },
    "after_unknown": {
        "id": true,
        "tags": [false, true, false]
    },

That is, the timestamp value would be omitted from the output
altogether, while the corresponding unknown marks would include a value
for each of the set members.

This commit changes the behaviour to:

    "after": {
        "tags": ["alpha", null, "charlie"]
    },
    "after_unknown": {
        "id": true,
        "tags": [false, true, false]
    },

This aligns tuples and sets with the prior behaviour for lists, and
makes it clear which elements are known and which are unknown.
2022-06-13 14:33:40 -04:00
Alisdair McDiarmid
48d64eabb2 json-output: Extended detail for unknown outputs
Planned output changes are represented in the JSON output format using
the same change object as planned resource changes. This structure
includes an `after` value and a parallel `after_unknown` value, which
can be combined to determine which specific parts of a value are known
only at apply time.

Previously, structured output values would be marked in the JSON plan as
coarsely known or unknown, even if only some subset of the structure
will be known only at apply time. This simplification was unnecessary,
and this commit reuses the same logic for resource changes to give more
information to consumers of this format.

For example, consider this output:

    output "bar" {
      value = tolist([
        "hello",
        timestamp(),
        "world",
      ])
    }

The plan output for this output would be:

    + bar = [
        + "hello",
        + (known after apply),
        + "world",
      ]

For the same plan, the JSON output was previously:

    "bar": {
      "actions": [
        "create"
      ],
      "before": null,
      "after_unknown": true,
      "before_sensitive": false,
      "after_sensitive": false
    }

After this commit, the output is instead:

    "bar": {
      "actions": [
        "create"
      ],
      "before": null,
      "after": [
        "hello",
        null,
        "world"
      ],
      "after_unknown": [
        false,
        true,
        false
      ],
      "before_sensitive": false,
      "after_sensitive": false
    }
2022-06-13 14:06:03 -04:00
James Bardin
47f9850f02 add XTerraformGetLimit to prevent redirect loops 2022-06-01 12:46:22 -04:00
kmoe
54b837416c
command: mention plan options in refresh help text (#30892)
* command: add parallelism option to refresh help text
2022-05-31 14:27:21 +01:00
Alisdair McDiarmid
06f6a901ea
Merge pull request #30985 from hashicorp/alisdair/var-flag-spaces-error
cli: Improved error for invalid -var "foo = bar"
2022-05-30 10:40:00 -04:00
Martin Atkins
096f0dc0da build: Write the detected version number into the generated executable
This also sets an additional variable if it detects that this is an alpha
or development build, which currently does nothing but might eventually
turn on the ability to use experimental features, if we make that
something available only in prereleases.
2022-05-23 16:48:34 -07:00
Craig Wright
be7f46a1ad
Merge pull request #29354 from srdecny/main
Fix CLI help text for output. Reviewed with @apparentlymart and @kmoe.
2022-05-20 10:58:09 -07:00
Martin Atkins
4cffff24b1 core: Report reason for deferring data read until apply
We have two different reasons why a data resource might be read only
during apply, rather than during planning as usual: the configuration
contains unknown values, or the data resource as a whole depends on a
managed resource which itself has a change pending.

However, we didn't previously distinguish these two in a way that allowed
the UI to describe the difference, and so we confusingly reported both
as "config refers to values not yet known", which in turn led to a number
of reasonable questions about why Terraform was claiming that but then
immediately below showing the configuration entirely known.

Now we'll use our existing "ActionReason" mechanism to tell the UI layer
which of the two reasons applies to a particular data resource instance.
The "dependency pending" situation tends to happen in conjunction with
"config unknown", so we'll prefer to refer that the configuration is
unknown if both are true.
2022-05-09 11:12:47 -07:00
Alisdair McDiarmid
91d75baba1 cli: Improved error for invalid -var "foo = bar"
When specifying variable values on the command line, name-value pairs
must be joined with an equals sign, without surrounding spaces.
Previously Terraform would interpret "foo = bar" as assigning the value
" bar" to the variable named "foo ". This is never valid, as variable
names may not include whitespace.

This commit looks for this specific error and returns a diagnostic with
a suggestion for correcting it. We cannot simply trim whitespace,
because it is valid to write "foo= bar" to assign the value " bar" to
the variable "foo", as unlikely as it seems.
2022-05-03 09:14:29 -04:00
Alisdair McDiarmid
12c8f9498c json-output: Add output type to JSON format
Previously the supported JSON plan and state formats included only
serialized output values, which was a lossy serialization of the
Terraform type system. This commit adds a type field in the usual cty
JSON format, which allows reconstitution of the original value.

For example, previously a list(string) and a set(string) containing the
same values were indistinguishable. This change serializes these as
follows:

{
  "value": ["a","b","c"],
  "type": ["list","string"]
}

and:

{
  "value": ["a","b","c"],
  "type": ["set","string"]
}
2022-04-27 13:30:15 -04:00
James Bardin
7da52d94f2
Merge pull request #30900 from hashicorp/jbardin/replace-triggered-by
Configurable instance replacement via lifecycle `replace_triggered_by`
2022-04-22 14:36:42 -04:00
James Bardin
c52e3ed37b test fixture race 2022-04-20 15:01:17 -04:00
James Bardin
3a0a019521 round-trip replace triggers 2022-04-20 09:17:10 -04:00
James Bardin
868052c9e3 set replace_trigered_by reason in diff output 2022-04-20 09:17:10 -04:00
Brandon Croft
1943af51a2
fix(creds): allow periods in TF_TOKEN_... credentials vars 2022-04-15 12:38:14 -06:00
Brandon Croft
f04202d222
Update credentials_test.go 2022-04-13 14:06:25 -06:00
Brandon Croft
fda05f3d44
feat(credentials): allow hyphens to be encoded as __ in variables
Hyphen characters are allowed in environment variable names, but are not valid POSIX variable names. Usually, it's still possible to set variable names with hyphens using utilities like env or docker. But, as a fallback, host names may encode their hyphens as double underscores in the variable name. For the example "café.fr", the variable name "TF_TOKEN_xn____caf__dma_fr" or "TF_TOKEN_xn--caf-dma_fr"
may be used.
2022-04-13 13:49:59 -06:00
Brandon Croft
307326fa3a
allow remote service creds to be configured using env
Introduces a new method of configuring token service credentials using a host-specific environment variable. This configuration was previously possible using the [terraform-credentials-env](https://github.com/apparentlymart/terraform-credentials-env) credentials helper.

This new method is now consulted first, as it is seen to be the most proximate source of credentials before CLI configuration while still falling back to the credentials helper.
2022-04-13 13:49:57 -06:00
Alisdair McDiarmid
fadcaaaad2 cli: Fix double-quoted map keys in diff UI
A previous change added missing quoting around object keys which do not
parse as barewords. At the same time we introduced a bug where map keys
could be double-quoted, due to calling the `displayAttributeName` helper
function (to quote non-bareword keys) then using the `writeValue` method
(which quotes all strings).

This commit fixes this and adds test coverage for map keys which require
quoting.
2022-04-13 09:10:00 -04:00
Alisdair McDiarmid
d23f0998fb cli: Fix plan diff for sensitive nested attributes
When rendering diffs for resources which use nested attribute types, we
must cope with collections backing those attributes which are entirely
sensitive. The most common way this will be seen is through sensitive
values being present in sets, which will result in the entire set being
marked sensitive.
2022-04-08 16:16:52 -04:00
Eng Zer Jun
fedd315275
test: use T.TempDir to create temporary test directory (#30803)
This commit replaces `ioutil.TempDir` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.

Prior to this commit, temporary directory created using `ioutil.TempDir`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
	defer func() {
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}
is also tedious, but `t.TempDir` handles this for us nicely.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2022-04-08 17:34:16 +01:00
Alisdair McDiarmid
bb35f02c95 Conclude preconditions/postconditions experiment 2022-04-04 15:54:40 -04:00
Alisdair McDiarmid
c5d10bdef1 core: Store condition block results in plan
In order to include condition block results in the JSON plan output, we
must store them in the plan and its serialization.

Terraform can evaluate condition blocks multiple times, so we must be
able to update the result. Accordingly, the plan.Conditions object is a
map with keys representing the condition block's address. Condition
blocks are not referenceable in any other context, so this address form
cannot be used anywhere in the configuration.

The commit includes a new test case for the JSON output of a
refresh-only plan, which is currently the only way for a failing
condition result to be rendered through this path.
2022-04-04 15:36:29 -04:00
Martin Atkins
49d7c879ac Fix problems caught by staticcheck v0.3.0
This will allow us to upgrade to this version in a later commit without
causing the our build checks to fail.
2022-04-04 08:12:44 -07:00
kmoe
5907a86301
command/format: Correctly quote diff object keys (#30766)
When rendering a diff, we should quote object attribute names if the
string representation is not a valid identifier. While this is not
strictly necessary, it makes the diff output more closely resemble the
configuration language, which is less confusing.

This commit applies to both top-level schema attributes and any object
value attributes. We use a simplistic "%q" Go format string to quote the
strings, which is not strictly identical to HCL's quoting requirements,
but is the pattern used elsewhere in HCL and Terraform.

Co-Authored-By: Katy Moe <katy@katy.moe>

Co-authored-by: Alisdair McDiarmid <alisdair@users.noreply.github.com>
2022-04-01 10:09:28 +01:00
James Bardin
0bd59238d6
Merge pull request #30765 from hashicorp/jbardin/contributing-attributes
improve the contributing attributes filter
2022-03-31 16:56:09 -04:00
UKEME BASSEY
1c9929a378
Merge pull request #30773 from hashicorp/uk1288/fix-for-cloud-integration-panic
command/meta_backend: fix for cloud integration panic
2022-03-31 13:53:00 -04:00
gabriel376
f5a8608989
cli: Make state commands check required version (#30511) 2022-03-31 13:42:42 -04:00
uk1288
9093b487fd fix for cloud integration panic 2022-03-30 17:50:08 -04:00
James Bardin
09dde8d5ed improve the contributing attributes filter
The initial rough implementation contained a bug where it would
incorrectly return a NilVal in some cases.

Improve the heuristics here to insert null values more precisely when
parent objects change to or from null. We also check for dynamic types
changing, in which case the entire object must be taken when we can't
match the individual attribute values.
2022-03-30 10:04:48 -04:00
Matthias Baur
971c899c12
Clarify multiple times usage (#30724)
* Clarify multiple times usage


Co-authored-by: kmoe <5575356+kmoe@users.noreply.github.com>
2022-03-30 09:52:49 +01:00
Markus Bösling
275ff5b223
double word removed (#30679) 2022-03-25 14:31:52 +00:00
Alisdair McDiarmid
b386f76b65
Merge pull request #30685 from hashicorp/alisdair/fix-30641
cli: Fix missing identifying attributes in diff
2022-03-21 09:11:45 -04:00
James Bardin
8c5e11d41a add relevant_attributes to the json plan format
Add the resource instances and individual attributes which may have
contributed to the planned changes to the json format of the plan. We
use the existing path encoding for individual attributes, which is
already used in the replace_paths change field.
2022-03-17 09:35:36 -04:00
James Bardin
f0cd8be66f add whole resource references 2022-03-17 09:35:36 -04:00