-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Update data stored in plan files to enable using PSS with saved plans #37246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e5701a9
Implement `ForPlan` method on `StateStoreConfigState`, add `Planner` …
SarahFrench fd7182a
Rename `ForPlan` method to `Plan`
SarahFrench ee6e9da
Allow plan files to contain information about state stores
SarahFrench 1bb87e8
Add code needed for representing a state store in the Plan struct, wh…
SarahFrench 9e076d8
Add ability to read/write either a backend or state store's data in a…
SarahFrench 44ce79b
Update plan's `ProviderAddrs` method to include the provider used for…
SarahFrench a2acc33
Split interfaces
SarahFrench 5255d9d
Merge branch 'main' into pss/store-pss-in-planfile
SarahFrench 1e17248
Apply feedback from code review
SarahFrench 627ba01
Refactor `SetVersion` to use appropriate constructor
SarahFrench 3169ed9
Split `ProviderAddrs` method test into two
SarahFrench 93fce21
Fix method call after rename
SarahFrench ee328d0
Fix test
SarahFrench b25ab7a
Remove change to `(p *Plan) ProviderAddrs()`
SarahFrench f8f52cf
Remove changes to test, now that the plan doesn't report the provider…
SarahFrench File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -179,9 +179,17 @@ func readTfplan(r io.Reader) (*plans.Plan, error) { | |
) | ||
} | ||
|
||
if rawBackend := rawPlan.Backend; rawBackend == nil { | ||
return nil, fmt.Errorf("plan file has no backend settings; backend settings are required") | ||
} else { | ||
switch { | ||
case rawPlan.Backend == nil && rawPlan.StateStore == nil: | ||
// Similar validation in writeTfPlan should prevent this occurring | ||
return nil, | ||
fmt.Errorf("plan file has neither backend nor state_store settings; one of these settings is required. This is a bug in Terraform and should be reported.") | ||
case rawPlan.Backend != nil && rawPlan.StateStore != nil: | ||
// Similar validation in writeTfPlan should prevent this occurring | ||
return nil, | ||
fmt.Errorf("plan file contains both backend and state_store settings when only one of these settings should be set. This is a bug in Terraform and should be reported.") | ||
case rawPlan.Backend != nil: | ||
rawBackend := rawPlan.Backend | ||
config, err := valueFromTfplan(rawBackend.Config) | ||
if err != nil { | ||
return nil, fmt.Errorf("plan file has invalid backend configuration: %s", err) | ||
|
@@ -191,6 +199,28 @@ func readTfplan(r io.Reader) (*plans.Plan, error) { | |
Config: config, | ||
Workspace: rawBackend.Workspace, | ||
} | ||
case rawPlan.StateStore != nil: | ||
rawStateStore := rawPlan.StateStore | ||
config, err := valueFromTfplan(rawStateStore.Config) | ||
if err != nil { | ||
return nil, fmt.Errorf("plan file has invalid state_store configuration: %s", err) | ||
} | ||
provider := &plans.Provider{} | ||
err = provider.SetSource(rawStateStore.Provider.Source) | ||
if err != nil { | ||
return nil, fmt.Errorf("plan file has invalid state_store provider source: %s", err) | ||
} | ||
err = provider.SetVersion(rawStateStore.Provider.Version) | ||
if err != nil { | ||
return nil, fmt.Errorf("plan file has invalid state_store provider version: %s", err) | ||
} | ||
|
||
plan.StateStore = plans.StateStore{ | ||
Type: rawStateStore.Type, | ||
Provider: provider, | ||
Config: config, | ||
Workspace: rawStateStore.Workspace, | ||
} | ||
} | ||
|
||
if plan.Timestamp, err = time.Parse(time.RFC3339, rawPlan.Timestamp); err != nil { | ||
|
@@ -636,17 +666,35 @@ func writeTfplan(plan *plans.Plan, w io.Writer) error { | |
) | ||
} | ||
|
||
if plan.Backend.Type == "" || plan.Backend.Config == nil { | ||
// Store details about accessing state | ||
backendInUse := plan.Backend.Type != "" && plan.Backend.Config != nil | ||
stateStoreInUse := plan.StateStore.Type != "" && plan.StateStore.Config != nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the variable extraction here along with great names. |
||
switch { | ||
case !backendInUse && !stateStoreInUse: | ||
// This suggests a bug in the code that created the plan, since it | ||
// ought to always have a backend populated, even if it's the default | ||
// ought to always have either a backend or state_store populated, even if it's the default | ||
// "local" backend with a local state file. | ||
return fmt.Errorf("plan does not have a backend configuration") | ||
} | ||
|
||
rawPlan.Backend = &planproto.Backend{ | ||
Type: plan.Backend.Type, | ||
Config: valueToTfplan(plan.Backend.Config), | ||
Workspace: plan.Backend.Workspace, | ||
return fmt.Errorf("plan does not have a backend or state_store configuration") | ||
case backendInUse && stateStoreInUse: | ||
// This suggests a bug in the code that created the plan, since it | ||
// should never have both a backend and state_store populated. | ||
return fmt.Errorf("plan contains both backend and state_store configurations, only one is expected") | ||
case backendInUse: | ||
rawPlan.Backend = &planproto.Backend{ | ||
Type: plan.Backend.Type, | ||
Config: valueToTfplan(plan.Backend.Config), | ||
Workspace: plan.Backend.Workspace, | ||
} | ||
case stateStoreInUse: | ||
rawPlan.StateStore = &planproto.StateStore{ | ||
Type: plan.StateStore.Type, | ||
Provider: &planproto.Provider{ | ||
Version: plan.StateStore.Provider.Version.String(), | ||
Source: plan.StateStore.Provider.Source.String(), | ||
}, | ||
Config: valueToTfplan(plan.StateStore.Config), | ||
Workspace: plan.StateStore.Workspace, | ||
} | ||
} | ||
|
||
rawPlan.Timestamp = plan.Timestamp.Format(time.RFC3339) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.