forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
591 additions
and
201 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,75 @@ | ||
package arguments | ||
|
||
import ( | ||
"github.com/hashicorp/terraform/tfdiags" | ||
) | ||
|
||
// Plan represents the command-line arguments for the plan command. | ||
type Plan struct { | ||
// State, Operation, and Vars are the common extended flags | ||
State *State | ||
Operation *Operation | ||
Vars *Vars | ||
|
||
// Destroy can be set to generate a plan to destroy all infrastructure. | ||
Destroy bool | ||
|
||
// DetailedExitCode enables different exit codes for error, success with | ||
// changes, and success with no changes. | ||
DetailedExitCode bool | ||
|
||
// InputEnabled is used to disable interactive input for unspecified | ||
// variable and backend config values. Default is true. | ||
InputEnabled bool | ||
|
||
// OutPath contains an optional path to store the plan file | ||
OutPath string | ||
|
||
// ViewType specifies which output format to use | ||
ViewType ViewType | ||
} | ||
|
||
// ParsePlan processes CLI arguments, returning a Plan value and errors. | ||
// If errors are encountered, a Plan value is still returned representing | ||
// the best effort interpretation of the arguments. | ||
func ParsePlan(args []string) (*Plan, tfdiags.Diagnostics) { | ||
var diags tfdiags.Diagnostics | ||
plan := &Plan{ | ||
State: &State{}, | ||
Operation: &Operation{}, | ||
Vars: &Vars{}, | ||
} | ||
|
||
cmdFlags := extendedFlagSet("plan", plan.State, plan.Operation, plan.Vars) | ||
cmdFlags.BoolVar(&plan.Destroy, "destroy", false, "destroy") | ||
cmdFlags.BoolVar(&plan.DetailedExitCode, "detailed-exitcode", false, "detailed-exitcode") | ||
cmdFlags.BoolVar(&plan.InputEnabled, "input", true, "input") | ||
cmdFlags.StringVar(&plan.OutPath, "out", "", "out") | ||
|
||
if err := cmdFlags.Parse(args); err != nil { | ||
diags = diags.Append(tfdiags.Sourceless( | ||
tfdiags.Error, | ||
"Failed to parse command-line flags", | ||
err.Error(), | ||
)) | ||
} | ||
|
||
args = cmdFlags.Args() | ||
|
||
if len(args) > 0 { | ||
diags = diags.Append(tfdiags.Sourceless( | ||
tfdiags.Error, | ||
"Too many command line arguments", | ||
"To specify a working directory for the plan, use the global -chdir flag.", | ||
)) | ||
} | ||
|
||
diags = diags.Append(plan.Operation.Parse()) | ||
|
||
switch { | ||
default: | ||
plan.ViewType = ViewHuman | ||
} | ||
|
||
return plan, diags | ||
} |
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,179 @@ | ||
package arguments | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/hashicorp/terraform/addrs" | ||
) | ||
|
||
func TestParsePlan_basicValid(t *testing.T) { | ||
testCases := map[string]struct { | ||
args []string | ||
want *Plan | ||
}{ | ||
"defaults": { | ||
nil, | ||
&Plan{ | ||
Destroy: false, | ||
DetailedExitCode: false, | ||
InputEnabled: true, | ||
OutPath: "", | ||
ViewType: ViewHuman, | ||
}, | ||
}, | ||
"setting all options": { | ||
[]string{"-destroy", "-detailed-exitcode", "-input=false", "-out=saved.tfplan"}, | ||
&Plan{ | ||
Destroy: true, | ||
DetailedExitCode: true, | ||
InputEnabled: false, | ||
OutPath: "saved.tfplan", | ||
ViewType: ViewHuman, | ||
}, | ||
}, | ||
} | ||
|
||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
got, diags := ParsePlan(tc.args) | ||
if len(diags) > 0 { | ||
t.Fatalf("unexpected diags: %v", diags) | ||
} | ||
// Ignore the extended arguments for simplicity | ||
got.State = nil | ||
got.Operation = nil | ||
got.Vars = nil | ||
if *got != *tc.want { | ||
t.Fatalf("unexpected result\n got: %#v\nwant: %#v", got, tc.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestParsePlan_invalid(t *testing.T) { | ||
got, diags := ParsePlan([]string{"-frob"}) | ||
if len(diags) == 0 { | ||
t.Fatal("expected diags but got none") | ||
} | ||
if got, want := diags.Err().Error(), "flag provided but not defined"; !strings.Contains(got, want) { | ||
t.Fatalf("wrong diags\n got: %s\nwant: %s", got, want) | ||
} | ||
if got.ViewType != ViewHuman { | ||
t.Fatalf("wrong view type, got %#v, want %#v", got.ViewType, ViewHuman) | ||
} | ||
} | ||
|
||
func TestParsePlan_tooManyArguments(t *testing.T) { | ||
got, diags := ParsePlan([]string{"saved.tfplan"}) | ||
if len(diags) == 0 { | ||
t.Fatal("expected diags but got none") | ||
} | ||
if got, want := diags.Err().Error(), "Too many command line arguments"; !strings.Contains(got, want) { | ||
t.Fatalf("wrong diags\n got: %s\nwant: %s", got, want) | ||
} | ||
if got.ViewType != ViewHuman { | ||
t.Fatalf("wrong view type, got %#v, want %#v", got.ViewType, ViewHuman) | ||
} | ||
} | ||
|
||
func TestParsePlan_targets(t *testing.T) { | ||
foobarbaz, _ := addrs.ParseTargetStr("foo_bar.baz") | ||
boop, _ := addrs.ParseTargetStr("module.boop") | ||
testCases := map[string]struct { | ||
args []string | ||
want []addrs.Targetable | ||
wantErr string | ||
}{ | ||
"no targets by default": { | ||
args: nil, | ||
want: nil, | ||
}, | ||
"one target": { | ||
args: []string{"-target=foo_bar.baz"}, | ||
want: []addrs.Targetable{foobarbaz.Subject}, | ||
}, | ||
"two targets": { | ||
args: []string{"-target=foo_bar.baz", "-target", "module.boop"}, | ||
want: []addrs.Targetable{foobarbaz.Subject, boop.Subject}, | ||
}, | ||
"invalid traversal": { | ||
args: []string{"-target=foo."}, | ||
want: nil, | ||
wantErr: "Dot must be followed by attribute name", | ||
}, | ||
"invalid target": { | ||
args: []string{"-target=data[0].foo"}, | ||
want: nil, | ||
wantErr: "A data source name is required", | ||
}, | ||
} | ||
|
||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
got, diags := ParsePlan(tc.args) | ||
if len(diags) > 0 { | ||
if tc.wantErr == "" { | ||
t.Fatalf("unexpected diags: %v", diags) | ||
} else if got := diags.Err().Error(); !strings.Contains(got, tc.wantErr) { | ||
t.Fatalf("wrong diags\n got: %s\nwant: %s", got, tc.wantErr) | ||
} | ||
} | ||
if !cmp.Equal(got.Operation.Targets, tc.want) { | ||
t.Fatalf("unexpected result\n%s", cmp.Diff(got.Operation.Targets, tc.want)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestParsePlan_vars(t *testing.T) { | ||
testCases := map[string]struct { | ||
args []string | ||
want []FlagNameValue | ||
}{ | ||
"no var flags by default": { | ||
args: nil, | ||
want: nil, | ||
}, | ||
"one var": { | ||
args: []string{"-var", "foo=bar"}, | ||
want: []FlagNameValue{ | ||
{Name: "-var", Value: "foo=bar"}, | ||
}, | ||
}, | ||
"one var-file": { | ||
args: []string{"-var-file", "cool.tfvars"}, | ||
want: []FlagNameValue{ | ||
{Name: "-var-file", Value: "cool.tfvars"}, | ||
}, | ||
}, | ||
"ordering preserved": { | ||
args: []string{ | ||
"-var", "foo=bar", | ||
"-var-file", "cool.tfvars", | ||
"-var", "boop=beep", | ||
}, | ||
want: []FlagNameValue{ | ||
{Name: "-var", Value: "foo=bar"}, | ||
{Name: "-var-file", Value: "cool.tfvars"}, | ||
{Name: "-var", Value: "boop=beep"}, | ||
}, | ||
}, | ||
} | ||
|
||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
got, diags := ParsePlan(tc.args) | ||
if len(diags) > 0 { | ||
t.Fatalf("unexpected diags: %v", diags) | ||
} | ||
if vars := got.Vars.All(); !cmp.Equal(vars, tc.want) { | ||
t.Fatalf("unexpected result\n%s", cmp.Diff(vars, tc.want)) | ||
} | ||
if got, want := got.Vars.Empty(), len(tc.want) == 0; got != want { | ||
t.Fatalf("expected Empty() to return %t, but was %t", want, got) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.