summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIsaku Yamahata <yamahata@valinux.co.jp>2011-07-23 16:55:25 +0900
committerIsaku Yamahata <yamahata@valinux.co.jp>2011-07-23 16:55:25 +0900
commitba6b6a20eeedb0311e06090d2f60d36964d67cf4 (patch)
treee61aad73d4aeb529db10a89dbc7afd0c19f9f912
parent1f55e116adbf00a0a5bd990f99a680e9d6b1618e (diff)
block_device: introduce helper function to check swap or ephemeral device
and move generic function, mappings_prepend_dev() from ec2utils to block_device
-rw-r--r--nova/api/ec2/cloud.py8
-rw-r--r--nova/api/ec2/ec2utils.py10
-rw-r--r--nova/block_device.py36
-rw-r--r--nova/tests/test_api.py2
4 files changed, 40 insertions, 16 deletions
diff --git a/nova/api/ec2/cloud.py b/nova/api/ec2/cloud.py
index b25f74f61..c35194f6f 100644
--- a/nova/api/ec2/cloud.py
+++ b/nova/api/ec2/cloud.py
@@ -106,7 +106,7 @@ def _parse_block_device_mapping(bdm):
def _properties_get_mappings(properties):
- return ec2utils.mappings_prepend_dev(properties.get('mappings', []))
+ return block_device.mappings_prepend_dev(properties.get('mappings', []))
def _format_block_device_mapping(bdm):
@@ -145,8 +145,7 @@ def _format_mappings(properties, result):
"""Format multiple BlockDeviceMappingItemType"""
mappings = [{'virtualName': m['virtual'], 'deviceName': m['device']}
for m in _properties_get_mappings(properties)
- if (m['virtual'] == 'swap' or
- m['virtual'].startswith('ephemeral'))]
+ if block_device.is_swap_or_ephemeral(m['virtual'])]
block_device_mapping = [_format_block_device_mapping(bdm) for bdm in
properties.get('block_device_mapping', [])]
@@ -1447,8 +1446,7 @@ class CloudController(object):
if virtual_name in ('ami', 'root'):
continue
- assert (virtual_name == 'swap' or
- virtual_name.startswith('ephemeral'))
+ assert block_device.is_swap_or_ephemeral(virtual_name)
device_name = m['device']
if device_name in [b['device_name'] for b in mapping
if not b.get('no_device', False)]:
diff --git a/nova/api/ec2/ec2utils.py b/nova/api/ec2/ec2utils.py
index 14891debb..bcdf2ba78 100644
--- a/nova/api/ec2/ec2utils.py
+++ b/nova/api/ec2/ec2utils.py
@@ -135,13 +135,3 @@ def dict_from_dotted_str(items):
args[key] = value
return args
-
-
-def mappings_prepend_dev(mappings):
- """Prepend '/dev/' to 'device' entry of swap/ephemeral virtual type"""
- for m in mappings:
- virtual = m['virtual']
- if ((virtual == 'swap' or virtual.startswith('ephemeral')) and
- (not m['device'].startswith('/'))):
- m['device'] = '/dev/' + m['device']
- return mappings
diff --git a/nova/block_device.py b/nova/block_device.py
index 963dffa37..8d95e0029 100644
--- a/nova/block_device.py
+++ b/nova/block_device.py
@@ -15,6 +15,8 @@
# License for the specific language governing permissions and limitations
# under the License.
+import re
+
def properties_root_device_name(properties):
"""get root device name from image meta data.
@@ -33,3 +35,37 @@ def properties_root_device_name(properties):
root_device_name = properties['root_device_name']
return root_device_name
+
+
+_ephemeral = re.compile('^ephemeral(\d|[1-9]\d+)$')
+
+
+def is_ephemeral(device_name):
+ return _ephemeral.match(device_name)
+
+
+def ephemeral_num(ephemeral_name):
+ assert is_ephemeral(ephemeral_name)
+ return int(_ephemeral.sub('\\1', ephemeral_name))
+
+
+def is_swap_or_ephemeral(device_name):
+ return device_name == 'swap' or is_ephemeral(device_name)
+
+
+def mappings_prepend_dev(mappings):
+ """Prepend '/dev/' to 'device' entry of swap/ephemeral virtual type"""
+ for m in mappings:
+ virtual = m['virtual']
+ if (is_swap_or_ephemeral(virtual) and
+ (not m['device'].startswith('/'))):
+ m['device'] = '/dev/' + m['device']
+ return mappings
+
+
+_dev = re.compile('^/dev/')
+
+
+def strip_dev(device_name):
+ """remove leading '/dev/'"""
+ return _dev.sub('', device_name)
diff --git a/nova/tests/test_api.py b/nova/tests/test_api.py
index d5f653bc6..e3d2ee2fc 100644
--- a/nova/tests/test_api.py
+++ b/nova/tests/test_api.py
@@ -187,7 +187,7 @@ class Ec2utilsTestCase(test.TestCase):
'device': '/dev/sdc1'},
{'virtual': 'ephemeral1',
'device': '/dev/sdc1'}]
- self.assertDictListMatch(ec2utils.mappings_prepend_dev(mappings),
+ self.assertDictListMatch(block_device.mappings_prepend_dev(mappings),
expected_result)