Beispiel #1
0
    def instances_show(self, req, protectable_type, protectable_id):
        """Return a instance about the given protectable_type and id."""

        context = req.environ['karbor.context']
        params = req.params.copy()
        utils.check_filters(params)
        parameters = params.get("parameters", None)

        LOG.info(_LI("Show the instance of a given protectable"
                     " type: %s"), protectable_type)

        if parameters is not None:
            if not isinstance(parameters, dict):
                msg = _("The parameters must be a dict.")
                raise exception.InvalidInput(reason=msg)

        protectable_types = self._get_all(context)

        if protectable_type not in protectable_types:
            msg = _("Invalid protectable type provided.")
            raise exception.InvalidInput(reason=msg)

        instance = self.protection_api.\
            show_protectable_instance(context, protectable_type,
                                      protectable_id, parameters=parameters)
        if instance is None:
            raise exception.InvalidProtectableInstance()

        dependents = self.protection_api.\
            list_protectable_dependents(context, protectable_id,
                                        protectable_type)
        instance["dependent_resources"] = dependents

        retval_instance = self._view_builder.detail(req, instance)
        return retval_instance
Beispiel #2
0
    def instances_index(self, req, protectable_type):
        """Return data about the given protectable_type."""
        context = req.environ['karbor.context']
        LOG.info(_LI("Show the instances of a given"
                     " protectable type: %s"), protectable_type)

        params = req.params.copy()
        marker, limit, offset = common.get_pagination_params(params)
        sort_keys, sort_dirs = common.get_sort_params(params)
        filters = params
        utils.check_filters(filters)
        parameters = filters.get("parameters", None)

        if parameters is not None:
            if not isinstance(parameters, dict):
                msg = _("The parameters must be a dict.")
                raise exception.InvalidInput(reason=msg)

        utils.remove_invalid_filter_options(
            context, filters, self._get_instance_filter_options())

        protectable_types = self._get_all(context)

        if protectable_type not in protectable_types:
            msg = _("Invalid protectable type provided.")
            raise exception.InvalidInput(reason=msg)

        instances = self._instances_get_all(context,
                                            protectable_type,
                                            marker,
                                            limit,
                                            sort_keys=sort_keys,
                                            sort_dirs=sort_dirs,
                                            filters=filters,
                                            offset=offset,
                                            parameters=parameters)

        for instance in instances:
            protectable_id = instance.get("id")
            instance["type"] = protectable_type
            if protectable_id is None:
                raise exception.InvalidProtectableInstance()
            dependents = self.protection_api.\
                list_protectable_dependents(context, protectable_id,
                                            protectable_type)
            instance["dependent_resources"] = dependents

        retval_instances = self._view_builder.detail_list(req, instances)

        return retval_instances