Ejemplo n.º 1
0
async def delete_filter_pipeline(request: web.Request) -> web.Response:
    """ DELETE filter pipeline

    :Example:
        curl -X DELETE http://localhost:8081/foglamp/filter/<user_name>/pipeline
    """
    user_name = request.match_info.get('user_name', None)
    try:
        put_url = request.url
        data = '{"pipeline": []}'
        async with aiohttp.ClientSession() as session:
            async with session.put(put_url, data=data) as resp:
                status_code = resp.status
                jdoc = await resp.text()
                if status_code not in range(200, 209):
                    _LOGGER.error(
                        "Error code: %d, reason: %s, details: %s, url: %s",
                        resp.status, resp.reason, jdoc, put_url)
                    raise StorageServerError(code=resp.status,
                                             reason=resp.reason,
                                             error=jdoc)
    except Exception:
        raise
    else:
        return web.json_response({
            'result':
            "Filter pipeline for {} deleted successfully".format(user_name)
        })
Ejemplo n.º 2
0
 async def test_create_user_exception(self):
     hashed_password = "******"
     expected = {
         'message': 'Something went wrong',
         'retryable': False,
         'entryPoint': 'insert'
     }
     payload = {
         "pwd": "dd7171406eaf4baa8bc805857f719bca",
         "role_id": 1,
         "uname": "aj"
     }
     storage_client_mock = MagicMock(StorageClientAsync)
     with patch.object(connect,
                       'get_storage_async',
                       return_value=storage_client_mock):
         with patch.object(User.Objects,
                           'hash_password',
                           return_value=hashed_password) as hash_pwd_patch:
             with patch.object(storage_client_mock,
                               'insert_into_tbl',
                               return_value=mock_coro(),
                               side_effect=StorageServerError(
                                   code=400, reason="blah",
                                   error=expected)) as insert_tbl_patch:
                 with pytest.raises(ValueError) as excinfo:
                     await User.Objects.create("aj", "foglamp", 1)
                 assert str(excinfo.value) == expected['message']
             args, kwargs = insert_tbl_patch.call_args
             assert 'users' == args[0]
             p = json.loads(args[1])
             assert payload == p
         hash_pwd_patch.assert_called_once_with('foglamp', )
Ejemplo n.º 3
0
 def test_delete_token_exception(self):
     expected = {'message': 'Something went wrong', 'retryable': False, 'entryPoint': 'delete'}
     payload = '{"where": {"column": "token", "condition": "=", "value": "eyx"}}'
     storage_client_mock = MagicMock(StorageClient)
     with patch.object(connect, 'get_storage', return_value=storage_client_mock):
         with patch.object(storage_client_mock, 'delete_from_tbl', side_effect=StorageServerError(code=400, reason="blah", error=expected)) as delete_tbl_patch:
             with pytest.raises(ValueError) as excinfo:
                 User.Objects.delete_token("eyx")
             assert str(excinfo.value) == expected['message']
     delete_tbl_patch.assert_called_once_with('user_logins', payload)
Ejemplo n.º 4
0
 def test_update_user_storage_exception(self):
     expected = {'message': 'Something went wrong', 'retryable': False, 'entryPoint': 'update'}
     payload = '{"values": {"role_id": 2}, "where": {"column": "id", "condition": "=", "value": 2, "and": {"column": "enabled", "condition": "=", "value": "t"}}}'
     storage_client_mock = MagicMock(StorageClient)
     with patch.object(connect, 'get_storage', return_value=storage_client_mock):
         with patch.object(storage_client_mock, 'update_tbl', side_effect=StorageServerError(code=400, reason="blah", error=expected)) as update_tbl_patch:
             with pytest.raises(ValueError) as excinfo:
                 User.Objects.update(2, {'role_id': 2})
             assert str(excinfo.value) == expected['message']
     update_tbl_patch.assert_called_once_with('users', payload)
Ejemplo n.º 5
0
async def _hit_post_url(post_url, data=None):
    try:
        async with aiohttp.ClientSession() as session:
            async with session.post(post_url, data=data) as resp:
                status_code = resp.status
                jdoc = await resp.text()
                if status_code not in range(200, 209):
                    _logger.error("Error code: %d, reason: %s, details: %s, url: %s", resp.status, resp.reason, jdoc,
                                  post_url)
                    raise StorageServerError(code=resp.status, reason=resp.reason, error=jdoc)
    except Exception:
        raise
    else:
        return jdoc
Ejemplo n.º 6
0
async def _hit_get_url(get_url, token=None):
    headers = {"Authorization": token} if token else None
    try:
        async with aiohttp.ClientSession() as session:
            async with session.get(get_url, headers=headers) as resp:
                status_code = resp.status
                jdoc = await resp.text()
                if status_code not in range(200, 209):
                    _logger.error("Error code: %d, reason: %s, details: %s, url: %s", resp.status, resp.reason, jdoc,
                                  get_url)
                    raise StorageServerError(code=resp.status, reason=resp.reason, error=jdoc)
    except Exception:
        raise
    else:
        return jdoc
Ejemplo n.º 7
0
async def modify_process_name(service_name, core_management_host,
                              core_management_port):
    storage = StorageClientAsync(core_management_host, core_management_port)

    try:
        payload = PayloadBuilder().SELECT("id").WHERE(
            ['schedule_name', '=', service_name]).payload()
        result = await storage.query_tbl_with_payload('schedules', payload)
    except Exception:
        raise

    if int(result['count']):
        sch_id = result['rows'][0]['id']
    else:
        _LOGGER.error('No schedule id found for %s. Exiting...', service_name)
        raise RuntimeError('No schedule id found for %s. Exiting...',
                           service_name)

    # Modify process name
    try:
        put_url = "{}://{}:{}/foglamp/schedule/{}".format(
            _HTTP, _HOST, _PORT, sch_id)
        data = '{"process_name": "south_c"}'
        verify_ssl = False if _HTTP == 'http' else True
        connector = aiohttp.TCPConnector(verify_ssl=verify_ssl)
        async with aiohttp.ClientSession(connector=connector) as session:
            async with session.put(put_url, data=data) as resp:
                status_code = resp.status
                jdoc = await resp.text()
                if status_code not in range(200, 209):
                    _LOGGER.error(
                        "Error code: %d, reason: %s, details: %s, url: %s",
                        resp.status, resp.reason, jdoc, put_url)
                    raise StorageServerError(code=resp.status,
                                             reason=resp.reason,
                                             error=jdoc)
    except Exception:
        raise
    else:
        _LOGGER.info(
            'Modified process_name from "south" to "south_c" for Python South service [%s]: %s',
            service_name, jdoc)
Ejemplo n.º 8
0
async def disable_service(service_name):
    try:
        put_url = "{}://{}:{}/foglamp/schedule/disable".format(
            _HTTP, _HOST, _PORT)
        data = '{"schedule_name": "%s"}' % service_name
        verify_ssl = False if _HTTP == 'http' else True
        connector = aiohttp.TCPConnector(verify_ssl=verify_ssl)
        async with aiohttp.ClientSession(connector=connector) as session:
            async with session.put(put_url, data=data) as resp:
                status_code = resp.status
                jdoc = await resp.json()
                if status_code not in range(200, 209):
                    _LOGGER.error(
                        "Error code: %d, reason: %s, details: %s, url: %s",
                        resp.status, resp.reason, jdoc, put_url)
                    raise StorageServerError(code=resp.status,
                                             reason=resp.reason,
                                             error=jdoc)
    except Exception:
        raise
    else:
        _LOGGER.info(
            'Disabled Python South service [%s] to change process_name to south_c',
            service_name)
Ejemplo n.º 9
0
    async def test_login_exception(self):
        async def mock_get_category_item():
            return {"value": "0"}

        pwd_result = {'count': 1, 'rows': [{'role_id': '1', 'pwd': '3759bf3302f5481e8c9cc9472c6088ac', 'id': '1', 'pwd_last_changed': '2018-03-30 12:32:08.216159'}]}
        expected = {'message': 'Something went wrong', 'retryable': False, 'entryPoint': 'delete'}
        payload = {"return": ["pwd", "id", "role_id", {"column": "pwd_last_changed", "format": "YYYY-MM-DD HH24:MI:SS.MS", "alias": "pwd_last_changed"}], "where": {"column": "uname", "condition": "=", "value": "user", "and": {"column": "enabled", "condition": "=", "value": "t"}}}
        storage_client_mock = MagicMock(StorageClient)
        with patch.object(connect, 'get_storage', return_value=storage_client_mock):
            with patch.object(ConfigurationManager, "get_category_item", return_value=mock_get_category_item()) as mock_get_cat_patch:
                with patch.object(storage_client_mock, 'query_tbl_with_payload', return_value=pwd_result) as query_tbl_patch:
                    with patch.object(User.Objects, 'check_password', return_value=True) as check_pwd_patch:
                        with patch.object(storage_client_mock, 'insert_into_tbl', side_effect=StorageServerError(code=400, reason="blah", error=expected)):
                            with pytest.raises(ValueError) as excinfo:
                                await User.Objects.login('user', 'foglamp', '0.0.0.0')
                            assert str(excinfo.value) == expected['message']
                    check_pwd_patch.assert_called_once_with('3759bf3302f5481e8c9cc9472c6088ac', 'foglamp')
                args, kwargs = query_tbl_patch.call_args
                assert 'users' == args[0]
                p = json.loads(args[1])
                assert payload == p
            mock_get_cat_patch.assert_called_once_with('rest_api', 'passwordChange')