Example #1
0
    def post(self, action_alias, requester_user):
        """
            Create a new ActionAlias.

            Handles requests:
                POST /actionalias/
        """

        permission_type = PermissionType.ACTION_ALIAS_CREATE
        rbac_utils = get_rbac_backend().get_utils_class()
        rbac_utils.assert_user_has_resource_api_permission(user_db=requester_user,
                                                           resource_api=action_alias,
                                                           permission_type=permission_type)

        try:
            action_alias_db = ActionAliasAPI.to_model(action_alias)
            LOG.debug('/actionalias/ POST verified ActionAliasAPI and formulated ActionAliasDB=%s',
                      action_alias_db)
            action_alias_db = ActionAlias.add_or_update(action_alias_db)
        except (ValidationError, ValueError, ValueValidationException) as e:
            LOG.exception('Validation failed for action alias data=%s.', action_alias)
            abort(http_client.BAD_REQUEST, six.text_type(e))
            return

        extra = {'action_alias_db': action_alias_db}
        LOG.audit('Action alias created. ActionAlias.id=%s' % (action_alias_db.id), extra=extra)
        action_alias_api = ActionAliasAPI.from_model(action_alias_db)

        return Response(json=action_alias_api, status=http_client.CREATED)
Example #2
0
    def _get_action_alias_db(self, pack, action_alias, ignore_metadata_file_error=False):
        """
        Retrieve ActionAliasDB object.

        :param ignore_metadata_file_error: True to ignore the error when we can't infer
                                            metadata_file attribute (e.g. inside tests).
        :type ignore_metadata_file_error: ``bool``
        """
        content = self._meta_loader.load(action_alias)
        pack_field = content.get('pack', None)
        if not pack_field:
            content['pack'] = pack
            pack_field = pack
        if pack_field != pack:
            raise Exception('Model is in pack "%s" but field "pack" is different: %s' %
                            (pack, pack_field))

        # Add in "metadata_file" attribute which stores path to the pack metadata file relative to
        # the pack directory
        try:
            metadata_file = content_utils.get_relative_path_to_pack_file(pack_ref=pack,
                                                                         file_path=action_alias,
                                                                         use_pack_cache=True)
        except ValueError as e:
            if not ignore_metadata_file_error:
                raise e
        else:
            content['metadata_file'] = metadata_file

        action_alias_api = ActionAliasAPI(**content)
        action_alias_api.validate()
        action_alias_db = ActionAliasAPI.to_model(action_alias_api)

        return action_alias_db
Example #3
0
    def _register_aliases(self, aliases=None):
        registered_count = 0

        for alias in aliases:
            LOG.debug('Loading alias from %s.', alias)
            try:
                content = self._meta_loader.load(alias)
                action_alias_api = ActionAliasAPI(**content)
                action_alias_db = ActionAliasAPI.to_model(action_alias_api)

                try:
                    action_alias_db.id = ActionAlias.get_by_name(action_alias_api.name).id
                except ValueError:
                    LOG.info('ActionAlias %s not found. Creating new one.', alias)

                try:
                    action_alias_db = ActionAlias.add_or_update(action_alias_db)
                    extra = {'action_alias_db': action_alias_db}
                    LOG.audit('Action alias updated. Action alias %s from %s.', action_alias_db,
                              alias, extra=extra)
                except Exception:
                    LOG.exception('Failed to create action alias %s.', action_alias_api.name)

            except Exception:
                LOG.exception('Failed registering alias from %s.', alias)
            else:
                registered_count += 1

        return registered_count
Example #4
0
    def put(self, action_alias_ref_or_id, action_alias):
        action_alias_db = self._get_by_ref_or_id(ref_or_id=action_alias_ref_or_id)
        LOG.debug("PUT /actionalias/ lookup with id=%s found object: %s", action_alias_ref_or_id, action_alias_db)

        try:
            if action_alias.id is not None and action_alias.id is not "" and action_alias.id != action_alias_ref_or_id:
                LOG.warning(
                    "Discarding mismatched id=%s found in payload and using uri_id=%s.",
                    action_alias.id,
                    action_alias_ref_or_id,
                )
            old_action_alias_db = action_alias_db
            action_alias_db = ActionAliasAPI.to_model(action_alias)
            action_alias_db.id = action_alias_ref_or_id
            action_alias_db = ActionAlias.add_or_update(action_alias_db)
        except (ValidationError, ValueError) as e:
            LOG.exception("Validation failed for action alias data=%s", action_alias)
            pecan.abort(http_client.BAD_REQUEST, str(e))
            return

        extra = {"old_action_alias_db": old_action_alias_db, "new_action_alias_db": action_alias_db}
        LOG.audit("Action alias updated. ActionAlias.id=%s." % (action_alias_db.id), extra=extra)
        action_alias_api = ActionAliasAPI.from_model(action_alias_db)

        return action_alias_api
