コード例 #1
0
ファイル: aggregate.py プロジェクト: ChaoyiHuang/trio2o
    def post(self, **kw):
        context = t_context.extract_context_from_environ()
        if not context.is_admin:
            return utils.format_nova_error(
                403, _("Policy doesn't allow os_compute_api:os-aggregates:"
                       "index to be performed."))
        if 'aggregate' not in kw:
            return utils.format_nova_error(
                400, _('aggregate is not set'))

        host_aggregate = kw['aggregate']
        name = host_aggregate['name'].strip()
        avail_zone = host_aggregate.get('availability_zone')
        if avail_zone:
            avail_zone = avail_zone.strip()

        try:
            with context.session.begin():
                aggregate = az_ag.create_ag_az(context,
                                               ag_name=name,
                                               az_name=avail_zone)
        except db_exc.DBDuplicateEntry:
            return utils.format_nova_error(
                409, _('Aggregate %s already exists.') % name)
        except Exception:
            return utils.format_nova_error(
                500, _('Fail to create aggregate'))

        return {'aggregate': aggregate}
コード例 #2
0
ファイル: aggregate.py プロジェクト: ChaoyiHuang/trio2o
 def delete(self, _id):
     context = t_context.extract_context_from_environ()
     try:
         with context.session.begin():
             az_ag.delete_ag(context, _id)
             pecan.response.status = 200
     except t_exc.ResourceNotFound:
         return utils.format_nova_error(
             404, _('Aggregate %s could not be found.') % _id)
     except Exception:
         return utils.format_nova_error(
             500, _('Fail to delete aggregate %s') % _id)
コード例 #3
0
ファイル: aggregate.py プロジェクト: ChaoyiHuang/trio2o
 def get_one(self, _id):
     context = t_context.extract_context_from_environ()
     try:
         with context.session.begin():
             aggregate = az_ag.get_one_ag(context, _id)
             return {'aggregate': aggregate}
     except t_exc.ResourceNotFound:
         return utils.format_nova_error(
             404, _('Aggregate %s could not be found.') % _id)
     except Exception:
         return utils.format_nova_error(
             500, _('Fail to get aggregate %s') % _id)
コード例 #4
0
ファイル: image.py プロジェクト: ChaoyiHuang/trio2o
 def get_one(self, _id):
     context = t_context.extract_context_from_environ()
     if _id == 'detail':
         return self.get_all()
     image = self.client.get_images(context, _id)
     if not image:
         return utils.format_nova_error(404, _('Image not found'))
     return {'image': self._construct_show_image_entry(context, image)}
コード例 #5
0
ファイル: aggregate.py プロジェクト: ChaoyiHuang/trio2o
    def get_all(self):
        context = t_context.extract_context_from_environ()

        try:
            with context.session.begin():
                aggregates = az_ag.get_all_ag(context)
        except Exception:
            return utils.format_nova_error(500, _('Fail to list aggregates'))
        return {'aggregates': aggregates}
コード例 #6
0
ファイル: action.py プロジェクト: ChaoyiHuang/trio2o
    def post(self, **kw):
        context = t_context.extract_context_from_environ()

        action_handle = None
        action_type = None
        for _type in self.handle_map:
            if _type in kw:
                action_handle = self.handle_map[_type]
                action_type = _type
        if not action_handle:
            return utils.format_nova_error(400,
                                           _('Server action not supported'))

        server_mappings = db_api.get_bottom_mappings_by_top_id(
            context, self.server_id, constants.RT_SERVER)
        if not server_mappings:
            return utils.format_nova_error(
                404,
                _('Server %s could not be found') % self.server_id)

        pod_name = server_mappings[0][0]['pod_name']
        try:
            resp, body = action_handle(context, pod_name, kw)
            pecan.response.status = resp.status_code
            if not body:
                return pecan.response
            else:
                return body
        except Exception as e:
            code = 500
            message = _('Action %(action)s on server %(server_id)s fails') % {
                'action': action_type,
                'server_id': self.server_id
            }
            if hasattr(e, 'code'):
                code = e.code
            ex_message = str(e)
            if ex_message:
                message = ex_message
            LOG.error(message)
            return utils.format_nova_error(code, message)
