Exemplo n.º 1
0
    def extarq_create(self, context, values):
        if not values.get('uuid'):
            values['uuid'] = uuidutils.generate_uuid()
        if values.get('id'):
            values.pop('id', None)

        if values.get('device_profile_id'):
            pass  # Already have the devprof id, so nothing to do
        elif values.get('device_profile_name'):
            devprof = self.device_profile_get(context,
                                              values['device_profile_name'])
            values['device_profile_id'] = devprof['id']
        else:
            raise exception.DeviceProfileNameNeeded()

        extarq = models.ExtArq()
        extarq.update(values)

        with _session_for_write() as session:
            try:
                session.add(extarq)
                session.flush()
            except db_exc.DBDuplicateEntry:
                raise exception.ExtArqAlreadyExists(uuid=values['uuid'])
            return extarq
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
    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
Exemplo n.º 4
0
    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