File: standard_attr.py

package info (click to toggle)
python-neutron-lib 3.21.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,660 kB
  • sloc: python: 22,829; sh: 137; makefile: 24
file content (259 lines) | stat: -rw-r--r-- 10,526 bytes parent folder | download
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#
# 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 timeutils
import sqlalchemy as sa
from sqlalchemy import event  # noqa
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.orm import attributes
from sqlalchemy.orm import declared_attr
from sqlalchemy.orm import session as se

from neutron_lib._i18n import _
from neutron_lib.db import constants as db_const
from neutron_lib.db import model_base
from neutron_lib.db import sqlalchemytypes


class StandardAttribute(model_base.BASEV2):
    """Common table to associate all Neutron API resources.

    By having Neutron objects related to this table, we can associate new
    tables that apply to many Neutron objects (e.g. timestamps, rbac entries)
    to this table to avoid schema duplication while maintaining referential
    integrity.

    NOTE(kevinbenton): This table should not have more columns added to it
    unless we are absolutely certain the new column will have a value for
    every single type of Neutron resource. Otherwise this table will be filled
    with NULL entries for combinations that don't make sense. Additionally,
    by keeping this table small we can ensure that performance isn't adversely
    impacted for queries on objects.
    """

    # sqlite doesn't support auto increment on big integers so we use big int
    # for everything but sqlite
    id = sa.Column(sa.BigInteger().with_variant(sa.Integer(), 'sqlite'),
                   primary_key=True, autoincrement=True)

    # NOTE(kevinbenton): this column is redundant information, but it allows
    # operators/devs to look at the contents of this table and know which table
    # the corresponding object is in.
    # 255 was selected as a max just because it's the varchar ceiling in mysql
    # before a 2-byte prefix is required. We shouldn't get anywhere near this
    # limit with our table names...
    resource_type = sa.Column(sa.String(255), nullable=False)
    description = sa.Column(sa.String(db_const.DESCRIPTION_FIELD_SIZE))

    revision_number = sa.Column(
        sa.BigInteger().with_variant(sa.Integer(), 'sqlite'),
        server_default='0', nullable=False)
    created_at = sa.Column(sqlalchemytypes.TruncatedDateTime,
                           default=timeutils.utcnow)
    updated_at = sa.Column(sqlalchemytypes.TruncatedDateTime,
                           onupdate=timeutils.utcnow)

    __mapper_args__ = {
        # see http://docs.sqlalchemy.org/en/latest/orm/versioning.html for
        # details about how this works
        "version_id_col": revision_number,
        "confirm_deleted_rows": False,
        "version_id_generator": False  # revision plugin increments manually
    }

    def bump_revision(self):
        if self.revision_number is None:
            # this is a brand new object uncommitted so we don't bump now
            return
        self.revision_number += 1

    def _set_updated_revision_number(self, revision_number, updated_at):
        attributes.set_committed_value(
            self, "revision_number", revision_number)
        attributes.set_committed_value(
            self, "updated_at", updated_at)

    @property
    def _effective_standard_attribute_id(self):
        return self.id


class HasStandardAttributes:

    @classmethod
    def get_api_collections(cls):
        """Define the API collection this object will appear under.

        This should return a list of API collections that the object
        will be exposed under. Most should be exposed in just one
        collection (e.g. the network model is just exposed under
        'networks').

        This is used by the standard attr extensions to discover which
        resources need to be extended with the standard attr fields
        (e.g. created_at/updated_at/etc).
        """
        # NOTE(kevinbenton): can't use abc because the metaclass conflicts
        # with the declarative base others inherit from.
        if hasattr(cls, 'api_collections'):
            return cls.api_collections
        raise NotImplementedError(_("%s must define api_collections") % cls)

    @classmethod
    def get_api_sub_resources(cls):
        """Define the API sub-resources this object will appear under.

        This should return a list of API sub-resources that the object
        will be exposed under.

        This is used by the standard attr extensions to discover which
        sub-resources need to be extended with the standard attr fields
        (e.g. created_at/updated_at/etc).
        """
        try:
            return cls.api_sub_resources
        except AttributeError:
            return []

    @classmethod
    def get_collection_resource_map(cls):
        try:
            return cls.collection_resource_map
        except AttributeError as e:
            raise NotImplementedError(
                _("%s must define collection_resource_map") % cls) from e

    @classmethod
    def validate_tag_support(cls):
        return getattr(cls, 'tag_support', False)

    @declared_attr
    def standard_attr_id(cls):
        return sa.Column(
            sa.BigInteger().with_variant(sa.Integer(), 'sqlite'),
            sa.ForeignKey(StandardAttribute.id, ondelete="CASCADE"),
            unique=True,
            nullable=False
        )

    # NOTE(kevinbenton): we have to disable the following pylint check because
    # it thinks we are overriding this method in the __init__ method.
    # pylint: disable=method-hidden
    @declared_attr
    def standard_attr(cls):
        return sa.orm.relationship(StandardAttribute,
                                   lazy='joined',
                                   cascade='all, delete-orphan',
                                   single_parent=True,
                                   uselist=False)

    @property
    def _effective_standard_attribute_id(self):
        return self.standard_attr_id

    def __init__(self, *args, **kwargs):
        standard_attr_keys = ['description', 'created_at',
                              'updated_at', 'revision_number']
        standard_attr_kwargs = {}
        for key in standard_attr_keys:
            if key in kwargs:
                standard_attr_kwargs[key] = kwargs.pop(key)
        super().__init__(*args, **kwargs)
        # here we automatically create the related standard attribute object
        self.standard_attr = StandardAttribute(
            resource_type=self.__tablename__, **standard_attr_kwargs)

    @declared_attr
    def description(cls):
        return association_proxy('standard_attr', 'description')

    @declared_attr
    def created_at(cls):
        return association_proxy('standard_attr', 'created_at')

    @declared_attr
    def updated_at(cls):
        return association_proxy('standard_attr', 'updated_at')

    def update(self, new_dict):
        # ignore the timestamps if they were passed in. For example, this
        # happens if code calls update_port with modified results of get_port
        new_dict.pop('created_at', None)
        new_dict.pop('updated_at', None)
        super().update(new_dict)

    @declared_attr
    def revision_number(cls):
        return association_proxy('standard_attr', 'revision_number')

    def bump_revision(self):
        # SQLAlchemy will bump the version for us automatically if the
        # standard attr record is being modified, but we must call this
        # for all other modifications or when relevant children are being
        # modified (e.g. fixed_ips change should bump port revision)
        self.standard_attr.bump_revision()

    def _set_updated_revision_number(self, revision_number, updated_at):
        self.standard_attr._set_updated_revision_number(
            revision_number, updated_at)


def _resource_model_map_helper(rs_map, resource, subclass):
    if resource in rs_map:
        raise RuntimeError(_("Model %(sub)s tried to register for API "
                             "resource %(res)s which conflicts with model "
                             "%(other)s.") %
                           {'sub': subclass,
                            'other': rs_map[resource],
                            'res': resource})
    rs_map[resource] = subclass


def get_standard_attr_resource_model_map(include_resources=True,
                                         include_sub_resources=True):
    rs_map = {}
    for subclass in HasStandardAttributes.__subclasses__():
        if include_resources:
            for resource in subclass.get_api_collections():
                _resource_model_map_helper(rs_map, resource, subclass)
        if include_sub_resources:
            for sub_resource in subclass.get_api_sub_resources():
                _resource_model_map_helper(rs_map, sub_resource, subclass)
    return rs_map


def get_tag_resource_parent_map():
    parent_map = {}
    for subclass in HasStandardAttributes.__subclasses__():
        if subclass.validate_tag_support():
            for collection, resource in (subclass.get_collection_resource_map()
                                         .items()):
                if collection in parent_map:
                    msg = (_("API parent %(collection)s/%(resource)s for "
                             "model %(subclass)s is already registered.") %
                           {'collection': collection, 'resource': resource,
                            'subclass': subclass})
                    raise RuntimeError(msg)
                parent_map[collection] = resource
    return parent_map


@event.listens_for(se.Session, 'after_bulk_delete')
def throw_exception_on_bulk_delete_of_listened_for_objects(delete_context):
    if hasattr(delete_context.mapper.class_, 'revises_on_change'):
        raise RuntimeError(_("%s may not be deleted in bulk because it "
                             "bumps the revision of other resources via "
                             "SQLAlchemy event handlers, which are not "
                             "compatible with bulk deletes.") %
                           delete_context.mapper.class_)