Beispiel #1
0
    def _index(self, req, tenant_safe=True):
        filter_whitelist = {
            'status': 'mixed',
            'name': 'mixed',
            'action': 'mixed',
            'tenant': 'mixed',
            'username': '******',
            'owner_id': 'mixed',
        }
        whitelist = {
            'limit': 'single',
            'marker': 'single',
            'sort_dir': 'single',
            'sort_keys': 'multi',
            'show_deleted': 'single',
            'show_nested': 'single',
        }
        params = util.get_allowed_params(req.params, whitelist)
        filter_params = util.get_allowed_params(req.params, filter_whitelist)

        show_deleted = False
        if rpc_api.PARAM_SHOW_DELETED in params:
            params[rpc_api.PARAM_SHOW_DELETED] = param_utils.extract_bool(
                params[rpc_api.PARAM_SHOW_DELETED])
            show_deleted = params[rpc_api.PARAM_SHOW_DELETED]
        show_nested = False
        if rpc_api.PARAM_SHOW_NESTED in params:
            params[rpc_api.PARAM_SHOW_NESTED] = param_utils.extract_bool(
                params[rpc_api.PARAM_SHOW_NESTED])
            show_nested = params[rpc_api.PARAM_SHOW_NESTED]
        # get the with_count value, if invalid, raise ValueError
        with_count = False
        if req.params.get('with_count'):
            with_count = param_utils.extract_bool(req.params.get('with_count'))

        if not filter_params:
            filter_params = None

        stacks = self.rpc_client.list_stacks(req.context,
                                             filters=filter_params,
                                             tenant_safe=tenant_safe,
                                             **params)

        count = None
        if with_count:
            try:
                # Check if engine has been updated to a version with
                # support to count_stacks before trying to use it.
                count = self.rpc_client.count_stacks(req.context,
                                                     filters=filter_params,
                                                     tenant_safe=tenant_safe,
                                                     show_deleted=show_deleted,
                                                     show_nested=show_nested)
            except AttributeError as exc:
                LOG.warn(_LW("Old Engine Version: %s") % exc)

        return stacks_view.collection(req,
                                      stacks=stacks,
                                      count=count,
                                      tenant_safe=tenant_safe)
Beispiel #2
0
 def test_extract_bool(self):
     for value in ('True', 'true', 'TRUE', True):
         self.assertTrue(param_utils.extract_bool(value))
     for value in ('False', 'false', 'FALSE', False):
         self.assertFalse(param_utils.extract_bool(value))
     for value in ('foo', 't', 'f', 'yes', 'no', 'y', 'n', '1', '0', None):
         self.assertRaises(ValueError, param_utils.extract_bool, value)
Beispiel #3
0
    def _index(self, req, tenant_safe=True):
        filter_whitelist = {
            'status': 'mixed',
            'name': 'mixed',
            'action': 'mixed',
            'tenant': 'mixed',
            'username': '******',
            'owner_id': 'mixed',
        }
        whitelist = {
            'limit': 'single',
            'marker': 'single',
            'sort_dir': 'single',
            'sort_keys': 'multi',
            'show_deleted': 'single',
            'show_nested': 'single',
        }
        params = util.get_allowed_params(req.params, whitelist)
        filter_params = util.get_allowed_params(req.params, filter_whitelist)

        show_deleted = False
        if engine_api.PARAM_SHOW_DELETED in params:
            params[engine_api.PARAM_SHOW_DELETED] = param_utils.extract_bool(
                params[engine_api.PARAM_SHOW_DELETED])
            show_deleted = params[engine_api.PARAM_SHOW_DELETED]
        show_nested = False
        if engine_api.PARAM_SHOW_NESTED in params:
            params[engine_api.PARAM_SHOW_NESTED] = param_utils.extract_bool(
                params[engine_api.PARAM_SHOW_NESTED])
            show_nested = params[engine_api.PARAM_SHOW_NESTED]
        # get the with_count value, if invalid, raise ValueError
        with_count = False
        if req.params.get('with_count'):
            with_count = param_utils.extract_bool(
                req.params.get('with_count'))

        if not filter_params:
            filter_params = None

        stacks = self.rpc_client.list_stacks(req.context,
                                             filters=filter_params,
                                             tenant_safe=tenant_safe,
                                             **params)

        count = None
        if with_count:
            try:
                # Check if engine has been updated to a version with
                # support to count_stacks before trying to use it.
                count = self.rpc_client.count_stacks(req.context,
                                                     filters=filter_params,
                                                     tenant_safe=tenant_safe,
                                                     show_deleted=show_deleted,
                                                     show_nested=show_nested)
            except AttributeError as exc:
                LOG.warn(_LW("Old Engine Version: %s") % exc)

        return stacks_view.collection(req, stacks=stacks, count=count,
                                      tenant_safe=tenant_safe)
