summaryrefslogtreecommitdiff
path: root/nova/notifications/objects/flavor.py
blob: 03f95cb9eb3fe078d65b483ef71288592169eda9 (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
#    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 oslo_utils import versionutils

from nova.notifications.objects import base
from nova.objects import base as nova_base
from nova.objects import fields


@base.notification_sample('flavor-create.json')
@base.notification_sample('flavor-update.json')
@base.notification_sample('flavor-delete.json')
@nova_base.NovaObjectRegistry.register_notification
class FlavorNotification(base.NotificationBase):
    # Version 1.0: Initial version
    VERSION = '1.0'

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


@nova_base.NovaObjectRegistry.register_notification
class FlavorPayload(base.NotificationPayloadBase):
    # Version 1.0: Initial version
    # Version 1.1: Add other fields for Flavor
    # Version 1.2: Add extra_specs and projects fields
    # Version 1.3: Make projects and extra_specs field nullable as they are
    # not always available when a notification is emitted.
    VERSION = '1.3'

    # NOTE: if we'd want to rename some fields(memory_mb->ram, root_gb->disk,
    # ephemeral_gb: ephemeral), bumping to payload version 2.0 will be needed.
    SCHEMA = {
        'flavorid': ('flavor', 'flavorid'),
        'memory_mb': ('flavor', 'memory_mb'),
        'vcpus': ('flavor', 'vcpus'),
        'root_gb': ('flavor', 'root_gb'),
        'ephemeral_gb': ('flavor', 'ephemeral_gb'),
        'name': ('flavor', 'name'),
        'swap': ('flavor', 'swap'),
        'rxtx_factor': ('flavor', 'rxtx_factor'),
        'vcpu_weight': ('flavor', 'vcpu_weight'),
        'disabled': ('flavor', 'disabled'),
        'is_public': ('flavor', 'is_public'),
        'extra_specs': ('flavor', 'extra_specs'),
        'projects': ('flavor', 'projects'),
    }

    fields = {
        'flavorid': fields.StringField(nullable=True),
        'memory_mb': fields.IntegerField(nullable=True),
        'vcpus': fields.IntegerField(nullable=True),
        'root_gb': fields.IntegerField(nullable=True),
        'ephemeral_gb': fields.IntegerField(nullable=True),
        'name': fields.StringField(),
        'swap': fields.IntegerField(),
        'rxtx_factor': fields.FloatField(nullable=True),
        'vcpu_weight': fields.IntegerField(nullable=True),
        'disabled': fields.BooleanField(),
        'is_public': fields.BooleanField(),
        'extra_specs': fields.DictOfStringsField(nullable=True),
        'projects': fields.ListOfStringsField(nullable=True),
    }

    def __init__(self, flavor, **kwargs):
        super(FlavorPayload, self).__init__(**kwargs)
        if 'projects' not in flavor:
            # NOTE(danms): If projects is not loaded in the flavor,
            # don't attempt to load it. If we're in a child cell then
            # we can't load the real flavor, and if we're a flavor on
            # an instance then we don't want to anyway.
            flavor = flavor.obj_clone()
            flavor._context = None
        self.populate_schema(flavor=flavor)

    def obj_make_compatible(self, primitive, target_version):
        super(FlavorPayload, self).obj_make_compatible(primitive,
                                                       target_version)
        target_version = versionutils.convert_version_to_tuple(target_version)
        if target_version < (1, 1):
            primitive.pop('name', None)
            primitive.pop('swap', None)
            primitive.pop('rxtx_factor', None)
            primitive.pop('vcpu_weight', None)
            primitive.pop('disabled', None)
            primitive.pop('is_public', None)
        if target_version < (1, 2):
            primitive.pop('extra_specs', None)
            primitive.pop('projects', None)
        if target_version < (1, 3):
            if 'projects' not in primitive or primitive['projects'] is None:
                primitive['projects'] = []
            if ('extra_specs' not in primitive or
                    primitive['extra_specs'] is None):
                primitive['extra_specs'] = {}