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

fix: don't assume driver isn't nil in StepConnect #437

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Changes from all 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
28 changes: 20 additions & 8 deletions builder/vsphere/common/step_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"context"
"fmt"
"log"
"reflect"

"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
Expand Down Expand Up @@ -68,14 +69,25 @@ func (s *StepConnect) Run(_ context.Context, state multistep.StateBag) multistep

func (s *StepConnect) Cleanup(state multistep.StateBag) {
ui := state.Get("ui").(packersdk.Ui)
d, ok := state.GetOk("driver")
if !ok {
log.Printf("[INFO] No driver in state; nothing to cleanup.")
return
}

driver, ok := d.(driver.Driver)
if !ok {
log.Printf("[ERROR] The object stored in the state under 'driver' key is of type '%s', not 'driver.Driver'. This could indicate a problem with the state initialization or management.", reflect.TypeOf(d))
return
}

ui.Message("Closing sessions ....")
if driver, ok := state.Get("driver").(driver.Driver); ok {
errorRestClient, errorSoapClient := driver.Cleanup()
if errorRestClient != nil {
log.Printf("[WARN] Failed to close REST client session. The session may already be closed: %s", errorRestClient.Error())
}
if errorSoapClient != nil {
log.Printf("[WARN] Failed to close SOAP client session. The session may already be closed: %s", errorSoapClient.Error())
}

errorRestClient, errorSoapClient := driver.Cleanup()
if errorRestClient != nil {
log.Printf("[WARN] Failed to close REST client session. The session may already be closed: %s", errorRestClient.Error())
}
if errorSoapClient != nil {
log.Printf("[WARN] Failed to close SOAP client session. The session may already be closed: %s", errorSoapClient.Error())
}
}