← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~stevenk/launchpad/add-enterpriseid into lp:launchpad

 

Steve Kowalik has proposed merging lp:~stevenk/launchpad/add-enterpriseid into lp:launchpad.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~stevenk/launchpad/add-enterpriseid/+merge/95827

Add two new methods, object_to_enterpriseid() and enterpriseid_to_object() under lp.services.enterpriseid for our codebase to call to either get the EID for a given object, or to return the object for a given EID. Only Person is properly supported both ways as it stands, but more types can be added quite trivially.
-- 
https://code.launchpad.net/~stevenk/launchpad/add-enterpriseid/+merge/95827
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~stevenk/launchpad/add-enterpriseid into lp:launchpad.
=== added file 'lib/lp/services/enterpriseid.py'
--- lib/lp/services/enterpriseid.py	1970-01-01 00:00:00 +0000
+++ lib/lp/services/enterpriseid.py	2012-03-05 02:16:19 +0000
@@ -0,0 +1,40 @@
+# Copyright 2012 Canonical Ltd.  This software is licensed under the
+# GNU Affero General Public License version 3 (see the file LICENSE).
+
+"""Character encoding utilities"""
+
+__metaclass__ = type
+__all__ = [
+    'object_to_enterpriseid',
+    'enterpriseid_to_object',
+    ]
+
+import os
+import sys
+
+from lp.registry.model.person import Person
+from lp.services.config import config
+
+known_types = {
+    'Person': Person,
+    }
+
+
+def object_to_enterpriseid(obj):
+    """Given an object, convert it to SOA Enterprise ID."""
+    otype = obj.__class__.__name__
+    instance = 'lp'
+    if config.devmode:
+        instance += '-development'
+    elif os.environ['LPCONFIG'] in ('dogfood', 'qastaing', 'staging'):
+        instance += '-%s' % os.environ['LPCONFIG']
+    return '%s:%s:%d' % (instance, otype, obj.id)
+
+
+def enterpriseid_to_object(eid):
+    """Given an SOA Enterprise ID, return the object that it references."""
+    scheme = eid.split(':')
+    if not scheme[0].startswith('lp'):
+        raise TypeError
+    klass = known_types[scheme[1]]
+    return klass.get(scheme[2])

=== added file 'lib/lp/services/tests/test_enterpriseid.py'
--- lib/lp/services/tests/test_enterpriseid.py	1970-01-01 00:00:00 +0000
+++ lib/lp/services/tests/test_enterpriseid.py	2012-03-05 02:16:19 +0000
@@ -0,0 +1,29 @@
+# Copyright 2012 Canonical Ltd.  This software is licensed under the
+# GNU Affero General Public License version 3 (see the file LICENSE).
+
+"""XXX: Module docstring goes here."""
+
+__metaclass__ = type
+
+from lp.services.enterpriseid import (
+    enterpriseid_to_object,
+    object_to_enterpriseid,
+    )
+from lp.testing import TestCaseWithFactory
+from lp.testing.layers import DatabaseFunctionalLayer
+
+
+class TestEnterpriseId(TestCaseWithFactory):
+    layer = DatabaseFunctionalLayer
+
+    def test_object_to_enterpriseid(self):
+        person = self.factory.makePerson()
+        eid = object_to_enterpriseid(person)
+        expected = 'lp-development:Person:%d' % person.id
+        self.assertEqual(expected, eid)
+
+    def test_enterpriseid_to_object(self):
+        person = self.factory.makePerson()
+        eid = 'lp-development:Person:%d' % person.id
+        obj = enterpriseid_to_object(eid)
+        self.assertEqual(person, obj)