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

Add virtual machine manager #69

Closed
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/synology_dsm/api/virtual_machine_manager/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Synology Virtual Machine Manager API models."""
90 changes: 90 additions & 0 deletions src/synology_dsm/api/virtual_machine_manager/guest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"""DSM Virtual Machine data."""
from synology_dsm.helpers import SynoFormatHelper


class SynoVirtualMachineManager:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this class should be moved to __init__.py

"""Class containing Virtual Machine Guests"""

API_KEY = "SYNO.Virtualization.API.Guest"
ACTION_API_KEY = "SYNO.Virtualization.API.Guest.Action"

def __init__(self, dsm):
"""Constructor method."""
self._dsm = dsm
self._data = {}

def update(self):
"""Updates Virtual Machine Guest data."""
raw_data = self._dsm.get(self.API_KEY, "list")
if raw_data:
self._data = raw_data["data"]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should create an own class for the guest object (suggest SynoVmmGuest) which holds all the data and exposes properties (compare with surveillance_station)


# Root
@property
def guests(self):
"""Gets all Virtual Machines."""
return self._data.get("guests", [])

@property
def guest_ids(self):
"""Gets a Virtual Machines name."""
guests = []
for guest in self.guests:
guests.append(guest["guest_id"])
return guests

def get_guest(self, guest_id):
"""Returns a specific guest."""
for guest in self.guests:
if guest["guest_id"] == guest_id:
return guest
return {}

def guest_name(self, guest_id):
"""Return the name of this guest."""
return self.get_guest(guest_id).get("guest_name")

def guest_status(self, guest_id):
"""Gets Status of Guest (Shutdown, Running etc)"""
return self.get_guest(guest_id).get("status")

def guest_network_name(self, guest_id):
"""Gets Network Name of Guest."""
return self.get_guest(guest_id).get("network_name")

def poweron(self, guest_id, guest_name):
"""Power on a virtual machine"""
res = self._dsm.post(
self.ACTION_API_KEY,
"poweron",
{
"guest_id": guest_id,
"guest_name": guest_name,
},
)
self.update()

def shutdown(self, guest_id, guest_name):
"""Gracefully Shutdown a virtual machine"""
res = self._dsm.post(
self.ACTION_API_KEY,
"shutdown",
{
"guest_id": guest_id,
"guest_name": guest_name,
},
)
self.update()

def poweroff(self, guest_id, guest_name):
"""Force Shutdown a virtual machine"""
res = self._dsm.post(
self.ACTION_API_KEY,
"poweroff",
{
"guest_id": guest_id,
"guest_name": guest_name,
},
)
self.update()
return res
19 changes: 19 additions & 0 deletions src/synology_dsm/synology_dsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .api.dsm.network import SynoDSMNetwork
from .api.storage.storage import SynoStorage
from .api.surveillance_station import SynoSurveillanceStation
from .api.virtual_machine_manager.guest import SynoVirtualMachineManager
from .const import API_AUTH
from .const import API_INFO
from .const import SENSITIV_PARAMS
Expand Down Expand Up @@ -84,6 +85,8 @@ def __init__(
self._system = None
self._utilisation = None
self._upgrade = None
self._guests = None


# Build variables
if use_https:
Expand Down Expand Up @@ -362,6 +365,9 @@ def update(self, with_information: bool = False, with_network: bool = False):
if self._surveillance:
self._surveillance.update()

if self._guests:
self._guests.update()

if self._system:
self._system.update()

Expand Down Expand Up @@ -400,6 +406,9 @@ def reset(self, api: any) -> bool:
if api == SynoSurveillanceStation.API_KEY:
self._surveillance = None
return True
if api == SynoVirtualMachineManager.API_KEY:
self._guests = None
return True
if isinstance(api, SynoCoreSecurity):
self._security = None
return True
Expand All @@ -424,6 +433,9 @@ def reset(self, api: any) -> bool:
if isinstance(api, SynoSurveillanceStation):
self._surveillance = None
return True
if isinstance(api, SynoVirtualMachineManager):
self._guests = None
return True
return False

@property
Expand Down Expand Up @@ -495,3 +507,10 @@ def utilisation(self) -> SynoCoreUtilization:
if not self._utilisation:
self._utilisation = SynoCoreUtilization(self)
return self._utilisation

@property
def virtual_machine_manager(self) -> SynoVirtualMachineManager:
"""Gets NAS utilisation informations."""
if not self._guests:
self._guests = SynoVirtualMachineManager(self)
return self._guests