def delete(self, _id): context = t_context.extract_context_from_environ() # TODO(joehuang): get the release of top and bottom t_release = cons.R_MITAKA b_release = cons.R_MITAKA s_ctx = hclient.get_res_routing_ref(context, _id, request.url, cons.ST_CINDER) if not s_ctx: return utils.format_cinder_error( 404, _('Volume %s could not be found.') % _id) if s_ctx['b_url'] == '': return utils.format_cinder_error( 404, _('Bottom Pod endpoint incorrect')) b_headers = hclient.convert_header(t_release, b_release, request.headers) resp = hclient.forward_req(context, 'DELETE', b_headers, s_ctx['b_url'], request.body) response.status = resp.status_code # don't remove the resource routing for delete is async. operation # remove the routing when query is executed but not find # No content in the resp actually return response
def get_all(self): # TODO(joehuang): here should return link instead, # now combined with 'detail' context = t_context.extract_context_from_environ() return {'volumes': self._get_all(context)}
def delete(self, _id): context = t_context.extract_context_from_environ() if not t_context.is_admin_context(context): pecan.abort(400, _('Admin role required to delete pods')) return try: with context.session.begin(): pod = core.get_resource(context, models.Pod, _id) if pod is not None: ag_name = utils.get_ag_name(pod['pod_name']) ag = az_ag.get_ag_by_name(context, ag_name) if ag is not None: az_ag.delete_ag(context, ag['id']) core.delete_resource(context, models.Pod, _id) pecan.response.status = 200 return {} except t_exc.ResourceNotFound: return Response(_('Pod not found'), 404) except Exception as e: LOG.exception( _LE('Failed to delete pod: %(pod_id)s,' '%(exception)s'), { 'pod_id': _id, 'exception': e }) return Response(_('Failed to delete pod'), 500)
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}
def get_one(self, _id): # NOTE(zhiyuan) this function handles two kinds of requests # GET /flavors/flavor_id # GET /flavors/detail context = t_context.extract_context_from_environ() if _id == 'detail': with context.session.begin(): flavors = core.query_resource(context, models.InstanceTypes, [], []) for flavor in flavors: flavor['id'] = flavor['flavorid'] del flavor['flavorid'] return {'flavors': flavors} else: with context.session.begin(): flavors = core.query_resource(context, models.InstanceTypes, [{ 'key': 'flavorid', 'comparator': 'eq', 'value': _id }], []) if not flavors: return utils.format_nova_error( 404, _('Flavor %s could not be found') % _id) flavor = flavors[0] flavor['id'] = flavor['flavorid'] del flavor['flavorid'] return {'flavor': flavor}
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)
def get_all(self): context = t_context.extract_context_from_environ() networks = self.client.list_networks(context) return { 'networks': [self._construct_network_entry(network) for network in networks] }
def delete(self, _id): """Marks volume types as deleted. :param _id: id of volume type to be deleted """ context = t_context.extract_context_from_environ() if not context.is_admin: return utils.format_cinder_error( 403, _("Policy doesn't allow volume_extension:types_manage " "to be performed.")) session = core.get_session() with session.begin(): try: db_api.volume_type_get(context, _id, session) except exceptions.VolumeTypeNotFound as e: return utils.format_cinder_error(404, e.message) try: db_api.volume_type_delete(context, _id, session) except Exception as e: LOG.exception( _LE('Fail to update volume type: %(id)s,' '%(exception)s'), { 'id': _id, 'exception': e }) return utils.format_cinder_error( 500, _('Fail to delete volume type.')) pecan.response.status = 202 return pecan.response
def delete(self, _id): context = t_context.extract_context_from_environ() if not policy.enforce(context, policy.ADMIN_API_PODS_DELETE): pecan.abort(401, _('Unauthorized to delete pods')) return try: with context.session.begin(): pod = core.get_resource(context, models.Pod, _id) if pod is not None: ag_name = utils.get_ag_name(pod['pod_name']) ag = az_ag.get_ag_by_name(context, ag_name) if ag is not None: az_ag.delete_ag(context, ag['id']) core.delete_resource(context, models.Pod, _id) pecan.response.status = 200 except t_exc.ResourceNotFound: return Response(_('Pod not found'), 404) except Exception as e: LOG.exception( _LE('Failed to delete pod: %(pod_id)s,' '%(exception)s'), { 'pod_id': _id, 'exception': e }) return Response(_('Failed to delete pod'), 500)
def post(self, **kw): context = t_context.extract_context_from_environ() if not t_context.is_admin_context(context): pecan.abort(400, _('Admin role required to create bindings')) return if 'pod_binding' not in kw: pecan.abort(400, _('Request body not found')) return pod_b = kw['pod_binding'] tenant_id = pod_b.get('tenant_id', '').strip() pod_id = pod_b.get('pod_id', '').strip() _uuid = uuidutils.generate_uuid() if tenant_id == '' or pod_id == '': return Response(_('Tenant_id and pod_id can not be empty'), 422) # the az_pod_map_id should be exist for in the pod map table try: with context.session.begin(): pod = core.get_resource(context, models.Pod, pod_id) if pod.get('az_name') == '': return Response(_('Top region can not be bound'), 422) except t_exc.ResourceNotFound: return Response(_('pod_id not found in pod'), 422) except Exception as e: LOG.exception( _LE('Failed to get_resource for pod_id: ' '%(pod_id)s ,' '%(exception)s '), { 'pod_id': pod_id, 'exception': e }) pecan.abort(500, _('Failed to create pod binding')) return try: with context.session.begin(): pod_binding = core.create_resource(context, models.PodBinding, { 'id': _uuid, 'tenant_id': tenant_id, 'pod_id': pod_id }) except db_exc.DBDuplicateEntry: return Response(_('Pod binding already exists'), 409) except db_exc.DBConstraintError: return Response(_('pod_id not exists in pod'), 422) except db_exc.DBReferenceError: return Response(_('DB reference not exists in pod'), 422) except Exception as e: LOG.exception(_LE('Failed to create pod binding: %(exception)s '), {'exception': e}) pecan.abort(500, _('Failed to create pod binding')) return return {'pod_binding': pod_binding}
def get_one(self, _id): context = t_context.extract_context_from_environ() if _id == 'detail': return {'volumes': self._get_all(context)} # TODO(joehuang): get the release of top and bottom t_release = cons.R_MITAKA b_release = cons.R_MITAKA b_headers = hclient.convert_header(t_release, b_release, request.headers) s_ctx = hclient.get_res_routing_ref(context, _id, request.url, cons.ST_CINDER) if not s_ctx: return utils.format_cinder_error( 404, _('Volume %s could not be found.') % _id) if s_ctx['b_url'] == '': return utils.format_cinder_error( 404, _('Bottom Pod endpoint incorrect')) resp = hclient.forward_req(context, 'GET', b_headers, s_ctx['b_url'], request.body) b_ret_body = jsonutils.loads(resp.content) b_status = resp.status_code response.status = b_status if b_status == 200: if b_ret_body.get('volume') is not None: b_vol_ret = b_ret_body['volume'] ret_vol = hclient.convert_object(b_release, t_release, b_vol_ret, res_type=cons.RT_VOLUME) pod = utils.get_pod_by_top_id(context, _id) if pod: ret_vol['availability_zone'] = pod['az_name'] return {'volume': ret_vol} # resource not find but routing exist, remove the routing if b_status == 404: filters = [{ 'key': 'top_id', 'comparator': 'eq', 'value': _id }, { 'key': 'resource_type', 'comparator': 'eq', 'value': cons.RT_VOLUME }] with context.session.begin(): core.delete_resources(context, models.ResourceRouting, filters) return b_ret_body
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)}
def post(self, **kw): context = t_context.extract_context_from_environ() if 'server' not in kw: return utils.format_nova_error(400, _('server is not set')) az = kw['server'].get('availability_zone', '') pod, b_az = az_ag.get_pod_by_az_tenant(context, az, self.project_id) if not pod: return utils.format_nova_error( 500, _('Pod not configured or scheduling failure')) t_server_dict = kw['server'] self._process_metadata_quota(context, t_server_dict) self._process_injected_file_quota(context, t_server_dict) server_body = self._get_create_server_body(kw['server'], b_az) security_groups = [] if 'security_groups' not in kw['server']: security_groups = ['default'] else: for sg in kw['server']['security_groups']: if 'name' not in sg: return utils.format_nova_error( 400, _('Invalid input for field/attribute')) security_groups.append(sg['name']) server_body['networks'] = [] if 'networks' in kw['server']: for net_info in kw['server']['networks']: if 'uuid' in net_info: nic = {'net-id': net_info['uuid']} server_body['networks'].append(nic) elif 'port' in net_info: nic = {'port-id': net_info['port']} server_body['networks'].append(nic) client = self._get_client(pod['pod_name']) server = client.create_servers(context, name=server_body['name'], image=server_body['imageRef'], flavor=server_body['flavorRef'], nics=server_body['networks'], security_groups=security_groups) with context.session.begin(): core.create_resource( context, models.ResourceRouting, { 'top_id': server['id'], 'bottom_id': server['id'], 'pod_id': pod['pod_id'], 'project_id': self.project_id, 'resource_type': constants.RT_SERVER }) pecan.response.status = 202 return {'server': server}
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()}
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}
def put(self, **kw): context = t_context.extract_context_from_environ() if not context.is_admin: # TODO(joahuang): changed to policy control later # to support reseller admin mode return Response(_('Admin role required to update quota'), 409) return self._quota_action('put', **kw)
def index(self): if pecan.request.method != 'GET': pecan.abort(405) context = ctx.extract_context_from_environ() payload = '#result from xjob rpc' return self.xjobapi.test_rpc(context, payload)
def get_all(self): context = t_context.extract_context_from_environ() with context.session.begin(): flavors = core.query_resource(context, models.InstanceTypes, [], []) return { 'flavors': [ dict([('id', flavor['flavorid']), ('name', flavor['name'])]) for flavor in flavors ] }
def get_one(self, _id): context = t_context.extract_context_from_environ() if not t_context.is_admin_context(context): pecan.abort(400, _('Admin role required to show pods')) return try: return {'pod': db_api.get_pod(context, _id)} except t_exc.ResourceNotFound: pecan.abort(404, _('Pod not found')) return
def get_one(self, _id): context = t_context.extract_context_from_environ() if not policy.enforce(context, policy.ADMIN_API_PODS_SHOW): pecan.abort(401, _('Unauthorized to show pods')) return try: return {'pod': db_api.get_pod(context, _id)} except t_exc.ResourceNotFound: pecan.abort(404, _('Pod not found')) return
def post(self, **kw): context = t_context.extract_context_from_environ() if not policy.enforce(context, policy.ADMIN_API_BINDINGS_CREATE): pecan.abort(401, _('Unauthorized to create bindings')) return if 'pod_binding' not in kw: pecan.abort(400, _('Request body not found')) return pod_b = kw['pod_binding'] tenant_id = pod_b.get('tenant_id', '').strip() pod_id = pod_b.get('pod_id', '').strip() if tenant_id == '' or pod_id == '': return Response(_('Tenant_id and pod_id can not be empty'), 422) # the az_pod_map_id should be exist for in the pod map table try: with context.session.begin(): pod = core.get_resource(context, models.Pod, pod_id) if pod.get('az_name') == '': return Response(_('Top region can not be bound'), 422) except t_exc.ResourceNotFound: return Response(_('pod_id not found in pod'), 422) except Exception as e: LOG.exception( _LE('Failed to get_resource for pod_id: ' '%(pod_id)s ,' '%(exception)s '), { 'pod_id': pod_id, 'exception': e }) pecan.abort(500, _('Failed to create pod binding')) return try: pod_binding = db_api.create_pod_binding(context, tenant_id, pod_id) except db_exc.DBDuplicateEntry: return Response(_('Pod binding already exists'), 409) except db_exc.DBConstraintError: return Response(_('pod_id not exists in pod'), 422) except db_exc.DBReferenceError: return Response(_('DB reference not exists in pod'), 422) except Exception as e: LOG.exception(_LE('Failed to create pod binding: %(exception)s '), {'exception': e}) pecan.abort(500, _('Failed to create pod binding')) return return {'pod_binding': pod_binding}
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)
def get_all(self, **kwargs): context = t_context.extract_context_from_environ() filters = self._get_filters(kwargs) filters = [{ 'key': key, 'comparator': 'eq', 'value': value } for key, value in filters.iteritems()] images = self.client.list_images(context, filters=filters) ret_images = [ self._construct_list_image_entry(context, image) for image in images ] return {'images': ret_images}
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)
def delete(self, _id): context = t_context.extract_context_from_environ() if not policy.enforce(context, policy.ADMIN_API_BINDINGS_DELETE): pecan.abort(401, _('Unauthorized to delete bindings')) return try: with context.session.begin(): core.delete_resource(context, models.PodBinding, _id) pecan.response.status = 200 except t_exc.ResourceNotFound: pecan.abort(404, _('Pod binding not found')) return
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-flavor-manage " "to be performed.")) required_fields = ['name', 'ram', 'vcpus', 'disk'] if 'flavor' not in kw: utils.format_nova_error(400, _('flavor is not set')) if not utils.validate_required_fields_set(kw['flavor'], required_fields): utils.format_nova_error( 400, _('Invalid input for field/attribute flavor.')) flavor_dict = { 'name': kw['flavor']['name'], 'flavorid': kw['flavor'].get('id'), 'memory_mb': kw['flavor']['ram'], 'vcpus': kw['flavor']['vcpus'], 'root_gb': kw['flavor']['disk'], 'ephemeral_gb': kw['flavor'].get('OS-FLV-EXT-DATA:ephemeral', 0), 'swap': kw['flavor'].get('swap', 0), 'rxtx_factor': kw['flavor'].get('rxtx_factor', 1.0), 'is_public': kw['flavor'].get('os-flavor-access:is_public', True), } try: with context.session.begin(): flavor = core.create_resource(context, models.InstanceTypes, flavor_dict) except db_exc.DBDuplicateEntry as e: if 'flavorid' in e.columns: return utils.format_nova_error( 409, _('Flavor with ID %s already ' 'exists.') % flavor_dict['flavorid']) else: return utils.format_nova_error( 409, _('Flavor with name %s already ' 'exists.') % flavor_dict['name']) except Exception: utils.format_nova_error(500, _('Failed to create flavor')) flavor['id'] = flavor['flavorid'] del flavor['flavorid'] return {'flavor': flavor}
def get_one(self, _id): context = t_context.extract_context_from_environ() if not policy.enforce(context, policy.ADMIN_API_BINDINGS_SHOW): pecan.abort(401, _('Unauthorized to show bindings')) return try: with context.session.begin(): pod_binding = core.get_resource(context, models.PodBinding, _id) return {'pod_binding': pod_binding} except t_exc.ResourceNotFound: pecan.abort(404, _('Tenant pod binding not found')) return
def get_all(self): context = t_context.extract_context_from_environ() if not policy.enforce(context, policy.ADMIN_API_PODS_LIST): pecan.abort(401, _('Unauthorized to list pods')) return try: return {'pods': db_api.list_pods(context)} except Exception as e: LOG.exception(_LE('Failed to list all pods: %(exception)s '), {'exception': e}) pecan.abort(500, _('Failed to list pods')) return
def get_one(self, _id): context = t_context.extract_context_from_environ() if not t_context.is_admin_context(context): pecan.abort(400, _('Admin role required to show bindings')) return try: with context.session.begin(): pod_binding = core.get_resource(context, models.PodBinding, _id) return {'pod_binding': pod_binding} except t_exc.ResourceNotFound: pecan.abort(404, _('Tenant pod binding not found')) return
def delete(self, _id): context = t_context.extract_context_from_environ() if not t_context.is_admin_context(context): pecan.abort(400, _('Admin role required to delete bindings')) return try: with context.session.begin(): core.delete_resource(context, models.PodBinding, _id) pecan.response.status = 200 return {} except t_exc.ResourceNotFound: pecan.abort(404, _('Pod binding not found')) return