Debian Codegraph @

neutron_lib/db/model_base.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from oslo_db.sqlalchemy import models
14 from oslo_utils import uuidutils
15 import sqlalchemy as sa
16 from sqlalchemy import orm
17
18 from neutron_lib.db import constants as db_const
19
20
21 class HasProject:
22 """Project mixin, add to subclasses that have a user."""
23
24 # NOTE: project_id is just a free form string
25 project_id = sa.Column(sa.String(db_const.PROJECT_ID_FIELD_SIZE),
26 index=True)
27
28 def get_tenant_id(self):
29 return self.project_id
30
31 def set_tenant_id(self, value):
32 self.project_id = value
33
34 @orm.declared_attr
35 def tenant_id(cls):
36 return orm.synonym(
37 'project_id',
38 descriptor=property(cls.get_tenant_id, cls.set_tenant_id))
39
40
41 class HasProjectNoIndex(HasProject):
42 """Project mixin, add to subclasses that have a user."""
43
44 # NOTE: project_id is just a free form string
45 project_id = sa.Column(sa.String(db_const.PROJECT_ID_FIELD_SIZE))
46
47
48 class HasProjectPrimaryKey(HasProject):
49 """Project mixin, add to subclasses that have a user."""
50
51 # NOTE: project_id is just a free form string
52 project_id = sa.Column(sa.String(db_const.PROJECT_ID_FIELD_SIZE),
53 nullable=False, primary_key=True)
54
55
56 class HasProjectPrimaryUniqueKey(HasProject):
57 """Project mixin, add to subclasses that have a user."""
58
59 # NOTE: project_id is just a free form string
60 project_id = sa.Column(sa.String(db_const.PROJECT_ID_FIELD_SIZE),
61 nullable=False, primary_key=True, unique=True)
62
63
64 class HasId:
65 """id mixin, add to subclasses that have an id."""
66
67 id = sa.Column(sa.String(db_const.UUID_FIELD_SIZE),
68 primary_key=True,
69 default=uuidutils.generate_uuid)
70
71
72 class HasStatusDescription:
73 """Status with description mixin."""
74
75 status = sa.Column(sa.String(db_const.STATUS_FIELD_SIZE),
76 nullable=False)
77 status_description = sa.Column(sa.String(db_const.DESCRIPTION_FIELD_SIZE))
78
79
80 class _NeutronBase(models.ModelBase):
81 """Base class for Neutron Models."""
82
83 __table_args__ = {'mysql_engine': 'InnoDB'}
84
85 def __iter__(self):
86 self._i = iter(orm.object_mapper(self).columns)
87 return self
88
89 def next(self):
90 n = next(self._i).name
91 return n, getattr(self, n)
92
93 __next__ = next
94
95 def __repr__(self):
96 """sqlalchemy based automatic __repr__ method."""
97 items = [f'{col.name}={getattr(self, col.name)!r}'
98 for col in self.__table__.columns]
99 return "<{}.{}[object at {:x}] {{{}}}>".format(
100 self.__class__.__module__, self.__class__.__name__,
101 id(self), ', '.join(items))
102
103
104 class NeutronBaseV2(_NeutronBase):
105
106 @orm.declared_attr
107 def __tablename__(cls):
108 # Use the pluralized name of the class as the table name.
109 return cls.__name__.lower() + 's'
110
111
112 class BASEV2(orm.DeclarativeBase, NeutronBaseV2):
113 pass