Skip to content

Commit

Permalink
show deprecation warning if -state is used with plan, apply, refresh (#…
Browse files Browse the repository at this point in the history
…35660)

* show deprecation warning if -state is used with plan, apply, refresh

* show deprecation warning if -state is used with plan, apply, refresh

* updated -state flag check condition

* added better diagnostic details view

* resolved failed test

* updated the content of the -state flag warning
  • Loading branch information
melsonic authored Sep 5, 2024
1 parent e312ffc commit 2a9a8c2
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 13 deletions.
3 changes: 2 additions & 1 deletion internal/command/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ func (c *ApplyCommand) Run(rawArgs []string) int {
}

// Attempt to load the plan file, if specified
planFile, diags := c.LoadPlanFile(args.PlanPath)
planFile, loadPlanFileDiags := c.LoadPlanFile(args.PlanPath)
diags = diags.Append(loadPlanFileDiags)
if diags.HasErrors() {
view.Diagnostics(diags)
return 1
Expand Down
8 changes: 8 additions & 0 deletions internal/command/arguments/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ func ParseApply(args []string) (*Apply, tfdiags.Diagnostics) {
))
}

if apply.State.StatePath != "" {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Warning,
"Deprecated flag: -state",
"Use `path` attribute within the `local` backend instead: https://developer.hashicorp.com/terraform/language/v1.10.x/settings/backends/local#path",
))
}

args = cmdFlags.Args()
if len(args) > 0 {
apply.PlanPath = args[0]
Expand Down
12 changes: 6 additions & 6 deletions internal/command/arguments/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestParseApply_basicValid(t *testing.T) {
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
got, diags := ParseApply(tc.args)
if len(diags) > 0 {
if len(diags) > 0 && diags.HasErrors() {
t.Fatalf("unexpected diags: %v", diags)
}
if diff := cmp.Diff(tc.want, got, cmpOpts); diff != "" {
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestParseApply_json(t *testing.T) {
got, diags := ParseApply(tc.args)

if tc.wantSuccess {
if len(diags) > 0 {
if len(diags) > 0 && diags.HasErrors() {
t.Errorf("unexpected diags: %v", diags)
}
} else {
Expand Down Expand Up @@ -200,7 +200,7 @@ func TestParseApply_targets(t *testing.T) {
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
got, diags := ParseApply(tc.args)
if len(diags) > 0 {
if len(diags) > 0 && diags.HasErrors() {
if tc.wantErr == "" {
t.Fatalf("unexpected diags: %v", diags)
} else if got := diags.Err().Error(); !strings.Contains(got, tc.wantErr) {
Expand Down Expand Up @@ -259,7 +259,7 @@ func TestParseApply_replace(t *testing.T) {
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
got, diags := ParseApply(tc.args)
if len(diags) > 0 {
if len(diags) > 0 && diags.HasErrors() {
if tc.wantErr == "" {
t.Fatalf("unexpected diags: %v", diags)
} else if got := diags.Err().Error(); !strings.Contains(got, tc.wantErr) {
Expand Down Expand Up @@ -311,7 +311,7 @@ func TestParseApply_vars(t *testing.T) {
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
got, diags := ParseApply(tc.args)
if len(diags) > 0 {
if len(diags) > 0 && diags.HasErrors() {
t.Fatalf("unexpected diags: %v", diags)
}
if vars := got.Vars.All(); !cmp.Equal(vars, tc.want) {
Expand Down Expand Up @@ -366,7 +366,7 @@ func TestParseApplyDestroy_basicValid(t *testing.T) {
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
got, diags := ParseApplyDestroy(tc.args)
if len(diags) > 0 {
if len(diags) > 0 && diags.HasErrors() {
t.Fatalf("unexpected diags: %v", diags)
}
if diff := cmp.Diff(tc.want, got, cmpOpts); diff != "" {
Expand Down
8 changes: 8 additions & 0 deletions internal/command/arguments/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ func ParsePlan(args []string) (*Plan, tfdiags.Diagnostics) {
))
}

if plan.State.StatePath != "" {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Warning,
"Deprecated flag: -state",
"Use `path` attribute within the `local` backend instead: https://developer.hashicorp.com/terraform/language/v1.10.x/settings/backends/local#path",
))
}

args = cmdFlags.Args()

if len(args) > 0 {
Expand Down
6 changes: 3 additions & 3 deletions internal/command/arguments/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestParsePlan_basicValid(t *testing.T) {
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
got, diags := ParsePlan(tc.args)
if len(diags) > 0 {
if len(diags) > 0 && diags.HasErrors() {
t.Fatalf("unexpected diags: %v", diags)
}
if diff := cmp.Diff(tc.want, got, cmpOpts); diff != "" {
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestParsePlan_targets(t *testing.T) {
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
got, diags := ParsePlan(tc.args)
if len(diags) > 0 {
if len(diags) > 0 && diags.HasErrors() {
if tc.wantErr == "" {
t.Fatalf("unexpected diags: %v", diags)
} else if got := diags.Err().Error(); !strings.Contains(got, tc.wantErr) {
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestParsePlan_vars(t *testing.T) {
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
got, diags := ParsePlan(tc.args)
if len(diags) > 0 {
if len(diags) > 0 && diags.HasErrors() {
t.Fatalf("unexpected diags: %v", diags)
}
if vars := got.Vars.All(); !cmp.Equal(vars, tc.want) {
Expand Down
8 changes: 8 additions & 0 deletions internal/command/arguments/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ func ParseRefresh(args []string) (*Refresh, tfdiags.Diagnostics) {
))
}

if refresh.State.StatePath != "" {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Warning,
"Deprecated flag: -state",
"Use `path` attribute within the `local` backend instead: https://developer.hashicorp.com/terraform/language/v1.10.x/settings/backends/local#path",
))
}

args = cmdFlags.Args()
if len(args) > 0 {
diags = diags.Append(tfdiags.Sourceless(
Expand Down
6 changes: 3 additions & 3 deletions internal/command/arguments/refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestParseRefresh_basicValid(t *testing.T) {
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
got, diags := ParseRefresh(tc.args)
if len(diags) > 0 {
if len(diags) > 0 && diags.HasErrors() {
t.Fatalf("unexpected diags: %v", diags)
}
// Ignore the extended arguments for simplicity
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestParseRefresh_targets(t *testing.T) {
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
got, diags := ParseRefresh(tc.args)
if len(diags) > 0 {
if len(diags) > 0 && diags.HasErrors() {
if tc.wantErr == "" {
t.Fatalf("unexpected diags: %v", diags)
} else if got := diags.Err().Error(); !strings.Contains(got, tc.wantErr) {
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestParseRefresh_vars(t *testing.T) {
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
got, diags := ParseRefresh(tc.args)
if len(diags) > 0 {
if len(diags) > 0 && diags.HasErrors() {
t.Fatalf("unexpected diags: %v", diags)
}
if vars := got.Vars.All(); !cmp.Equal(vars, tc.want) {
Expand Down
1 change: 1 addition & 0 deletions internal/command/testdata/apply/output.jsonlog
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{"@level":"info","@message":"Terraform 0.15.0-dev","@module":"terraform.ui","terraform":"0.15.0-dev","type":"version","ui":"0.1.0"}
{"@level":"warn","@message":"Warning: Deprecated flag: -state","@module":"terraform.ui","diagnostic":{"detail":"Use `path` attribute within the `local` backend instead: https://developer.hashicorp.com/terraform/language/v1.10.x/settings/backends/local#path","severity":"warning","summary":"Deprecated flag: -state"},"type":"diagnostic"}
{"@level":"info","@message":"test_instance.foo: Plan to create","@module":"terraform.ui","change":{"resource":{"addr":"test_instance.foo","module":"","resource":"test_instance.foo","implied_provider":"test","resource_type":"test_instance","resource_name":"foo","resource_key":null},"action":"create"},"type":"planned_change"}
{"@level":"info","@message":"Plan: 1 to add, 0 to change, 0 to destroy.","@module":"terraform.ui","changes":{"add":1,"import":0,"change":0,"remove":0,"operation":"plan"},"type":"change_summary"}
{"@level":"info","@message":"test_instance.foo: Creating...","@module":"terraform.ui","hook":{"resource":{"addr":"test_instance.foo","module":"","resource":"test_instance.foo","implied_provider":"test","resource_type":"test_instance","resource_name":"foo","resource_key":null},"action":"create"},"type":"apply_start"}
Expand Down

0 comments on commit 2a9a8c2

Please sign in to comment.