Example #5
0
    def put(self, action_alias_ref_or_id, action_alias):
        action_alias_db = self._get_by_ref_or_id(
            ref_or_id=action_alias_ref_or_id)
        LOG.debug('PUT /actionalias/ lookup with id=%s found object: %s',
                  action_alias_ref_or_id, action_alias_db)

        try:
            if action_alias.id is not None and action_alias.id is not '' and \
               action_alias.id != action_alias_ref_or_id:
                LOG.warning(
                    'Discarding mismatched id=%s found in payload and using uri_id=%s.',
                    action_alias.id, action_alias_ref_or_id)
            old_action_alias_db = action_alias_db
            action_alias_db = ActionAliasAPI.to_model(action_alias)
            action_alias_db.id = action_alias_ref_or_id
            action_alias_db = ActionAlias.add_or_update(action_alias_db)
        except (ValidationError, ValueError) as e:
            LOG.exception('Validation failed for action alias data=%s',
                          action_alias)
            pecan.abort(http_client.BAD_REQUEST, str(e))
            return

        extra = {
            'old_action_alias_db': old_action_alias_db,
            'new_action_alias_db': action_alias_db
        }
        LOG.audit('Action alias updated. ActionAlias.id=%s.' %
                  (action_alias_db.id),
                  extra=extra)
        action_alias_api = ActionAliasAPI.from_model(action_alias_db)

        return action_alias_api
Example #6
0
    def _register_aliases(self, aliases=None):
        registered_count = 0

        for alias in aliases:
            LOG.debug('Loading alias from %s.', alias)
            try:
                content = self._meta_loader.load(alias)
                action_alias_api = ActionAliasAPI(**content)
                action_alias_db = ActionAliasAPI.to_model(action_alias_api)

                try:
                    action_alias_db.id = ActionAlias.get_by_name(
                        action_alias_api.name).id
                except ValueError:
                    LOG.info('ActionAlias %s not found. Creating new one.',
                             alias)

                try:
                    action_alias_db = ActionAlias.add_or_update(
                        action_alias_db)
                    extra = {'action_alias_db': action_alias_db}
                    LOG.audit('Action alias updated. Action alias %s from %s.',
                              action_alias_db,
                              alias,
                              extra=extra)
                except Exception:
                    LOG.exception('Failed to create action alias %s.',
                                  action_alias_api.name)

            except Exception:
                LOG.exception('Failed registering alias from %s.', alias)
            else:
                registered_count += 1

        return registered_count
Example #7
0
    def post(self, action_alias):
        """
            Create a new ActionAlias.

            Handles requests:
                POST /actionalias/
        """
        try:
            action_alias_db = ActionAliasAPI.to_model(action_alias)
            LOG.debug('/actionalias/ POST verified ActionAliasAPI and formulated ActionAliasDB=%s',
                      action_alias_db)
            action_alias_db = ActionAlias.add_or_update(action_alias_db)
        except (ValidationError, ValueError, ValueValidationException) as e:
            LOG.exception('Validation failed for action alias data=%s.', action_alias)
            pecan.abort(http_client.BAD_REQUEST, str(e))
            return
        except StackStormDBObjectConflictError as e:
            LOG.warn('ActionAlias creation of %s failed with uniqueness conflict.', action_alias,
                     exc_info=True)
            pecan.abort(http_client.CONFLICT, str(e), body={'conflict-id': e.conflict_id})
            return

        extra = {'action_alias_db': action_alias_db}
        LOG.audit('Action alias created. ActionAlias.id=%s' % (action_alias_db.id), extra=extra)
        action_alias_api = ActionAliasAPI.from_model(action_alias_db)

        return action_alias_api
