~soren/nova/lp658257

« back to all changes in this revision

Viewing changes to nova/api/rackspace/_id_translator.py

  • Committer: Tarmac
  • Author(s): mdietz
  • Date: 2010-10-08 21:18:37 UTC
  • mfrom: (332.2.4 rs_api_rename)
  • Revision ID: hudson@openstack.org-20101008211837-j9khriwzr28bdd2k
Renames every instance of "rackspace" in the API and test code base. Also includes a minor patch for the API Servers controller to use images correctly in the absence of Glance. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from nova import datastore
2
 
 
3
 
class RackspaceAPIIdTranslator(object):
4
 
    """
5
 
    Converts Rackspace API ids to and from the id format for a given
6
 
    strategy.
7
 
    """
8
 
 
9
 
    def __init__(self, id_type, service_name):
10
 
        """
11
 
        Creates a translator for ids of the given type (e.g. 'flavor'), for the
12
 
        given storage service backend class name (e.g. 'LocalFlavorService').
13
 
        """
14
 
 
15
 
        self._store = datastore.Redis.instance()
16
 
        key_prefix = "rsapi.idtranslator.%s.%s" % (id_type, service_name)
17
 
        # Forward (strategy format -> RS format) and reverse translation keys
18
 
        self._fwd_key = "%s.fwd" % key_prefix
19
 
        self._rev_key = "%s.rev" % key_prefix
20
 
 
21
 
    def to_rs_id(self, opaque_id):
22
 
        """Convert an id from a strategy-specific one to a Rackspace one."""
23
 
        result = self._store.hget(self._fwd_key, str(opaque_id))
24
 
        if result: # we have a mapping from opaque to RS for this strategy
25
 
            return int(result)
26
 
        else:
27
 
            # Store the mapping.
28
 
            nextid = self._store.incr("%s.lastid" % self._fwd_key)
29
 
            if self._store.hsetnx(self._fwd_key, str(opaque_id), nextid):
30
 
                # If someone else didn't beat us to it, store the reverse
31
 
                # mapping as well.
32
 
                self._store.hset(self._rev_key, nextid, str(opaque_id))
33
 
                return nextid
34
 
            else:
35
 
                # Someone beat us to it; use their number instead, and
36
 
                # discard nextid (which is OK -- we don't require that
37
 
                # every int id be used.)
38
 
                return int(self._store.hget(self._fwd_key, str(opaque_id)))
39
 
 
40
 
    def from_rs_id(self, rs_id):
41
 
        """Convert a Rackspace id to a strategy-specific one."""
42
 
        return self._store.hget(self._rev_key, rs_id)