/
output.go
70 lines (56 loc) · 1.67 KB
/
output.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package json
import (
"encoding/json"
"fmt"
ctyjson "github.com/zclconf/go-cty/cty/json"
"github.com/hashicorp/terraform/internal/plans"
"github.com/hashicorp/terraform/internal/states"
"github.com/hashicorp/terraform/internal/tfdiags"
)
type Output struct {
Sensitive bool `json:"sensitive"`
Type json.RawMessage `json:"type,omitempty"`
Value json.RawMessage `json:"value,omitempty"`
Action ChangeAction `json:"action,omitempty"`
}
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
}
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
}
func (o Outputs) String() string {
return fmt.Sprintf("Outputs: %d", len(o))
}