Merge lp:~termie/nova/ec2_ids into lp:~hudson-openstack/nova/trunk

Proposed by termie
Status: Merged
Approved by: Eric Day
Approved revision: 561
Merged at revision: 560
Proposed branch: lp:~termie/nova/ec2_ids
Merge into: lp:~hudson-openstack/nova/trunk
Diff against target: 181 lines (+36/-23)
4 files modified
nova/api/ec2/cloud.py (+20/-16)
nova/db/api.py (+4/-0)
nova/db/sqlalchemy/models.py (+2/-2)
nova/tests/test_cloud.py (+10/-5)
To merge this branch: bzr merge lp:~termie/nova/ec2_ids
Reviewer Review Type Date Requested Status
Eric Day (community) Approve
Jay Pipes (community) Approve
Todd Willey (community) Approve
Review via email: mp+46068@code.launchpad.net

Description of the change

Fixes related to how EC2 ids are displayed and dealt with.

Additionally adds two flags that define a template string that is used for the internal naming of things (like the volume name of a logical volume on disk), default being similar to the EC2 format, so that the ids are easy to match while testing when you may need to manually delete or check something.

To post a comment you must log in.
Revision history for this message
Todd Willey (xtoddx) wrote :

lgtm

review: Approve
Revision history for this message
Jay Pipes (jaypipes) wrote :

I likee.

review: Approve
Revision history for this message
Eric Day (eday) wrote :

