forked from openstack/openstacksdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_filter.py
More file actions
190 lines (159 loc) · 6.65 KB
/
service_filter.py
File metadata and controls
190 lines (159 loc) · 6.65 KB
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
# 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.
"""
The :class:`~openstack.auth.service_filter.ServiceFilter` is the base class
for service identifiers and user service preferences. Each
:class:`~openstack.resource.Resource` has a service identifier to
associate the resource with a service. An example of a service identifier
would be ``openstack.compute.compute_service.ComputeService``.
The preferences are stored in the
:class:`~openstack.user_preference.UserPreference` object.
The service preference and the service identifier are joined to create a
filter to match a service.
Examples
--------
The :class:`~openstack.auth.service_filter.ServiceFilter` class can be built
with a service type, visibility, region, name, and version.
Create a service filter
~~~~~~~~~~~~~~~~~~~~~~~
Create a compute service and service preference. Join the services
and match::
from openstack.auth import service_filter
from openstack.compute import compute_service
default = compute_service.ComputeService()
preference = service_filter.ServiceFilter('compute', version='v2')
result = preference.join(default)
matches = (result.match_service_type('compute') and
result.match_service_name('Hal9000') and
result.match_region('DiscoveryOne') and
result.match_visibility('public'))
print(str(result))
print("matches=" + str(matches))
The resulting output from the code::
service_type=compute,visibility=public,version=v2
matches=True
"""
from openstack import exceptions
class ValidVersion(object):
def __init__(self, module, path=None):
"""" Valid service version.
:param string module: Module associated with version.
:param string path: URL path version.
"""
self.module = module
self.path = path or module
class ServiceFilter(object):
UNVERSIONED = ''
ANY = 'any'
PUBLIC = 'public'
INTERNAL = 'internal'
ADMIN = 'admin'
VISIBILITY = [PUBLIC, INTERNAL, ADMIN]
valid_versions = []
def __init__(self, service_type=ANY, visibility=PUBLIC, region=None,
service_name=None, version=None):
"""Create a service identifier.
:param string service_type: The desired type of service.
:param string visibility: The exposure of the endpoint. Should be
`public` (default), `internal` or `admin`.
:param string region: The desired region (optional).
:param string service_name: Name of the service
:param string version: Version of service to use.
"""
self.service_type = service_type.lower()
self.set_visibility(visibility)
self.region = region
self.service_name = service_name
self.version = version
def __repr__(self):
ret = "service_type=%s" % self.service_type
if self.visibility is not None:
ret += ",visibility=%s" % self.visibility
if self.region is not None:
ret += ",region=%s" % self.region
if self.service_name:
ret += ",service_name=%s" % self.service_name
if self.version:
ret += ",version=%s" % self.version
return ret
def join(self, default):
"""Create a new service filter by joining filters.
Create a new service filter by joining this service preference with
the default service identifier.
:param default: Default service identifier from the resource.
:type default: :class:`~openstack.auth.service_filter.ServiceFilter`
"""
if default.version == self.UNVERSIONED:
version = default.version
else:
version = self.version
return ServiceFilter(service_type=default.service_type,
visibility=self.visibility or default.visibility,
region=self.region,
service_name=self.service_name,
version=version)
def match_service_type(self, service_type):
"""Service types are equavilent."""
if self.service_type == self.ANY:
return True
return self.service_type == service_type
def match_service_name(self, service_name):
"""Service names are equavilent."""
if not self.service_name:
return True
if self.service_name == service_name:
return True
return False
def match_region(self, region):
"""Service regions are equavilent."""
if not self.region:
return True
if self.region == region:
return True
return False
def match_visibility(self, visibility):
"""Service visibilities are equavilent."""
if not self.visibility:
return True
return self.visibility == visibility
def set_visibility(self, visibility):
"""Set the visibility of the service filter."""
if not visibility:
self.visibility = None
return
visibility = visibility.replace('URL', '')
visibility = visibility.lower()
if visibility not in self.VISIBILITY:
msg = "Visibility <%s> not in %s" % (visibility, self.VISIBILITY)
raise exceptions.SDKException(msg)
self.visibility = visibility
def get_module(self):
"""Get the full module name associated with the service."""
module = self.__class__.__module__.split('.')
module = ".".join(module[:-1])
# NOTE(thowe): Only support for one valid version right now.
module = module + "." + self.valid_versions[0].module
return module
def get_service_module(self):
"""Get the module version of the service name.
This would often be the same as the service type except in cases like
object store where the service type is `object-store` and the module
is `object_store`.
"""
return self.__class__.__module__.split('.')[1]
def get_version(self, version):
"""Get the desired version.
If the service does not have a version, use the suggested version.
"""
if self.version is None:
return version
return self.version