Beispiel #4
0
def extract_args(params):
    """Extract arguments passed as parameters and return them as a dictionary.

    Extract any arguments passed as parameters through the API and return them
    as a dictionary. This allows us to filter the passed args and do type
    conversion where appropriate
    """
    kwargs = {}
    timeout_mins = params.get(rpc_api.PARAM_TIMEOUT)
    if timeout_mins not in ('0', 0, None):
        try:
            timeout = int(timeout_mins)
        except (ValueError, TypeError):
            LOG.exception('Timeout conversion failed')
        else:
            if timeout > 0:
                kwargs[rpc_api.PARAM_TIMEOUT] = timeout
            else:
                raise ValueError(_('Invalid timeout value %s') % timeout)

    name = rpc_api.PARAM_DISABLE_ROLLBACK
    if name in params:
        disable_rollback = param_utils.extract_bool(name, params[name])
        kwargs[name] = disable_rollback

    name = rpc_api.PARAM_SHOW_DELETED
    if name in params:
        params[name] = param_utils.extract_bool(name, params[name])

    adopt_data = params.get(rpc_api.PARAM_ADOPT_STACK_DATA)
    if adopt_data:
        try:
            adopt_data = template_format.simple_parse(adopt_data)
        except ValueError as exc:
            raise ValueError(_('Invalid adopt data: %s') % exc)
        kwargs[rpc_api.PARAM_ADOPT_STACK_DATA] = adopt_data

    tags = params.get(rpc_api.PARAM_TAGS)
    if tags:
        if not isinstance(tags, list):
            raise ValueError(_('Invalid tags, not a list: %s') % tags)

        for tag in tags:
            if not isinstance(tag, six.string_types):
                raise ValueError(_('Invalid tag, "%s" is not a string') % tag)

            if len(tag) > 80:
                raise ValueError(
                    _('Invalid tag, "%s" is longer than 80 '
                      'characters') % tag)

            # Comma is not allowed as per the API WG tagging guidelines
            if ',' in tag:
                raise ValueError(_('Invalid tag, "%s" contains a comma') % tag)

        kwargs[rpc_api.PARAM_TAGS] = tags

    return kwargs
Beispiel #5
0
Datei: api.py Projekt: agiza/heat
def extract_args(params):
    """Extract arguments passed as parameters and return them as a dictionary.

    Extract any arguments passed as parameters through the API and return them
    as a dictionary. This allows us to filter the passed args and do type
    conversion where appropriate
    """
    kwargs = {}
    timeout_mins = params.get(rpc_api.PARAM_TIMEOUT)
    if timeout_mins not in ('0', 0, None):
        try:
            timeout = int(timeout_mins)
        except (ValueError, TypeError):
            LOG.exception(_LE('Timeout conversion failed'))
        else:
            if timeout > 0:
                kwargs[rpc_api.PARAM_TIMEOUT] = timeout
            else:
                raise ValueError(_('Invalid timeout value %s') % timeout)

    name = rpc_api.PARAM_DISABLE_ROLLBACK
    if name in params:
        disable_rollback = param_utils.extract_bool(name, params[name])
        kwargs[name] = disable_rollback

    name = rpc_api.PARAM_SHOW_DELETED
    if name in params:
        params[name] = param_utils.extract_bool(name, params[name])

    adopt_data = params.get(rpc_api.PARAM_ADOPT_STACK_DATA)
    if adopt_data:
        try:
            adopt_data = template_format.simple_parse(adopt_data)
        except ValueError as exc:
            raise ValueError(_('Invalid adopt data: %s') % exc)
        kwargs[rpc_api.PARAM_ADOPT_STACK_DATA] = adopt_data

    tags = params.get(rpc_api.PARAM_TAGS)
    if tags:
        if not isinstance(tags, list):
            raise ValueError(_('Invalid tags, not a list: %s') % tags)

        for tag in tags:
            if not isinstance(tag, six.string_types):
                raise ValueError(_('Invalid tag, "%s" is not a string') % tag)

            if len(tag) > 80:
                raise ValueError(_('Invalid tag, "%s" is longer than 80 '
                                   'characters') % tag)

            # Comma is not allowed as per the API WG tagging guidelines
            if ',' in tag:
                raise ValueError(_('Invalid tag, "%s" contains a comma') % tag)

        kwargs[rpc_api.PARAM_TAGS] = tags

    return kwargs