Example #8
0
    def post(self, action_alias):
        """
            Create a new ActionAlias.

            Handles requests:
                POST /actionalias/
        """
        try:
            action_alias_db = ActionAliasAPI.to_model(action_alias)
            LOG.debug(
                '/actionalias/ POST verified ActionAliasAPI and formulated ActionAliasDB=%s',
                action_alias_db)
            action_alias_db = ActionAlias.add_or_update(action_alias_db)
        except (ValidationError, ValueError, ValueValidationException) as e:
            LOG.exception('Validation failed for action alias data=%s.',
                          action_alias)
            pecan.abort(http_client.BAD_REQUEST, str(e))
            return

        extra = {'action_alias_db': action_alias_db}
        LOG.audit('Action alias created. ActionAlias.id=%s' %
                  (action_alias_db.id),
                  extra=extra)
        action_alias_api = ActionAliasAPI.from_model(action_alias_db)

        return action_alias_api
Example #9
0
    def post(self, action_alias, requester_user):
        """
            Create a new ActionAlias.

            Handles requests:
                POST /actionalias/
        """

        permission_type = PermissionType.ACTION_ALIAS_CREATE
        rbac_utils = get_rbac_backend().get_utils_class()
        rbac_utils.assert_user_has_resource_api_permission(user_db=requester_user,
                                                           resource_api=action_alias,
                                                           permission_type=permission_type)

        try:
            action_alias_db = ActionAliasAPI.to_model(action_alias)
            LOG.debug('/actionalias/ POST verified ActionAliasAPI and formulated ActionAliasDB=%s',
                      action_alias_db)
            action_alias_db = ActionAlias.add_or_update(action_alias_db)
        except (ValidationError, ValueError, ValueValidationException) as e:
            LOG.exception('Validation failed for action alias data=%s.', action_alias)
            abort(http_client.BAD_REQUEST, six.text_type(e))
            return

        extra = {'action_alias_db': action_alias_db}
        LOG.audit('Action alias created. ActionAlias.id=%s' % (action_alias_db.id), extra=extra)
        action_alias_api = ActionAliasAPI.from_model(action_alias_db)

        return Response(json=action_alias_api, status=http_client.CREATED)
Example #10
0
    def _register_action_alias(self, pack, action_alias):
        content = self._meta_loader.load(action_alias)
        pack_field = content.get('pack', None)
        if not pack_field:
            content['pack'] = pack
            pack_field = pack
        if pack_field != pack:
            raise Exception('Model is in pack "%s" but field "pack" is different: %s' %
                            (pack, pack_field))

        action_alias_api = ActionAliasAPI(**content)
        action_alias_api.validate()
        action_alias_db = ActionAliasAPI.to_model(action_alias_api)

        try:
            action_alias_db.id = ActionAlias.get_by_name(action_alias_api.name).id
        except ValueError:
            LOG.info('ActionAlias %s not found. Creating new one.', action_alias)

        try:
            action_alias_db = ActionAlias.add_or_update(action_alias_db)
            extra = {'action_alias_db': action_alias_db}
            LOG.audit('Action alias updated. Action alias %s from %s.', action_alias_db,
                      action_alias, extra=extra)
        except Exception:
            LOG.exception('Failed to create action alias %s.', action_alias_api.name)
            raise
Example #11
0
    def _register_action_alias(self, pack, action_alias):
        content = self._meta_loader.load(action_alias)
        pack_field = content.get('pack', None)
        if not pack_field:
            content['pack'] = pack
            pack_field = pack
        if pack_field != pack:
            raise Exception(
                'Model is in pack "%s" but field "pack" is different: %s' %
                (pack, pack_field))

        action_alias_api = ActionAliasAPI(**content)
        action_alias_api.validate()
        action_alias_db = ActionAliasAPI.to_model(action_alias_api)

        try:
            action_alias_db.id = ActionAlias.get_by_name(
                action_alias_api.name).id
        except ValueError:
            LOG.info('ActionAlias %s not found. Creating new one.',
                     action_alias)

        try:
            action_alias_db = ActionAlias.add_or_update(action_alias_db)
            extra = {'action_alias_db': action_alias_db}
            LOG.audit('Action alias updated. Action alias %s from %s.',
                      action_alias_db,
                      action_alias,
                      extra=extra)
        except Exception:
            LOG.exception('Failed to create action alias %s.',
                          action_alias_api.name)
            raise
