Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Commit

Permalink
Look up service components by interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
jerith committed May 20, 2014
1 parent 5a270f3 commit 2e5d564
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 13 deletions.
9 changes: 9 additions & 0 deletions go/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ def get_service_definition(service_type, vumi_api, service=None):
return service_pkg.definition.ServiceComponentDefinition(vumi_api, service)


def get_service_types_with_interface(interface_name):
service_types = []
for service_type in configured_service_types():
service_def = get_service_definition(service_type, None)
if interface_name in service_def.service_component_interfaces:
service_types.append(service_type)
return service_types


_VUMI_INSTALLED_APPS = {
'go.apps.bulk_message': {
'namespace': 'bulk_message',
Expand Down
63 changes: 54 additions & 9 deletions go/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
from twisted.trial.unittest import TestCase

from go.config import (
get_conversation_pkg, get_router_pkg,
get_conversation_definition, get_router_definition,
get_conversation_pkg, get_router_pkg, get_service_pkg,
get_conversation_definition, get_router_definition, get_service_definition,
configured_conversations, configured_routers, configured_services,
obsolete_conversation_types, obsolete_router_types, obsolete_service_types,
configured_conversation_types, configured_router_types,
configured_conversations, configured_routers,
obsolete_conversation_types, obsolete_router_types)
from go.errors import UnknownConversationType, UnknownRouterType
configured_service_types, get_service_types_with_interface)
from go.errors import (
UnknownConversationType, UnknownRouterType, UnknownServiceComponentType)


class ConversationDefinitionHelpersTestCase(TestCase):
Expand Down Expand Up @@ -51,8 +53,8 @@ def test_get_conversation_definition_with_conv(self):

class RouterDefinitionHelpersTestCase(TestCase):
def test_configured_router_types(self):
conv_types = configured_router_types()
self.assertEqual(conv_types['keyword'], 'Keyword')
router_types = configured_router_types()
self.assertEqual(router_types['keyword'], 'Keyword')

def test_configured_routers(self):
routers = configured_routers()
Expand All @@ -63,8 +65,7 @@ def test_configured_routers(self):

def test_obsolete_router_types(self):
obsolete_types = obsolete_router_types()
self.assertEqual(obsolete_types, set([
]))
self.assertEqual(obsolete_types, set())

def test_get_router_pkg(self):
pkg = get_router_pkg('keyword', ['definition'])
Expand All @@ -84,3 +85,47 @@ def test_get_router_definition_with_router(self):
dummy_router = object()
router_def = get_router_definition('keyword', dummy_router)
self.assertTrue(router_def.router is dummy_router)


class ServiceComponentDefinitionHelpersTestCase(TestCase):
def test_configured_service_types(self):
service_types = configured_service_types()
self.assertEqual(service_types['metrics'], 'Metrics store')

def test_configured_services(self):
services = configured_services()
self.assertEqual(services['go.services.metrics'], {
'namespace': 'metrics',
'display_name': 'Metrics store',
})

def test_obsolete_service_types(self):
obsolete_types = obsolete_service_types()
self.assertEqual(obsolete_types, set())

def test_get_service_pkg(self):
pkg = get_service_pkg('metrics', ['definition'])
self.assertEqual(pkg.__name__, 'go.services.metrics')

def test_get_service_pkg_fails(self):
self.assertRaises(UnknownServiceComponentType,
get_service_pkg, 'unknown', ['definition'])

def test_get_service_definition(self):
dummy_api = object()
service_def = get_service_definition('metrics', dummy_api)
from go.services.metrics.definition import ServiceComponentDefinition
self.assertTrue(isinstance(service_def, ServiceComponentDefinition))
self.assertEqual(service_def.service_component_type, 'metrics')

def test_get_service_definition_with_service(self):
dummy_api = object()
dummy_service = object()
service_def = get_service_definition(
'metrics', dummy_api, dummy_service)
self.assertTrue(service_def.service is dummy_service)

def test_get_service_types_with_interface(self):
self.assertEqual(get_service_types_with_interface('foo'), [])
self.assertEqual(
get_service_types_with_interface('metrics'), ['metrics'])
17 changes: 13 additions & 4 deletions go/vumitools/service/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from vumi.persist.model import Model, Manager
from vumi.persist.fields import Unicode, ForeignKey, Timestamp, Json

from go.config import get_service_types_with_interface
from go.vumitools.account import UserAccount, PerAccountStore


Expand Down Expand Up @@ -100,14 +101,22 @@ def new_service_component(self, service_component_type, name, description,
service_component = yield service_component.save()
returnValue(service_component)

def list_running_service_components(self):
return self.service_components.index_keys(
'status', SERVICE_COMPONENT_RUNNING)

def list_active_service_components(self):
return self.service_components.index_keys(
'archive_status', SERVICE_COMPONENT_ACTIVE)

def list_service_components_for_type(self, service_type):
return self.service_components.index_keys(
'service_component_type', service_type)

@Manager.calls_manager
def list_service_components_for_interface(self, interface_name):
all_keys = []
for service_type in get_service_types_with_interface(interface_name):
keys = yield self.list_service_components_for_type(service_type)
all_keys.extend(keys)
returnValue(all_keys)

def load_all_bunches(self, keys):
# Convenience to avoid the extra attribute lookup everywhere.
return self.service_components.load_all_bunches(keys)
25 changes: 25 additions & 0 deletions go/vumitools/service/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,31 @@ def test_new_service_component_unicode(self):
service.key)
self.assert_models_equal(service, dbservice)

@inlineCallbacks
def test_list_service_components_for_type(self):
store = self.service_store
foo_service = yield store.new_service_component(
u'foo_service', u'name', u'desc', {u'foo': u'bar'})
bar_service = yield store.new_service_component(
u'bar_service', u'name', u'desc', {u'foo': u'bar'})
foo_keys = yield store.list_service_components_for_type(u'foo_service')
bar_keys = yield store.list_service_components_for_type(u'bar_service')
self.assertEqual([foo_service.key], foo_keys)
self.assertEqual([bar_service.key], bar_keys)

@inlineCallbacks
def test_list_service_components_for_interface(self):
# TODO: Test multiple service types with the same interface
# TODO: Test a service type with multiple interfaces
store = self.service_store
yield store.new_service_component(
u'foo_service', u'name', u'desc', {u'foo': u'bar'})
metrics_service = yield store.new_service_component(
u'metrics', u'name', u'desc', {u'foo': u'bar'})
metrics_keys = yield store.list_service_components_for_interface(
u'metrics')
self.assertEqual([metrics_service.key], metrics_keys)


class TestServiceStoreSync(TestServiceStore):
sync_persistence = True

0 comments on commit 2e5d564

Please sign in to comment.