-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fixed some issues with the ensure processes where we could get nil pointers - Added a new process to delete vms that were created but the creation process failed - Added the enforce of the flag force_changes on the create where it will delete vms if the pre-exist - Fixed some issues with the getVms method where the id was not being escaped - Added a fix for the intel macs where we now add the user to the sudoers list if not present - Added a fix where if PD failed to install we would get an error with dependencies
- Loading branch information
Showing
13 changed files
with
340 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"time" | ||
|
||
"terraform-provider-parallels-desktop/internal/apiclient" | ||
"terraform-provider-parallels-desktop/internal/constants" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
) | ||
|
||
func EnsureMachineIsRemoved(ctx context.Context, hostConfig apiclient.HostConfig, vmIdOrName string) diag.Diagnostics { | ||
diagnostics := diag.Diagnostics{} | ||
maxRetries := constants.DEFAULT_OPERATION_MAX_RETRY_COUNT | ||
vmIdOrName = strings.ReplaceAll(vmIdOrName, "\"", "") | ||
retryCount := 0 | ||
for { | ||
diagnostics = diag.Diagnostics{} | ||
retryCount += 1 | ||
|
||
// We have run out of retries, add an error and break out of the loop | ||
if retryCount >= maxRetries { | ||
diagnostics.AddError("Error starting vm", "Could not verify the state of the machine after starting, retry count exceeded") | ||
break | ||
} | ||
|
||
currentVm, refreshDiags := apiclient.GetVm(ctx, hostConfig, vmIdOrName) | ||
if refreshDiags.HasError() { | ||
diagnostics.Append(refreshDiags...) | ||
continue | ||
} | ||
|
||
// If the machine is not found, return ok as we cannot remove something that does not exist | ||
if currentVm == nil { | ||
// We are waiting for half of the time to wait for the orchestrator to update | ||
time.Sleep(constants.DEFAULT_OPERATION_RETRY_INTERVAL_IN_SECONDS * time.Second) | ||
currentVm, refreshDiags := apiclient.GetVm(ctx, hostConfig, vmIdOrName) | ||
if refreshDiags.HasError() { | ||
diagnostics.Append(refreshDiags...) | ||
continue | ||
} | ||
if currentVm == nil { | ||
diagnostics = diag.Diagnostics{} | ||
break | ||
} | ||
} | ||
|
||
tflog.Info(ctx, "Checking if "+currentVm.Name+" is running") | ||
// making sure the VM is stopped before removing it | ||
if _, ensureStopped := EnsureMachineStopped(ctx, hostConfig, currentVm); ensureStopped.HasError() { | ||
diagnostics.Append(ensureStopped...) | ||
continue | ||
} | ||
|
||
// Refresh the state of the machine | ||
refreshedVm, refreshedVmDiag := apiclient.GetVm(ctx, hostConfig, currentVm.ID) | ||
if refreshedVmDiag.HasError() { | ||
diagnostics.Append(refreshedVmDiag...) | ||
continue | ||
} | ||
|
||
if refreshedVm == nil { | ||
// We are waiting for half of the time to wait for the orchestrator to update | ||
time.Sleep(constants.DEFAULT_OPERATION_RETRY_INTERVAL_IN_SECONDS * time.Second) | ||
refreshedVm, refreshDiags := apiclient.GetVm(ctx, hostConfig, vmIdOrName) | ||
if refreshDiags.HasError() { | ||
diagnostics.Append(refreshDiags...) | ||
continue | ||
} | ||
if refreshedVm == nil { | ||
diagnostics = diag.Diagnostics{} | ||
break | ||
} | ||
} | ||
|
||
// The machine is stopped, we can remove it | ||
if refreshedVm.State == "stopped" { | ||
tflog.Info(ctx, "Machine "+currentVm.Name+" is stopped, removing it") | ||
if removeDiag := apiclient.DeleteVm(ctx, hostConfig, refreshedVm.ID); removeDiag.HasError() { | ||
diagnostics.Append(removeDiag...) | ||
continue | ||
} | ||
// The machine has been removed, break out of the loop | ||
break | ||
} | ||
|
||
time.Sleep(constants.DEFAULT_OPERATION_RETRY_INTERVAL_IN_SECONDS * time.Second) | ||
} | ||
|
||
return diagnostics | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.