backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
package views
import (
"bytes"
"fmt"
"sort"
"strings"
2021-05-17 21:00:50 +02:00
"github.com/hashicorp/terraform/internal/addrs"
2021-05-17 21:07:38 +02:00
"github.com/hashicorp/terraform/internal/command/arguments"
"github.com/hashicorp/terraform/internal/command/format"
2021-05-17 21:33:17 +02:00
"github.com/hashicorp/terraform/internal/plans"
2021-05-17 21:43:35 +02:00
"github.com/hashicorp/terraform/internal/states"
2021-05-17 21:46:19 +02:00
"github.com/hashicorp/terraform/internal/terraform"
2021-05-17 19:11:06 +02:00
"github.com/hashicorp/terraform/internal/tfdiags"
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
)
2021-02-22 17:55:00 +01:00
// The Plan view is used for the plan command.
type Plan interface {
Operation ( ) Operation
Hooks ( ) [ ] terraform . Hook
Diagnostics ( diags tfdiags . Diagnostics )
2021-02-26 22:31:12 +01:00
HelpPrompt ( )
2021-02-22 17:55:00 +01:00
}
// NewPlan returns an initialized Plan implementation for the given ViewType.
2021-05-10 19:20:43 +02:00
func NewPlan ( vt arguments . ViewType , view * View ) Plan {
2021-02-22 17:55:00 +01:00
switch vt {
2021-02-23 16:16:09 +01:00
case arguments . ViewJSON :
return & PlanJSON {
view : NewJSONView ( view ) ,
}
2021-02-22 17:55:00 +01:00
case arguments . ViewHuman :
return & PlanHuman {
2021-02-26 22:31:12 +01:00
view : view ,
2021-05-10 19:20:43 +02:00
inAutomation : view . RunningInAutomation ( ) ,
2021-02-22 17:55:00 +01:00
}
default :
panic ( fmt . Sprintf ( "unknown view type %v" , vt ) )
}
}
// The PlanHuman implementation renders human-readable text logs, suitable for
// a scrolling terminal.
type PlanHuman struct {
2021-02-26 22:31:12 +01:00
view * View
2021-02-22 17:55:00 +01:00
inAutomation bool
}
var _ Plan = ( * PlanHuman ) ( nil )
func ( v * PlanHuman ) Operation ( ) Operation {
2021-02-26 22:31:12 +01:00
return NewOperation ( arguments . ViewHuman , v . inAutomation , v . view )
2021-02-22 17:55:00 +01:00
}
func ( v * PlanHuman ) Hooks ( ) [ ] terraform . Hook {
return [ ] terraform . Hook {
2021-02-26 22:31:12 +01:00
NewUiHook ( v . view ) ,
2021-02-22 17:55:00 +01:00
}
}
2021-02-26 22:31:12 +01:00
func ( v * PlanHuman ) Diagnostics ( diags tfdiags . Diagnostics ) {
v . view . Diagnostics ( diags )
}
func ( v * PlanHuman ) HelpPrompt ( ) {
v . view . HelpPrompt ( "plan" )
}
2021-02-23 16:16:09 +01:00
// The PlanJSON implementation renders streaming JSON logs, suitable for
// integrating with other software.
type PlanJSON struct {
view * JSONView
}
var _ Plan = ( * PlanJSON ) ( nil )
func ( v * PlanJSON ) Operation ( ) Operation {
return & OperationJSON { view : v . view }
}
func ( v * PlanJSON ) Hooks ( ) [ ] terraform . Hook {
return [ ] terraform . Hook {
newJSONHook ( v . view ) ,
}
}
func ( v * PlanJSON ) Diagnostics ( diags tfdiags . Diagnostics ) {
v . view . Diagnostics ( diags )
}
func ( v * PlanJSON ) HelpPrompt ( ) {
}
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
// The plan renderer is used by the Operation view (for plan and apply
// commands) and the Show view (for the show command).
2021-05-06 00:24:58 +02:00
func renderPlan ( plan * plans . Plan , schemas * terraform . Schemas , view * View ) {
2021-05-06 02:35:25 +02:00
haveRefreshChanges := renderChangesDetectedByRefresh ( plan . PrevRunState , plan . PriorState , schemas , view )
if haveRefreshChanges {
switch plan . UIMode {
case plans . RefreshOnlyMode :
view . streams . Println ( format . WordWrap (
"\nThis is a refresh-only plan, so Terraform will not take any actions to undo these. If you were expecting these changes then you can apply this plan to record the updated values in the Terraform state without changing any remote objects." ,
view . outputColumns ( ) ,
) )
default :
view . streams . Println ( format . WordWrap (
"\nUnless you have made equivalent changes to your configuration, or ignored the relevant attributes using ignore_changes, the following plan may include actions to undo or respond to these changes." ,
view . outputColumns ( ) ,
) )
}
}
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
counts := map [ plans . Action ] int { }
var rChanges [ ] * plans . ResourceInstanceChangeSrc
for _ , change := range plan . Changes . Resources {
2021-09-03 18:10:16 +02:00
if change . Action == plans . NoOp && ! change . Moved ( ) {
2021-05-07 00:22:48 +02:00
continue // We don't show anything for no-op changes
}
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
if change . Action == plans . Delete && change . Addr . Resource . Resource . Mode == addrs . DataResourceMode {
// Avoid rendering data sources on deletion
continue
}
rChanges = append ( rChanges , change )
2021-09-03 18:10:16 +02:00
// Don't count move-only changes
if change . Action != plans . NoOp {
counts [ change . Action ] ++
}
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
}
2021-05-07 00:22:48 +02:00
var changedRootModuleOutputs [ ] * plans . OutputChangeSrc
for _ , output := range plan . Changes . Outputs {
if ! output . Addr . Module . IsRoot ( ) {
continue
}
if output . ChangeSrc . Action == plans . NoOp {
continue
}
changedRootModuleOutputs = append ( changedRootModuleOutputs , output )
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
}
2021-09-03 18:10:16 +02:00
if len ( rChanges ) == 0 && len ( changedRootModuleOutputs ) == 0 {
2021-05-07 00:22:48 +02:00
// If we didn't find any changes to report at all then this is a
// "No changes" plan. How we'll present this depends on whether
// the plan is "applyable" and, if so, whether it had refresh changes
// that we already would've presented above.
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
2021-05-07 00:22:48 +02:00
switch plan . UIMode {
case plans . RefreshOnlyMode :
if haveRefreshChanges {
// We already generated a sufficient prompt about what will
// happen if applying this change above, so we don't need to
// say anything more.
return
}
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
2021-05-07 00:22:48 +02:00
view . streams . Print (
view . colorize . Color ( "\n[reset][bold][green]No changes.[reset][bold] Your infrastructure still matches the configuration.[reset]\n\n" ) ,
)
view . streams . Println ( format . WordWrap (
"Terraform has checked that the real remote objects still match the result of your most recent changes, and found no differences." ,
view . outputColumns ( ) ,
) )
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
2021-05-07 00:22:48 +02:00
case plans . DestroyMode :
if haveRefreshChanges {
view . streams . Print ( format . HorizontalRule ( view . colorize , view . outputColumns ( ) ) )
view . streams . Println ( "" )
}
view . streams . Print (
view . colorize . Color ( "\n[reset][bold][green]No changes.[reset][bold] No objects need to be destroyed.[reset]\n\n" ) ,
)
view . streams . Println ( format . WordWrap (
"Either you have not created any objects yet or the existing objects were already deleted outside of Terraform." ,
view . outputColumns ( ) ,
) )
default :
if haveRefreshChanges {
view . streams . Print ( format . HorizontalRule ( view . colorize , view . outputColumns ( ) ) )
view . streams . Println ( "" )
}
view . streams . Print (
view . colorize . Color ( "\n[reset][bold][green]No changes.[reset][bold] Your infrastructure matches the configuration.[reset]\n\n" ) ,
)
if haveRefreshChanges && ! plan . CanApply ( ) {
if plan . CanApply ( ) {
// In this case, applying this plan will not change any
// remote objects but _will_ update the state to match what
// we detected during refresh, so we'll reassure the user
// about that.
view . streams . Println ( format . WordWrap (
"Your configuration already matches the changes detected above, so applying this plan will only update the state to include the changes detected above and won't change any real infrastructure." ,
view . outputColumns ( ) ,
) )
} else {
// In this case we detected changes during refresh but this isn't
// a planning mode where we consider those to be applyable. The
// user must re-run in refresh-only mode in order to update the
// state to match the upstream changes.
suggestion := "."
if ! view . runningInAutomation {
// The normal message includes a specific command line to run.
suggestion = ":\n terraform apply -refresh-only"
}
view . streams . Println ( format . WordWrap (
"Your configuration already matches the changes detected above. If you'd like to update the Terraform state to match, create and apply a refresh-only plan" + suggestion ,
view . outputColumns ( ) ,
) )
}
return
}
// If we get down here then we're just in the simple situation where
// the plan isn't applyable at all.
view . streams . Println ( format . WordWrap (
"Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed." ,
view . outputColumns ( ) ,
) )
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
}
2021-05-07 00:22:48 +02:00
return
}
if haveRefreshChanges {
view . streams . Print ( format . HorizontalRule ( view . colorize , view . outputColumns ( ) ) )
view . streams . Println ( "" )
}
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
2021-09-03 18:10:16 +02:00
if len ( counts ) > 0 {
2021-05-07 00:22:48 +02:00
headerBuf := & bytes . Buffer { }
fmt . Fprintf ( headerBuf , "\n%s\n" , strings . TrimSpace ( format . WordWrap ( planHeaderIntro , view . outputColumns ( ) ) ) )
if counts [ plans . Create ] > 0 {
fmt . Fprintf ( headerBuf , "%s create\n" , format . DiffActionSymbol ( plans . Create ) )
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
}
2021-05-07 00:22:48 +02:00
if counts [ plans . Update ] > 0 {
fmt . Fprintf ( headerBuf , "%s update in-place\n" , format . DiffActionSymbol ( plans . Update ) )
}
if counts [ plans . Delete ] > 0 {
fmt . Fprintf ( headerBuf , "%s destroy\n" , format . DiffActionSymbol ( plans . Delete ) )
}
if counts [ plans . DeleteThenCreate ] > 0 {
fmt . Fprintf ( headerBuf , "%s destroy and then create replacement\n" , format . DiffActionSymbol ( plans . DeleteThenCreate ) )
}
if counts [ plans . CreateThenDelete ] > 0 {
fmt . Fprintf ( headerBuf , "%s create replacement and then destroy\n" , format . DiffActionSymbol ( plans . CreateThenDelete ) )
}
if counts [ plans . Read ] > 0 {
fmt . Fprintf ( headerBuf , "%s read (data resources)\n" , format . DiffActionSymbol ( plans . Read ) )
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
}
2021-09-03 18:10:16 +02:00
view . streams . Print ( view . colorize . Color ( headerBuf . String ( ) ) )
}
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
2021-09-03 18:10:16 +02:00
if len ( rChanges ) > 0 {
view . streams . Printf ( "\nTerraform will perform the following actions:\n\n" )
2021-05-07 00:22:48 +02:00
// Note: we're modifying the backing slice of this plan object in-place
// here. The ordering of resource changes in a plan is not significant,
// but we can only do this safely here because we can assume that nobody
// is concurrently modifying our changes while we're trying to print it.
sort . Slice ( rChanges , func ( i , j int ) bool {
iA := rChanges [ i ] . Addr
jA := rChanges [ j ] . Addr
if iA . String ( ) == jA . String ( ) {
return rChanges [ i ] . DeposedKey < rChanges [ j ] . DeposedKey
}
return iA . Less ( jA )
} )
for _ , rcs := range rChanges {
2021-09-03 18:10:16 +02:00
if rcs . Action == plans . NoOp && ! rcs . Moved ( ) {
2021-05-07 00:22:48 +02:00
continue
}
providerSchema := schemas . ProviderSchema ( rcs . ProviderAddr . Provider )
if providerSchema == nil {
// Should never happen
view . streams . Printf ( "(schema missing for %s)\n\n" , rcs . ProviderAddr )
continue
}
rSchema , _ := providerSchema . SchemaForResourceAddr ( rcs . Addr . Resource . Resource )
if rSchema == nil {
// Should never happen
view . streams . Printf ( "(schema missing for %s)\n\n" , rcs . Addr )
continue
}
view . streams . Println ( format . ResourceChange (
rcs ,
rSchema ,
view . colorize ,
) )
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
}
2021-05-07 00:22:48 +02:00
// stats is similar to counts above, but:
// - it considers only resource changes
// - it simplifies "replace" into both a create and a delete
stats := map [ plans . Action ] int { }
for _ , change := range rChanges {
switch change . Action {
case plans . CreateThenDelete , plans . DeleteThenCreate :
stats [ plans . Create ] ++
stats [ plans . Delete ] ++
default :
stats [ change . Action ] ++
}
}
view . streams . Printf (
view . colorize . Color ( "[reset][bold]Plan:[reset] %d to add, %d to change, %d to destroy.\n" ) ,
stats [ plans . Create ] , stats [ plans . Update ] , stats [ plans . Delete ] ,
)
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
}
// If there is at least one planned change to the root module outputs
// then we'll render a summary of those too.
if len ( changedRootModuleOutputs ) > 0 {
view . streams . Println (
view . colorize . Color ( "[reset]\n[bold]Changes to Outputs:[reset]" ) +
format . OutputChanges ( changedRootModuleOutputs , view . colorize ) ,
)
2021-05-07 00:22:48 +02:00
if len ( counts ) == 0 {
// If we have output changes but not resource changes then we
// won't have output any indication about the changes at all yet,
// so we need some extra context about what it would mean to
// apply a change that _only_ includes output changes.
view . streams . Println ( format . WordWrap (
"\nYou can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure." ,
view . outputColumns ( ) ,
) )
}
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
}
}
2021-05-06 02:35:25 +02:00
// renderChangesDetectedByRefresh is a part of renderPlan that generates
// the note about changes detected by refresh (sometimes considered as "drift").
//
// It will only generate output if there's at least one difference detected.
// Otherwise, it will produce nothing at all. To help the caller recognize
// those two situations incase subsequent output must accommodate it,
// renderChangesDetectedByRefresh returns true if it produced at least one
// line of output, and guarantees to always produce whole lines terminated
// by newline characters.
func renderChangesDetectedByRefresh ( before , after * states . State , schemas * terraform . Schemas , view * View ) bool {
2021-05-25 14:32:44 +02:00
// ManagedResourceEqual checks that the state is exactly equal for all
2021-05-24 23:02:15 +02:00
// managed resources; but semantically equivalent states, or changes to
// deposed instances may not actually represent changes we need to present
// to the user, so for now this only serves as a short-circuit to skip
// attempting to render the diffs below.
2021-05-06 02:35:25 +02:00
if after . ManagedResourcesEqual ( before ) {
return false
}
2021-05-24 23:02:15 +02:00
var diffs [ ] string
2021-05-06 02:35:25 +02:00
for _ , bms := range before . Modules {
for _ , brs := range bms . Resources {
if brs . Addr . Resource . Mode != addrs . ManagedResourceMode {
continue // only managed resources can "drift"
}
addr := brs . Addr
prs := after . Resource ( brs . Addr )
provider := brs . ProviderConfig . Provider
providerSchema := schemas . ProviderSchema ( provider )
if providerSchema == nil {
// Should never happen
view . streams . Printf ( "(schema missing for %s)\n" , provider )
continue
}
rSchema , _ := providerSchema . SchemaForResourceAddr ( addr . Resource )
if rSchema == nil {
// Should never happen
view . streams . Printf ( "(schema missing for %s)\n" , addr )
continue
}
for key , bis := range brs . Instances {
2021-05-24 21:40:10 +02:00
if bis . Current == nil {
// No current instance to render here
continue
}
2021-05-06 02:35:25 +02:00
var pis * states . ResourceInstance
if prs != nil {
pis = prs . Instance ( key )
}
diff := format . ResourceInstanceDrift (
addr . Instance ( key ) ,
bis , pis ,
rSchema ,
view . colorize ,
)
if diff != "" {
2021-05-24 23:02:15 +02:00
diffs = append ( diffs , diff )
2021-05-06 02:35:25 +02:00
}
}
}
}
2021-05-24 23:02:15 +02:00
// If we only have changes regarding deposed instances, or the diff
// renderer is suppressing irrelevant changes from the legacy SDK, there
// may not have been anything to display to the user.
if len ( diffs ) > 0 {
view . streams . Print (
view . colorize . Color ( "[reset]\n[bold][cyan]Note:[reset][bold] Objects have changed outside of Terraform[reset]\n\n" ) ,
)
view . streams . Print ( format . WordWrap (
"Terraform detected the following changes made outside of Terraform since the last \"terraform apply\":\n\n" ,
view . outputColumns ( ) ,
) )
for _ , diff := range diffs {
view . streams . Print ( diff )
}
return true
}
return false
2021-05-06 02:35:25 +02:00
}
backend/local: Replace CLI with view instance
This commit extracts the remaining UI logic from the local backend,
and removes access to the direct CLI output. This is replaced with an
instance of a `views.Operation` interface, which codifies the current
requirements for the local backend to interact with the user.
The exception to this at present is interactivity: approving a plan
still depends on the `UIIn` field for the backend. This is out of scope
for this commit and can be revisited separately, at which time the
`UIOut` field can also be removed.
Changes in support of this:
- Some instances of direct error output have been replaced with
diagnostics, most notably in the emergency state backup handler. This
requires reformatting the error messages to allow the diagnostic
renderer to line-wrap them;
- The "in-automation" logic has moved out of the backend and into the
view implementation;
- The plan, apply, refresh, and import commands instantiate a view and
set it on the `backend.Operation` struct, as these are the only code
paths which call the `local.Operation()` method that requires it;
- The show command requires the plan rendering code which is now in the
views package, so there is a stub implementation of a `views.Show`
interface there.
Other refactoring work in support of migrating these commands to the
common views code structure will come in follow-up PRs, at which point
we will be able to remove the UI instances from the unit tests for those
commands.
2021-02-17 19:01:30 +01:00
const planHeaderIntro = `
Terraform used the selected providers to generate the following execution plan . Resource actions are indicated with the following symbols :
`