def device_profile_get_by_id(self, context, id): query = model_query(context, models.DeviceProfile).filter_by(id=id) try: return query.one() except NoResultFound: raise exception.ResourceNotFound(resource='Device Profile', msg='with id=%s' % id)
def get_one(self, dp_uuid_or_name): """Retrieve a single device profile by uuid or name.""" context = pecan.request.context if uuidutils.is_uuid_like(dp_uuid_or_name): LOG.info('[device_profiles] get_one. uuid=%s', dp_uuid_or_name) obj_devprof = objects.DeviceProfile.get_by_uuid( context, dp_uuid_or_name) else: if api.request.version.minor >= versions.MINOR_2_DP_BY_NAME: LOG.info('[device_profiles] get_one. name=%s', dp_uuid_or_name) obj_devprof = \ objects.DeviceProfile.get_by_name(context, dp_uuid_or_name) else: raise exception.NotAcceptable( _("Request not acceptable. The minimal required API " "version should be %(base)s.%(opr)s") % { 'base': versions.BASE_VERSION, 'opr': versions.MINOR_2_DP_BY_NAME }) if not obj_devprof: LOG.warning("Device profile with %s not found!", dp_uuid_or_name) raise exception.ResourceNotFound(resource='Device profile', msg='with %s' % dp_uuid_or_name) api_obj_devprof = self.get_device_profile(obj_devprof) ret = {"device_profile": api_obj_devprof} LOG.info('[device_profiles] get_one returned: %s', ret) # TODO(Sundar) Replace this with convert_with_links() return wsme.api.Response(ret, status_code=HTTPStatus.OK, return_type=wsme.types.DictType)
def control_path_get_by_uuid(self, context, uuid): query = model_query(context, models.ControlpathID).filter_by(uuid=uuid) try: return query.one() except NoResultFound: raise exception.ResourceNotFound(resource='ControlpathID', msg='with uuid=%s' % uuid)
def device_profile_get(self, context, name): query = model_query(context, models.DeviceProfile).filter_by(name=name) try: return query.one() except NoResultFound: raise exception.ResourceNotFound(resource='Device Profile', msg='with name=%s' % name)
def attribute_get(self, context, uuid): query = model_query(context, models.Attribute).filter_by(uuid=uuid) try: return query.one() except NoResultFound: raise exception.ResourceNotFound(resource='Attribute', msg='with uuid=%s' % uuid)
def attach_handle_get_by_id(self, context, id): query = model_query(context, models.AttachHandle).filter_by(id=id) try: return query.one() except NoResultFound: raise exception.ResourceNotFound(resource='AttachHandle', msg='with id=%s' % id)
def device_profile_delete(self, context, uuid): with _session_for_write(): query = model_query(context, models.DeviceProfile) query = add_identity_filter(query, uuid) count = query.delete() if count != 1: raise exception.ResourceNotFound(resource='Device Profile', msg='with uuid=%s' % uuid)
def extarq_delete(self, context, uuid): with _session_for_write(): query = model_query(context, models.ExtArq) query = add_identity_filter(query, uuid) count = query.delete() if count != 1: raise exception.ResourceNotFound(resource='ExtArq', msg='with uuid=%s' % uuid)
def attach_handle_list_by_type(self, context, attach_type='PCI'): query = model_query(context, models.AttachHandle). \ filter_by(attach_type=attach_type) try: return query.all() except NoResultFound: raise exception.ResourceNotFound(resource='AttachHandle', msg='with type=%s' % attach_type)
def test_get_by_non_existed_id(self): device_id = self.fake_device['id'] with mock.patch.object(self.dbapi, 'device_get_by_id', autospec=True) as mock_device_get_by_id: mock_device_get_by_id.side_effect = exception.ResourceNotFound( resource='Device', msg='with uuid=%s' % device_id) self.assertRaises(exception.ResourceNotFound, objects.Device.get_by_device_id, self.context, device_id)
def deployable_delete(self, context, uuid): with _session_for_write(): query = model_query(context, models.Deployable) query = add_identity_filter(query, uuid) query.update({'root_id': None}) count = query.delete() if count != 1: raise exception.ResourceNotFound(resource='Deployable', msg='with uuid=%s' % uuid)
def deployable_get_by_rp_uuid(self, context, rp_uuid): """Get a deployable by resource provider UUID.""" query = model_query(context, models.Deployable).filter_by(rp_uuid=rp_uuid) try: return query.one() except NoResultFound: raise exception.ResourceNotFound( resource='Deployable', msg='with resource provider uuid=%s' % rp_uuid)
def extarq_get(self, context, uuid, lock=False): query = model_query(context, models.ExtArq).filter_by(uuid=uuid) # NOTE we will support aync bind, so get query by lock if lock: query = query.with_for_update() try: return query.one() except NoResultFound: raise exception.ResourceNotFound(resource='ExtArq', msg='with uuid=%s' % uuid)
def _do_update_attach_handle(self, context, uuid, values): with _session_for_write(): query = model_query(context, models.AttachHandle) query = add_identity_filter(query, uuid) try: ref = query.with_for_update().one() except NoResultFound: raise exception.ResourceNotFound(resource='AttachHandle', msg='with uuid=%s' % uuid) ref.update(values) return ref
def attach_handle_allocate(self, context, deployable_id): """Allocate an attach handle with given deployable. To allocate is to get an unused resource and mark it as in_use. """ try: ah = self._do_allocate_attach_handle(context, deployable_id) except NoResultFound: msg = 'Matching deployable_id {0}'.format(deployable_id) raise exception.ResourceNotFound(resource='AttachHandle', msg=msg) return ah
def _translate_image_exception(image_id, exc_value): if isinstance(exc_value, (glanceclient.exc.Forbidden, glanceclient.exc.Unauthorized)): return exception.ImageNotAuthorized(image_id=image_id) if isinstance(exc_value, glanceclient.exc.NotFound): return exception.ResourceNotFound(resource='Image', msg='with uuid=%s' % image_id) if isinstance(exc_value, glanceclient.exc.BadRequest): return exception.ImageBadRequest(image_id=image_id, response=six.text_type(exc_value)) return exc_value
def _do_update_control_path(self, context, uuid, values): with _session_for_write(): query = model_query(context, models.ControlpathID) query = add_identity_filter(query, uuid) try: ref = query.with_lockmode('update').one() except NoResultFound: raise exception.ResourceNotFound(resource='ControlpathID', msg='with uuid=%s' % uuid) ref.update(values) return ref
def post(self, req): """Create one or more ARQs for a single device profile. Request body: { 'device_profile_name': <string> } Future: { 'device_profile_name': <string> # required 'device_profile_group_id': <integer>, # opt, default=0 'image_uuid': <glance-image-UUID>, #optional, for future } :param req: request body. """ LOG.info("[arq] post req = (%s)", req) context = pecan.request.context dp_name = req.get('device_profile_name') if dp_name is not None: try: devprof = objects.DeviceProfile.get_by_name(context, dp_name) except exception.ResourceNotFound: raise exception.ResourceNotFound(resource='Device Profile', msg='with name=%s' % dp_name) except Exception as e: raise e else: raise exception.DeviceProfileNameNeeded() LOG.info('[arqs] post. device profile name=%s', dp_name) extarq_list = [] for group_id, group in enumerate(devprof.groups): accel_resources = [ int(val) for key, val in group.items() if key.startswith('resources') ] # If/when we introduce non-accelerator resources, like # device-local memory, the key search above needs to be # made specific to accelerator resources only. num_accels = sum(accel_resources) arq_fields = { 'device_profile_name': devprof.name, 'device_profile_group_id': group_id, } for i in range(num_accels): obj_arq = objects.ARQ(context, **arq_fields) extarq_fields = {'arq': obj_arq} obj_extarq = objects.ExtARQ(context, **extarq_fields) new_extarq = pecan.request.conductor_api.arq_create( context, obj_extarq, devprof.id) extarq_list.append(new_extarq) ret = ARQCollection.convert_with_links( [extarq.arq for extarq in extarq_list]) LOG.info('[arqs] post returned: %s', ret) return ret
def _do_update_deployable(self, context, uuid, values): with _session_for_write(): query = model_query(context, models.Deployable) # query = add_identity_filter(query, uuid) query = query.filter_by(uuid=uuid) try: ref = query.with_lockmode('update').one() except NoResultFound: raise exception.ResourceNotFound(resource='Deployable', msg='with uuid=%s' % uuid) ref.update(values) return ref
def _do_update_attribute(self, context, uuid, key, value): update_fields = {'key': key, 'value': value} with _session_for_write(): query = model_query(context, models.Attribute) query = add_identity_filter(query, uuid) try: ref = query.with_lockmode('update').one() except NoResultFound: raise exception.ResourceNotFound(resource='Attribute', msg='with uuid=%s' % uuid) ref.update(update_fields) return ref
def _do_update_extarq(self, context, uuid, values, state_scope=None): with _session_for_write(): query = model_query(context, models.ExtArq) query = query_update = query.filter_by(uuid=uuid).with_for_update() if type(state_scope) is list: query_update = query_update.filter( models.ExtArq.state.in_(state_scope)) try: query_update.update(values, synchronize_session="fetch") except NoResultFound: raise exception.ResourceNotFound(resource='ExtArq', msg='with uuid=%s' % uuid) ref = query.first() return ref
def test_create_with_wrong_dp(self, mock_obj_extarq, mock_obj_dp): params = {'device_profile_name': 'wrong_device_profile_name'} mock_obj_dp.side_effect = exception.ResourceNotFound( resource='Device Profile', msg='with name=%s' % params.get('device_profile_name')) mock_obj_extarq.side_effect = self.fake_extarqs exc = None try: self.post_json(self.ARQ_URL, params, headers=self.headers) except Exception as e: exc = e self.assertIn( "Device Profile not found with " "name=wrong_device_profile_name", exc.args[0])
def test_allocate_attach_handle_with_error_log(self, mock_check_state, mock_allocate, mock_log): obj_extarq = self.fake_obj_extarqs[0] dep_uuid = self.deployable_uuids[0] e = exception.ResourceNotFound(resource='AttachHandle', msg="Just for Test") msg = ("Failed to allocate attach handle for ARQ %s" "from deployable %s. Reason: %s") mock_allocate.side_effect = e fake_dep = fake_deployable.fake_deployable_obj(self.context, uuid=dep_uuid) self.assertRaises(exception.ResourceNotFound, obj_extarq._allocate_attach_handle, self.context, fake_dep) mock_log.assert_called_once_with(msg, obj_extarq.arq.uuid, fake_dep.uuid, str(e))
def _do_allocate_attach_handle(self, context, deployable_id): """Atomically get a set of attach handles that match the query and mark one of those as in_use. """ with _session_for_write() as session: query = model_query(context, models.AttachHandle). \ filter_by(deployable_id=deployable_id, in_use=False) values = {"in_use": True} ref = query.with_lockmode('update').first() if not ref: msg = 'Matching deployable_id {0}'.format(deployable_id) raise exception.ResourceNotFound(resource='AttachHandle', msg=msg) ref.update(values) session.flush() return ref
def get_one(self, uuid): """Retrieve a single device profile by uuid.""" LOG.info('[device_profiles] get_one. uuid=%s', uuid) api_obj_devprofs = self._get_device_profile_list(uuid=uuid) if len(api_obj_devprofs) == 0: raise exception.ResourceNotFound(resource='Device profile', msg='with uuid %s' % uuid) count = len(api_obj_devprofs) if count != 1: # Should never happen because names are unique raise exception.ExpectedOneObject(obj='device profile', count=count) ret = {"device_profile": api_obj_devprofs[0]} LOG.info('[device_profiles] get_one returned: %s', ret) # TODO(Sundar) Replace this with convert_with_links() return wsme.api.Response(ret, status_code=http_client.OK, return_type=wsme.types.DictType)
def update_check_state(self, context, state, scope=None): if self.arq.state == state: LOG.info("ExtARQ(%s) state is %s, no need to update", self.arq.uuid, state) return False old = self.arq.state scope = scope or ARQ_STATES_TRANSFORM_MATRIX[state] self.update_state(context, state, scope) ea = ExtARQ.get(context, self.arq.uuid, lock=True) if not ea: raise exception.ResourceNotFound("Can not find ExtARQ(%s)" % self.arq.uuid) current = ea.arq.state if state != current: msg = ("Failed to change ARQ state from %s to %s, the current " "state is %s" % (old, state, current)) LOG.error(msg) raise exception.ARQInvalidState(msg) return True
def _fill_obj_extarq_fields(cls, context, db_extarq): """ExtARQ object has some fields that are not present in db_extarq. We fill them out here. """ # From the 2 fields in the ExtARQ, we obtain other fields. devprof_id = db_extarq['device_profile_id'] devprof_group_id = db_extarq['device_profile_group_id'] devprof = DeviceProfile.get_by_id(context, devprof_id) db_extarq['device_profile_name'] = devprof['name'] db_extarq['attach_handle_type'] = '' db_extarq['attach_handle_info'] = '' if db_extarq['state'] == 'Bound': # TODO() Do proper bind db_ah = cls.dbapi.attach_handle_get_by_id( context, db_extarq['attach_handle_id']) if db_ah is not None: db_extarq['attach_handle_type'] = db_ah['attach_type'] db_extarq['attach_handle_info'] = db_ah['attach_info'] else: raise exception.ResourceNotFound(resource='Attach Handle', msg='with uuid=%s' % db_extarq['attach_handle_id']) if db_extarq['deployable_id']: dep = objects.Deployable.get_by_id(context, db_extarq['deployable_id']) db_extarq['deployable_uuid'] = dep.uuid else: LOG.debug('Setting deployable UUID to zeroes for db_extarq %s', db_extarq['uuid']) db_extarq['deployable_uuid'] = ( '00000000-0000-0000-0000-000000000000') groups = devprof['groups'] db_extarq['device_profile_group'] = groups[devprof_group_id] return db_extarq
def delete_by_uuid(cls, context, arq_uuid_list): """Delete a list of ARQs based on their UUIDs. This is not idempotent, i.e., if the first call to delete an ARQ has succeeded, second and later calls to delete the same ARQ will get errored out, but it will raise the exception only after all input arq being operated. """ unexisted = [] for uuid in arq_uuid_list: try: obj_extarq = objects.ExtARQ.get(context, uuid) # TODO() Defer deletion to conductor if obj_extarq.arq.state != constants.ARQ_INITIAL: obj_extarq.unbind(context) obj_extarq.destroy(context) except exception.ResourceNotFound: unexisted.append(uuid) continue if unexisted: LOG.warning('There are unexisted arqs: %s', unexisted) raise exception.ResourceNotFound(resource='ARQ', msg='with uuids %s' % unexisted)