summaryrefslogtreecommitdiff
path: root/mogan/notifications/objects/server.py
blob: 2eeabbf787d30b0e7263ef01d410fcccb39b2a56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from mogan.notifications.objects import base
from mogan.objects import base as mogan_base
from mogan.objects import fields


@mogan_base.MoganObjectRegistry.register_notification
class ServerPayload(base.NotificationPayloadBase):
    SCHEMA = {
        'name': ('server', 'name'),
        'uuid': ('server', 'uuid'),
        'user_id': ('server', 'user_id'),
        'project_id': ('server', 'project_id'),
        'availability_zone': ('server', 'availability_zone'),
        'image_uuid': ('server', 'image_uuid'),
        'created_at': ('server', 'created_at'),
        'launched_at': ('server', 'launched_at'),
        'updated_at': ('server', 'updated_at'),
        'status': ('server', 'status'),
        'power_state': ('server', 'power_state'),
        'flavor_uuid': ('server', 'flavor_uuid'),
        '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'
    fields = {
        'name': fields.StringField(nullable=False),
        'uuid': fields.UUIDField(nullable=False),
        'user_id': fields.StringField(nullable=True),
        'project_id': fields.StringField(nullable=True),
        'description': fields.StringField(nullable=True),
        'flavor_uuid': fields.UUIDField(nullable=False),
        'image_uuid': fields.UUIDField(nullable=True),
        'availability_zone': fields.StringField(nullable=True),
        'power_state': fields.StringField(nullable=True),
        'created_at': fields.DateTimeField(nullable=True),
        'launched_at': fields.DateTimeField(nullable=True),
        'updated_at': fields.DateTimeField(nullable=True),
        'status': fields.StringField(nullable=True),
        '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):
        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
class ServerActionPayload(ServerPayload):
    # No SCHEMA as all the additional fields are calculated

    VERSION = '1.0'
    fields = {
        'fault': fields.ObjectField('ExceptionPayload', nullable=True),
    }

    def __init__(self, server, fault):
        super(ServerActionPayload, self).__init__(server=server)
        self.fault = fault


@base.notification_sample('server-create-start.json')
@base.notification_sample('server-create-end.json')
@base.notification_sample('server-create-error.json')
@base.notification_sample('server-delete-start.json')
@base.notification_sample('server-delete-end.json')
@mogan_base.MoganObjectRegistry.register_notification
class ServerActionNotification(base.NotificationBase):
    # Version 1.0: Initial version
    VERSION = '1.0'

    fields = {
        'payload': fields.ObjectField('ServerActionPayload')
    }