Skip to content

Commit

Permalink
adds debug logging (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallford authored Aug 4, 2024
1 parent f826626 commit 8f82010
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
19 changes: 16 additions & 3 deletions internal/provider/navigator_run_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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...)

Expand All @@ -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() {
Expand Down Expand Up @@ -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
}

Expand All @@ -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...)

Expand All @@ -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() {
Expand All @@ -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
}

Expand All @@ -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...)

Expand All @@ -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)
}
6 changes: 3 additions & 3 deletions internal/provider/navigator_run_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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,
},
},
Expand Down
29 changes: 20 additions & 9 deletions internal/provider/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/ansible/navigator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8f82010

Please sign in to comment.