diff --git a/internal/provider/navigator_run_resource.go b/internal/provider/navigator_run_resource.go index 0683a05..f21b23d 100644 --- a/internal/provider/navigator_run_resource.go +++ b/internal/provider/navigator_run_resource.go @@ -23,6 +23,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/types/basetypes" + "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/marshallford/terraform-provider-ansible/pkg/ansible" ) @@ -590,6 +591,8 @@ func (r *NavigatorRunResource) ModifyPlan(ctx context.Context, req resource.Modi } if !r.ShouldRun(data, state) { + tflog.Debug(ctx, "skipping run") + return } @@ -630,6 +633,8 @@ func (r *NavigatorRunResource) Create(ctx context.Context, req resource.CreateRe return } + tflog.SetField(ctx, "runs", runs) + timeout, newDiags := terraformOperationTimeout(ctx, terraformOperationCreate, data.Timeouts, defaultNavigatorRunTimeout) resp.Diagnostics.Append(newDiags...) @@ -649,7 +654,7 @@ func (r *NavigatorRunResource) Create(ctx context.Context, req resource.CreateRe return } - run(&resp.Diagnostics, timeout, terraformOperationCreate, &navigatorRun) + run(ctx, &resp.Diagnostics, timeout, terraformOperationCreate, &navigatorRun) resp.Diagnostics.Append(data.Set(ctx, navigatorRun)...) if resp.Diagnostics.HasError() { @@ -679,6 +684,8 @@ func (r *NavigatorRunResource) Update(ctx context.Context, req resource.UpdateRe }() if !r.ShouldRun(data, state) { + tflog.Debug(ctx, "skipping run") + return } @@ -688,6 +695,8 @@ func (r *NavigatorRunResource) Update(ctx context.Context, req resource.UpdateRe return } + tflog.SetField(ctx, "runs", runs) + timeout, newDiags := terraformOperationTimeout(ctx, terraformOperationUpdate, data.Timeouts, defaultNavigatorRunTimeout) resp.Diagnostics.Append(newDiags...) @@ -705,7 +714,7 @@ func (r *NavigatorRunResource) Update(ctx context.Context, req resource.UpdateRe return } - run(&resp.Diagnostics, timeout, terraformOperationUpdate, &navigatorRun) + run(ctx, &resp.Diagnostics, timeout, terraformOperationUpdate, &navigatorRun) resp.Diagnostics.Append(data.Set(ctx, navigatorRun)...) if resp.Diagnostics.HasError() { @@ -723,6 +732,8 @@ func (r *NavigatorRunResource) Delete(ctx context.Context, req resource.DeleteRe } if !data.RunOnDestroy.ValueBool() { + tflog.Debug(ctx, "skipping run, 'run_on_destroy' disabled") + return } @@ -732,6 +743,8 @@ func (r *NavigatorRunResource) Delete(ctx context.Context, req resource.DeleteRe return } + tflog.SetField(ctx, "runs", runs) + timeout, newDiags := terraformOperationTimeout(ctx, terraformOperationDelete, data.Timeouts, defaultNavigatorRunTimeout) resp.Diagnostics.Append(newDiags...) @@ -749,5 +762,5 @@ func (r *NavigatorRunResource) Delete(ctx context.Context, req resource.DeleteRe return } - run(&resp.Diagnostics, timeout, terraformOperationDelete, &navigatorRun) + run(ctx, &resp.Diagnostics, timeout, terraformOperationDelete, &navigatorRun) } diff --git a/internal/provider/navigator_run_resource_test.go b/internal/provider/navigator_run_resource_test.go index d072f82..0297a6b 100644 --- a/internal/provider/navigator_run_resource_test.go +++ b/internal/provider/navigator_run_resource_test.go @@ -352,9 +352,9 @@ func TestAccNavigatorRun_errors(t *testing.T) { t.Run(test.name, func(t *testing.T) { t.Parallel() - variables := testDefaultConfigVariables(t) + variables := config.Variables{} if test.variables != nil { - variables = testConfigVariables(t, test.variables(t)) + variables = test.variables(t) } resource.Test(t, resource.TestCase{ @@ -363,7 +363,7 @@ func TestAccNavigatorRun_errors(t *testing.T) { Steps: []resource.TestStep{ { Config: testTerraformFile(t, filepath.Join("navigator_run", "errors", test.name)), - ConfigVariables: variables, + ConfigVariables: testConfigVariables(t, variables), ExpectError: test.expected, }, }, diff --git a/internal/provider/run.go b/internal/provider/run.go index 57395d6..b08b66d 100644 --- a/internal/provider/run.go +++ b/internal/provider/run.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/path" + "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/marshallford/terraform-provider-ansible/pkg/ansible" ) @@ -75,21 +76,31 @@ type navigatorRun struct { command string } -func run(diags *diag.Diagnostics, timeout time.Duration, operation terraformOperation, run *navigatorRun) { +func run(ctx context.Context, diags *diag.Diagnostics, timeout time.Duration, operation terraformOperation, run *navigatorRun) { var err error + ctx = tflog.SetField(ctx, "dir", run.dir) + ctx = tflog.SetField(ctx, "workingDir", run.workingDir) + tflog.Debug(ctx, "starting run") + + tflog.Trace(ctx, "directory preflight") err = ansible.DirectoryPreflight(run.workingDir) addPathError(diags, path.Root("working_directory"), "Working directory preflight check", err) + tflog.Trace(ctx, "container engine preflight") err = ansible.ContainerEnginePreflight(run.navigatorSettings.ContainerEngine) addPathError(diags, path.Root("execution_environment").AtMapKey("container_engine"), "Container engine preflight check", err) - binary, err := ansible.NavigatorPath(run.navigatorBinary) + tflog.Trace(ctx, "navigator path preflight") + binary, err := ansible.NavigatorPathPreflight(run.navigatorBinary) addPathError(diags, path.Root("ansible_navigator_binary"), "Ansible navigator not found", err) + tflog.Trace(ctx, "navigator preflight") err = ansible.NavigatorPreflight(binary) addPathError(diags, path.Root("ansible_navigator_binary"), "Ansible navigator preflight check", err) + tflog.Trace(ctx, "creating directories and files") + err = ansible.CreateRunDir(run.dir) addError(diags, "Run directory not created", err) @@ -111,13 +122,6 @@ func run(diags *diag.Diagnostics, timeout time.Duration, operation terraformOper err = ansible.CreateNavigatorSettingsFile(run.dir, navigatorSettingsContents) addError(diags, "Ansible navigator settings file not created", err) - command := ansible.GenerateNavigatorRunCommand( - run.dir, - run.workingDir, - binary, - &run.options, - ) - if diags.HasError() { if !run.persistDir { err = ansible.RemoveRunDir(run.dir) @@ -127,6 +131,13 @@ func run(diags *diag.Diagnostics, timeout time.Duration, operation terraformOper return } + command := ansible.GenerateNavigatorRunCommand( + run.dir, + run.workingDir, + binary, + &run.options, + ) + run.command = command.String() commandOutput, err := ansible.ExecNavigatorRunCommand(command) diff --git a/pkg/ansible/navigator.go b/pkg/ansible/navigator.go index 05f6fe4..1f7023e 100644 --- a/pkg/ansible/navigator.go +++ b/pkg/ansible/navigator.go @@ -83,7 +83,7 @@ func ContainerEnginePreflight(containerEngine string) error { return nil } -func NavigatorPath(path string) (string, error) { +func NavigatorPathPreflight(path string) (string, error) { if path == "" { path, err := exec.LookPath(NavigatorProgram) if err != nil {