Add base for Proxy classes to inherit from

While exploring adding a parameter that each Proxy class would need, I
was reminded that we have to manually ensure they all take the same
parameters. Ideally we should just have them all inherit from one base,
and then when we load them, ensure they're all subclasses of that base.

This doesn't add any new tests because 1) it would require a ton of
mocking to the other things which are implicitly in the way (since this
is called from _open, which is called during __init__), and  that
doesn't seem worth it because 2) it's already in a code path that gets
tested by many other Connection related tests.

Change-Id: I953bd78f05e8d72a53d262b16c349d5d69da2fbf
This commit is contained in:
Brian Curtin
2015-03-02 18:54:26 -06:00
parent f372146d30
commit 18feab0745
14 changed files with 65 additions and 46 deletions

View File

@@ -0,0 +1,20 @@
# 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.
from openstack.cdn.v1 import ping
from openstack import proxy
class Proxy(proxy.BaseProxy):
def ping(self):
return ping.Ping().get(self.session)

View File

@@ -18,12 +18,10 @@ from openstack.compute.v2 import limits
from openstack.compute.v2 import server
from openstack.compute.v2 import server_interface
from openstack.compute.v2 import server_ip
from openstack import proxy
class Proxy(object):
def __init__(self, session):
self.session = session
class Proxy(proxy.BaseProxy):
def find_extension(self, name_or_id):
return extension.Extension.find(self.session, name_or_id)

View File

@@ -61,6 +61,7 @@ import logging
import sys
from openstack import module_loader
from openstack import proxy
from openstack import session
from openstack import transport as xport
@@ -151,8 +152,11 @@ class Connection(object):
module = service.get_module() + "._proxy"
try:
__import__(module)
proxy = getattr(sys.modules[module], "Proxy")
setattr(self, attr_name, proxy(self.session))
proxy_class = getattr(sys.modules[module], "Proxy")
if not issubclass(proxy_class, proxy.BaseProxy):
raise TypeError("%s.Proxy must inherit from BaseProxy" %
proxy_class.__module__)
setattr(self, attr_name, proxy_class(self.session))
except Exception as e:
_logger.warn("Unable to load %s: %s" % (module, e))

View File

@@ -14,12 +14,10 @@ from openstack.database.v1 import database
from openstack.database.v1 import flavor
from openstack.database.v1 import instance
from openstack.database.v1 import user
from openstack import proxy
class Proxy(object):
def __init__(self, session):
self.session = session
class Proxy(proxy.BaseProxy):
def create_database(self, **data):
return database.Database(data).create(self.session)

View File

@@ -13,12 +13,10 @@
from openstack.identity.v2 import role
from openstack.identity.v2 import tenant
from openstack.identity.v2 import user
from openstack import proxy
class Proxy(object):
def __init__(self, session):
self.session = session
class Proxy(proxy.BaseProxy):
def create_role(self, **data):
return role.Role(data).create(self.session)

View File

@@ -18,12 +18,10 @@ from openstack.identity.v3 import policy
from openstack.identity.v3 import project
from openstack.identity.v3 import service
from openstack.identity.v3 import user
from openstack import proxy
class Proxy(object):
def __init__(self, session):
self.session = session
class Proxy(proxy.BaseProxy):
def create_credential(self, **data):
return credential.Credential(data).create(self.session)

View File

@@ -11,12 +11,10 @@
# under the License.
from openstack.image.v1 import image
from openstack import proxy
class Proxy(object):
def __init__(self, session):
self.session = session
class Proxy(proxy.BaseProxy):
def create_image(self, **data):
return image.Image(data).create(self.session)

View File

@@ -13,12 +13,10 @@
from openstack.keystore.v1 import container
from openstack.keystore.v1 import order
from openstack.keystore.v1 import secret
from openstack import proxy
class Proxy(object):
def __init__(self, session):
self.session = session
class Proxy(proxy.BaseProxy):
def create_container(self, **data):
return container.Container(data).create(self.session)

View File

@@ -11,12 +11,10 @@
# under the License.
from openstack.metric.v1 import capabilities
from openstack import proxy
class Proxy(object):
def __init__(self, session):
self.session = session
class Proxy(proxy.BaseProxy):
def list_capabilities(self):
return capabilities.Capabilities.list(self.session)

View File

@@ -26,12 +26,10 @@ from openstack.network.v2 import router
from openstack.network.v2 import security_group
from openstack.network.v2 import security_group_rule
from openstack.network.v2 import subnet
from openstack import proxy
class Proxy(object):
def __init__(self, session):
self.session = session
class Proxy(proxy.BaseProxy):
def find_extension(self, name_or_id):
return extension.Extension.find(self.session, name_or_id)

View File

@@ -12,12 +12,10 @@
from openstack.object_store.v1 import container as _container
from openstack.object_store.v1 import obj as _obj
from openstack import proxy
class Proxy(object):
def __init__(self, session):
self.session = session
class Proxy(proxy.BaseProxy):
def get_account_metadata(self):
"""Get metatdata for this account.

View File

@@ -11,12 +11,10 @@
# under the License.
from openstack.orchestration.v1 import stack
from openstack import proxy
class Proxy(object):
def __init__(self, session):
self.session = session
class Proxy(proxy.BaseProxy):
def find_stack(self, name_or_id):
return stack.Stack.find(self.session, name_or_id)

17
openstack/proxy.py Normal file
View File

@@ -0,0 +1,17 @@
# 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.
class BaseProxy(object):
def __init__(self, session):
self.session = session

View File

@@ -10,6 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.
from openstack import proxy
from openstack.telemetry.v2 import alarm
from openstack.telemetry.v2 import alarm_change
from openstack.telemetry.v2 import capability
@@ -19,10 +20,7 @@ from openstack.telemetry.v2 import sample
from openstack.telemetry.v2 import statistics
class Proxy(object):
def __init__(self, session):
self.session = session
class Proxy(proxy.BaseProxy):
def create_alarm(self, **data):
return alarm.Alarm(data).create(self.session)