Beispiel #6
0
    def mark_unhealthy(self, req, identity, resource_name, body):
        """Mark a resource as healthy or unhealthy."""
        data = dict()
        VALID_KEYS = (RES_UPDATE_MARK_UNHEALTHY,
                      RES_UPDATE_STATUS_REASON) = ('mark_unhealthy',
                                                   rpc_api.RES_STATUS_DATA)

        invalid_keys = set(body) - set(VALID_KEYS)
        if invalid_keys:
            raise exc.HTTPBadRequest(
                _("Invalid keys in resource "
                  "mark unhealthy %s") % invalid_keys)

        if RES_UPDATE_MARK_UNHEALTHY not in body:
            raise exc.HTTPBadRequest(
                _("Missing mandatory (%s) key from mark unhealthy "
                  "request") % RES_UPDATE_MARK_UNHEALTHY)

        try:
            data[RES_UPDATE_MARK_UNHEALTHY] = param_utils.extract_bool(
                RES_UPDATE_MARK_UNHEALTHY, body[RES_UPDATE_MARK_UNHEALTHY])
        except ValueError as e:
            raise exc.HTTPBadRequest(six.text_type(e))

        data[RES_UPDATE_STATUS_REASON] = body.get(RES_UPDATE_STATUS_REASON, "")
        self.rpc_client.resource_mark_unhealthy(req.context,
                                                stack_identity=identity,
                                                resource_name=resource_name,
                                                **data)
Beispiel #7
0
    def mark_unhealthy(self, req, identity, resource_name, body):
        """Mark a resource as healthy or unhealthy."""
        data = dict()
        VALID_KEYS = (RES_UPDATE_MARK_UNHEALTHY, RES_UPDATE_STATUS_REASON) = (
            'mark_unhealthy', rpc_api.RES_STATUS_DATA)

        invalid_keys = set(body) - set(VALID_KEYS)
        if invalid_keys:
            raise exc.HTTPBadRequest(_("Invalid keys in resource "
                                       "mark unhealthy %s") % invalid_keys)

        if RES_UPDATE_MARK_UNHEALTHY not in body:
            raise exc.HTTPBadRequest(
                _("Missing mandatory (%s) key from mark unhealthy "
                  "request") % RES_UPDATE_MARK_UNHEALTHY)

        try:
            data[RES_UPDATE_MARK_UNHEALTHY] = param_utils.extract_bool(
                RES_UPDATE_MARK_UNHEALTHY,
                body[RES_UPDATE_MARK_UNHEALTHY])
        except ValueError as e:
            raise exc.HTTPBadRequest(six.text_type(e))

        data[RES_UPDATE_STATUS_REASON] = body.get(RES_UPDATE_STATUS_REASON, "")
        self.rpc_client.resource_mark_unhealthy(req.context,
                                                stack_identity=identity,
                                                resource_name=resource_name,
                                                **data)
Beispiel #8
0
    def index(self, req):
        """
        Lists summary information for all stacks
        """
        global_tenant = False
        if rpc_api.PARAM_GLOBAL_TENANT in req.params:
            global_tenant = param_utils.extract_bool(
                req.params.get(rpc_api.PARAM_GLOBAL_TENANT))

        if global_tenant:
            return self.global_index(req, req.context.tenant_id)

        return self._index(req)
Beispiel #9
0
def extract_args(params):
    '''
    Extract any arguments passed as parameters through the API and return them
    as a dictionary. This allows us to filter the passed args and do type
    conversion where appropriate
    '''
    kwargs = {}
    timeout_mins = params.get(api.PARAM_TIMEOUT)
    if timeout_mins not in ('0', 0, None):
        try:
            timeout = int(timeout_mins)
        except (ValueError, TypeError):
            LOG.exception(_('Timeout conversion failed'))
        else:
            if timeout > 0:
                kwargs[api.PARAM_TIMEOUT] = timeout
            else:
                raise ValueError(_('Invalid timeout value %s') % timeout)

    if api.PARAM_DISABLE_ROLLBACK in params:
        disable_rollback = param_utils.extract_bool(
            params[api.PARAM_DISABLE_ROLLBACK])
        kwargs[api.PARAM_DISABLE_ROLLBACK] = disable_rollback

    if api.PARAM_SHOW_DELETED in params:
        params[api.PARAM_SHOW_DELETED] = param_utils.extract_bool(
            params[api.PARAM_SHOW_DELETED])

    adopt_data = params.get(api.PARAM_ADOPT_STACK_DATA)
    if adopt_data:
        adopt_data = template_format.simple_parse(adopt_data)
        if not isinstance(adopt_data, dict):
            raise ValueError(
                _('Unexpected adopt data "%s". Adopt data must be a dict.')
                % adopt_data)
        kwargs[api.PARAM_ADOPT_STACK_DATA] = adopt_data

    return kwargs
