From d3d72d4b19b073d72810b54a2c17c9408cc9292b Mon Sep 17 00:00:00 2001 From: liusheng Date: Wed, 13 Sep 2017 15:40:07 +0800 Subject: [PATCH] Enrich the payload of notifications of server creation Change-Id: Ic86ae41bbb8fcfaac0ea82b9ac2427066412ff42 --- mogan/notifications/objects/exception.py | 3 - mogan/notifications/objects/server.py | 72 ++++++++++++++++--- .../unit/notifications/test_notification.py | 5 +- 3 files changed, 65 insertions(+), 15 deletions(-) diff --git a/mogan/notifications/objects/exception.py b/mogan/notifications/objects/exception.py index 22b6e4a2..7a5f7023 100644 --- a/mogan/notifications/objects/exception.py +++ b/mogan/notifications/objects/exception.py @@ -32,9 +32,6 @@ class ExceptionPayload(base.NotificationPayloadBase): @classmethod def from_exception(cls, fault): trace = inspect.trace()[-1] - # TODO(gibi): apply strutils.mask_password on exception_message and - # consider emitting the exception_message only if the safe flag is - # true in the exception like in the REST API module = inspect.getmodule(trace[0]) module_name = module.__name__ if module else 'unknown' return cls( diff --git a/mogan/notifications/objects/server.py b/mogan/notifications/objects/server.py index a01341c6..dc466e4d 100644 --- a/mogan/notifications/objects/server.py +++ b/mogan/notifications/objects/server.py @@ -30,7 +30,14 @@ class ServerPayload(base.NotificationPayloadBase): 'status': ('server', 'status'), 'power_state': ('server', 'power_state'), 'flavor_uuid': ('server', 'flavor_uuid'), - 'description': ('server', 'description') + 'description': ('server', 'description'), + 'locked': ('server', 'locked'), + 'locked_by': ('server', 'locked_by'), + 'affinity_zone': ('server', 'affinity_zone'), + 'metadata': ('server', 'metadata'), + 'partitions': ('server', 'partitions'), + 'key_name': ('server', 'key_name'), + 'node': ('server', 'node') } # Version 1.0: Initial version VERSION = '1.0' @@ -48,13 +55,60 @@ class ServerPayload(base.NotificationPayloadBase): 'launched_at': fields.DateTimeField(nullable=True), 'updated_at': fields.DateTimeField(nullable=True), 'status': fields.StringField(nullable=True), - # 'network_info' - # 'extra' + 'locked': fields.BooleanField(nullable=True), + 'locked_by': fields.StringField(nullable=True), + 'affinity_zone': fields.StringField(nullable=True), + 'metadata': fields.FlexibleDictField(nullable=True), + 'partitions': fields.FlexibleDictField(nullable=True), + 'key_name': fields.StringField(nullable=True), + 'node': fields.StringField(nullable=True), + 'addresses': fields.ListOfObjectsField('ServerAddressesPayload', + nullable=True) } - def __init__(self, server, **kwargs): - super(ServerPayload, self).__init__(**kwargs) + def __init__(self, server): + super(ServerPayload, self).__init__() self.populate_schema(server=server) + self.addresses = ServerAddressesPayload.from_server_obj(server) + + +@mogan_base.MoganObjectRegistry.register_notification +class ServerAddressesPayload(base.NotificationPayloadBase): + # Version 1.0: Initial version + VERSION = '1.0' + fields = { + 'port_id': fields.UUIDField(nullable=True), + 'mac_address': fields.MACAddressField(), + 'fixed_ips': fields.ListOfDictOfNullableStringsField( + nullable=True), + 'network_id': fields.UUIDField(nullable=True), + 'floating_ip': fields.StringField(nullable=True), + 'preserve_on_delete': fields.BooleanField(nullable=True) + } + + SCHEMA = { + 'port_id': ('nic', 'port_id'), + 'mac_address': ('nic', 'mac_address'), + 'fixed_ips': ('nic', 'fixed_ips'), + 'network_id': ('nic', 'network_id'), + 'floating_ip': ('nic', 'floating_ip'), + 'preserve_on_delete': ('nic', 'preserve_on_delete'), + } + + def __init__(self, nic_obj): + super(ServerAddressesPayload, self).__init__() + self.populate_schema(nic=nic_obj) + + @classmethod + def from_server_obj(cls, server): + """Returns a list of a server's addresses. + """ + if not server.nics: + return [] + addresses = [] + for nic in server.nics: + addresses.append(cls(nic)) + return addresses @mogan_base.MoganObjectRegistry.register_notification @@ -66,11 +120,9 @@ class ServerActionPayload(ServerPayload): 'fault': fields.ObjectField('ExceptionPayload', nullable=True), } - def __init__(self, server, fault, **kwargs): - super(ServerActionPayload, self).__init__( - server=server, - fault=fault, - **kwargs) + def __init__(self, server, fault): + super(ServerActionPayload, self).__init__(server=server) + self.fault = fault @mogan_base.MoganObjectRegistry.register_notification diff --git a/mogan/tests/unit/notifications/test_notification.py b/mogan/tests/unit/notifications/test_notification.py index 17e60c3e..bb75b6e4 100644 --- a/mogan/tests/unit/notifications/test_notification.py +++ b/mogan/tests/unit/notifications/test_notification.py @@ -226,8 +226,9 @@ class TestNotificationBase(test_base.TestCase): notification_object_data = { - 'ServerPayload': '1.0-6a060a6bebc672c105c14b4cef979527', - 'ServerActionPayload': '1.0-b558fd2bcce6388507b67a834f09689f', + 'ServerPayload': '1.0-55ce3a2c615a32c80b152aaf2b905703', + 'ServerAddressesPayload': '1.0-69caf4c36f36756bb1f6970d093ee1f6', + 'ServerActionPayload': '1.0-a22c2f18b8dd17a3990e5b4c64989d26', 'ServerActionNotification': '1.0-20087e599436bd9db62ae1fb5e2dfef2', 'ExceptionPayload': '1.0-7c31986d8d78bed910c324965c431e18', 'ExceptionNotification': '1.0-20087e599436bd9db62ae1fb5e2dfef2',