summaryrefslogtreecommitdiffstats
path: root/nova
diff options
context:
space:
mode:
authormdietz <mdietz@openstack>2010-10-04 21:08:08 +0000
committermdietz <mdietz@openstack>2010-10-04 21:08:08 +0000
commitec6af5b56545a7ec62033a6574683e3b93dc862c (patch)
tree5a5c2041818d1387aee07bf23797d1a5a1a28bed /nova
parent7e66ee636910763630fcf5e6ff23848389713c81 (diff)
parentdd0f365c98ae68afff9a0fbc75e7d5b88499b282 (diff)
merge from gundlach ec2 conversion
Diffstat (limited to 'nova')
-rw-r--r--nova/api/ec2/cloud.py24
-rw-r--r--nova/db/sqlalchemy/api.py3
-rw-r--r--nova/tests/cloud_unittest.py3
-rw-r--r--nova/utils.py12
4 files changed, 28 insertions, 14 deletions
diff --git a/nova/api/ec2/cloud.py b/nova/api/ec2/cloud.py
index f43da42bd..3f440c85c 100644
--- a/nova/api/ec2/cloud.py
+++ b/nova/api/ec2/cloud.py
@@ -72,6 +72,20 @@ def _gen_key(context, user_id, key_name):
return {'private_key': private_key, 'fingerprint': fingerprint}
+def ec2_id_to_internal_id(ec2_id):
+ """Convert an ec2 ID (i-[base 36 number]) to an internal id (int)"""
+ return int(ec2_id[2:], 36)
+
+
+def internal_id_to_ec2_id(internal_id):
+ """Convert an internal ID (int) to an ec2 ID (i-[base 36 number])"""
+ digits = []
+ while internal_id != 0:
+ internal_id, remainder = divmod(internal_id, 36)
+ digits.append('0123456789abcdefghijklmnopqrstuvwxyz'[remainder])
+ return "i-%s" % ''.join(reversed(digits))
+
+
class CloudController(object):
""" CloudController provides the critical dispatch between
inbound API calls through the endpoint and messages
@@ -113,16 +127,6 @@ class CloudController(object):
result[key] = [line]
return result
- def ec2_id_to_internal_id(ec2_id):
- """Convert an ec2 ID (i-[base 36 number]) to an internal id (int)"""
- # TODO(gundlach): Maybe this should actually work?
- return ec2_id[2:]
-
- def internal_id_to_ec2_id(internal_id):
- """Convert an internal ID (int) to an ec2 ID (i-[base 36 number])"""
- # TODO(gundlach): Yo maybe this should actually convert to base 36
- return "i-%d" % internal_id
-
def get_metadata(self, address):
instance_ref = db.fixed_ip_get_instance(None, address)
if instance_ref is None:
diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py
index f79473c00..ad8d81f24 100644
--- a/nova/db/sqlalchemy/api.py
+++ b/nova/db/sqlalchemy/api.py
@@ -378,6 +378,9 @@ def fixed_ip_update(_context, address, values):
###################
+#TODO(gundlach): instance_create and volume_create are nearly identical
+#and should be refactored. I expect there are other copy-and-paste
+#functions between the two of them as well.
def instance_create(_context, values):
instance_ref = models.Instance()
for (key, value) in values.iteritems():
diff --git a/nova/tests/cloud_unittest.py b/nova/tests/cloud_unittest.py
index d316db153..615e589cf 100644
--- a/nova/tests/cloud_unittest.py
+++ b/nova/tests/cloud_unittest.py
@@ -236,7 +236,8 @@ class CloudTestCase(test.TrialTestCase):
def test_update_of_instance_display_fields(self):
inst = db.instance_create({}, {})
- self.cloud.update_instance(self.context, inst['internal_id'],
+ ec2_id = cloud.internal_id_to_ec2_id(inst['internal_id'])
+ self.cloud.update_instance(self.context, ec2_id,
display_name='c00l 1m4g3')
inst = db.instance_get({}, inst['id'])
self.assertEqual('c00l 1m4g3', inst['display_name'])
diff --git a/nova/utils.py b/nova/utils.py
index 86ff3d22e..b1699bda8 100644
--- a/nova/utils.py
+++ b/nova/utils.py
@@ -126,9 +126,15 @@ def runthis(prompt, cmd, check_exit_code = True):
def generate_uid(topic, size=8):
- #TODO(gundlach): we want internal ids to just be ints now. i just dropped
- #off a topic prefix, so what have I broken?
- return random.randint(0, 2**64-1)
+ if topic == "i":
+ # Instances have integer internal ids.
+ #TODO(gundlach): We should make this more than 32 bits, but we need to
+ #figure out how to make the DB happy with 64 bit integers.
+ return random.randint(0, 2**32-1)
+ else:
+ characters = '01234567890abcdefghijklmnopqrstuvwxyz'
+ choices = [random.choice(characters) for x in xrange(size)]
+ return '%s-%s' % (topic, ''.join(choices))
def generate_mac():