Add Central.find_*() methods

Change-Id: Icc7fd5e1cee108e20511f35f5bde9468f14b3fd2
This commit is contained in:
Kiall Mac Innes 2013-04-11 14:02:48 +01:00
parent 49b7a34626
commit 9f5ba038f6
7 changed files with 159 additions and 17 deletions

View File

@ -20,6 +20,8 @@
"create_domain": "rule:admin_or_owner",
"get_domains": "rule:admin_or_owner",
"get_domain": "rule:admin_or_owner",
"find_domains": "rule:admin_or_owner",
"find_domain": "rule:admin_or_owner",
"update_domain": "rule:admin_or_owner",
"delete_domain": "rule:admin_or_owner",
"get_domain_servers": "rule:admin_or_owner",
@ -27,6 +29,8 @@
"create_record": "rule:admin_or_owner",
"get_records": "rule:admin_or_owner",
"get_record": "rule:admin_or_owner",
"find_records": "rule:admin_or_owner",
"find_record": "rule:admin_or_owner",
"update_record": "rule:admin_or_owner",
"delete_record": "rule:admin_or_owner",

View File

@ -27,6 +27,7 @@ class CentralAPI(rpc_proxy.RpcProxy):
API version history:
1.0 - Initial version
1.1 - Add new finder methods
"""
def __init__(self, topic=None):
topic = topic if topic else cfg.CONF.central_topic
@ -102,6 +103,16 @@ class CentralAPI(rpc_proxy.RpcProxy):
return self.call(context, msg)
def find_domains(self, context, criterion):
msg = self.make_msg('find_domains', criterion=criterion)
return self.call(context, msg, version='1.1')
def find_domain(self, context, criterion):
msg = self.make_msg('find_domain', criterion=criterion)
return self.call(context, msg, version='1.1')
def update_domain(self, context, domain_id, values, increment_serial=True):
msg = self.make_msg('update_domain',
domain_id=domain_id,
@ -148,6 +159,16 @@ class CentralAPI(rpc_proxy.RpcProxy):
return self.call(context, msg)
def find_records(self, context, criterion):
msg = self.make_msg('find_records', criterion=criterion)
return self.call(context, msg, version='1.1')
def find_record(self, context, criterion):
msg = self.make_msg('find_record', criterion=criterion)
return self.call(context, msg, version='1.1')
def update_record(self, context, domain_id, record_id, values,
increment_serial=True):
msg = self.make_msg('update_record',

View File

@ -27,6 +27,8 @@ LOG = logging.getLogger(__name__)
class Service(rpc_service.Service):
RPC_API_VERSION = '1.1'
def __init__(self, *args, **kwargs):
backend_driver = cfg.CONF['service:central'].backend_driver
self.backend = backend.get_backend(backend_driver,
@ -398,6 +400,24 @@ class Service(rpc_service.Service):
return domain
def find_domains(self, context, criterion):
target = {'tenant_id': context.tenant_id}
policy.check('find_domains', context, target)
if not context.is_admin:
criterion['tenant_id'] = context.tenant_id
return self.storage.find_domains(context, criterion)
def find_domain(self, context, criterion):
target = {'tenant_id': context.tenant_id}
policy.check('find_domain', context, target)
if not context.is_admin:
criterion['tenant_id'] = context.tenant_id
return self.storage.find_domain(context, criterion)
def update_domain(self, context, domain_id, values, increment_serial=True):
domain = self.storage.get_domain(context, domain_id)
@ -569,6 +589,24 @@ class Service(rpc_service.Service):
return record
def find_records(self, context, criterion):
target = {'tenant_id': context.tenant_id}
policy.check('find_records', context, target)
if not context.is_admin:
criterion['tenant_id'] = context.tenant_id
return self.storage.find_records(context, criterion)
def find_record(self, context, criterion):
target = {'tenant_id': context.tenant_id}
policy.check('find_record', context, target)
if not context.is_admin:
criterion['tenant_id'] = context.tenant_id
return self.storage.find_record(context, criterion)
def update_record(self, context, domain_id, record_id, values,
increment_serial=True):
domain = self.storage.get_domain(context, domain_id)

View File

@ -95,6 +95,24 @@ class Storage(Plugin):
:param domain_id: ID of the Domain.
"""
@abc.abstractmethod
def find_domains(self, context, criterion):
"""
Find Domains
:param context: RPC Context.
:param criterion: Criteria to filter by.
"""
@abc.abstractmethod
def find_domain(self, context, criterion):
"""
Find a single Domain.
:param context: RPC Context.
:param criterion: Criteria to filter by.
"""
@abc.abstractmethod
def update_domain(self, context, domain_id, values):
"""
@ -151,6 +169,24 @@ class Storage(Plugin):
:param record_id: Record ID to get
"""
@abc.abstractmethod
def find_records(self, context, criterion):
"""
Find Records.
:param context: RPC Context.
:param criterion: Criteria to filter by.
"""
@abc.abstractmethod
def find_record(self, context, criterion):
"""
Find a single Record.
:param context: RPC Context.
:param criterion: Criteria to filter by.
"""
@abc.abstractmethod
def update_record(self, context, record_id, values):
"""

View File

@ -216,16 +216,25 @@ class SQLAlchemyStorage(base.Storage):
return dict(domain)
def find_domain(self, context, criterion):
def _find_domains(self, context, criterion, one=False):
query = self.session.query(models.Domain)
query = self._apply_criterion(models.Domain, query, criterion)
try:
domain = query.one()
except (exc.NoResultFound, exc.MultipleResultsFound):
raise exceptions.DomainNotFound()
if one:
try:
domain = query.one()
return dict(domain)
except (exc.NoResultFound, exc.MultipleResultsFound):
raise exceptions.DomainNotFound()
else:
return dict(domain)
domains = query.all()
return [dict(d) for d in domains]
def find_domains(self, context, criterion):
return self._find_domains(context, criterion)
def find_domain(self, context, criterion):
return self._find_domains(context, criterion, one=True)
def update_domain(self, context, domain_id, values):
domain = self._get_domain(context, domain_id)
@ -282,17 +291,25 @@ class SQLAlchemyStorage(base.Storage):
return dict(record)
def find_record(self, context, domain_id, criterion):
def _find_records(self, context, criterion, one=False):
query = self.session.query(models.Record)
query = query.filter_by(domain_id=domain_id)
query = self._apply_criterion(models.Record, query, criterion)
try:
record = query.one()
except (exc.NoResultFound, exc.MultipleResultsFound):
raise exceptions.RecordNotFound()
if one:
try:
record = query.one()
return dict(record)
except (exc.NoResultFound, exc.MultipleResultsFound):
raise exceptions.RecordNotFound()
else:
return dict(record)
records = query.all()
return [dict(r) for r in records]
def find_records(self, context, criterion):
return self._find_records(context, criterion)
def find_record(self, context, criterion):
return self._find_records(context, criterion, one=True)
def update_record(self, context, record_id, values):
record = self._get_record(context, record_id)

View File

@ -480,6 +480,20 @@ class CentralServiceTest(CentralTestCase):
self.assertEqual(domain['name'], expected_domain['name'])
self.assertEqual(domain['email'], expected_domain['email'])
def test_find_domain(self):
context = self.get_admin_context()
# Create a domain
domain_name = '%d.example.com.' % random.randint(10, 1000)
expected_domain = self.create_domain(name=domain_name)
# Retrieve it, and ensure it's the same
criterion = {'name': domain_name}
domain = self.central_service.find_domain(context, criterion)
self.assertEqual(domain['id'], expected_domain['id'])
self.assertEqual(domain['name'], expected_domain['name'])
self.assertEqual(domain['email'], expected_domain['email'])
def test_update_domain(self):
context = self.get_admin_context()
@ -769,6 +783,20 @@ class CentralServiceTest(CentralTestCase):
self.assertEqual(record['id'], expected_record['id'])
self.assertEqual(record['name'], expected_record['name'])
def test_find_record(self):
context = self.get_admin_context()
domain = self.create_domain()
# Create a record
record_name = '%d.%s' % (random.randint(10, 1000), domain['name'])
expected_record = self.create_record(domain, name=record_name)
# Retrieve it, and ensure it's the same
criterion = {'name': record_name}
record = self.central_service.find_record(context, criterion)
self.assertEqual(record['id'], expected_record['id'])
self.assertEqual(record['name'], expected_record['name'])
def test_get_record_incorrect_domain_id(self):
context = self.get_admin_context()
domain = self.create_domain()

View File

@ -549,8 +549,7 @@ class StorageTestCase(TestCase):
name=expected['name']
)
actual = self.storage.find_record(self.admin_context, domain['id'],
criterion)
actual = self.storage.find_record(self.admin_context, criterion)
self.assertEqual(actual['name'], expected['name'])
self.assertEqual(actual['type'], expected['type'])
@ -565,8 +564,7 @@ class StorageTestCase(TestCase):
)
with self.assertRaises(exceptions.RecordNotFound):
self.storage.find_record(self.admin_context, domain['id'],
criterion)
self.storage.find_record(self.admin_context, criterion)
def test_update_record(self):
domain_fixture, domain = self.create_domain()