Example #12
0
    def put(self, action_alias, ref_or_id, requester_user):
        """
        Update an action alias.

        Handles requests:
            PUT /actionalias/1
        """
        action_alias_db = self._get_by_ref_or_id(ref_or_id=ref_or_id)
        LOG.debug(
            "PUT /actionalias/ lookup with id=%s found object: %s",
            ref_or_id,
            action_alias_db,
        )

        permission_type = PermissionType.ACTION_ALIAS_MODIFY
        rbac_utils = get_rbac_backend().get_utils_class()
        rbac_utils.assert_user_has_resource_db_permission(
            user_db=requester_user,
            resource_db=action_alias_db,
            permission_type=permission_type,
        )

        if not hasattr(action_alias, "id"):
            action_alias.id = None

        try:
            if (
                action_alias.id is not None
                and action_alias.id != ""
                and action_alias.id != ref_or_id
            ):
                LOG.warning(
                    "Discarding mismatched id=%s found in payload and using uri_id=%s.",
                    action_alias.id,
                    ref_or_id,
                )
            old_action_alias_db = action_alias_db
            action_alias_db = ActionAliasAPI.to_model(action_alias)
            action_alias_db.id = ref_or_id
            action_alias_db = ActionAlias.add_or_update(action_alias_db)
        except (ValidationError, ValueError) as e:
            LOG.exception("Validation failed for action alias data=%s", action_alias)
            abort(http_client.BAD_REQUEST, six.text_type(e))
            return

        extra = {
            "old_action_alias_db": old_action_alias_db,
            "new_action_alias_db": action_alias_db,
        }
        LOG.audit(
            "Action alias updated. ActionAlias.id=%s." % (action_alias_db.id),
            extra=extra,
        )
        action_alias_api = ActionAliasAPI.from_model(action_alias_db)

        return action_alias_api
Example #13
0
    def _get_action_alias_db(self, pack, action_alias):
        """
        Retrieve ActionAliasDB object.
        """
        content = self._meta_loader.load(action_alias)
        pack_field = content.get("pack", None)
        if not pack_field:
            content["pack"] = pack
            pack_field = pack
        if pack_field != pack:
            raise Exception('Model is in pack "%s" but field "pack" is different: %s' % (pack, pack_field))

        action_alias_api = ActionAliasAPI(**content)
        action_alias_api.validate()
        action_alias_db = ActionAliasAPI.to_model(action_alias_api)

        return action_alias_db
Example #14
0
    def put(self, action_alias, ref_or_id, requester_user):
        """
            Update an action alias.

            Handles requests:
                PUT /actionalias/1
        """
        action_alias_db = self._get_by_ref_or_id(ref_or_id=ref_or_id)
        LOG.debug('PUT /actionalias/ lookup with id=%s found object: %s',
                  ref_or_id, action_alias_db)

        permission_type = PermissionType.ACTION_ALIAS_MODIFY
        rbac_utils.assert_user_has_resource_db_permission(
            user_db=requester_user,
            resource_db=action_alias_db,
            permission_type=permission_type)

        if not hasattr(action_alias, 'id'):
            action_alias.id = None

        try:
            if action_alias.id is not None and action_alias.id is not '' and \
               action_alias.id != ref_or_id:
                LOG.warning(
                    'Discarding mismatched id=%s found in payload and using uri_id=%s.',
                    action_alias.id, ref_or_id)
            old_action_alias_db = action_alias_db
            action_alias_db = ActionAliasAPI.to_model(action_alias)
            action_alias_db.id = ref_or_id
            action_alias_db = ActionAlias.add_or_update(action_alias_db)
        except (ValidationError, ValueError) as e:
            LOG.exception('Validation failed for action alias data=%s',
                          action_alias)
            abort(http_client.BAD_REQUEST, str(e))
            return

        extra = {
            'old_action_alias_db': old_action_alias_db,
            'new_action_alias_db': action_alias_db
        }
        LOG.audit('Action alias updated. ActionAlias.id=%s.' %
                  (action_alias_db.id),
                  extra=extra)
        action_alias_api = ActionAliasAPI.from_model(action_alias_db)

        return action_alias_api
