-
Notifications
You must be signed in to change notification settings - Fork 11
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
realtek2017
wants to merge
3
commits into
mib1185:master
from
realtek2017:add-virtual-machine-manager
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Synology Virtual Machine Manager API models.""" |
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,90 @@ | ||
"""DSM Virtual Machine data.""" | ||
from synology_dsm.helpers import SynoFormatHelper | ||
|
||
|
||
class SynoVirtualMachineManager: | ||
"""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"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should create an own class for the guest object (suggest |
||
|
||
# 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 |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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