Commit graph

13 commits

Author SHA1 Message Date
Sarah French
7a27366b39
PSS: Add ability to create hashes of the state_store block and of its nested provider block (#37278)
* Add the ability to make a hash of state store config

* Add test demonstrating that the provider block doesn't impact the hash of a state_store block

* Make sure test asserts what would happen if the schema DID include the provider block

* Update the Hash method to return diagnostics, ignore nested provider blocks, and validate incoming schema and config

* Update tests to use more representative config, fix code under test as a result

* Update Hash method to return hashes for both the state_store and provider blocks

* Add test cases to cover how required fields are tolerated when making the hash

This is because ENVs may supply those values.

* Fix inaccurate comments

* Add test to show that hashes are consistent and exclude the provider block

* Update backend state file to contain hash of provider block's config

* Fix test to expect a hash for the provider config block.

* Fix bug in DeepCopy method, update test to have better error messages when diffs are detected

* Update test to explicitly check hash values

* Try make test intention clearer

* Improve user feedback when state store schema contains the protected word "provider"

* Update tests

* Update test to test the Hash method in a more true-to-life way

Copy of 04a1201878cd1f6f117c43c43c1ee9d0fc17cec1 by Radek Simko

* Update test to use new approach

* Fix `TestInit_stateStoreBlockIsExperimental` test failure
2025-07-14 13:52:27 +01:00
Radek Simko
5437cd7c8a
Prepare main branch for 1.14 development (cleanup) (#37312)
* Prepare main branch for 1.14 development (cleanup)

* fix version-dependent tests

* update PR template
2025-07-09 17:42:04 +01:00
Sarah French
d02a1ed1b7
PSS: Make the state storage provider's config stateful (backend state, planfiles) (#37286) 2025-07-08 15:32:27 +01:00
Sarah French
2349481b23
Update data stored in plan files to enable using PSS with saved plans (#37246)
* Implement `ForPlan` method on `StateStoreConfigState`, add `Planner` interface

* Rename `ForPlan` method to `Plan`

* Allow plan files to contain information about state stores

* Add code needed for representing a state store in the Plan struct, which is used when handling plan files

* Add ability to read/write either a backend or state store's data in a plan file. Add some test coverage.

* Update plan's `ProviderAddrs` method to include the provider used for PSS, if present

* Split interfaces

* Apply feedback from code review

* Refactor `SetVersion` to use appropriate constructor

* Split `ProviderAddrs` method test into two

* Fix method call after rename

* Fix test

* Remove change to `(p *Plan) ProviderAddrs()`

We may re-add this when we implement PSS for use during apply commands with plan files

* Remove changes to test, now that the plan doesn't report the provider used for PSS anymore.
2025-06-30 11:39:15 +01:00
Sarah French
d3b02a8469
Refactor code to use backend config state's SetConfig method, protect against nil receiver (#37254)
* Add protection against nil receivers to SetConfig

* Refactor code to use SetConfig method instead of setting ConfigRaw directly
2025-06-27 19:16:44 +01:00
Sarah French
a91d3aafa8
Fix bug in DeepCopy of backend state data that causes loss of StateStore data (#37276) 2025-06-26 21:38:45 +01:00
Sarah French
2559f0a3db
Update backend state file so it can describe PSS state (#37179)
* Split code for backend state file vs backend state

* Rename BackendState to BackendConfigState

* Spelling error

* Add `StateStorageConfigState` struct as new implementation of new `ConfigState[T any]` interface.

* Split tests for backend state file vs backend config state structs

* Rename StateStorageConfigState to StateStoreConfigState

* Clarify test name, add comments

* Add tests for StateStoreConfigState methods

* Add test showing state_store in JSON is parsed correctly

* Add detection of malformed backend state files that contain both backend and state_store fields

* Add validation that stops a backend state file being written if it will contain state for both backend and state_store blocks

* Rename `state_storage` to `state_store`

* Rename `state_storage` to `state_store` in filenames

* Move`ConfigState` to its own file

* Fix test name, remove whitespace

* Update `StateStoreConfigState` comment using review suggestion

* Update error message to no longer allude to the environment TF is being run in

* Update the state_store state to use `version.Version` and an adapted version of `tfaddr.Provider` for marshalling version and source data

* Update test helper so it doesn't accidentally supply validation in tests

* Add protection against saving an empty backend state file

* Remove direct testing of (s *Source) MarshalText() and UnmarshalText() methods

* Add Validate method to StateStoreConfigState, use in backend state encoding logic

* Refactor to use new features in registry dependency
2025-06-11 14:10:26 +00:00
Martin Atkins
06f51dbe04 command/workdir: Model the "backend state" file format
Historically (before there were "backends") Terraform had a single state
file format used both for real state snapshots and for tracking where
remote state was stored.

Terraform v0.12 caused these two to be split because we adopted state
snapshot version 4 for real snapshots but retained a subset of version 3
for tracking the remote backend configuration in the local working
directory.

Unfortunately we previously kept that working by retaining a snapshot of
the entire Terraform v0.11 "terraform" package as legacy/terraform, which
happened to still be around because we also needed to retain a copy of
the entire legacy SDK to keep the remote state backends working.

This now hoists just the tiny slice of legacy terraform package
functionality needed to implement the "backend state" file format into
package workdir. This package is a good home for it because it's part of
the working directory state. Ideally it would be accessed through methods
of the workdir.Dir type, but that's too disruptive a refactor to combine
into this and so that'll need to wait for another day; for now we'll keep
the existing callers doing their access through our "clistate" package
that is itself a forked snapshot of what statemgr.Filesystem used to be
in Terraform v0.11.

This removes all but one of the uses of "legacy/terraform" aside from calls
in the other packages under "legacy". We'll clean up the last one in a
later commit, because it's not related to the backend state file format.
2024-03-08 17:21:53 -08:00
hashicorp-copywrite[bot]
53c34ff49c
Update copyright file headers to BUSL-1.1 2023-08-10 23:43:27 +01:00
hashicorp-copywrite[bot]
325d18262e [COMPLIANCE] Add Copyright and License Headers 2023-05-02 15:33:06 +00:00
Martin Atkins
783a07d9e8 build: Use Go 1.19
Go 1.19's "fmt" has some awareness of the new doc comment formatting
conventions and adjusts the presentation of the source comments to make
it clearer how godoc would interpret them. Therefore this commit includes
various updates made by "go fmt" to acheve that.

In line with our usual convention that we make stylistic/grammar/spelling
tweaks typically only when we're "in the area" changing something else
anyway, I also took this opportunity to review most of the comments that
this updated to see if there were any other opportunities to improve them.
2022-08-22 10:59:12 -07: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
Martin Atkins
65e0c448a0 workdir: Start of a new package for working directory state management
Thus far our various interactions with the bits of state we keep
associated with a working directory have all been implemented directly
inside the "command" package -- often in the huge command.Meta type -- and
not managed collectively via a single component.

There's too many little codepaths reading and writing from the working
directory and data directory to refactor it all in one step, but this is
an attempt at a first step towards a future where everything that reads
and writes from the current working directory would do so via an object
that encapsulates the implementation details and offers a high-level API
to read and write all of these session-persistent settings.

The design here continues our gradual path towards using a dependency
injection style where "package main" is solely responsible for directly
interacting with the OS command line, the OS environment, the OS working
directory, the stdio streams, and the CLI configuration, and then
communicating the resulting information to the rest of Terraform by wiring
together objects. It seems likely that eventually we'll have enough wiring
code in package main to justify a more explicit organization of that code,
but for this commit the new "workdir.Dir" object is just wired directly in
place of its predecessors, without any significant change of code
organization at that top layer.

This first commit focuses on the main files and directories we use to
find provider plugins, because a subsequent commit will lightly reorganize
the separation of concerns for plugin launching with a similar goal of
collecting all of the relevant logic together into one spot.
2021-09-10 14:56:49 -07:00