Skip to content

Conversation

SarahFrench
Copy link
Member

@SarahFrench SarahFrench commented Sep 8, 2025

Context / How code before this PR worked

Terraform command implementations access operations backends through the (Meta).Backend method. This operations backend may be used for accessing state and/or for running operations.

When using the (Meta).Backend method, calling code may include parsed backend configuration in the BackendOpts argument passed into the Backend method:

// Get the backend config from the root module
backendConfig, configDiags := c.loadBackendConfig(".")
// handle configDiags errors etc

// Pass the config into the Backend method
be, beDiags = c.Backend(&BackendOpts{
	BackendConfig: backendConfig,
	ViewType:      viewType,
})

Or, the calling code could pass a nil value into the Backend method and instead rely on downstream logic to parse any backend blocks in the configuration (this happens if the BackendOpts.BackendConfig value is missing).

// Load the backend
b, backendDiags := c.Backend(nil)

Changes in this PR

This PR adds two new methods:

  • (m *Meta) getStateStoreProviderFactory
  • (m *Meta) prepareBackend

*(m Meta) prepareBackend - see commit 2ecbed9. This method makes it easier to access an instance of an operations backend. This method parses any backend or state_store configuration in the root module, provides that input to the (Meta).Backend method, and returns the operations backend from there.

*(m Meta) getStateStoreProviderFactory - see commit 60d5333. This method makes it easy to quickly pull the factory needed for a state store from the available providers, or return the correct errors if data or a provider is missing.

Overall, this PR makes Terraform commands compatible with state stored via state_store. The exceptions are commands like terraform cloud * which expect a specific type of state storage to be present in the configuration when the user executes the command.

Target Release

N/A

Rollback Plan

  • If a change needs to be reverted, we will roll out an update to the code within 7 days.

Changes to Security Controls

Are there any changes to security controls (access controls, encryption, logging) in this pull request? If so, explain.

CHANGELOG entry

  • This change is user-facing and I added a changelog entry.
  • This change is not user-facing.

@SarahFrench SarahFrench added the no-changelog-needed Add this to your PR if the change does not require a changelog entry label Sep 8, 2025
Comment on lines 284 to 294
func (c *ShowCommand) getDataFromCloudPlan(plan *cloudplan.SavedPlanBookmark, redacted bool) (*cloudplan.RemotePlanJSON, error) {
mod, diags := c.Meta.loadSingleModule(".")
if diags.HasErrors() {
return nil, diags.Err()
}

// Set up the backend
b, backendDiags := c.Backend(nil)
if backendDiags.HasErrors() {
return nil, errUnusable(backendDiags.Err(), "cloud plan")
b, diags := c.Meta.prepareBackend(mod)
if diags.HasErrors() {
return nil, errUnusable(diags.Err(), "cloud plan")
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This currently doesn't have any test coverage

Comment on lines +1555 to +1567
// TODO(SarahFrench/radeksimko): Use locks from here in opts below
_, lDiags := m.lockedDependencies()
diags = diags.Append(lDiags)
if lDiags.HasErrors() {
return nil, diags
}

opts = &BackendOpts{
StateStoreConfig: root.StateStore,
ProviderFactory: factory,
// TODO(SarahFrench/radeksimko): update once other work is merged into main
// Locks: locks,
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a (blocked) PR in draft that adds the ability to pass locks into the methods used for initialising an operations backend that uses state_store. While that work is in progress there are some parts of this code that aren't possible yet but I think it's reasonable to get this completed version into main, to help make other subsequent PRs smaller.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-changelog-needed Add this to your PR if the change does not require a changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant