summaryrefslogtreecommitdiff
path: root/vendor/github.com/hashicorp/terraform/dag/marshal.go
blob: 16d5dd6dde83d4a189080659add508fb109d5fa2 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package dag

import (
	"encoding/json"
	"fmt"
	"io"
	"log"
	"reflect"
	"sort"
	"strconv"
	"sync"
)

const (
	typeOperation             = "Operation"
	typeTransform             = "Transform"
	typeWalk                  = "Walk"
	typeDepthFirstWalk        = "DepthFirstWalk"
	typeReverseDepthFirstWalk = "ReverseDepthFirstWalk"
	typeTransitiveReduction   = "TransitiveReduction"
	typeEdgeInfo              = "EdgeInfo"
	typeVertexInfo            = "VertexInfo"
	typeVisitInfo             = "VisitInfo"
)

// the marshal* structs are for serialization of the graph data.
type marshalGraph struct {
	// Type is always "Graph", for identification as a top level object in the
	// JSON stream.
	Type string

	// Each marshal structure requires a unique ID so that it can be referenced
	// by other structures.
	ID string `json:",omitempty"`

	// Human readable name for this graph.
	Name string `json:",omitempty"`

	// Arbitrary attributes that can be added to the output.
	Attrs map[string]string `json:",omitempty"`

	// List of graph vertices, sorted by ID.
	Vertices []*marshalVertex `json:",omitempty"`

	// List of edges, sorted by Source ID.
	Edges []*marshalEdge `json:",omitempty"`

	// Any number of subgraphs. A subgraph itself is considered a vertex, and
	// may be referenced by either end of an edge.
	Subgraphs []*marshalGraph `json:",omitempty"`

	// Any lists of vertices that are included in cycles.
	Cycles [][]*marshalVertex `json:",omitempty"`
}

// The add, remove, connect, removeEdge methods mirror the basic Graph
// manipulations to reconstruct a marshalGraph from a debug log.
func (g *marshalGraph) add(v *marshalVertex) {
	g.Vertices = append(g.Vertices, v)
	sort.Sort(vertices(g.Vertices))
}

func (g *marshalGraph) remove(v *marshalVertex) {
	for i, existing := range g.Vertices {
		if v.ID == existing.ID {
			g.Vertices = append(g.Vertices[:i], g.Vertices[i+1:]...)
			return
		}
	}
}

func (g *marshalGraph) connect(e *marshalEdge) {
	g.Edges = append(g.Edges, e)
	sort.Sort(edges(g.Edges))
}

func (g *marshalGraph) removeEdge(e *marshalEdge) {
	for i, existing := range g.Edges {
		if e.Source == existing.Source && e.Target == existing.Target {
			g.Edges = append(g.Edges[:i], g.Edges[i+1:]...)
			return
		}
	}
}

func (g *marshalGraph) vertexByID(id string) *marshalVertex {
	for _, v := range g.Vertices {
		if id == v.ID {
			return v
		}
	}
	return nil
}

type marshalVertex struct {
	// Unique ID, used to reference this vertex from other structures.
	ID string

	// Human readable name
	Name string `json:",omitempty"`

	Attrs map[string]string `json:",omitempty"`

	// This is to help transition from the old Dot interfaces. We record if the
	// node was a GraphNodeDotter here, so we can call it to get attributes.
	graphNodeDotter GraphNodeDotter
}

func newMarshalVertex(v Vertex) *marshalVertex {
	dn, ok := v.(GraphNodeDotter)
	if !ok {
		dn = nil
	}

	return &marshalVertex{
		ID:              marshalVertexID(v),
		Name:            VertexName(v),
		Attrs:           make(map[string]string),
		graphNodeDotter: dn,
	}
}

// vertices is a sort.Interface implementation for sorting vertices by ID
type vertices []*marshalVertex

func (v vertices) Less(i, j int) bool { return v[i].Name < v[j].Name }
func (v vertices) Len() int           { return len(v) }
func (v vertices) Swap(i, j int)      { v[i], v[j] = v[j], v[i] }

type marshalEdge struct {
	// Human readable name
	Name string

	// Source and Target Vertices by ID
	Source string
	Target string

	Attrs map[string]string `json:",omitempty"`
}