Example #15
0
    def _get_action_alias_db(self, pack, action_alias):
        """
        Retrieve ActionAliasDB object.
        """
        content = self._meta_loader.load(action_alias)
        pack_field = content.get('pack', None)
        if not pack_field:
            content['pack'] = pack
            pack_field = pack
        if pack_field != pack:
            raise Exception(
                'Model is in pack "%s" but field "pack" is different: %s' %
                (pack, pack_field))

        action_alias_api = ActionAliasAPI(**content)
        action_alias_api.validate()
        action_alias_db = ActionAliasAPI.to_model(action_alias_api)

        return action_alias_db
Example #16
0
    def post(self, action_alias):
        """
            Create a new ActionAlias.

            Handles requests:
                POST /actionalias/
        """
        try:
            action_alias_db = ActionAliasAPI.to_model(action_alias)
            LOG.debug("/actionalias/ POST verified ActionAliasAPI and formulated ActionAliasDB=%s", action_alias_db)
            action_alias_db = ActionAlias.add_or_update(action_alias_db)
        except (ValidationError, ValueError, ValueValidationException) as e:
            LOG.exception("Validation failed for action alias data=%s.", action_alias)
            pecan.abort(http_client.BAD_REQUEST, str(e))
            return

        extra = {"action_alias_db": action_alias_db}
        LOG.audit("Action alias created. ActionAlias.id=%s" % (action_alias_db.id), extra=extra)
        action_alias_api = ActionAliasAPI.from_model(action_alias_db)

        return action_alias_api
Example #17
0
    def _get_action_alias_db(self,
                             pack,
                             action_alias,
                             ignore_metadata_file_error=False):
        """
        Retrieve ActionAliasDB object.

        :param ignore_metadata_file_error: True to ignore the error when we can't infer
                                            metadata_file attribute (e.g. inside tests).
        :type ignore_metadata_file_error: ``bool``
        """
        content = self._meta_loader.load(action_alias)
        pack_field = content.get("pack", None)
        if not pack_field:
            content["pack"] = pack
            pack_field = pack
        if pack_field != pack:
            raise Exception(
                'Model is in pack "%s" but field "pack" is different: %s' %
                (pack, pack_field))

        # Add in "metadata_file" attribute which stores path to the pack metadata file relative to
        # the pack directory
        try:
            metadata_file = content_utils.get_relative_path_to_pack_file(
                pack_ref=pack, file_path=action_alias, use_pack_cache=True)
        except ValueError as e:
            if not ignore_metadata_file_error:
                raise e
        else:
            content["metadata_file"] = metadata_file

        # Pass override information
        altered = self._override_loader.override(pack, "aliases", content)

        action_alias_api = ActionAliasAPI(**content)
        action_alias_api.validate()
        action_alias_db = ActionAliasAPI.to_model(action_alias_api)

        return action_alias_db, altered
Example #18
0
    def put(self, action_alias, ref_or_id, requester_user):
        """
            Update an action alias.

            Handles requests:
                PUT /actionalias/1
        """
        action_alias_db = self._get_by_ref_or_id(ref_or_id=ref_or_id)
        LOG.debug('PUT /actionalias/ lookup with id=%s found object: %s', ref_or_id,
                  action_alias_db)

        permission_type = PermissionType.ACTION_ALIAS_MODIFY
        rbac_utils = get_rbac_backend().get_utils_class()
        rbac_utils.assert_user_has_resource_db_permission(user_db=requester_user,
                                                          resource_db=action_alias_db,
                                                          permission_type=permission_type)

        if not hasattr(action_alias, 'id'):
            action_alias.id = None

        try:
            if action_alias.id is not None and action_alias.id != '' and \
               action_alias.id != ref_or_id:
                LOG.warning('Discarding mismatched id=%s found in payload and using uri_id=%s.',
                            action_alias.id, ref_or_id)
            old_action_alias_db = action_alias_db
            action_alias_db = ActionAliasAPI.to_model(action_alias)
            action_alias_db.id = ref_or_id
            action_alias_db = ActionAlias.add_or_update(action_alias_db)
        except (ValidationError, ValueError) as e:
            LOG.exception('Validation failed for action alias data=%s', action_alias)
            abort(http_client.BAD_REQUEST, six.text_type(e))
            return

        extra = {'old_action_alias_db': old_action_alias_db, 'new_action_alias_db': action_alias_db}
        LOG.audit('Action alias updated. ActionAlias.id=%s.' % (action_alias_db.id), extra=extra)
        action_alias_api = ActionAliasAPI.from_model(action_alias_db)

        return action_alias_api