コード例 #7
0
ファイル: aggregate.py プロジェクト: ChaoyiHuang/trio2o
 def post(self, **kw):
     context = t_context.extract_context_from_environ()
     if not context.is_admin:
         return utils.format_nova_error(
             403, _("Policy doesn't allow os_compute_api:os-aggregates:"
                    "index to be performed."))
     try:
         with context.session.begin():
             core.get_resource(context, models.Aggregate, self.aggregate_id)
     except t_exc.ResourceNotFound:
         return utils.format_nova_error(
             404, _('Aggregate %s could not be found.') % self.aggregate_id)
     if 'add_host' in kw or 'remove_host' in kw:
         return utils.format_nova_error(
             400, _('Add and remove host action not supported'))
     # TODO(zhiyuan) handle aggregate metadata updating
     try:
         aggregate = az_ag.get_one_ag(context, self.aggregate_id)
         return {'aggregate': aggregate}
     except Exception:
         return utils.format_nova_error(
             500, _('Aggregate operation on %s failed') % self.aggregate_id)
コード例 #8
0
ファイル: volume.py プロジェクト: ChaoyiHuang/trio2o
    def post(self, **kw):
        context = t_context.extract_context_from_environ()

        if 'volumeAttachment' not in kw:
            return utils.format_nova_error(
                400, _('volumeAttachment is not set'))
        body = kw['volumeAttachment']
        if 'volumeId' not in body:
            return utils.format_nova_error(
                400, _('Invalid input for field/attribute volumeAttachment'))

        server_mappings = db_api.get_bottom_mappings_by_top_id(
            context, self.server_id, constants.RT_SERVER)
        if not server_mappings:
            return utils.format_nova_error(404, _('Instance %s could not be '
                                                  'found.') % self.server_id)
        volume_mappings = db_api.get_bottom_mappings_by_top_id(
            context, body['volumeId'], constants.RT_VOLUME)
        if not volume_mappings:
            return utils.format_nova_error(
                404, _('Volume %s could not be found') % body['volumeId'])

        server_pod_name = server_mappings[0][0]['pod_name']
        volume_pod_name = volume_mappings[0][0]['pod_name']
        if server_pod_name != volume_pod_name:
            LOG.error(_LE('Server %(server)s is in pod %(server_pod)s and '
                          'volume %(volume)s is in pod %(volume_pod)s, which '
                          'are not the same.'),
                      {'server': self.server_id,
                       'server_pod': server_pod_name,
                       'volume': body['volumeId'],
                       'volume_pod': volume_pod_name})
            return utils.format_nova_error(
                400, _('Server and volume not in the same pod'))

        device = None
        if 'device' in body:
            device = body['device']
            # this regular expression is copied from nova/block_device.py
            match = re.match('(^/dev/x{0,1}[a-z]{0,1}d{0,1})([a-z]+)[0-9]*$',
                             device)
            if not match:
                return utils.format_nova_error(
                    400, _('The supplied device path (%s) is '
                           'invalid.') % device)

        client = self._get_client(server_pod_name)
        volume = client.action_server_volumes(
            context, 'create_server_volume',
            server_mappings[0][1], volume_mappings[0][1], device)
        return {'volumeAttachment': volume.to_dict()}
コード例 #9
0
 def get_one(self, _id):
     context = t_context.extract_context_from_environ()
     network = self.client.get_networks(context, _id)
     if not network:
         return utils.format_nova_error(404, _('Network not found'))
     return {'network': self._construct_network_entry(network)}
コード例 #10
0
 def test_format_nova_error(self, mock_format_error):
     output = utils.format_nova_error(400, 'this is error')
     self.assertEqual(mock_format_error.return_value, output)