def _validate_post_request(self, req_devprof): NAME = "^[a-zA-Z0-9-_]+$" keys = '|'.join(["resources:", "trait:", "accel:"]) GROUP_KEYS = "^(%s)" % keys TRAIT_VALUES = ["required", "forbidden"] name = req_devprof.get("name") if not name: raise exception.DeviceProfileNameNeeded() elif not re.match(NAME, name): raise exception.InvalidParameterValue( err="Device profile name must be of the form %s" % NAME) groups = req_devprof.get("groups") if not groups: raise exception.DeviceProfileGroupsExpected() else: for group in groups: for key, value in group.items(): if not re.match(GROUP_KEYS, key): raise exception.InvalidParameterValue( err="Device profile group keys must be of " " the form %s" % GROUP_KEYS) if key.startswith("trait:") and value not in TRAIT_VALUES: raise exception.InvalidParameterValue( err="Device profile trait values must be one " "among %s" % TRAIT_VALUES)
def _needs_programming(self, context, deployable): bs_id = self._get_bitstream_id() fun_id = self._get_function_id() if all([bs_id, fun_id]): self.update_check_state(context, constants.ARQ_BIND_FAILED) raise exception.InvalidParameterValue( 'In device profile {0}, only one among bitstream_id ' 'and function_id must be set, but both are set') # TODO(Shaohe) Optimize: check if deployable already has # bitstream/function if any([bs_id, fun_id]): LOG.info( '[arqs:objs] bind. Programming needed. ' 'bitstream: (%s) function: (%s) Deployable UUID: (%s)', bs_id or '', fun_id or '', deployable.uuid) else: # One situation is that fun_id is zero and device_profile # has't bitstream. We should return False. LOG.info('No programming is required. ') return False if bs_id and deployable.bitstream_id == bs_id: LOG.info( 'Deployable %s already has the needed ' 'bitstream %s. Skipping programming.', deployable.uuid, bs_id) return False return True
def post(self, req_devprof_list): """Create one or more device_profiles. NOTE: Only one device profile supported in Train. :param devprof: a list of device_profiles. [{ "name": <string>, "groups": [ {"key1: "value1", "key2": "value2"} ] "uuid": <uuid> # optional }] :returns: The list of created device profiles """ # TODO(Sundar) Support more than one devprof per request, if needed LOG.info("[device_profiles] POST request = (%s)", req_devprof_list) if len(req_devprof_list) != 1: raise exception.InvalidParameterValue( err="Only one device profile allowed " "per POST request for now.") req_devprof = req_devprof_list[0] self._validate_post_request(req_devprof) context = pecan.request.context obj_devprof = objects.DeviceProfile(context, **req_devprof) new_devprof = pecan.request.conductor_api.device_profile_create( context, obj_devprof) ret = DeviceProfile.get_api_obj(new_devprof) return wsme.api.Response(ret, status_code=http_client.CREATED, return_type=wsme.types.DictType)
def post(self, req_devprof_list): """Create one or more device_profiles. NOTE: Only one device profile supported in Train. :param devprof: a list of device_profiles. [{ "name": <string>, "groups": [ {"key1: "value1", "key2": "value2"} ] "uuid": <uuid> # optional "description": <description> # optional }] :returns: The list of created device profiles """ # TODO(Sundar) Support more than one devprof per request, if needed LOG.info("[device_profiles] POST request = (%s)", req_devprof_list) if len(req_devprof_list) != 1: raise exception.InvalidParameterValue( err="Only one device profile allowed " "per POST request for now.") req_devprof = req_devprof_list[0] self._validate_post_request(req_devprof) context = pecan.request.context obj_devprof = objects.DeviceProfile(context, **req_devprof) new_devprof = pecan.request.conductor_api.device_profile_create( context, obj_devprof) return DeviceProfile.convert_with_links(new_devprof)
def deployable_update(self, context, uuid, values): if 'uuid' in values: msg = _("Cannot overwrite UUID for an existing Deployable.") raise exception.InvalidParameterValue(err=msg) try: return self._do_update_deployable(context, uuid, values) except db_exc.DBDuplicateEntry as e: if 'name' in e.columns: raise exception.DuplicateDeployableName(name=values['name'])
def accelerator_update(self, context, uuid, values): if 'uuid' in values: msg = _("Cannot overwrite UUID for an existing Accelerator.") raise exception.InvalidParameterValue(err=msg) try: return self._do_update_accelerator(context, uuid, values) except db_exc.DBDuplicateEntry as e: if 'name' in e.columns: raise exception.DuplicateAcceleratorName(name=values['name'])
def port_update(self, context, uuid, values): if 'uuid' in values: msg = _("Cannot overwrite UUID for existing Port.") raise exception.InvalidParameterValue(err=msg) try: return self._do_update_port(context, uuid, values) except db_exc.DBDuplicateEntry as e: if 'name' in e.columns: raise exception.PortDuplicateName(name=values['name'])
def get_resources_from_device_profile_group(self): """parser device profile group.""" group = self.device_profile_group # example: {"resources:CUSTOM_ACCELERATOR_FPGA": "1"} resources = [(k.lstrip(constants.RESOURCES_PREFIX), v) for k, v in group.items() if k.startswith(constants.RESOURCES_PREFIX)] if not resources: raise exception.InvalidParameterValue( 'No resources in device_profile_group: %s' % group) res_type = resources[0][0] return res_type
def _validate_post_request(self, req_devprof): NAME = "^[a-zA-Z0-9-_]+$" keys = '|'.join(["resources:", "trait:", "accel:"]) GROUP_KEYS = "^(%s)" % keys TRAIT_VALUES = ["required", "forbidden"] name = req_devprof.get("name") if not name: raise exception.DeviceProfileNameNeeded() elif not re.match(NAME, name): raise exception.InvalidParameterValue( err="Device profile name must be of the form %s" % NAME) groups = req_devprof.get("groups") if not groups: raise exception.DeviceProfileGroupsExpected() for group in groups: tmp_group = copy.deepcopy(group) for key, value in tmp_group.items(): # check resource and trait prefix format if not re.match(GROUP_KEYS, key): raise exception.InvalidParameterValue( err="Device profile group keys must be of " " the form %s" % GROUP_KEYS) # check trait name and it's value if key.startswith("trait:"): inner_origin_trait = ":".join(key.split(":")[1:]) inner_trait = inner_origin_trait.strip(" ") if not inner_trait.startswith('CUSTOM_'): raise exception.InvalidParameterValue( err="Unsupported trait name format %s, should " "start with CUSTOM_" % inner_trait) if value not in TRAIT_VALUES: raise exception.InvalidParameterValue( err="Unsupported trait value %s, the value must" " be one among %s" % TRAIT_VALUES) # strip " " and update old group key. if inner_origin_trait != inner_trait: del group[key] standard_key = "trait:" + inner_trait group[standard_key] = value # check rc name and it's value if key.startswith("resources:"): inner_origin_rc = ":".join(key.split(":")[1:]) inner_rc = inner_origin_rc.strip(" ") if inner_rc not in constants.SUPPORT_RESOURCES and \ not inner_rc.startswith('CUSTOM_'): raise exception.InvalidParameterValue( err="Unsupported resource class %s" % inner_rc) try: int(value) except ValueError: raise exception.InvalidParameterValue( err="Resources nummber %s is invalid" % value) # strip " " and update old group key. if inner_origin_rc != inner_rc: del group[key] standard_key = "resources:" + inner_rc group[standard_key] = value
def _paginate_query(context, model, limit, marker, sort_key, sort_dir, query): sort_keys = ['id'] if sort_key and sort_key not in sort_keys: sort_keys.insert(0, sort_key) try: query = sqlalchemyutils.paginate_query(query, model, limit, sort_keys, marker=marker, sort_dir=sort_dir) except db_exc.InvalidSortKey: raise exception.InvalidParameterValue( _('The sort_key value "%(key)s" is an invalid field for sorting') % {'key': sort_key}) return query.all()
def get_resources_from_device_profile_group(self): """parser device profile group.""" group = self.device_profile_group # example: {"resources:CUSTOM_ACCELERATOR_FPGA": "1"} resources = [ (k.lstrip(constants.RESOURCES_PREFIX), v) for k, v in group.items() if k.startswith(constants.RESOURCES_PREFIX)] if not resources: raise exception.InvalidParameterValue( 'No resources in device_profile_group: %s' % group) res_type, res_num = resources[0] # TODO(Sundar): this should be caught in ARQ create, not bind. if res_type not in constants.SUPPORT_RESOURCES: raise exception.InvalidParameterValue( 'Unsupport resources %s from device_profile_group: %s' % (res_type, group)) try: res_num = int(res_num) except ValueError: raise exception.InvalidParameterValue( 'Resources nummber is a invalid in' 'device_profile_group: %s' % group) return res_type, res_num
def extarq_update(self, context, uuid, values): if 'uuid' in values: msg = _("Cannot overwrite UUID for an existing ExtArq.") raise exception.InvalidParameterValue(err=msg) return self._do_update_extarq(context, uuid, values)
def control_path_update(self, context, uuid, values): if 'uuid' in values: msg = _("Cannot overwrite UUID for an existing ControlpathID.") raise exception.InvalidParameterValue(err=msg) return self._do_update_control_path(context, uuid, values)
def attach_handle_update(self, context, uuid, values): if 'uuid' in values: msg = _("Cannot overwrite UUID for an existing AttachHandle.") raise exception.InvalidParameterValue(err=msg) return self._do_update_attach_handle(context, uuid, values)