~0x44/nova/bug838466

« back to all changes in this revision

Viewing changes to nova/api/openstack/servers.py

  • Committer: Cerberus
  • Date: 2011-02-28 17:39:23 UTC
  • mfrom: (749 nova)
  • mto: This revision was merged to the branch mainline in revision 762.
  • Revision ID: matt.dietz@rackspace.com-20110228173923-1e3upi2ddoc2j4xl
Merge from trunk and merge conflict resolution

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
 
34
34
 
35
35
LOG = logging.getLogger('server')
36
 
LOG.setLevel(logging.DEBUG)
37
36
 
38
37
 
39
38
FLAGS = flags.FLAGS
64
63
    inst_dict['addresses'] = dict(public=[], private=[])
65
64
 
66
65
    # grab single private fixed ip
67
 
    try:
68
 
        private_ip = inst['fixed_ip']['address']
69
 
        if private_ip:
70
 
            inst_dict['addresses']['private'].append(private_ip)
71
 
    except KeyError:
72
 
        LOG.debug(_("Failed to read private ip"))
 
66
    private_ips = utils.get_from_path(inst, 'fixed_ip/address')
 
67
    inst_dict['addresses']['private'] = private_ips
73
68
 
74
69
    # grab all public floating ips
75
 
    try:
76
 
        for floating in inst['fixed_ip']['floating_ips']:
77
 
            inst_dict['addresses']['public'].append(floating['address'])
78
 
    except KeyError:
79
 
        LOG.debug(_("Failed to read public ip(s)"))
 
70
    public_ips = utils.get_from_path(inst, 'fixed_ip/floating_ips/address')
 
71
    inst_dict['addresses']['public'] = public_ips
80
72
 
81
 
    inst_dict['metadata'] = {}
82
73
    inst_dict['hostId'] = ''
83
74
 
 
75
    # Return the metadata as a dictionary
 
76
    metadata = {}
 
77
    for item in inst['metadata']:
 
78
        metadata[item['key']] = item['value']
 
79
    inst_dict['metadata'] = metadata
 
80
 
84
81
    return dict(server=inst_dict)
85
82
 
86
83
 
148
145
            try:
149
146
                return image['properties'][param]
150
147
            except KeyError:
151
 
                raise exception.NotFound(
 
148
                LOG.debug(
152
149
                    _("%(param)s property not found for image %(_image_id)s") %
153
150
                      locals())
 
151
            return None
154
152
 
155
153
        image_id = str(image_id)
156
154
        image = self._image_service.show(req.environ['nova.context'], image_id)
162
160
        if not env:
163
161
            return faults.Fault(exc.HTTPUnprocessableEntity())
164
162
 
165
 
        key_pair = auth_manager.AuthManager.get_key_pairs(
166
 
            req.environ['nova.context'])[0]
 
163
        context = req.environ['nova.context']
 
164
        key_pairs = auth_manager.AuthManager.get_key_pairs(context)
 
165
        if not key_pairs:
 
166
            raise exception.NotFound(_("No keypairs defined"))
 
167
        key_pair = key_pairs[0]
 
168
 
167
169
        image_id = common.get_image_id_from_image_hash(self._image_service,
168
 
            req.environ['nova.context'], env['server']['imageId'])
 
170
            context, env['server']['imageId'])
169
171
        kernel_id, ramdisk_id = self._get_kernel_ramdisk_from_image(
170
172
            req, image_id)
 
173
 
 
174
        # Metadata is a list, not a Dictionary, because we allow duplicate keys
 
175
        # (even though JSON can't encode this)
 
176
        # In future, we may not allow duplicate keys.
 
177
        # However, the CloudServers API is not definitive on this front,
 
178
        #  and we want to be compatible.
 
179
        metadata = []
 
180
        if env['server'].get('metadata'):
 
181
            for k, v in env['server']['metadata'].items():
 
182
                metadata.append({'key': k, 'value': v})
 
183
 
171
184
        instances = self.compute_api.create(
172
 
            req.environ['nova.context'],
 
185
            context,
173
186
            instance_types.get_by_flavor_id(env['server']['flavorId']),
174
187
            image_id,
175
188
            kernel_id=kernel_id,
178
191
            display_description=env['server']['name'],
179
192
            key_name=key_pair['name'],
180
193
            key_data=key_pair['public_key'],
 
194
            metadata=metadata,
181
195
            onset_files=env.get('onset_files', []))
182
196
        return _translate_keys(instances[0])
183
197
 
326
340
            return faults.Fault(exc.HTTPUnprocessableEntity())
327
341
        return exc.HTTPAccepted()
328
342
 
 
343
    def inject_network_info(self, req, id):
 
344
        """
 
345
        Inject network info for an instance (admin only).
 
346
 
 
347
        """
 
348
        context = req.environ['nova.context']
 
349
        try:
 
350
            self.compute_api.inject_network_info(context, id)
 
351
        except:
 
352
            readable = traceback.format_exc()
 
353
            LOG.exception(_("Compute.api::inject_network_info %s"), readable)
 
354
            return faults.Fault(exc.HTTPUnprocessableEntity())
 
355
        return exc.HTTPAccepted()
 
356
 
329
357
    def pause(self, req, id):
330
358
        """ Permit Admins to Pause the server. """
331
359
        ctxt = req.environ['nova.context']