Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 22e868b

Browse files
committed
command/graph: update for new graphs
1 parent fb8f2e2 commit 22e868b

2 files changed

Lines changed: 45 additions & 11 deletions

File tree

command/graph.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ func (c *GraphCommand) Run(args []string) int {
2020
var moduleDepth int
2121
var verbose bool
2222
var drawCycles bool
23+
var graphTypeStr string
2324

2425
args = c.Meta.process(args, false)
2526

2627
cmdFlags := flag.NewFlagSet("graph", flag.ContinueOnError)
2728
c.addModuleDepthFlag(cmdFlags, &moduleDepth)
2829
cmdFlags.BoolVar(&verbose, "verbose", false, "verbose")
2930
cmdFlags.BoolVar(&drawCycles, "draw-cycles", false, "draw-cycles")
31+
cmdFlags.StringVar(&graphTypeStr, "type", "", "type")
3032
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
3133
if err := cmdFlags.Parse(args); err != nil {
3234
return 1
@@ -48,7 +50,7 @@ func (c *GraphCommand) Run(args []string) int {
4850
}
4951
}
5052

51-
ctx, _, err := c.Context(contextOpts{
53+
ctx, planFile, err := c.Context(contextOpts{
5254
Path: path,
5355
StatePath: "",
5456
})
@@ -57,9 +59,25 @@ func (c *GraphCommand) Run(args []string) int {
5759
return 1
5860
}
5961

62+
// Determine the graph type
63+
graphType := terraform.GraphTypePlan
64+
if planFile {
65+
graphType = terraform.GraphTypeApply
66+
}
67+
68+
if graphTypeStr != "" {
69+
v, ok := terraform.GraphTypeMap[graphTypeStr]
70+
if !ok {
71+
c.Ui.Error(fmt.Sprintf("Invalid graph type requested: %s", graphTypeStr))
72+
return 1
73+
}
74+
75+
graphType = v
76+
}
77+
6078
// Skip validation during graph generation - we want to see the graph even if
6179
// it is invalid for some reason.
62-
g, err := ctx.Graph(&terraform.ContextGraphOpts{
80+
g, err := ctx.Graph(graphType, &terraform.ContextGraphOpts{
6381
Verbose: verbose,
6482
Validate: false,
6583
})
@@ -87,25 +105,28 @@ func (c *GraphCommand) Help() string {
87105
helpText := `
88106
Usage: terraform graph [options] [DIR]
89107
90-
Outputs the visual dependency graph of Terraform resources according to
108+
Outputs the visual execution graph of Terraform resources according to
91109
configuration files in DIR (or the current directory if omitted).
92110
93111
The graph is outputted in DOT format. The typical program that can
94112
read this format is GraphViz, but many web services are also available
95113
to read this format.
96114
97-
Options:
115+
The -type flag can be used to control the type of graph shown. Terraform
116+
creates different graphs for different operations. See the options below
117+
for the list of types supported. The default type is "plan" if a
118+
configuration is given, and "apply" if a plan file is passed as an
119+
argument.
98120
99-
-draw-cycles Highlight any cycles in the graph with colored edges.
100-
This helps when diagnosing cycle errors.
121+
Options:
101122
102-
-module-depth=n The maximum depth to expand modules. By default this is
103-
-1, which will expand resources within all modules.
123+
-draw-cycles Highlight any cycles in the graph with colored edges.
124+
This helps when diagnosing cycle errors.
104125
105-
-verbose Generate a verbose, "worst-case" graph, with all nodes
106-
for potential operations in place.
126+
-no-color If specified, output won't contain any color.
107127
108-
-no-color If specified, output won't contain any color.
128+
-type=plan Type of graph to output. Can be: plan, plan-destroy, apply,
129+
legacy.
109130
110131
`
111132
return strings.TrimSpace(helpText)

command/graph_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ func TestGraph_noArgs(t *testing.T) {
8080

8181
func TestGraph_plan(t *testing.T) {
8282
planPath := testPlanFile(t, &terraform.Plan{
83+
Diff: &terraform.Diff{
84+
Modules: []*terraform.ModuleDiff{
85+
&terraform.ModuleDiff{
86+
Path: []string{"root"},
87+
Resources: map[string]*terraform.InstanceDiff{
88+
"test_instance.bar": &terraform.InstanceDiff{
89+
Destroy: true,
90+
},
91+
},
92+
},
93+
},
94+
},
95+
8396
Module: testModule(t, "graph"),
8497
})
8598

0 commit comments

Comments
 (0)