Skip to content

Commit 8baa156

Browse files
committed
High level interface
High level interface using the connection classes to access the various services. Only identity projects and list_flavors in compute are implemented: projects = conn.identity.list_projects() The service filter has a valid_versions added to it to map the module name to the version component of the endpoint URL. For example, the v2.0 endpoint path is supported by the v2 SDK network module: https://region-a.geo-1.network.hpcloudsvc.com/v2.0/ is supported by the SDK module openstack.network.v2. Implements: blueprint highlevel-interface Change-Id: Ie52986a9da84c8f3395757c08de7a01be30e0f2a
1 parent 7631351 commit 8baa156

29 files changed

+327
-4
lines changed

openstack/auth/service_filter.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,25 @@
2525
from openstack import exceptions
2626

2727

28+
class ValidVersion(object):
29+
30+
def __init__(self, module, path=None):
31+
"""" Valid service version.
32+
33+
:param string module: Module associated with version.
34+
:param string path: URL path version.
35+
"""
36+
self.module = module
37+
self.path = path or module
38+
39+
2840
class ServiceFilter(object):
2941
ANY = 'any'
3042
PUBLIC = 'public'
3143
INTERNAL = 'internal'
3244
ADMIN = 'admin'
3345
VISIBILITY = [PUBLIC, INTERNAL, ADMIN]
46+
valid_versions = []
3447

3548
def __init__(self, service_type=ANY, visibility=PUBLIC, region=None,
3649
service_name=None, version=None):
@@ -115,3 +128,20 @@ def set_visibility(self, visibility):
115128
msg = "Visibility <%s> not in %s" % (visibility, self.VISIBILITY)
116129
raise exceptions.SDKException(msg)
117130
self.visibility = visibility
131+
132+
def get_module(self):
133+
"""Get the full module name associated with the service."""
134+
module = self.__class__.__module__.split('.')
135+
module = ".".join(module[:-1])
136+
# NOTE(thowe): Only support for one valid version right now.
137+
module = module + "." + self.valid_versions[0].module
138+
return module
139+
140+
def get_service_module(self):
141+
"""Get the module version of the service name.
142+
143+
This would often be the same as the service type except in cases like
144+
object store where the service type is `object-store` and the module
145+
is `object_store`.
146+
"""
147+
return self.__class__.__module__.split('.')[1]

openstack/compute/compute_service.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
class ComputeService(service_filter.ServiceFilter):
1717
"""The compute service."""
1818

19+
valid_versions = [service_filter.ValidVersion('v2')]
20+
1921
def __init__(self):
2022
"""Create an compute service."""
2123
super(ComputeService, self).__init__(service_type='compute')

openstack/compute/v2/_proxy.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 openstack.compute.v2 import flavor
14+
15+
16+
class Proxy(object):
17+
18+
def __init__(self, session):
19+
self.session = session
20+
21+
def list_flavors(self, **params):
22+
return flavor.Flavor.list(self.session, **params)

openstack/connection.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
network = conn.network.create_network({"name": "jenkins"})
5959
6060
"""
61+
import logging
62+
import sys
6163

6264
from stevedore import driver
6365

@@ -68,6 +70,7 @@
6870

6971
USER_AGENT = 'OSPythonSDK'
7072
"""Default value for the HTTP User-Agent header"""
73+
_logger = logging.getLogger(__name__)
7174

7275

7376
class Connection(object):
@@ -128,6 +131,7 @@ def __init__(self, transport=None, authenticator=None, preference=None,
128131
**auth_args)
129132
self.session = session.Session(self.transport, self.authenticator,
130133
preference)
134+
self._open()
131135

132136
def _create_transport(self, transport, verify, user_agent):
133137
if transport:
@@ -157,3 +161,21 @@ def _create_authenticator(self, authenticator, auth_plugin, **auth_args):
157161
valid_list = plugin.valid_options
158162
args = dict((n, auth_args[n]) for n in valid_list if n in auth_args)
159163
return plugin(**args)
164+
165+
def _open(self):
166+
"""Open the connection.
167+
168+
NOTE(thowe): Have this set up some lazy loader instead.
169+
"""
170+
for service in self.session.get_services():
171+
self._load(service)
172+
173+
def _load(self, service):
174+
attr_name = service.get_service_module()
175+
module = service.get_module() + "._proxy"
176+
try:
177+
__import__(module)
178+
proxy = getattr(sys.modules[module], "Proxy")
179+
setattr(self, attr_name, proxy(self.session))
180+
except Exception as e:
181+
_logger.warn("Unable to load %s: %s" % (module, e))

openstack/database/database_service.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
class DatabaseService(service_filter.ServiceFilter):
1717
"""The database service."""
1818

19+
valid_versions = [service_filter.ValidVersion('v1')]
20+
1921
def __init__(self):
2022
"""Create an database service."""
2123
super(DatabaseService, self).__init__(service_type='database')

openstack/database/v1/_proxy.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
14+
class Proxy(object):
15+
16+
def __init__(self, session):
17+
self.session = session

openstack/identity/identity_service.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
class IdentityService(service_filter.ServiceFilter):
1717
"""The identity service."""
1818

19+
valid_versions = [
20+
service_filter.ValidVersion('v3'),
21+
service_filter.ValidVersion('v2'),
22+
]
23+
1924
def __init__(self, **kwargs):
2025
"""Create an identity service."""
2126
kwargs['service_type'] = 'identity'

openstack/identity/v2/_proxy.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
14+
class Proxy(object):
15+
16+
def __init__(self, session):
17+
self.session = session

openstack/identity/v3/_proxy.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 openstack.identity.v3 import project
14+
15+
16+
class Proxy(object):
17+
18+
def __init__(self, session):
19+
self.session = session
20+
21+
def create_project(self, **data):
22+
obj = project.Project(**data)
23+
obj.create(self.session)
24+
return obj
25+
26+
def get_project(self, r_id):
27+
obj = project.Project({'id': r_id})
28+
obj.get(self.session)
29+
return obj
30+
31+
def update_project(self, **data):
32+
obj = project.Project(**data)
33+
obj.update(self.session)
34+
35+
def delete_project(self, r_id):
36+
obj = project.Project({'id': r_id})
37+
obj.delete(self.session)
38+
39+
def list_projects(self, **params):
40+
return project.Project.list(self.session, **params)
41+
42+
def find_project(self, name_or_id):
43+
return project.Project.find(self.session, name_or_id)

openstack/image/image_service.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
class ImageService(service_filter.ServiceFilter):
1717
"""The image service."""
1818

19+
valid_versions = [service_filter.ValidVersion('v1')]
20+
1921
def __init__(self):
2022
"""Create an image service."""
2123
super(ImageService, self).__init__(service_type='image')

0 commit comments

Comments
 (0)