github.com/pulumi/terraform@v1.4.0/pkg/command/views/json/output.go (about)

     1  package json
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	ctyjson "github.com/zclconf/go-cty/cty/json"
     8  
     9  	"github.com/pulumi/terraform/pkg/plans"
    10  	"github.com/pulumi/terraform/pkg/states"
    11  	"github.com/pulumi/terraform/pkg/tfdiags"
    12  )
    13  
    14  type Output struct {
    15  	Sensitive bool            `json:"sensitive"`
    16  	Type      json.RawMessage `json:"type,omitempty"`
    17  	Value     json.RawMessage `json:"value,omitempty"`
    18  	Action    ChangeAction    `json:"action,omitempty"`
    19  }
    20  
    21  type Outputs map[string]Output
    22  
    23  func OutputsFromMap(outputValues map[string]*states.OutputValue) (Outputs, tfdiags.Diagnostics) {
    24  	var diags tfdiags.Diagnostics
    25  
    26  	outputs := make(map[string]Output, len(outputValues))
    27  
    28  	for name, ov := range outputValues {
    29  		unmarked, _ := ov.Value.UnmarkDeep()
    30  		value, err := ctyjson.Marshal(unmarked, unmarked.Type())
    31  		if err != nil {
    32  			diags = diags.Append(tfdiags.Sourceless(
    33  				tfdiags.Error,
    34  				fmt.Sprintf("Error serializing output %q", name),
    35  				fmt.Sprintf("Error: %s", err),
    36  			))
    37  			return nil, diags
    38  		}
    39  		valueType, err := ctyjson.MarshalType(unmarked.Type())
    40  		if err != nil {
    41  			diags = diags.Append(err)
    42  			return nil, diags
    43  		}
    44  
    45  		var redactedValue json.RawMessage
    46  		if !ov.Sensitive {
    47  			redactedValue = json.RawMessage(value)
    48  		}
    49  
    50  		outputs[name] = Output{
    51  			Sensitive: ov.Sensitive,
    52  			Type:      json.RawMessage(valueType),
    53  			Value:     redactedValue,
    54  		}
    55  	}
    56  
    57  	return outputs, nil
    58  }
    59  
    60  func OutputsFromChanges(changes []*plans.OutputChangeSrc) Outputs {
    61  	outputs := make(map[string]Output, len(changes))
    62  
    63  	for _, change := range changes {
    64  		outputs[change.Addr.OutputValue.Name] = Output{
    65  			Sensitive: change.Sensitive,
    66  			Action:    changeAction(change.Action),
    67  		}
    68  	}
    69  
    70  	return outputs
    71  }
    72  
    73  func (o Outputs) String() string {
    74  	return fmt.Sprintf("Outputs: %d", len(o))
    75  }