Correctly process node.updated event on controller

This commit is contained in:
Julien Duponchelle committed 2016-05-18 16:12:57 +02:00
1 parent 694e1a2e68
commit fa0af7f4a2
9 files changed
+118 -33

No files matched your search

-1
View File
@@ -188,4 +188,3 @@ class Controller:
if not hasattr(Controller, '_instance') or Controller._instance is None:
Controller._instance = Controller()
return Controller._instance
+1 -1
View File
@@ -196,7 +196,7 @@ class Compute:
msg = json.loads(response.data)
action = msg.pop("action")
event = msg.pop("event")
self._controller.notification.emit(action, event, compute_id=self.id, **msg)
self._controller.notification.dispatch(action, event, compute_id=self.id)
def _getUrl(self, path):
return "{}://{}:{}/v2/compute{}".format(self._protocol, self._host, self._port, path)
+11 -5
View File
@@ -102,7 +102,7 @@ class Node:
data = self._node_data()
data["node_id"] = self._id
response = yield from self._compute.post("/projects/{}/{}/nodes".format(self._project.id, self._node_type), data=data)
self._parse_node_response(response)
self.parse_node_response(response.json)
@asyncio.coroutine
def update(self, name=None, console=None, console_type="telnet", properties={}):
@@ -128,20 +128,26 @@ class Node:
data = self._node_data()
response = yield from self.put(None, data=data)
self._parse_node_response(response)
self.parse_node_response(response.json)
def _parse_node_response(self, response):
def parse_node_response(self, response):
"""
Update the object with the remote node object
"""
for key, value in response.json.items():
for key, value in response.items():
if key == "console":
self._console = value
elif key == "node_directory":
self._node_directory = value
elif key == "command_line":
self._command_line = value
elif key in ["console_type", "name", "node_id", "project_id", "status"]:
elif key == "status":
self._status = value
elif key == "console_type":
self._console_type = value
elif key == "name":
self._name = value
elif key in ["node_id", "project_id"]:
pass
else:
self._properties[key] = value
+34 -12
View File
@@ -15,6 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import aiohttp
from contextlib import contextmanager
from ..notification_queue import NotificationQueue
@@ -42,21 +43,44 @@ class Notification:
yield queue
self._listeners[project.id].remove(queue)
def emit(self, action, event, **kwargs):
def dispatch(self, action, event, compute_id):
"""
Notification received from compute node. Send it directly
to clients or process it
:param action: Action name
:param event: Event to send
:param compute_id: Compute id of the sender
"""
if action == "node.updated":
try:
# Update controller node data and send the event node.updated
project = self._controller.get_project(event["project_id"])
node = project.get_node(event["node_id"])
node.parse_node_response(event)
self.emit("node.updated", node.__json__())
except aiohttp.web.HTTPNotFound:
return
elif action == "ping":
event["compute_id"] = compute_id
self.emit(action, event)
else:
self.emit(action, event)
def emit(self, action, event):
"""
Send a notification to clients scoped by projects
:param action: Action name
:param event: Event to send
:param kwargs: Add this meta to the notification
"""
if "project_id" in kwargs:
project_id = kwargs.pop("project_id")
self._send_event_to_project(project_id, action, event, **kwargs)
if "project_id" in event:
self._send_event_to_project(event["project_id"], action, event)
else:
self._send_event_to_all(action, event, **kwargs)
self._send_event_to_all(action, event)
def _send_event_to_project(self, project_id, action, event, **kwargs):
def _send_event_to_project(self, project_id, action, event):
"""
Send an event to all the client listening for notifications for
this project
@@ -64,24 +88,22 @@ class Notification:
:param project: Project where we need to send the event
:param action: Action name
:param event: Event to send
:param kwargs: Add this meta to the notification
"""
try:
project_listeners = self._listeners[project_id]
except KeyError:
return
for listener in project_listeners:
listener.put_nowait((action, event, kwargs))
listener.put_nowait((action, event, {}))
def _send_event_to_all(self, action, event, **kwargs):
def _send_event_to_all(self, action, event):
"""
Send an event to all the client listening for notifications on all
projects
:param action: Action name
:param event: Event to send
:param kwargs: Add this meta to the notification
"""
for project_listeners in self._listeners.values():
for listener in project_listeners:
listener.put_nowait((action, event, kwargs))
listener.put_nowait((action, event, {}))