lgtm

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'nova/api/ec2/cloud.py'
2--- nova/api/ec2/cloud.py 2011-01-12 11:34:16 +0000
3+++ nova/api/ec2/cloud.py 2011-01-13 00:59:40 +0000
4@@ -73,17 +73,13 @@
5
6
7 def ec2_id_to_id(ec2_id):
8- """Convert an ec2 ID (i-[base 36 number]) to an instance id (int)"""
9- return int(ec2_id[2:], 36)
10-
11-
12-def id_to_ec2_id(instance_id):
13- """Convert an instance ID (int) to an ec2 ID (i-[base 36 number])"""
14- digits = []
15- while instance_id != 0:
16- instance_id, remainder = divmod(instance_id, 36)
17- digits.append('0123456789abcdefghijklmnopqrstuvwxyz'[remainder])
18- return "i-%s" % ''.join(reversed(digits))
19+ """Convert an ec2 ID (i-[base 16 number]) to an instance id (int)"""
20+ return int(ec2_id.split('-')[-1], 16)
21+
22+
23+def id_to_ec2_id(instance_id, template='i-%08x'):
24+ """Convert an instance ID (int) to an ec2 ID (i-[base 16 number])"""
25+ return template % instance_id
26
27
28 class CloudController(object):
29@@ -541,6 +537,8 @@
30 return self.compute_api.get_ajax_console(context, internal_id)
31
32 def describe_volumes(self, context, volume_id=None, **kwargs):
33+ if volume_id:
34+ volume_id = [ec2_id_to_id(x) for x in volume_id]
35 volumes = self.volume_api.get_all(context)
36 # NOTE(vish): volume_id is an optional list of volume ids to filter by.
37 volumes = [self._format_volume(context, v) for v in volumes
38@@ -556,7 +554,7 @@
39 instance_data = '%s[%s]' % (instance_ec2_id,
40 volume['instance']['host'])
41 v = {}
42- v['volumeId'] = volume['id']
43+ v['volumeId'] = id_to_ec2_id(volume['id'], 'vol-%08x')
44 v['status'] = volume['status']
45 v['size'] = volume['size']
46 v['availabilityZone'] = volume['availability_zone']
47@@ -574,7 +572,8 @@
48 'device': volume['mountpoint'],
49 'instanceId': instance_ec2_id,
50 'status': 'attached',
51- 'volume_id': volume['ec2_id']}]
52+ 'volumeId': id_to_ec2_id(volume['id'],
53+ 'vol-%08x')}]
54 else:
55 v['attachmentSet'] = [{}]
56
57@@ -593,10 +592,12 @@
58 return {'volumeSet': [self._format_volume(context, dict(volume_ref))]}
59
60 def delete_volume(self, context, volume_id, **kwargs):
61+ volume_id = ec2_id_to_id(volume_id)
62 self.volume_api.delete(context, volume_id)
63 return True
64
65 def update_volume(self, context, volume_id, **kwargs):
66+ volume_id = ec2_id_to_id(volume_id)
67 updatable_fields = ['display_name', 'display_description']
68 changes = {}
69 for field in updatable_fields:
70@@ -607,18 +608,21 @@
71 return True
72
73 def attach_volume(self, context, volume_id, instance_id, device, **kwargs):
74+ volume_id = ec2_id_to_id(volume_id)
75+ instance_id = ec2_id_to_id(instance_id)
76 LOG.audit(_("Attach volume %s to instacne %s at %s"), volume_id,
77 instance_id, device, context=context)
78 self.compute_api.attach_volume(context, instance_id, volume_id, device)
79 volume = self.volume_api.get(context, volume_id)
80 return {'attachTime': volume['attach_time'],
81 'device': volume['mountpoint'],
82- 'instanceId': instance_id,
83+ 'instanceId': id_to_ec2_id(instance_id),
84 'requestId': context.request_id,
85 'status': volume['attach_status'],
86- 'volumeId': volume_id}
87+ 'volumeId': id_to_ec2_id(volume_id, 'vol-%08x')}
88
89 def detach_volume(self, context, volume_id, **kwargs):
90+ volume_id = ec2_id_to_id(volume_id)
91 LOG.audit(_("Detach volume %s"), volume_id, context=context)
92 volume = self.volume_api.get(context, volume_id)
93 instance = self.compute_api.detach_volume(context, volume_id)
94@@ -627,7 +631,7 @@
95 'instanceId': id_to_ec2_id(instance['id']),
96 'requestId': context.request_id,
97 'status': volume['attach_status'],
98- 'volumeId': volume_id}
99+ 'volumeId': id_to_ec2_id(volume_id, 'vol-%08x')}
100
101 def _convert_to_set(self, lst, label):
102 if lst == None or lst == []:
103
104=== modified file 'nova/db/api.py'
105--- nova/db/api.py 2011-01-12 11:34:16 +0000
106+++ nova/db/api.py 2011-01-13 00:59:40 +0000
107@@ -42,6 +42,10 @@
108 'The backend to use for db')
109 flags.DEFINE_boolean('enable_new_services', True,
110 'Services to be added to the available pool on create')
111+flags.DEFINE_string('instance_name_template', 'instance-%08x',
112+ 'Template string to be used to generate instance names')
113+flags.DEFINE_string('volume_name_template', 'volume-%08x',
114+ 'Template string to be used to generate instance names')
115
116
117 IMPL = utils.LazyPluggable(FLAGS['db_backend'],
118
119=== modified file 'nova/db/sqlalchemy/models.py'
120--- nova/db/sqlalchemy/models.py 2011-01-12 11:34:16 +0000
121+++ nova/db/sqlalchemy/models.py 2011-01-13 00:59:40 +0000
122@@ -169,7 +169,7 @@
123
124 @property
125 def name(self):
126- return "instance-%08x" % self.id
127+ return FLAGS.instance_name_template % self.id
128
129 admin_pass = Column(String(255))
130 user_id = Column(String(255))
131@@ -256,7 +256,7 @@
132
133 @property
134 def name(self):
135- return "volume-%08x" % self.id
136+ return FLAGS.volume_name_template % self.id
137
138 user_id = Column(String(255))
139 project_id = Column(String(255))
140
141=== modified file 'nova/tests/test_cloud.py'
142--- nova/tests/test_cloud.py 2011-01-12 11:34:16 +0000
143+++ nova/tests/test_cloud.py 2011-01-13 00:59:40 +0000
144@@ -126,10 +126,13 @@
145 vol2 = db.volume_create(self.context, {})
146 result = self.cloud.describe_volumes(self.context)
147 self.assertEqual(len(result['volumeSet']), 2)
148+ volume_id = cloud.id_to_ec2_id(vol2['id'], 'vol-%08x')
149 result = self.cloud.describe_volumes(self.context,
150- volume_id=[vol2['id']])
151+ volume_id=[volume_id])
152 self.assertEqual(len(result['volumeSet']), 1)
153- self.assertEqual(result['volumeSet'][0]['volumeId'], vol2['id'])
154+ self.assertEqual(
155+ cloud.ec2_id_to_id(result['volumeSet'][0]['volumeId']),
156+ vol2['id'])
157 db.volume_destroy(self.context, vol1['id'])
158 db.volume_destroy(self.context, vol2['id'])
159
160@@ -385,7 +388,8 @@
161
162 def test_update_of_volume_display_fields(self):
163 vol = db.volume_create(self.context, {})
164- self.cloud.update_volume(self.context, vol['id'],
165+ self.cloud.update_volume(self.context,
166+ cloud.id_to_ec2_id(vol['id'], 'vol-%08x'),
167 display_name='c00l v0lum3')
168 vol = db.volume_get(self.context, vol['id'])
169 self.assertEqual('c00l v0lum3', vol['display_name'])
170@@ -393,8 +397,9 @@
171
172 def test_update_of_volume_wont_update_private_fields(self):
173 vol = db.volume_create(self.context, {})
174- self.cloud.update_volume(self.context, vol['id'],
175- mountpoint='/not/here')
176+ self.cloud.update_volume(self.context,
177+ cloud.id_to_ec2_id(vol['id'], 'vol-%08x'),
178+ mountpoint='/not/here')
179 vol = db.volume_get(self.context, vol['id'])
180 self.assertEqual(None, vol['mountpoint'])
181 db.volume_destroy(self.context, vol['id'])