func newMarshalEdge(e Edge) *marshalEdge {
	return &marshalEdge{
		Name:   fmt.Sprintf("%s|%s", VertexName(e.Source()), VertexName(e.Target())),
		Source: marshalVertexID(e.Source()),
		Target: marshalVertexID(e.Target()),
		Attrs:  make(map[string]string),
	}
}

// edges is a sort.Interface implementation for sorting edges by Source ID
type edges []*marshalEdge

func (e edges) Less(i, j int) bool { return e[i].Name < e[j].Name }
func (e edges) Len() int           { return len(e) }
func (e edges) Swap(i, j int)      { e[i], e[j] = e[j], e[i] }

// build a marshalGraph structure from a *Graph
func newMarshalGraph(name string, g *Graph) *marshalGraph {
	mg := &marshalGraph{
		Type:  "Graph",
		Name:  name,
		Attrs: make(map[string]string),
	}

	for _, v := range g.Vertices() {
		id := marshalVertexID(v)
		if sg, ok := marshalSubgrapher(v); ok {
			smg := newMarshalGraph(VertexName(v), sg)
			smg.ID = id
			mg.Subgraphs = append(mg.Subgraphs, smg)
		}

		mv := newMarshalVertex(v)
		mg.Vertices = append(mg.Vertices, mv)
	}

	sort.Sort(vertices(mg.Vertices))

	for _, e := range g.Edges() {
		mg.Edges = append(mg.Edges, newMarshalEdge(e))
	}

	sort.Sort(edges(mg.Edges))

	for _, c := range (&AcyclicGraph{*g}).Cycles() {
		var cycle []*marshalVertex
		for _, v := range c {
			mv := newMarshalVertex(v)
			cycle = append(cycle, mv)
		}
		mg.Cycles = append(mg.Cycles, cycle)
	}

	return mg
}

// Attempt to return a unique ID for any vertex.
func marshalVertexID(v Vertex) string {
	val := reflect.ValueOf(v)
	switch val.Kind() {
	case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
		return strconv.Itoa(int(val.Pointer()))
	case reflect.Interface:
		return strconv.Itoa(int(val.InterfaceData()[1]))
	}

	if v, ok := v.(Hashable); ok {
		h := v.Hashcode()
		if h, ok := h.(string); ok {
			return h
		}
	}

	// fallback to a name, which we hope is unique.
	return VertexName(v)

	// we could try harder by attempting to read the arbitrary value from the
	// interface, but we shouldn't get here from terraform right now.
}

// check for a Subgrapher, and return the underlying *Graph.
func marshalSubgrapher(v Vertex) (*Graph, bool) {
	sg, ok := v.(Subgrapher)
	if !ok {
		return nil, false
	}

	switch g := sg.Subgraph().DirectedGraph().(type) {
	case *Graph:
		return g, true
	case *AcyclicGraph:
		return &g.Graph, true
	}

	return nil, false
}

// The DebugOperationEnd func type provides a way to call an End function via a
// method call, allowing for the chaining of methods in a defer statement.
type DebugOperationEnd func(string)

// End calls function e with the info parameter, marking the end of this
// operation in the logs.
func (e DebugOperationEnd) End(info string) { e(info) }

// encoder provides methods to write debug data to an io.Writer, and is a noop
// when no writer is present
type encoder struct {
	sync.Mutex
	w io.Writer
}

// Encode is analogous to json.Encoder.Encode
func (e *encoder) Encode(i interface{}) {
	if e == nil || e.w == nil {
		return
	}
	e.Lock()
	defer e.Unlock()

	js, err := json.Marshal(i)
	if err != nil {
		log.Println("[ERROR] dag:", err)
		return
	}
	js = append(js, '\n')

	_, err = e.w.Write(js)
	if err != nil {
		log.Println("[ERROR] dag:", err)
		return
	}
}

func (e *encoder) Add(v Vertex) {
	e.Encode(marshalTransform{
		Type:      typeTransform,
		AddVertex: newMarshalVertex(v),
	})
}

// Remove records the removal of Vertex v.
func (e *encoder) Remove(v Vertex) {
	e.Encode(marshalTransform{
		Type:         typeTransform,
		RemoveVertex: newMarshalVertex(v),
	})
}

func (e *encoder) Connect(edge Edge) {
	e.Encode(marshalTransform{
		Type:    typeTransform,
		AddEdge: newMarshalEdge(edge),
	})
}

