| 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 |
import abc |
| 14 |
import copy |
| 15 |
|
| 16 |
from neutron_lib import exceptions |
| 17 |
|
| 18 |
|
| 19 |
def convert_filters(**kwargs): |
| 20 |
result = copy.deepcopy(kwargs) |
| 21 |
if 'tenant_id' in result: |
| 22 |
if 'project_id' in result: |
| 23 |
raise exceptions.TenantIdProjectIdFilterConflict() |
| 24 |
|
| 25 |
result['project_id'] = result.pop('tenant_id') |
| 26 |
return result |
| 27 |
|
| 28 |
|
| 29 |
class FilterObj(metaclass=abc.ABCMeta): |
| 30 |
|
| 31 |
@abc.abstractmethod |
| 32 |
def filter(self, column): |
| 33 |
pass |
| 34 |
|
| 35 |
|
| 36 |
class StringMatchingFilterObj(FilterObj): |
| 37 |
@property |
| 38 |
def is_contains(self): |
| 39 |
return bool(getattr(self, "contains", False)) |
| 40 |
|
| 41 |
@property |
| 42 |
def is_starts(self): |
| 43 |
return bool(getattr(self, "starts", False)) |
| 44 |
|
| 45 |
@property |
| 46 |
def is_ends(self): |
| 47 |
return bool(getattr(self, "ends", False)) |
| 48 |
|
| 49 |
|
| 50 |
class StringContains(StringMatchingFilterObj): |
| 51 |
|
| 52 |
def __init__(self, matching_string): |
| 53 |
super().__init__() |
| 54 |
self.contains = matching_string |
| 55 |
|
| 56 |
def filter(self, column): |
| 57 |
return column.contains(self.contains) |
| 58 |
|
| 59 |
|
| 60 |
class StringStarts(StringMatchingFilterObj): |
| 61 |
|
| 62 |
def __init__(self, matching_string): |
| 63 |
super().__init__() |
| 64 |
self.starts = matching_string |
| 65 |
|
| 66 |
def filter(self, column): |
| 67 |
return column.startswith(self.starts) |
| 68 |
|
| 69 |
|
| 70 |
class StringEnds(StringMatchingFilterObj): |
| 71 |
|
| 72 |
def __init__(self, matching_string): |
| 73 |
super().__init__() |
| 74 |
self.ends = matching_string |
| 75 |
|
| 76 |
def filter(self, column): |
| 77 |
return column.endswith(self.ends) |
| 78 |
|
| 79 |
|
| 80 |
class NotIn(FilterObj): |
| 81 |
|
| 82 |
def __init__(self, value): |
| 83 |
super().__init__() |
| 84 |
self.value = value |
| 85 |
|
| 86 |
def filter(self, column): |
| 87 |
return ~column.in_(self.value) |
| 88 |
|
| 89 |
|
| 90 |
class NotEqual(FilterObj): |
| 91 |
|
| 92 |
def __init__(self, value): |
| 93 |
super().__init__() |
| 94 |
self.value = value |
| 95 |
|
| 96 |
def filter(self, column): |
| 97 |
return column != self.value |
| 98 |
|
| 99 |
|
| 100 |
def get_updatable_fields(cls, fields): |
| 101 |
fields = fields.copy() |
| 102 |
for field in cls.fields_no_update: |
| 103 |
if field in fields: |
| 104 |
del fields[field] |
| 105 |
return fields |