-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implements the graph command * Adds version gating, additional tests
- Loading branch information
Showing
5 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package tfexec | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
type graphConfig struct { | ||
plan string | ||
drawCycles bool | ||
graphType string | ||
} | ||
|
||
var defaultGraphOptions = graphConfig{} | ||
|
||
type GraphOption interface { | ||
configureGraph(*graphConfig) | ||
} | ||
|
||
func (opt *GraphPlanOption) configureGraph(conf *graphConfig) { | ||
conf.plan = opt.file | ||
} | ||
|
||
func (opt *DrawCyclesOption) configureGraph(conf *graphConfig) { | ||
conf.drawCycles = opt.drawCycles | ||
} | ||
|
||
func (opt *GraphTypeOption) configureGraph(conf *graphConfig) { | ||
conf.graphType = opt.graphType | ||
} | ||
|
||
func (tf *Terraform) Graph(ctx context.Context, opts ...GraphOption) (string, error) { | ||
graphCmd, err := tf.graphCmd(ctx, opts...) | ||
if err != nil { | ||
return "", err | ||
} | ||
var outBuf strings.Builder | ||
graphCmd.Stdout = &outBuf | ||
err = tf.runTerraformCmd(ctx, graphCmd) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return outBuf.String(), nil | ||
|
||
} | ||
|
||
func (tf *Terraform) graphCmd(ctx context.Context, opts ...GraphOption) (*exec.Cmd, error) { | ||
c := defaultGraphOptions | ||
|
||
for _, o := range opts { | ||
o.configureGraph(&c) | ||
} | ||
|
||
args := []string{"graph"} | ||
|
||
if c.plan != "" { | ||
// plan was a positional arguement prior to Terraform 0.15.0. Ensure proper use by checking version. | ||
if err := tf.compatible(ctx, tf0_15_0, nil); err == nil { | ||
args = append(args, "-plan="+c.plan) | ||
} else { | ||
args = append(args, c.plan) | ||
} | ||
} | ||
|
||
if c.drawCycles { | ||
err := tf.compatible(ctx, tf0_5_0, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("-draw-cycles was first introduced in Terraform 0.5.0: %w", err) | ||
} | ||
args = append(args, "-draw-cycles") | ||
} | ||
|
||
if c.graphType != "" { | ||
err := tf.compatible(ctx, tf0_8_0, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("-graph-type was first introduced in Terraform 0.8.0: %w", err) | ||
} | ||
args = append(args, "-type="+c.graphType) | ||
} | ||
|
||
return tf.buildTerraformCmd(ctx, nil, args...), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package tfexec | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-exec/tfexec/internal/testutil" | ||
) | ||
|
||
func TestGraphCmd(t *testing.T) { | ||
td := t.TempDir() | ||
|
||
tf, err := NewTerraform(td, tfVersion(t, testutil.Latest013)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// empty env, to avoid environ mismatch in testing | ||
tf.SetEnv(map[string]string{}) | ||
|
||
t.Run("defaults", func(t *testing.T) { | ||
graphCmd, _ := tf.graphCmd(context.Background()) | ||
|
||
assertCmd(t, []string{ | ||
"graph", | ||
}, nil, graphCmd) | ||
}) | ||
|
||
t.Run("override all defaults", func(t *testing.T) { | ||
graphCmd, _ := tf.graphCmd(context.Background(), | ||
GraphPlan("teststate"), | ||
DrawCycles(true), | ||
GraphType("output")) | ||
|
||
assertCmd(t, []string{ | ||
"graph", | ||
"teststate", | ||
"-draw-cycles", | ||
"-type=output", | ||
}, nil, graphCmd) | ||
}) | ||
} | ||
|
||
func TestGraphCmd15(t *testing.T) { | ||
td := t.TempDir() | ||
|
||
tf, err := NewTerraform(td, tfVersion(t, testutil.Latest015)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// empty env, to avoid environ mismatch in testing | ||
tf.SetEnv(map[string]string{}) | ||
|
||
t.Run("defaults", func(t *testing.T) { | ||
graphCmd, _ := tf.graphCmd(context.Background()) | ||
|
||
assertCmd(t, []string{ | ||
"graph", | ||
}, nil, graphCmd) | ||
}) | ||
|
||
t.Run("override all defaults", func(t *testing.T) { | ||
graphCmd, _ := tf.graphCmd(context.Background(), | ||
GraphPlan("teststate"), | ||
DrawCycles(true), | ||
GraphType("output")) | ||
|
||
assertCmd(t, []string{ | ||
"graph", | ||
"-plan=teststate", | ||
"-draw-cycles", | ||
"-type=output", | ||
}, nil, graphCmd) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package e2etest | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/go-version" | ||
|
||
"github.com/hashicorp/terraform-exec/tfexec" | ||
) | ||
|
||
func TestGraph(t *testing.T) { | ||
runTest(t, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { | ||
err := tf.Init(context.Background()) | ||
if err != nil { | ||
t.Fatalf("error running Init in test directory: %s", err) | ||
} | ||
|
||
err = tf.Apply(context.Background()) | ||
if err != nil { | ||
t.Fatalf("error running Apply: %s", err) | ||
} | ||
|
||
graphOutput, err := tf.Graph(context.Background()) | ||
if err != nil { | ||
t.Fatalf("error running Graph: %s", err) | ||
} | ||
|
||
// Graph output differs slightly between versions, but resource subgraph remains consistent | ||
if !strings.Contains(graphOutput, `"[root] null_resource.foo" [label = "null_resource.foo", shape = "box"]`) { | ||
t.Fatalf("error running Graph. Graph output does not contain expected strings. Returned: %s", graphOutput) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters