~ubuntu-branches/ubuntu/saucy/nova/saucy-proposed

« back to all changes in this revision

Viewing changes to nova/api/ec2/ec2utils.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-05-24 13:12:53 UTC
  • mfrom: (1.1.55)
  • Revision ID: package-import@ubuntu.com-20120524131253-ommql08fg1en06ut
Tags: 2012.2~f1-0ubuntu1
* New upstream release.
* Prepare for quantal:
  - Dropped debian/patches/upstream/0006-Use-project_id-in-ec2.cloud._format_image.patch
  - Dropped debian/patches/upstream/0005-Populate-image-properties-with-project_id-again.patch
  - Dropped debian/patches/upstream/0004-Fixed-bug-962840-added-a-test-case.patch
  - Dropped debian/patches/upstream/0003-Allow-unprivileged-RADOS-users-to-access-rbd-volumes.patch
  - Dropped debian/patches/upstream/0002-Stop-libvirt-test-from-deleting-instances-dir.patch
  - Dropped debian/patches/upstream/0001-fix-bug-where-nova-ignores-glance-host-in-imageref.patch 
  - Dropped debian/patches/0001-fix-useexisting-deprecation-warnings.patch
* debian/control: Add python-keystone as a dependency. (LP: #907197)
* debian/patches/kombu_tests_timeout.patch: Refreshed.
* debian/nova.conf, debian/nova-common.postinst: Convert to new ini
  file configuration
* debian/patches/nova-manage_flagfile_location.patch: Refreshed

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
import re
20
20
 
 
21
from nova import context
21
22
from nova import db
22
23
from nova import exception
23
24
from nova import flags
24
25
from nova import log as logging
25
 
from nova import network
26
26
from nova.network import model as network_model
 
27
from nova import utils
27
28
 
28
29
 
29
30
FLAGS = flags.FLAGS
132
133
    return template % int(instance_id)
133
134
 
134
135
 
135
 
def id_to_ec2_snap_id(instance_id):
136
 
    """Convert an snapshot ID (int) to an ec2 snapshot ID
137
 
    (snap-[base 16 number])"""
138
 
    return id_to_ec2_id(instance_id, 'snap-%08x')
139
 
 
140
 
 
141
 
def id_to_ec2_vol_id(instance_id):
142
 
    """Convert an volume ID (int) to an ec2 volume ID (vol-[base 16 number])"""
143
 
    return id_to_ec2_id(instance_id, 'vol-%08x')
144
 
 
 
136
def id_to_ec2_snap_id(snapshot_id):
 
137
    """Get or create an ec2 volume ID (vol-[base 16 number]) from uuid."""
 
138
    if utils.is_uuid_like(snapshot_id):
 
139
        ctxt = context.get_admin_context()
 
140
        int_id = get_int_id_from_snapshot_uuid(ctxt, snapshot_id)
 
141
        return id_to_ec2_id(int_id)
 
142
    else:
 
143
        return id_to_ec2_id(snapshot_id, 'snap-%08x')
 
144
 
 
145
 
 
146
def id_to_ec2_vol_id(volume_id):
 
147
    """Get or create an ec2 volume ID (vol-[base 16 number]) from uuid."""
 
148
    if utils.is_uuid_like(volume_id):
 
149
        ctxt = context.get_admin_context()
 
150
        int_id = get_int_id_from_volume_uuid(ctxt, volume_id)
 
151
        return id_to_ec2_id(int_id)
 
152
    else:
 
153
        return id_to_ec2_id(volume_id, 'vol-%08x')
 
154
 
 
155
 
 
156
def ec2_vol_id_to_uuid(ec2_id):
 
157
    """Get the cooresponding UUID for the given ec2-id."""
 
158
    ctxt = context.get_admin_context()
 
159
 
 
160
    # NOTE(jgriffith) first strip prefix to get just the numeric
 
161
    int_id = ec2_id_to_id(ec2_id)
 
162
    return get_volume_uuid_from_int_id(ctxt, int_id)
 
163
 
 
164
 
 
165
def get_int_id_from_volume_uuid(context, volume_uuid):
 
166
    if volume_uuid is None:
 
167
        return
 
168
    try:
 
169
        return db.get_ec2_volume_id_by_uuid(context, volume_uuid)
 
170
    except exception.NotFound:
 
171
        raise exception.VolumeNotFound()
 
172
 
 
173
 
 
174
def get_volume_uuid_from_int_id(context, int_id):
 
175
    return db.get_volume_uuid_by_ec2_id(context, int_id)
 
176
 
 
177
 
 
178
def ec2_snap_id_to_uuid(ec2_id):
 
179
    """Get the cooresponding UUID for the given ec2-id."""
 
180
    ctxt = context.get_admin_context()
 
181
 
 
182
    # NOTE(jgriffith) first strip prefix to get just the numeric
 
183
    int_id = ec2_id_to_id(ec2_id)
 
184
    return get_snapshot_uuid_from_int_id(ctxt, int_id)
 
185
 
 
186
 
 
187
def get_int_id_from_snapshot_uuid(context, snapshot_uuid):
 
188
    if snapshot_uuid is None:
 
189
        return
 
190
    try:
 
191
        return db.get_ec2_snapshot_id_by_uuid(context, snapshot_uuid)
 
192
    except exception.NotFound:
 
193
        raise exception.SnapshotNotFound()
 
194
 
 
195
 
 
196
def get_snapshot_uuid_from_int_id(context, int_id):
 
197
    return db.get_snapshot_uuid_by_ec2_id(context, int_id)
 
198
 
 
199
 
 
200
def ec2_instance_id_to_uuid(context, ec2_id):
 
201
    int_id = ec2_id_to_id(ec2_id)
 
202
    return db.instance_get(context, int_id)['uuid']
145
203
 
146
204
_c2u = re.compile('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))')
147
205
 
166
224
    *             try conversion to int, float, complex, fallback value
167
225
 
168
226
    """
 
227
    def _negative_zero(value):
 
228
        epsilon = 1e-7
 
229
        return 0 if abs(value) < epsilon else value
 
230
 
169
231
    if len(value) == 0:
170
232
        return ''
171
233
    if value == 'None':
175
237
        return True
176
238
    if lowered_value == 'false':
177
239
        return False
178
 
    valueneg = value[1:] if value[0] == '-' else value
179
 
    if valueneg == '0':
180
 
        return 0
181
 
    if valueneg == '':
182
 
        return value
183
 
    if valueneg[0] == '0':
184
 
        if valueneg[1] in 'xX':
185
 
            return int(value, 16)
186
 
        elif valueneg[1] in 'bB':
187
 
            return int(value, 2)
188
 
        else:
189
 
            try:
190
 
                return int(value, 8)
191
 
            except ValueError:
192
 
                pass
193
 
    try:
194
 
        return int(value)
195
 
    except ValueError:
196
 
        pass
197
 
    try:
198
 
        return float(value)
199
 
    except ValueError:
200
 
        pass
201
 
    try:
202
 
        return complex(value)
 
240
    for prefix, base in [('0x', 16), ('0b', 2), ('0', 8), ('', 10)]:
 
241
        try:
 
242
            if lowered_value.startswith((prefix, "-" + prefix)):
 
243
                return int(lowered_value, base)
 
244
        except ValueError:
 
245
            pass
 
246
    try:
 
247
        return _negative_zero(float(value))
203
248
    except ValueError:
204
249
        return value
205
250