Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/command/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func (c *ApplyCommand) Run(rawArgs []string) int {
return 1
}

if diags.HasWarnings() {
view.Diagnostics(diags)
diags = nil
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this, the warning should still be printed whenever the diagnostics are chosen to be delayed.

Is there something else that is dropping / ignoring the warning diagnostics later?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it is required to print the diagnostics before, since on line number 73 planFile, diags := c.LoadPlanFile(args.PlanPath) returns a new diagnostic slice, it doesn't append to the already available diags variable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, I think we can change the other part instead of doing this 👍 It's more in keeping with the way the diagnostics are intended to work to just gather them up and report once.

You can change line 73 to something like:

	planFile, moreDiags := c.LoadPlanFile(args.PlanPath)
        diags = diags.Append(moreDiags)
	if moreDiags.HasErrors() {
		view.Diagnostics(diags)
		return 1
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, I think we can change the other part instead of doing this 👍 It's more in keeping with the way the diagnostics are intended to work to just gather them up and report once.

You can change line 73 to something like:

	planFile, moreDiags := c.LoadPlanFile(args.PlanPath)
        diags = diags.Append(moreDiags)
	if moreDiags.HasErrors() {
		view.Diagnostics(diags)
		return 1
	}

Yeah, this can be done.

// Check for user-supplied plugin path
var err error
if c.pluginPath, err = c.loadPluginPath(); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/command/arguments/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func ParseApply(args []string) (*Apply, tfdiags.Diagnostics) {
}

cmdFlags := extendedFlagSet("apply", apply.State, apply.Operation, apply.Vars)
if apply.State != nil {
diags = append(diags, tfdiags.SimpleWarning("state is deprecated"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice if this error message could present an alternative instead.

The correct thing to do is update the configuration to point to the chosen state file: #35562 (comment)

Maybe we could make this a proper diagnostic with the detail stating to use the path variable in the local backend?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will update and add a commit. And also, I have found a better way to check if the state flag is specified, will add this also in the next commit.

Copy link
Contributor Author

@melsonic melsonic Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @liamcervante
image
Does this align with your thoughts? I also added the link to the resource for easier navigation.

}
cmdFlags.BoolVar(&apply.AutoApprove, "auto-approve", false, "auto-approve")
cmdFlags.BoolVar(&apply.InputEnabled, "input", true, "input")

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
3 changes: 3 additions & 0 deletions internal/command/arguments/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func ParsePlan(args []string) (*Plan, tfdiags.Diagnostics) {
}

cmdFlags := extendedFlagSet("plan", plan.State, plan.Operation, plan.Vars)
if plan.State != nil {
diags = append(diags, tfdiags.SimpleWarning("state is deprecated"))
}
cmdFlags.BoolVar(&plan.DetailedExitCode, "detailed-exitcode", false, "detailed-exitcode")
cmdFlags.BoolVar(&plan.InputEnabled, "input", true, "input")
cmdFlags.StringVar(&plan.OutPath, "out", "", "out")
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
3 changes: 3 additions & 0 deletions internal/command/arguments/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func ParseRefresh(args []string) (*Refresh, tfdiags.Diagnostics) {
}

cmdFlags := extendedFlagSet("refresh", refresh.State, refresh.Operation, refresh.Vars)
if refresh.State != nil {
diags = append(diags, tfdiags.SimpleWarning("state is deprecated"))
}
cmdFlags.BoolVar(&refresh.InputEnabled, "input", true, "input")

var json bool
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: state is deprecated","@module":"terraform.ui","diagnostic":{"detail":"","severity":"warning","summary":"state is deprecated"},"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
1 change: 1 addition & 0 deletions internal/command/testdata/plan/output.jsonlog
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{"@level":"info","@message":"Terraform 1.3.0-dev","@module":"terraform.ui","terraform":"1.3.0-dev","type":"version","ui":"1.0"}
{"@level":"warn","@message":"Warning: state is deprecated","@module":"terraform.ui","diagnostic":{"detail":"","severity":"warning","summary":"state is deprecated"},"type":"diagnostic"}
{"@level":"info","@message":"data.test_data_source.a: Refreshing...","@module":"terraform.ui","hook":{"resource":{"addr":"data.test_data_source.a","module":"","resource":"data.test_data_source.a","implied_provider":"test","resource_type":"test_data_source","resource_name":"a","resource_key":null},"action":"read"},"type":"apply_start"}
{"@level":"info","@message":"data.test_data_source.a: Refresh complete after 0s [id=zzzzz]","@module":"terraform.ui","hook":{"resource":{"addr":"data.test_data_source.a","module":"","resource":"data.test_data_source.a","implied_provider":"test","resource_type":"test_data_source","resource_name":"a","resource_key":null},"action":"read","id_key":"id","id_value":"zzzzz","elapsed_seconds":0},"type":"apply_complete"}
{"@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"}
Expand Down
Loading