| 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 |
"""Custom SQLAlchemy types.""" |
| 14 |
|
| 15 |
import netaddr |
| 16 |
from sqlalchemy import types |
| 17 |
|
| 18 |
from neutron_lib._i18n import _ |
| 19 |
|
| 20 |
|
| 21 |
class IPAddress(types.TypeDecorator): |
| 22 |
|
| 23 |
impl = types.String(64) |
| 24 |
|
| 25 |
cache_ok = True |
| 26 |
|
| 27 |
def process_result_value(self, value, dialect): |
| 28 |
return netaddr.IPAddress(value) |
| 29 |
|
| 30 |
def process_bind_param(self, value, dialect): |
| 31 |
if not isinstance(value, netaddr.IPAddress): |
| 32 |
raise AttributeError(_("Received type '%(type)s' and value " |
| 33 |
"'%(value)s'. Expecting netaddr.IPAddress " |
| 34 |
"type.") % {'type': type(value), |
| 35 |
'value': value}) |
| 36 |
|
| 37 |
return str(value) |
| 38 |
|
| 39 |
|
| 40 |
class CIDR(types.TypeDecorator): |
| 41 |
|
| 42 |
impl = types.String(64) |
| 43 |
|
| 44 |
cache_ok = True |
| 45 |
|
| 46 |
def process_result_value(self, value, dialect): |
| 47 |
return netaddr.IPNetwork(value) |
| 48 |
|
| 49 |
def process_bind_param(self, value, dialect): |
| 50 |
if not isinstance(value, netaddr.IPNetwork): |
| 51 |
raise AttributeError(_("Received type '%(type)s' and value " |
| 52 |
"'%(value)s'. Expecting netaddr.IPNetwork " |
| 53 |
"type.") % {'type': type(value), |
| 54 |
'value': value}) |
| 55 |
return str(value) |
| 56 |
|
| 57 |
|
| 58 |
class MACAddress(types.TypeDecorator): |
| 59 |
|
| 60 |
impl = types.String(64) |
| 61 |
|
| 62 |
cache_ok = True |
| 63 |
|
| 64 |
def process_result_value(self, value, dialect): |
| 65 |
return netaddr.EUI(value) |
| 66 |
|
| 67 |
def process_bind_param(self, value, dialect): |
| 68 |
if not isinstance(value, netaddr.EUI): |
| 69 |
raise AttributeError(_("Received type '%(type)s' and value " |
| 70 |
"'%(value)s'. Expecting netaddr.EUI " |
| 71 |
"type.") % {'type': type(value), |
| 72 |
'value': value}) |
| 73 |
return str(value) |
| 74 |
|
| 75 |
|
| 76 |
class TruncatedDateTime(types.TypeDecorator): |
| 77 |
"""Truncates microseconds. |
| 78 |
|
| 79 |
Use this for datetime fields so we don't have to worry about DB-specific |
| 80 |
behavior when it comes to rounding/truncating microseconds off of |
| 81 |
timestamps. |
| 82 |
""" |
| 83 |
|
| 84 |
impl = types.DateTime |
| 85 |
|
| 86 |
cache_ok = True |
| 87 |
|
| 88 |
def process_bind_param(self, value, dialect): |
| 89 |
return value.replace(microsecond=0) if value else value |
| 90 |
|
| 91 |
process_result_value = process_bind_param |