func (e *encoder) RemoveEdge(edge Edge) {
	e.Encode(marshalTransform{
		Type:       typeTransform,
		RemoveEdge: newMarshalEdge(edge),
	})
}

// BeginOperation marks the start of set of graph transformations, and returns
// an EndDebugOperation func to be called once the opration is complete.
func (e *encoder) BeginOperation(op string, info string) DebugOperationEnd {
	if e == nil {
		return func(string) {}
	}

	e.Encode(marshalOperation{
		Type:  typeOperation,
		Begin: op,
		Info:  info,
	})

	return func(info string) {
		e.Encode(marshalOperation{
			Type: typeOperation,
			End:  op,
			Info: info,
		})
	}
}

// structure for recording graph transformations
type marshalTransform struct {
	// Type: "Transform"
	Type         string
	AddEdge      *marshalEdge   `json:",omitempty"`
	RemoveEdge   *marshalEdge   `json:",omitempty"`
	AddVertex    *marshalVertex `json:",omitempty"`
	RemoveVertex *marshalVertex `json:",omitempty"`
}

func (t marshalTransform) Transform(g *marshalGraph) {
	switch {
	case t.AddEdge != nil:
		g.connect(t.AddEdge)
	case t.RemoveEdge != nil:
		g.removeEdge(t.RemoveEdge)
	case t.AddVertex != nil:
		g.add(t.AddVertex)
	case t.RemoveVertex != nil:
		g.remove(t.RemoveVertex)
	}
}

// this structure allows us to decode any object in the json stream for
// inspection, then re-decode it into a proper struct if needed.
type streamDecode struct {
	Type string
	Map  map[string]interface{}
	JSON []byte
}

func (s *streamDecode) UnmarshalJSON(d []byte) error {
	s.JSON = d
	err := json.Unmarshal(d, &s.Map)
	if err != nil {
		return err
	}

	if t, ok := s.Map["Type"]; ok {
		s.Type, _ = t.(string)
	}
	return nil
}

// structure for recording the beginning and end of any multi-step
// transformations. These are informational, and not required to reproduce the
// graph state.
type marshalOperation struct {
	Type  string
	Begin string `json:",omitempty"`
	End   string `json:",omitempty"`
	Info  string `json:",omitempty"`
}

// decodeGraph decodes a marshalGraph from an encoded graph stream.
func decodeGraph(r io.Reader) (*marshalGraph, error) {
	dec := json.NewDecoder(r)

	// a stream should always start with a graph
	g := &marshalGraph{}

	err := dec.Decode(g)
	if err != nil {
		return nil, err
	}

	// now replay any operations that occurred on the original graph
	for dec.More() {
		s := &streamDecode{}
		err := dec.Decode(s)
		if err != nil {
			return g, err
		}

		// the only Type we're concerned with here is Transform to complete the
		// Graph
		if s.Type != typeTransform {
			continue
		}

		t := &marshalTransform{}
		err = json.Unmarshal(s.JSON, t)
		if err != nil {
			return g, err
		}
		t.Transform(g)
	}
	return g, nil
}

// marshalVertexInfo allows encoding arbitrary information about the a single
// Vertex in the logs. These are accumulated for informational display while
// rebuilding the graph.
type marshalVertexInfo struct {
	Type   string
	Vertex *marshalVertex
	Info   string
}

func newVertexInfo(infoType string, v Vertex, info string) *marshalVertexInfo {
	return &marshalVertexInfo{
		Type:   infoType,
		Vertex: newMarshalVertex(v),
		Info:   info,
	}
}

// marshalEdgeInfo allows encoding arbitrary information about the a single
// Edge in the logs. These are accumulated for informational display while
// rebuilding the graph.
type marshalEdgeInfo struct {
	Type string
	Edge *marshalEdge
	Info string
}

func newEdgeInfo(infoType string, e Edge, info string) *marshalEdgeInfo {
	return &marshalEdgeInfo{
		Type: infoType,
		Edge: newMarshalEdge(e),
		Info: info,
	}
}

// JSON2Dot reads a Graph debug log from and io.Reader, and converts the final
// graph dot format.
//
// TODO: Allow returning the output at a certain point during decode.
//       Encode extra information from the json log into the Dot.
func JSON2Dot(r io.Reader) ([]byte, error) {
	g, err := decodeGraph(r)
	if err != nil {
		return nil, err
	}

	return g.Dot(nil), nil
}