Beispiel #10
0
def extract_args(params):
    '''
    Extract any arguments passed as parameters through the API and return them
    as a dictionary. This allows us to filter the passed args and do type
    conversion where appropriate
    '''
    kwargs = {}
    timeout_mins = params.get(api.PARAM_TIMEOUT)
    if timeout_mins not in ('0', 0, None):
        try:
            timeout = int(timeout_mins)
        except (ValueError, TypeError):
            LOG.exception(_('Timeout conversion failed'))
        else:
            if timeout > 0:
                kwargs[api.PARAM_TIMEOUT] = timeout
            else:
                raise ValueError(_('Invalid timeout value %s') % timeout)

    if api.PARAM_DISABLE_ROLLBACK in params:
        disable_rollback = param_utils.extract_bool(
            params[api.PARAM_DISABLE_ROLLBACK])
        kwargs[api.PARAM_DISABLE_ROLLBACK] = disable_rollback

    if api.PARAM_SHOW_DELETED in params:
        params[api.PARAM_SHOW_DELETED] = param_utils.extract_bool(
            params[api.PARAM_SHOW_DELETED])

    adopt_data = params.get(api.PARAM_ADOPT_STACK_DATA)
    if adopt_data:
        adopt_data = template_format.simple_parse(adopt_data)
        if not isinstance(adopt_data, dict):
            raise ValueError(
                _('Unexpected adopt data "%s". Adopt data must be a dict.') %
                adopt_data)
        kwargs[api.PARAM_ADOPT_STACK_DATA] = adopt_data

    return kwargs
Beispiel #11
0
    def _index(self, req, tenant_safe=True):
        filter_whitelist = {"status": "mixed", "name": "mixed", "action": "mixed"}
        whitelist = {
            "limit": "single",
            "marker": "single",
            "sort_dir": "single",
            "sort_keys": "multi",
            "show_deleted": "single",
        }
        params = util.get_allowed_params(req.params, whitelist)
        filter_params = util.get_allowed_params(req.params, filter_whitelist)

        show_deleted = False
        if engine_api.PARAM_SHOW_DELETED in params:
            params[engine_api.PARAM_SHOW_DELETED] = param_utils.extract_bool(params[engine_api.PARAM_SHOW_DELETED])
            show_deleted = params[engine_api.PARAM_SHOW_DELETED]
        # get the with_count value, if invalid, raise ValueError
        with_count = False
        if req.params.get("with_count"):
            with_count = param_utils.extract_bool(req.params.get("with_count"))

        if not filter_params:
            filter_params = None

        stacks = self.rpc_client.list_stacks(req.context, filters=filter_params, tenant_safe=tenant_safe, **params)

        count = None
        if with_count:
            try:
                # Check if engine has been updated to a version with
                # support to count_stacks before trying to use it.
                count = self.rpc_client.count_stacks(
                    req.context, filters=filter_params, tenant_safe=tenant_safe, show_deleted=show_deleted
                )
            except AttributeError as exc:
                LOG.warning(_("Old Engine Version: %s") % exc)

        return stacks_view.collection(req, stacks=stacks, count=count, tenant_safe=tenant_safe)
Beispiel #12
0
    def _index(self, req, tenant_safe=True):
        filter_whitelist = {
            'status': 'mixed',
            'name': 'mixed',
            'action': 'mixed',
        }
        whitelist = {
            'limit': 'single',
            'marker': 'single',
            'sort_dir': 'single',
            'sort_keys': 'multi',
            'show_deleted': 'single',
        }
        params = util.get_allowed_params(req.params, whitelist)
        filter_params = util.get_allowed_params(req.params, filter_whitelist)

        if engine_api.PARAM_SHOW_DELETED in params:
            params[engine_api.PARAM_SHOW_DELETED] = param_utils.extract_bool(
                params[engine_api.PARAM_SHOW_DELETED])

        if not filter_params:
            filter_params = None

        stacks = self.rpc_client.list_stacks(req.context,
                                             filters=filter_params,
                                             tenant_safe=tenant_safe,
                                             **params)

        count = None
        if req.params.get('with_count'):
            try:
                # Check if engine has been updated to a version with
                # support to count_stacks before trying to use it.
                count = self.rpc_client.count_stacks(req.context,
                                                     filters=filter_params,
                                                     tenant_safe=tenant_safe)
            except AttributeError as exc:
                logger.warning(_("Old Engine Version: %s") % exc)

        return stacks_view.collection(req,
                                      stacks=stacks,
                                      count=count,
                                      tenant_safe=tenant_safe)
Beispiel #13
0
 def _extract_bool_param(self, name, value):
     try:
         return param_utils.extract_bool(name, value)
     except ValueError as e:
         raise exc.HTTPBadRequest(six.text_type(e))
Beispiel #14
0
 def _extract_bool_param(self, name, value):
     try:
         return param_utils.extract_bool(name, value)
     except ValueError as e:
         raise exc.HTTPBadRequest(six.text_type(e))