github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/internal/command/views/json_view.go (about)

     1  package views
     2  
     3  import (
     4  	encJson "encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/go-hclog"
     8  	"github.com/iaas-resource-provision/iaas-rpc/internal/command/views/json"
     9  	"github.com/iaas-resource-provision/iaas-rpc/internal/tfdiags"
    10  	tfversion "github.com/iaas-resource-provision/iaas-rpc/version"
    11  )
    12  
    13  // This version describes the schema of JSON UI messages. This version must be
    14  // updated after making any changes to this view, the jsonHook, or any of the
    15  // command/views/json package.
    16  const JSON_UI_VERSION = "0.1.0"
    17  
    18  func NewJSONView(view *View) *JSONView {
    19  	log := hclog.New(&hclog.LoggerOptions{
    20  		Name:       "terraform.ui",
    21  		Output:     view.streams.Stdout.File,
    22  		JSONFormat: true,
    23  	})
    24  	jv := &JSONView{
    25  		log:  log,
    26  		view: view,
    27  	}
    28  	jv.Version()
    29  	return jv
    30  }
    31  
    32  type JSONView struct {
    33  	// hclog is used for all output in JSON UI mode. The logger has an internal
    34  	// mutex to ensure that messages are not interleaved.
    35  	log hclog.Logger
    36  
    37  	// We hold a reference to the view entirely to allow us to access the
    38  	// ConfigSources function pointer, in order to render source snippets into
    39  	// diagnostics. This is even more unfortunate than the same reference in the
    40  	// view.
    41  	//
    42  	// Do not be tempted to dereference the configSource value upon logger init,
    43  	// as it will likely be updated later.
    44  	view *View
    45  }
    46  
    47  func (v *JSONView) Version() {
    48  	version := tfversion.String()
    49  	v.log.Info(
    50  		fmt.Sprintf("RPC %s", version),
    51  		"type", json.MessageVersion,
    52  		"terraform", version,
    53  		"ui", JSON_UI_VERSION,
    54  	)
    55  }
    56  
    57  func (v *JSONView) Log(message string) {
    58  	v.log.Info(message, "type", json.MessageLog)
    59  }
    60  
    61  func (v *JSONView) StateDump(state string) {
    62  	v.log.Info(
    63  		"Emergency state dump",
    64  		"type", json.MessageLog,
    65  		"state", encJson.RawMessage(state),
    66  	)
    67  }
    68  
    69  func (v *JSONView) Diagnostics(diags tfdiags.Diagnostics) {
    70  	sources := v.view.configSources()
    71  	for _, diag := range diags {
    72  		diagnostic := json.NewDiagnostic(diag, sources)
    73  		switch diag.Severity() {
    74  		case tfdiags.Warning:
    75  			v.log.Warn(
    76  				fmt.Sprintf("Warning: %s", diag.Description().Summary),
    77  				"type", json.MessageDiagnostic,
    78  				"diagnostic", diagnostic,
    79  			)
    80  		default:
    81  			v.log.Error(
    82  				fmt.Sprintf("Error: %s", diag.Description().Summary),
    83  				"type", json.MessageDiagnostic,
    84  				"diagnostic", diagnostic,
    85  			)
    86  		}
    87  	}
    88  }
    89  
    90  func (v *JSONView) PlannedChange(c *json.ResourceInstanceChange) {
    91  	v.log.Info(
    92  		c.String(),
    93  		"type", json.MessagePlannedChange,
    94  		"change", c,
    95  	)
    96  }
    97  
    98  func (v *JSONView) ResourceDrift(c *json.ResourceInstanceChange) {
    99  	v.log.Info(
   100  		fmt.Sprintf("%s: Drift detected (%s)", c.Resource.Addr, c.Action),
   101  		"type", json.MessageResourceDrift,
   102  		"change", c,
   103  	)
   104  }
   105  
   106  func (v *JSONView) ChangeSummary(cs *json.ChangeSummary) {
   107  	v.log.Info(
   108  		cs.String(),
   109  		"type", json.MessageChangeSummary,
   110  		"changes", cs,
   111  	)
   112  }
   113  
   114  func (v *JSONView) Hook(h json.Hook) {
   115  	v.log.Info(
   116  		h.String(),
   117  		"type", h.HookType(),
   118  		"hook", h,
   119  	)
   120  }
   121  
   122  func (v *JSONView) Outputs(outputs json.Outputs) {
   123  	v.log.Info(
   124  		outputs.String(),
   125  		"type", json.MessageOutputs,
   126  		"outputs", outputs,
   127  	)
   128  }