github.com/hugorut/terraform@v1.1.3/src/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/hugorut/terraform/src/plans"
    10  	"github.com/hugorut/terraform/src/states"
    11  	"github.com/hugorut/terraform/src/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  		outputs[name] = Output{
    46  			Sensitive: ov.Sensitive,
    47  			Type:      json.RawMessage(valueType),
    48  			Value:     json.RawMessage(value),
    49  		}
    50  	}
    51  
    52  	return outputs, nil
    53  }
    54  
    55  func OutputsFromChanges(changes []*plans.OutputChangeSrc) Outputs {
    56  	outputs := make(map[string]Output, len(changes))
    57  
    58  	for _, change := range changes {
    59  		outputs[change.Addr.OutputValue.Name] = Output{
    60  			Sensitive: change.Sensitive,
    61  			Action:    changeAction(change.Action),
    62  		}
    63  	}
    64  
    65  	return outputs
    66  }
    67  
    68  func (o Outputs) String() string {
    69  	return fmt.Sprintf("Outputs: %d", len(o))
    70  }