2021-02-23 16:16:09 +01:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
ctyjson "github.com/zclconf/go-cty/cty/json"
|
|
|
|
|
2021-07-30 19:37:23 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/plans"
|
2021-05-17 21:43:35 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/states"
|
2021-05-17 19:11:06 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
2021-02-23 16:16:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Output struct {
|
|
|
|
Sensitive bool `json:"sensitive"`
|
2021-07-30 19:37:23 +02:00
|
|
|
Type json.RawMessage `json:"type,omitempty"`
|
|
|
|
Value json.RawMessage `json:"value,omitempty"`
|
|
|
|
Action ChangeAction `json:"action,omitempty"`
|
2021-02-23 16:16:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Outputs map[string]Output
|
|
|
|
|
|
|
|
func OutputsFromMap(outputValues map[string]*states.OutputValue) (Outputs, tfdiags.Diagnostics) {
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
|
|
|
outputs := make(map[string]Output, len(outputValues))
|
|
|
|
|
|
|
|
for name, ov := range outputValues {
|
|
|
|
unmarked, _ := ov.Value.UnmarkDeep()
|
|
|
|
value, err := ctyjson.Marshal(unmarked, unmarked.Type())
|
|
|
|
if err != nil {
|
|
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
|
|
tfdiags.Error,
|
|
|
|
fmt.Sprintf("Error serializing output %q", name),
|
|
|
|
fmt.Sprintf("Error: %s", err),
|
|
|
|
))
|
|
|
|
return nil, diags
|
|
|
|
}
|
|
|
|
valueType, err := ctyjson.MarshalType(unmarked.Type())
|
|
|
|
if err != nil {
|
|
|
|
diags = diags.Append(err)
|
|
|
|
return nil, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
outputs[name] = Output{
|
|
|
|
Sensitive: ov.Sensitive,
|
|
|
|
Type: json.RawMessage(valueType),
|
|
|
|
Value: json.RawMessage(value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return outputs, nil
|
|
|
|
}
|
|
|
|
|
2021-07-30 19:37:23 +02:00
|
|
|
func OutputsFromChanges(changes []*plans.OutputChangeSrc) Outputs {
|
|
|
|
outputs := make(map[string]Output, len(changes))
|
|
|
|
|
|
|
|
for _, change := range changes {
|
|
|
|
outputs[change.Addr.OutputValue.Name] = Output{
|
|
|
|
Sensitive: change.Sensitive,
|
|
|
|
Action: changeAction(change.Action),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return outputs
|
|
|
|
}
|
|
|
|
|
2021-02-23 16:16:09 +01:00
|
|
|
func (o Outputs) String() string {
|
|
|
|
return fmt.Sprintf("Outputs: %d", len(o))
|
|
|
|
}
|