Skip to content

Commit

Permalink
Merge pull request #365 from digitronik/hot-add
Browse files Browse the repository at this point in the history
[RFR][VirtualCenter] Added method set hot add for cpu and memory
  • Loading branch information
mshriver authored Mar 25, 2019
2 parents a80743d + bb827f0 commit c3ef859
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
12 changes: 12 additions & 0 deletions wrapanapi/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,15 @@ class VMError(Exception):
class VMCreationDateError(Exception):
"""Raised when we cannot determine a creation date for a VM"""
pass


class VMInstanceNotStopped(Exception):
"""Raised if a VM or instance is not in stopped state."""
def __init__(self, vm_name, action="action"):
self.vm_name = vm_name
self.action = action

def __str__(self):
return "Could not perform '{a}' on '{vm}' because it's not stopped.".format(
a=self.action, vm=self.vm_name
)
54 changes: 52 additions & 2 deletions wrapanapi/systems/virtualcenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
from wrapanapi.entities.base import Entity
from wrapanapi.exceptions import (HostNotRemoved, NotFoundError,
VMCreationDateError, VMInstanceNotCloned,
VMInstanceNotFound, VMInstanceNotSuspended,
VMNotFoundViaIP)
VMInstanceNotFound, VMInstanceNotStopped,
VMInstanceNotSuspended, VMNotFoundViaIP)
from wrapanapi.systems.base import System


Expand Down Expand Up @@ -492,6 +492,56 @@ def creation_time(self):
# localize and make tz-naive
return creation_time.astimezone(pytz.UTC)

@property
def cpu_hot_plug(self):
return self.raw.config.cpuHotAddEnabled

@property
def memory_hot_plug(self):
return self.raw.config.memoryHotAddEnabled

@cpu_hot_plug.setter
def cpu_hot_plug(self, value):
"""
Set cpuHotPlug (enabled/disabled) for VM/Instance.
Args:
value (bool): cpu hot plug state
"""
if self.cpu_hot_plug != value:
if self.is_stopped:
spec = vim.vm.ConfigSpec()
spec.cpuHotAddEnabled = value
task = self.raw.ReconfigVM_Task(spec)

try:
wait_for(lambda: task.info.state not in ["running", "queued"])
except TimedOutError:
self.logger.exception("Task did not go to success state: %s", task)
else:
raise VMInstanceNotStopped(self.name, "cpuHotPlug")

@memory_hot_plug.setter
def memory_hot_plug(self, value):
"""
Set memoryHotPlug (enabled/disabled) for VM/Instance
Args:
value (bool): memory hot plug state
"""
if self.memory_hot_plug != value:
if self.is_stopped:
spec = vim.vm.ConfigSpec()
spec.memoryHotAddEnabled = value
task = self.raw.ReconfigVM_Task(spec)

try:
wait_for(lambda: task.info.state not in ["running", "queued"])
except TimedOutError:
self.logger.exception("Task did not go to success state: %s", task)
else:
raise VMInstanceNotStopped(self.name, "memoryHotPlug")

def start(self):
if self.is_running:
self.logger.info(" vSphere VM %s is already running", self.name)
Expand Down

0 comments on commit c3ef859

Please sign in to comment.