Skip to content

Instantly share code, notes, and snippets.

@otherwiseguy
Last active May 31, 2017 19:32
Show Gist options
  • Save otherwiseguy/cb86c8ead5774af1c47dba7f700aeef2 to your computer and use it in GitHub Desktop.
Save otherwiseguy/cb86c8ead5774af1c47dba7f700aeef2 to your computer and use it in GitHub Desktop.
import abc
import unittest
import six
### base stuff ###
@six.add_metaclass(abc.ABCMeta)
class API(object):
@abc.abstractproperty
def backend(self):
"""Backend to use for common ops"""
@abc.abstractmethod
def create_transaction(self):
"""Create transaction"""
def db_add(self, *args, **kwargs):
return self.backend.db_add(*args, **kwargs)
### Backend-specific, schema-agnostic impl stuff ###
class IdlDb(object):
def db_add(self, *args, **kwargs):
return IdlDb
class CliDb(object):
def db_add(self, *args, **kwargs):
return CliDb
### Open_vSwitch schema stuff ###
@six.add_metaclass(abc.ABCMeta)
class VsctlAPI(API):
@abc.abstractmethod
def add_bridge(self, br):
"""Add a bridge"""
# We could use composition to avoid multiple-inheritance
class VsctlIdlImpl(VsctlAPI):
backend = IdlDb()
def create_transaction(self):
return True
def add_bridge(self, br):
return True
class VsctlCliImpl(VsctlAPI):
backend = CliDb()
def create_transaction(self):
return True
def add_bridge(self, br):
return True
### OVN_Northbound schema stuff ###
@six.add_metaclass(abc.ABCMeta)
class OVNNbAPI(API):
@abc.abstractmethod
def acl_add(self, **acl_args):
"""Add an ACL"""
class OvnNbIdlImpl(OVNNbAPI):
backend = IdlDb()
def create_transaction(self):
return True
def acl_add(self, **acl_args):
return True
class OvnNbCliImpl(OVNNbAPI):
backend = CliDb()
def create_transaction(self):
return True
def acl_add(self, **acl_args):
return True
class ApiTest(unittest.TestCase):
impl_map = {
VsctlIdlImpl: IdlDb,
VsctlCliImpl: CliDb,
OvnNbIdlImpl: IdlDb,
OvnNbCliImpl: CliDb,
}
def test_impl_db_usage(self):
for impl, db in self.impl_map.items():
i = impl()
self.assertEqual(i.db_add(), db)
import abc
import unittest
import six
### base stuff ###
@six.add_metaclass(abc.ABCMeta)
class API(object):
@abc.abstractmethod
def create_transaction(self):
"""Create transaction"""
@abc.abstractmethod
def db_add(self, *args, **kwargs):
"""Add object to db"""
### Backend-specific, schema-agnostic impl stuff ###
class IdlDb(API):
def db_add(self, *args, **kwargs):
return IdlDb
class CliDb(API):
def db_add(self, *args, **kwargs):
return CliDb
### Open_vSwitch schema stuff ###
@six.add_metaclass(abc.ABCMeta)
class VsctlAPI(API):
@abc.abstractmethod
def add_bridge(self, br):
"""Add a bridge"""
# how would we do this with single-inheritance?
# we need the schema-specific API and the schema-agnostic Db ops
# we can't add the schema-agnostic stuff to the VsctlAPI
class VsctlIdlImpl(VsctlAPI, IdlDb):
def create_transaction(self):
return True
def add_bridge(self, br):
return True
class VsctlCliImpl(VsctlAPI, CliDb):
def create_transaction(self):
return True
def add_bridge(self, br):
return True
### OVN_Northbound schema stuff ###
@six.add_metaclass(abc.ABCMeta)
class OVNNbAPI(API):
@abc.abstractmethod
def acl_add(self, **acl_args):
"""Add an ACL"""
class OvnNbIdlImpl(OVNNbAPI, IdlDb):
def create_transaction(self):
return True
def acl_add(self, **acl_args):
return True
class OvnNbCliImpl(OVNNbAPI, CliDb):
def create_transaction(self):
return True
def acl_add(self, **acl_args):
return True
class ApiTest(unittest.TestCase):
impl_map = {
VsctlIdlImpl: IdlDb,
VsctlCliImpl: CliDb,
OvnNbIdlImpl: IdlDb,
OvnNbCliImpl: CliDb,
}
def test_impl_db_usage(self):
for impl, db in self.impl_map.items():
i = impl()
self.assertEqual(i.db_add(), db)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment