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

Commit 93fbb9e

Browse files
committed
command/graph
1 parent 2ffbe56 commit 93fbb9e

4 files changed

Lines changed: 158 additions & 71 deletions

File tree

command/apply.go

Lines changed: 8 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"os"
77
"strings"
88

9-
"github.com/hashicorp/terraform/config"
109
"github.com/hashicorp/terraform/terraform"
1110
"github.com/mitchellh/cli"
1211
)
@@ -54,26 +53,14 @@ func (c *ApplyCommand) Run(args []string) int {
5453

5554
// Attempt to read a plan from the path given. This is how we test that
5655
// it is a plan or not (kind of jank, but if it quacks like a duck...)
57-
var plan *terraform.Plan
58-
f, err := os.Open(configPath)
59-
if err == nil {
60-
plan, err = terraform.ReadPlan(f)
61-
f.Close()
62-
if err != nil {
63-
// Make sure the plan is nil so that we try to load as
64-
// configuration.
65-
plan = nil
66-
}
56+
planStatePath := statePath
57+
if init {
58+
planStatePath = ""
6759
}
68-
69-
if plan == nil {
70-
// No plan was given, so we're loading from configuration. Generate
71-
// the plan given the configuration.
72-
plan, err = c.configToPlan(tf, init, statePath, configPath)
73-
if err != nil {
74-
c.Ui.Error(err.Error())
75-
return 1
76-
}
60+
plan, err := PlanArg(configPath, planStatePath, tf)
61+
if err != nil {
62+
c.Ui.Error(err.Error())
63+
return 1
7764
}
7865

7966
state, err := tf.Apply(plan)
@@ -83,7 +70,7 @@ func (c *ApplyCommand) Run(args []string) int {
8370
}
8471

8572
// Write state out to the file
86-
f, err = os.Create(stateOutPath)
73+
f, err := os.Create(stateOutPath)
8774
if err == nil {
8875
err = terraform.WriteState(state, f)
8976
f.Close()
@@ -120,53 +107,3 @@ Options:
120107
func (c *ApplyCommand) Synopsis() string {
121108
return "Builds or changes infrastructure"
122109
}
123-
124-
func (c *ApplyCommand) configToPlan(
125-
tf *terraform.Terraform,
126-
init bool,
127-
statePath string,
128-
configPath string) (*terraform.Plan, error) {
129-
if !init {
130-
if _, err := os.Stat(statePath); err != nil {
131-
return nil, fmt.Errorf(
132-
"There was an error reading the state file. The path\n"+
133-
"and error are shown below. If you're trying to build a\n"+
134-
"brand new infrastructure, explicitly pass the '-init'\n"+
135-
"flag to Terraform to tell it it is okay to build new\n"+
136-
"state.\n\n"+
137-
"Path: %s\n"+
138-
"Error: %s",
139-
statePath,
140-
err)
141-
}
142-
}
143-
144-
// Load up the state
145-
var state *terraform.State
146-
if !init {
147-
f, err := os.Open(statePath)
148-
if err == nil {
149-
state, err = terraform.ReadState(f)
150-
f.Close()
151-
}
152-
153-
if err != nil {
154-
return nil, fmt.Errorf("Error loading state: %s", err)
155-
}
156-
}
157-
158-
config, err := config.Load(configPath)
159-
if err != nil {
160-
return nil, fmt.Errorf("Error loading config: %s", err)
161-
}
162-
163-
plan, err := tf.Plan(&terraform.PlanOpts{
164-
Config: config,
165-
State: state,
166-
})
167-
if err != nil {
168-
return nil, fmt.Errorf("Error running plan: %s", err)
169-
}
170-
171-
return plan, nil
172-
}

command/command.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package command
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/hashicorp/terraform/config"
8+
"github.com/hashicorp/terraform/terraform"
9+
)
10+
11+
func PlanArg(
12+
path string,
13+
statePath string,
14+
tf *terraform.Terraform) (*terraform.Plan, error) {
15+
// First try to just read the plan directly from the path given.
16+
f, err := os.Open(path)
17+
if err == nil {
18+
plan, err := terraform.ReadPlan(f)
19+
f.Close()
20+
if err == nil {
21+
return plan, nil
22+
}
23+
}
24+
25+
if statePath != "" {
26+
if _, err := os.Stat(statePath); err != nil {
27+
return nil, fmt.Errorf(
28+
"There was an error reading the state file. The path\n"+
29+
"and error are shown below. If you're trying to build a\n"+
30+
"brand new infrastructure, explicitly pass the '-init'\n"+
31+
"flag to Terraform to tell it it is okay to build new\n"+
32+
"state.\n\n"+
33+
"Path: %s\n"+
34+
"Error: %s",
35+
statePath,
36+
err)
37+
}
38+
}
39+
40+
// Load up the state
41+
var state *terraform.State
42+
if statePath != "" {
43+
f, err := os.Open(statePath)
44+
if err == nil {
45+
state, err = terraform.ReadState(f)
46+
f.Close()
47+
}
48+
49+
if err != nil {
50+
return nil, fmt.Errorf("Error loading state: %s", err)
51+
}
52+
}
53+
54+
config, err := config.Load(path)
55+
if err != nil {
56+
return nil, fmt.Errorf("Error loading config: %s", err)
57+
}
58+
59+
plan, err := tf.Plan(&terraform.PlanOpts{
60+
Config: config,
61+
State: state,
62+
})
63+
if err != nil {
64+
return nil, fmt.Errorf("Error running plan: %s", err)
65+
}
66+
67+
return plan, nil
68+
}

command/graph.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package command
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"os"
7+
"strings"
8+
9+
"github.com/hashicorp/terraform/config"
10+
"github.com/hashicorp/terraform/digraph"
11+
"github.com/hashicorp/terraform/terraform"
12+
"github.com/mitchellh/cli"
13+
)
14+
15+
// GraphCommand is a Command implementation that takes a Terraform
16+
// configuration and outputs the dependency tree in graphical form.
17+
type GraphCommand struct {
18+
TFConfig *terraform.Config
19+
Ui cli.Ui
20+
}
21+
22+
func (c *GraphCommand) Run(args []string) int {
23+
cmdFlags := flag.NewFlagSet("graph", flag.ContinueOnError)
24+
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
25+
if err := cmdFlags.Parse(args); err != nil {
26+
return 1
27+
}
28+
29+
args = cmdFlags.Args()
30+
if len(args) != 1 {
31+
c.Ui.Error("The graph command expects one argument.\n")
32+
cmdFlags.Usage()
33+
return 1
34+
}
35+
36+
conf, err := config.Load(args[0])
37+
if err != nil {
38+
c.Ui.Error(fmt.Sprintf("Error loading config: %s", err))
39+
return 1
40+
}
41+
42+
g, err := terraform.Graph(&terraform.GraphOpts{
43+
Config: conf,
44+
Providers: c.TFConfig.Providers,
45+
})
46+
if err != nil {
47+
c.Ui.Error(fmt.Sprintf("Error creating graph: %s", err))
48+
return 1
49+
}
50+
51+
nodes := make([]digraph.Node, len(g.Nouns))
52+
for i, n := range g.Nouns {
53+
nodes[i] = n
54+
}
55+
digraph.GenerateDot(nodes, os.Stdout)
56+
57+
return 0
58+
}
59+
60+
func (c *GraphCommand) Help() string {
61+
helpText := `
62+
Usage: terraform graph [options] PATH
63+
64+
Outputs the visual graph of Terraform resources. If the path given is
65+
the path to a configuration, the dependency graph of the resources are
66+
shown. If the path is a plan file, then the dependency graph of the
67+
plan itself is shown.
68+
69+
`
70+
return strings.TrimSpace(helpText)
71+
}
72+
73+
func (c *GraphCommand) Synopsis() string {
74+
return "Output visual graph of Terraform resources"
75+
}

commands.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ func init() {
3333
}, nil
3434
},
3535

36+
"graph": func() (cli.Command, error) {
37+
return &command.GraphCommand{
38+
TFConfig: &TFConfig,
39+
Ui: Ui,
40+
}, nil
41+
},
42+
3643
"plan": func() (cli.Command, error) {
3744
return &command.PlanCommand{
3845
TFConfig: &TFConfig,

0 commit comments

Comments
 (0)