summaryrefslogtreecommitdiffstats
path: root/nova/api
diff options
context:
space:
mode:
authorMichael Gundlach <michael.gundlach@rackspace.com>2010-08-25 16:46:53 -0400
committerMichael Gundlach <michael.gundlach@rackspace.com>2010-08-25 16:46:53 -0400
commitcf0b5de1f78fd81ada2bada8c84e26b3238b8596 (patch)
treeb8909cbcd50c265b2e55a352d8d8743f3e0cce3b /nova/api
parent0828326898e3bc219c8205e27a3cc942e2790934 (diff)
downloadnova-cf0b5de1f78fd81ada2bada8c84e26b3238b8596.tar.gz
nova-cf0b5de1f78fd81ada2bada8c84e26b3238b8596.tar.xz
nova-cf0b5de1f78fd81ada2bada8c84e26b3238b8596.zip
Turn imageid translator into general translator for rackspace api ids
Diffstat (limited to 'nova/api')
-rw-r--r--nova/api/rackspace/_id_translator.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/nova/api/rackspace/_id_translator.py b/nova/api/rackspace/_id_translator.py
new file mode 100644
index 000000000..aec5fb6a5
--- /dev/null
+++ b/nova/api/rackspace/_id_translator.py
@@ -0,0 +1,42 @@
+from nova import datastore
+
+class RackspaceAPIIdTranslator(object):
+ """
+ Converts Rackspace API ids to and from the id format for a given
+ strategy.
+ """
+
+ def __init__(self, id_type, service_name):
+ """
+ Creates a translator for ids of the given type (e.g. 'flavor'), for the
+ given storage service backend class name (e.g. 'LocalFlavorService').
+ """
+
+ self._store = datastore.Redis.instance()
+ key_prefix = "rsapi.idtranslator.%s.%s" % (id_type, service_name)
+ # Forward (strategy format -> RS format) and reverse translation keys
+ self._fwd_key = "%s.fwd" % key_prefix
+ self._rev_key = "%s.rev" % key_prefix
+
+ def to_rs_id(self, opaque_id):
+ """Convert an id from a strategy-specific one to a Rackspace one."""
+ result = self._store.hget(self._fwd_key, str(opaque_id))
+ if result: # we have a mapping from opaque to RS for this strategy
+ return int(result)
+ else:
+ # Store the mapping.
+ nextid = self._store.incr("%s.lastid" % self._fwd_key)
+ if self._store.hsetnx(self._fwd_key, str(opaque_id), nextid):
+ # If someone else didn't beat us to it, store the reverse
+ # mapping as well.
+ self._store.hset(self._rev_key, nextid, str(opaque_id))
+ return nextid
+ else:
+ # Someone beat us to it; use their number instead, and
+ # discard nextid (which is OK -- we don't require that
+ # every int id be used.)
+ return int(self._store.hget(self._fwd_key, str(opaque_id)))
+
+ def from_rs_id(self, strategy_name, rs_id):
+ """Convert a Rackspace id to a strategy-specific one."""
+ return self._store.hget(self._rev_key, rs_id)