Skip to content

Commit

Permalink
PTFE-1959 support deletion of non-existent vsphere vm
Browse files Browse the repository at this point in the history
  • Loading branch information
tcarmet committed Aug 29, 2024
1 parent 2d4bb12 commit a6819cd
Show file tree
Hide file tree
Showing 3 changed files with 1,872 additions and 1 deletion.
8 changes: 7 additions & 1 deletion runner_manager/backend/vsphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from com.vmware.content.library_client import Item
from com.vmware.content_client import Library
from com.vmware.vapi.std.errors_client import NotFound
from com.vmware.vcenter.ovf_client import (
DiskProvisioningType,
LibraryItem,
Expand Down Expand Up @@ -167,7 +168,12 @@ def create(self, runner: Runner) -> Runner:
def delete(self, runner: Runner):
client = self._create_client()
if runner.instance_id is not None:
state = client.vcenter.vm.Power.get(runner.instance_id)
try:
state = client.vcenter.vm.Power.get(runner.instance_id)
log.debug(f"VM {runner.name} state: {state}")
except NotFound:
log.info(f"VM {runner.name} not found.")
return super().delete(runner)
if state == Power.Info(state=Power.State.POWERED_ON):
client.vcenter.vm.Power.stop(runner.instance_id)
elif state == Power.Info(state=Power.State.SUSPENDED):
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/backend/test_vsphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def vsphere_group(settings) -> RunnerGroup:

@mark.skipif(not os.getenv("GOVC_URL"), reason="GOVC_URL environment variable not set")
def test_vsphere_client(vsphere_group: RunnerGroup, runner: Runner):
# deleting the runner should not raise an exception
runner.instance_id = "i-1234"
vsphere_group.backend.delete(runner)
runner = vsphere_group.backend.create(runner)
assert runner.instance_id is not None
vsphere_group.backend.delete(runner)
Loading

0 comments on commit a6819cd

Please sign in to comment.