2017-01-19 05:49:42 +01:00
|
|
|
package format
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2019-01-30 19:08:59 +01:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
2018-09-24 18:30:39 +02:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2018-09-25 00:00:07 +02:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
2018-09-24 18:30:39 +02:00
|
|
|
"github.com/hashicorp/terraform/plans"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/states"
|
2018-09-25 00:00:07 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/colorstring"
|
2017-01-19 05:49:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// StateOpts are the options for formatting a state.
|
|
|
|
type StateOpts struct {
|
|
|
|
// State is the state to format. This is required.
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
State *states.State
|
2017-01-19 05:49:42 +01:00
|
|
|
|
2018-09-25 00:00:07 +02:00
|
|
|
// Schemas are used to decode attributes. This is required.
|
|
|
|
Schemas *terraform.Schemas
|
|
|
|
|
2017-01-19 05:49:42 +01:00
|
|
|
// Color is the colorizer. This is optional.
|
|
|
|
Color *colorstring.Colorize
|
|
|
|
}
|
|
|
|
|
|
|
|
// State takes a state and returns a string
|
|
|
|
func State(opts *StateOpts) string {
|
|
|
|
if opts.Color == nil {
|
|
|
|
panic("colorize not given")
|
|
|
|
}
|
|
|
|
|
2018-09-25 00:00:07 +02:00
|
|
|
if opts.Schemas == nil {
|
|
|
|
panic("schemas not given")
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:49:42 +01:00
|
|
|
s := opts.State
|
|
|
|
if len(s.Modules) == 0 {
|
|
|
|
return "The state file is empty. No resources are represented."
|
|
|
|
}
|
|
|
|
|
2018-10-19 15:38:11 +02:00
|
|
|
buf := bytes.NewBufferString("[reset]")
|
2018-09-24 18:30:39 +02:00
|
|
|
p := blockBodyDiffPrinter{
|
2018-10-19 15:38:11 +02:00
|
|
|
buf: buf,
|
2018-09-24 18:30:39 +02:00
|
|
|
color: opts.Color,
|
|
|
|
action: plans.NoOp,
|
|
|
|
}
|
2017-01-19 05:49:42 +01:00
|
|
|
|
|
|
|
// Format all the modules
|
|
|
|
for _, m := range s.Modules {
|
2018-09-25 00:00:07 +02:00
|
|
|
formatStateModule(p, m, opts.Schemas)
|
2017-01-19 05:49:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the outputs for the root module
|
|
|
|
m := s.RootModule()
|
2018-09-24 18:30:39 +02:00
|
|
|
|
|
|
|
if m.OutputValues != nil {
|
|
|
|
if len(m.OutputValues) > 0 {
|
2018-09-26 19:11:18 +02:00
|
|
|
p.buf.WriteString("Outputs:\n\n")
|
2018-09-24 18:30:39 +02:00
|
|
|
}
|
2017-01-19 05:49:42 +01:00
|
|
|
|
|
|
|
// Sort the outputs
|
2018-09-24 18:30:39 +02:00
|
|
|
ks := make([]string, 0, len(m.OutputValues))
|
|
|
|
for k := range m.OutputValues {
|
2017-01-19 05:49:42 +01:00
|
|
|
ks = append(ks, k)
|
|
|
|
}
|
|
|
|
sort.Strings(ks)
|
|
|
|
|
|
|
|
// Output each output k/v pair
|
|
|
|
for _, k := range ks {
|
2018-09-24 18:30:39 +02:00
|
|
|
v := m.OutputValues[k]
|
2018-09-25 00:00:07 +02:00
|
|
|
p.buf.WriteString(fmt.Sprintf("%s = ", k))
|
2020-10-28 20:46:09 +01:00
|
|
|
if v.Sensitive {
|
|
|
|
p.buf.WriteString("(sensitive value)")
|
|
|
|
} else {
|
|
|
|
p.writeValue(v.Value, plans.NoOp, 0)
|
|
|
|
}
|
2019-05-16 15:52:06 +02:00
|
|
|
p.buf.WriteString("\n")
|
2017-01-19 05:49:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 15:52:06 +02:00
|
|
|
trimmedOutput := strings.TrimSpace(p.buf.String())
|
|
|
|
trimmedOutput += "[reset]"
|
|
|
|
|
|
|
|
return opts.Color.Color(trimmedOutput)
|
2018-09-24 18:30:39 +02:00
|
|
|
|
2017-01-19 05:49:42 +01:00
|
|
|
}
|
|
|
|
|
2018-10-19 15:38:11 +02:00
|
|
|
func formatStateModule(p blockBodyDiffPrinter, m *states.Module, schemas *terraform.Schemas) {
|
2017-01-19 05:49:42 +01:00
|
|
|
// First get the names of all the resources so we can show them
|
|
|
|
// in alphabetical order.
|
|
|
|
names := make([]string, 0, len(m.Resources))
|
2018-09-24 18:30:39 +02:00
|
|
|
for name := range m.Resources {
|
2017-01-19 05:49:42 +01:00
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
sort.Strings(names)
|
|
|
|
|
|
|
|
// Go through each resource and begin building up the output.
|
2018-09-25 00:00:07 +02:00
|
|
|
for _, key := range names {
|
2018-10-19 15:38:11 +02:00
|
|
|
for k, v := range m.Resources[key].Instances {
|
2019-07-19 22:39:16 +02:00
|
|
|
// keep these in order to keep the current object first, and
|
|
|
|
// provide deterministic output for the deposed objects
|
|
|
|
type obj struct {
|
|
|
|
header string
|
|
|
|
instance *states.ResourceInstanceObjectSrc
|
|
|
|
}
|
|
|
|
instances := []obj{}
|
|
|
|
|
2018-09-25 00:00:07 +02:00
|
|
|
addr := m.Resources[key].Addr
|
2020-03-16 21:50:48 +01:00
|
|
|
resAddr := addr.Resource
|
2018-10-19 15:38:11 +02:00
|
|
|
|
|
|
|
taintStr := ""
|
2019-07-19 22:39:16 +02:00
|
|
|
if v.Current != nil && v.Current.Status == 'T' {
|
2019-05-16 15:52:06 +02:00
|
|
|
taintStr = " (tainted)"
|
2018-09-25 00:00:07 +02:00
|
|
|
}
|
2019-07-19 22:39:16 +02:00
|
|
|
|
|
|
|
instances = append(instances,
|
2020-03-16 21:50:48 +01:00
|
|
|
obj{fmt.Sprintf("# %s:%s\n", addr.Instance(k), taintStr), v.Current})
|
2019-07-19 22:39:16 +02:00
|
|
|
|
|
|
|
for dk, v := range v.Deposed {
|
|
|
|
instances = append(instances,
|
2020-03-16 21:50:48 +01:00
|
|
|
obj{fmt.Sprintf("# %s: (deposed object %s)\n", addr.Instance(k), dk), v})
|
2018-10-11 19:51:54 +02:00
|
|
|
}
|
2018-09-25 00:00:07 +02:00
|
|
|
|
2019-07-19 22:39:16 +02:00
|
|
|
// Sort the instances for consistent output.
|
|
|
|
// Starting the sort from the second index, so the current instance
|
|
|
|
// is always first.
|
|
|
|
sort.Slice(instances[1:], func(i, j int) bool {
|
|
|
|
return instances[i+1].header < instances[j+1].header
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, obj := range instances {
|
|
|
|
header := obj.header
|
|
|
|
instance := obj.instance
|
|
|
|
p.buf.WriteString(header)
|
|
|
|
if instance == nil {
|
|
|
|
// this shouldn't happen, but there's nothing to do here so
|
|
|
|
// don't panic below.
|
2018-10-11 19:51:54 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-19 22:39:16 +02:00
|
|
|
var schema *configschema.Block
|
2020-02-03 14:18:04 +01:00
|
|
|
|
2020-02-13 21:32:58 +01:00
|
|
|
provider := m.Resources[key].ProviderConfig.Provider
|
2019-07-19 22:39:16 +02:00
|
|
|
if _, exists := schemas.Providers[provider]; !exists {
|
|
|
|
// This should never happen in normal use because we should've
|
|
|
|
// loaded all of the schemas and checked things prior to this
|
|
|
|
// point. We can't return errors here, but since this is UI code
|
|
|
|
// we will try to do _something_ reasonable.
|
2020-04-02 18:58:44 +02:00
|
|
|
p.buf.WriteString(fmt.Sprintf("# missing schema for provider %q\n\n", provider.String()))
|
2018-10-11 19:51:54 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-16 21:50:48 +01:00
|
|
|
switch resAddr.Mode {
|
2019-07-19 22:39:16 +02:00
|
|
|
case addrs.ManagedResourceMode:
|
|
|
|
schema, _ = schemas.ResourceTypeConfig(
|
|
|
|
provider,
|
2020-03-16 21:50:48 +01:00
|
|
|
resAddr.Mode,
|
|
|
|
resAddr.Type,
|
2019-07-19 22:39:16 +02:00
|
|
|
)
|
|
|
|
if schema == nil {
|
|
|
|
p.buf.WriteString(fmt.Sprintf(
|
2020-03-16 21:50:48 +01:00
|
|
|
"# missing schema for provider %q resource type %s\n\n", provider, resAddr.Type))
|
2019-07-19 22:39:16 +02:00
|
|
|
continue
|
|
|
|
}
|
2018-09-25 00:00:07 +02:00
|
|
|
|
2019-07-19 22:39:16 +02:00
|
|
|
p.buf.WriteString(fmt.Sprintf(
|
|
|
|
"resource %q %q {",
|
2020-03-16 21:50:48 +01:00
|
|
|
resAddr.Type,
|
|
|
|
resAddr.Name,
|
2019-07-19 22:39:16 +02:00
|
|
|
))
|
|
|
|
case addrs.DataResourceMode:
|
|
|
|
schema, _ = schemas.ResourceTypeConfig(
|
|
|
|
provider,
|
2020-03-16 21:50:48 +01:00
|
|
|
resAddr.Mode,
|
|
|
|
resAddr.Type,
|
2019-07-19 22:39:16 +02:00
|
|
|
)
|
|
|
|
if schema == nil {
|
|
|
|
p.buf.WriteString(fmt.Sprintf(
|
2020-03-16 21:50:48 +01:00
|
|
|
"# missing schema for provider %q data source %s\n\n", provider, resAddr.Type))
|
2019-07-19 22:39:16 +02:00
|
|
|
continue
|
|
|
|
}
|
2018-10-19 15:38:11 +02:00
|
|
|
|
2019-07-19 22:39:16 +02:00
|
|
|
p.buf.WriteString(fmt.Sprintf(
|
|
|
|
"data %q %q {",
|
2020-03-16 21:50:48 +01:00
|
|
|
resAddr.Type,
|
|
|
|
resAddr.Name,
|
2019-07-19 22:39:16 +02:00
|
|
|
))
|
|
|
|
default:
|
|
|
|
// should never happen, since the above is exhaustive
|
2020-03-16 21:50:48 +01:00
|
|
|
p.buf.WriteString(resAddr.String())
|
2019-07-19 22:39:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
val, err := instance.Decode(schema.ImpliedType())
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
path := make(cty.Path, 0, 3)
|
command: Add experimental concise diff renderer
When rendering a diff between current state and projected state, we only
show resources and outputs which have changes. However, we show a full
structural diff for these values, which includes all attributes and
blocks for a changed resource or output. The result can be a very long
diff, which makes it difficult to verify what the changed fields are.
This commit adds an experimental concise diff renderer, which suppresses
most unchanged fields, only displaying the most relevant changes and
some identifying context. This means:
- Always show all identifying attributes, initially defined as `id`,
`name`, and `tags`, even if unchanged;
- Only show changed, added, or removed primitive values: `string`,
`number`, or `bool`;
- Only show added or removed elements in unordered collections and
structural types: `map`, `set`, and `object`;
- Show added or removed elements with any surrounding unchanged elements
for sequence types: `list` and `tuple`;
- Only show added or removed nested blocks, or blocks with changed
attributes.
If any attributes, collection elements, or blocks are hidden, a count
is kept and displayed at the end of the parent scope. This ensures that
it is clear that the diff is only displaying a subset of the resource.
The experiment is currently enabled by default, but can be disabled by
setting the TF_X_CONCISE_DIFF environment variable to 0.
2020-08-19 22:47:56 +02:00
|
|
|
result := p.writeBlockBodyDiff(schema, val.Value, val.Value, 2, path)
|
|
|
|
if result.bodyWritten {
|
2019-07-19 22:39:16 +02:00
|
|
|
p.buf.WriteString("\n")
|
|
|
|
}
|
2019-01-30 19:08:59 +01:00
|
|
|
|
2019-07-19 22:39:16 +02:00
|
|
|
p.buf.WriteString("}\n\n")
|
|
|
|
}
|
2017-01-19 05:49:42 +01:00
|
|
|
}
|
|
|
|
}
|
2019-05-16 15:52:06 +02:00
|
|
|
p.buf.WriteString("\n")
|
2017-01-19 05:49:42 +01:00
|
|
|
}
|