github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/jsonformat/state.go (about)

     1  package jsonformat
     2  
     3  import (
     4  	"sort"
     5  
     6  	ctyjson "github.com/zclconf/go-cty/cty/json"
     7  
     8  	"github.com/hashicorp/terraform/internal/command/jsonformat/computed"
     9  	"github.com/hashicorp/terraform/internal/command/jsonformat/differ"
    10  	"github.com/hashicorp/terraform/internal/command/jsonprovider"
    11  	"github.com/hashicorp/terraform/internal/command/jsonstate"
    12  )
    13  
    14  type State struct {
    15  	StateFormatVersion string                      `json:"state_format_version"`
    16  	RootModule         jsonstate.Module            `json:"root"`
    17  	RootModuleOutputs  map[string]jsonstate.Output `json:"root_module_outputs"`
    18  
    19  	ProviderFormatVersion string                            `json:"provider_format_version"`
    20  	ProviderSchemas       map[string]*jsonprovider.Provider `json:"provider_schemas"`
    21  }
    22  
    23  func (state State) Empty() bool {
    24  	return len(state.RootModuleOutputs) == 0 && len(state.RootModule.Resources) == 0 && len(state.RootModule.ChildModules) == 0
    25  }
    26  
    27  func (state State) GetSchema(resource jsonstate.Resource) *jsonprovider.Schema {
    28  	switch resource.Mode {
    29  	case jsonstate.ManagedResourceMode:
    30  		return state.ProviderSchemas[resource.ProviderName].ResourceSchemas[resource.Type]
    31  	case jsonstate.DataResourceMode:
    32  		return state.ProviderSchemas[resource.ProviderName].DataSourceSchemas[resource.Type]
    33  	default:
    34  		panic("found unrecognized resource mode: " + resource.Mode)
    35  	}
    36  }
    37  
    38  func (state State) renderHumanStateModule(renderer Renderer, module jsonstate.Module, opts computed.RenderHumanOpts, first bool) {
    39  	if len(module.Resources) > 0 && !first {
    40  		renderer.Streams.Println()
    41  	}
    42  
    43  	for _, resource := range module.Resources {
    44  
    45  		if !first {
    46  			renderer.Streams.Println()
    47  		}
    48  
    49  		if first {
    50  			first = false
    51  		}
    52  
    53  		if len(resource.DeposedKey) > 0 {
    54  			renderer.Streams.Printf("# %s: (deposed object %s)", resource.Address, resource.DeposedKey)
    55  		} else if resource.Tainted {
    56  			renderer.Streams.Printf("# %s: (tainted)", resource.Address)
    57  		} else {
    58  			renderer.Streams.Printf("# %s:", resource.Address)
    59  		}
    60  
    61  		renderer.Streams.Println()
    62  
    63  		schema := state.GetSchema(resource)
    64  		switch resource.Mode {
    65  		case jsonstate.ManagedResourceMode:
    66  			renderer.Streams.Printf("resource %q %q %s", resource.Type, resource.Name, differ.FromJsonResource(resource).ComputeDiffForBlock(schema.Block).RenderHuman(0, opts))
    67  		case jsonstate.DataResourceMode:
    68  			renderer.Streams.Printf("data %q %q %s", resource.Type, resource.Name, differ.FromJsonResource(resource).ComputeDiffForBlock(schema.Block).RenderHuman(0, opts))
    69  		default:
    70  			panic("found unrecognized resource mode: " + resource.Mode)
    71  		}
    72  
    73  		renderer.Streams.Println()
    74  	}
    75  
    76  	for _, child := range module.ChildModules {
    77  		state.renderHumanStateModule(renderer, child, opts, first)
    78  	}
    79  }
    80  
    81  func (state State) renderHumanStateOutputs(renderer Renderer, opts computed.RenderHumanOpts) {
    82  
    83  	if len(state.RootModuleOutputs) > 0 {
    84  		renderer.Streams.Printf("\n\nOutputs:\n\n")
    85  
    86  		var keys []string
    87  		for key := range state.RootModuleOutputs {
    88  			keys = append(keys, key)
    89  		}
    90  		sort.Strings(keys)
    91  
    92  		for _, key := range keys {
    93  			output := state.RootModuleOutputs[key]
    94  			ctype, err := ctyjson.UnmarshalType(output.Type)
    95  			if err != nil {
    96  				// We can actually do this without the type, so even if we fail
    97  				// to work out the type let's just render this anyway.
    98  				renderer.Streams.Printf("%s = %s\n", key, differ.FromJsonOutput(state.RootModuleOutputs[key]).ComputeDiffForOutput().RenderHuman(0, opts))
    99  			} else {
   100  				renderer.Streams.Printf("%s = %s\n", key, differ.FromJsonOutput(state.RootModuleOutputs[key]).ComputeDiffForType(ctype).RenderHuman(0, opts))
   101  			}
   102  		}
   103  	}
   104  }