Exemplo n.º 1
0
    def login(self, engine_api, user, password):
        if (engine_api.get('major') < 0 or engine_api.get('minor') < 0
                or engine_api.get('micro') < 0):
            raise exceptions.HttpError(
                200, {
                    'type':
                    'APIError',
                    'details':
                    'Invalid API version.',
                    'action':
                    'Change the API version to one supported by your'
                    ' version of the Delphix Engine. Review the Release'
                    ' Notes for your Delphix Engine version to determine'
                    ' which API version is supported.',
                    'id':
                    'exception.webservices.session.invalid.version'
                })

        found_password = self.__users.get(user)
        if password != found_password:
            raise exceptions.HttpError(
                401, {
                    'type': 'APIError',
                    'details': 'Invalid username or password.',
                    'action': 'Try with a different set of credentials.',
                    'id': 'exception.webservices.login.failed'
                })
Exemplo n.º 2
0
    def login(self, engine_api, user, password):
        if (engine_api.get("major") < 0 or engine_api.get("minor") < 0
                or engine_api.get("micro") < 0):
            raise exceptions.HttpError(
                200,
                {
                    "type":
                    "APIError",
                    "details":
                    "Invalid API version.",
                    "action":
                    "Change the API version to one supported by your"
                    " version of the Delphix Engine. Review the Release"
                    " Notes for your Delphix Engine version to determine"
                    " which API version is supported.",
                    "id":
                    "exception.webservices.session.invalid.version",
                },
            )

        found_password = self.__users.get(user)
        if password != found_password:
            raise exceptions.HttpError(
                401,
                {
                    "type": "APIError",
                    "details": "Invalid username or password.",
                    "action": "Try with a different set of credentials.",
                    "id": "exception.webservices.login.failed",
                },
            )
Exemplo n.º 3
0
    def test_bad_password(mock_download_logs, plugin_config_file):
        engine = 'engine'
        user = '******'
        password = '******'
        directory = os.getcwd()
        mock_download_logs.side_effect = exceptions.HttpError(
            401, {
                'type': 'APIError',
                'details': 'Invalid username or password.',
                'action': 'Try with a different set of credentials.',
                'id': 'exception.webservices.login.failed'
            })

        runner = click_testing.CliRunner()
        result = runner.invoke(cli.delphix_sdk, [
            'download-logs', '-e', engine, '-c', plugin_config_file, '-u',
            user, '--password', password, '-d', directory
        ])

        assert result.exit_code == 1
        assert result.output == (
            'API request failed with HTTP Status 401'
            '\nDetails: Invalid username or password.'
            '\nAction: Try with a different set of credentials.'
            '\n')
        mock_download_logs.assert_called_once_with(engine, plugin_config_file,
                                                   user, password, directory)
Exemplo n.º 4
0
    def test_bad_password(mock_upload, artifact_file):
        engine = 'engine'
        user = '******'
        password = '******'
        mock_upload.side_effect = exceptions.HttpError(
            401, {
                'type': 'APIError',
                'details': 'Invalid username or password.',
                'action': 'Try with a different set of credentials.',
                'id': 'exception.webservices.login.failed'
            })

        runner = click_testing.CliRunner()
        result = runner.invoke(cli.delphix_sdk, [
            'upload', '-e', engine, '-u', user, '-a', artifact_file,
            '--password', password
        ])

        assert result.exit_code == 1
        assert result.output == (
            'API request failed with HTTP Status 401'
            '\nDetails: Invalid username or password.'
            '\nAction: Try with a different set of credentials.'
            '\n')
        mock_upload.assert_called_once_with(engine, user, artifact_file,
                                            password)
Exemplo n.º 5
0
    def __post(self, resource, content_type='application/json', data=None):
        """
        Generates the http request post based on the resource. If no
        content_type is passed in assume it's a json. If the post fails we
        attempt to raise a specific error between HttpPostError and
        UnexpectedError.
        """
        # Form HTTP header and add the cookie if one has been set.
        headers = {'Content-type': content_type}
        if self.__cookie is not None:
            logger.debug('Cookie being used: {}'.format(self.__cookie))
            headers['Cookie'] = self.__cookie
        else:
            logger.debug('No cookie being used')

        url = 'http://{}/resources/json/{}'.format(self.__engine, resource)
        #
        # Issue post request that was passed in, if data is a dict then convert
        # it to a json string.
        #
        if data is not None and not isinstance(data, (str, bytes, unicode)):
            data = json.dumps(data)
        try:
            response = requests.post(url=url, data=data, headers=headers)
        except requests.exceptions.RequestException as err:
            raise exceptions.UserError('Encountered a http request failure.'
                                       '\n{}'.format(err))

        #
        # Save cookie if one was received because the next time a request
        # happens the newest cookie is expected. If the post recent cookie
        # returned isn't used the request will fail.
        #
        if 'set-cookie' in response.headers:
            self.__cookie = response.headers['set-cookie']
            logger.debug('New cookie found: {}'.format(self.__cookie))

        try:
            response_json = response.json()
        except ValueError:
            raise exceptions.UnexpectedError(response.status_code,
                                             response.text)

        logger.debug('Response body: {}'.format(json.dumps(response_json)))

        if (response.status_code == 200
                and (response_json.get('type') == 'OKResult'
                     or response_json.get('type') == 'DataResult')):
            return response_json

        if response_json.get('type') == 'ErrorResult':
            raise exceptions.HttpError(response.status_code,
                                       response_json['error'])
        raise exceptions.UnexpectedError(response.status_code,
                                         json.dumps(response_json, indent=2))
Exemplo n.º 6
0
 def upload_plugin(self, name, content):
     if content.get('discoveryDefinition') is None:
         raise exceptions.HttpError(
             200, {
                 'type':
                 'APIError',
                 'details':
                 'The schema "repositorySchema" in the'
                 ' "DiscoveryDefinition" cannot be updated because'
                 ' there exist objects that conform to the original'
                 ' schema.',
                 'action':
                 'Delete the following objects and try again:'
                 ' db_centos75_157_2,db_centos75_157_1,'
                 'db_centos75_157_0',
                 'id':
                 'exception.toolkit.cannot.update.schema'
             })
     self.uploaded_files[name] = content
Exemplo n.º 7
0
    def __get(self, resource, content_type='application/json', stream=False):
        """
        Generates the http request get based on the resource provided. If no
        content_type is passed in assume it's application/json. If the get
        fails we attempt to raise a specific error between HttpGetError and
        UnexpectedError.
        """
        # Form HTTP header and add the cookie if one has been set.
        headers = {'Content-type': content_type}
        if self.__cookie is not None:
            logger.debug('Cookie being used: {}'.format(self.__cookie))
            headers['Cookie'] = self.__cookie
        else:
            logger.debug('No cookie being used')

        url = 'http://{}/resources/json/{}'.format(self.__engine, resource)

        try:
            response = requests.get(url=url, headers=headers, stream=stream)
        except requests.exceptions.RequestException as err:
            raise exceptions.UserError('Encountered a http request failure.'
                                       '\n{}'.format(err))

        #
        # Save cookie if one was received because the next time a request
        # happens the newest cookie is expected. If the post recent cookie
        # returned isn't used the request will fail.
        #
        if 'set-cookie' in response.headers:
            self.__cookie = response.headers['set-cookie']
            logger.debug('New cookie found: {}'.format(self.__cookie))

        if response.status_code == 200:
            return response
        else:
            response_error = None
            try:
                response_error = response.json()['error']
            except ValueError:
                pass
            raise exceptions.HttpError(response.status_code, response_error)
Exemplo n.º 8
0
    def test_bad_password(mock_download_logs, plugin_config_file):
        engine = "engine"
        user = "******"
        password = "******"
        directory = os.getcwd()
        mock_download_logs.side_effect = exceptions.HttpError(
            401,
            {
                "type": "APIError",
                "details": "Invalid username or password.",
                "action": "Try with a different set of credentials.",
                "id": "exception.webservices.login.failed",
            },
        )

        runner = click_testing.CliRunner()
        result = runner.invoke(
            cli.delphix_sdk,
            [
                "download-logs",
                "-e",
                engine,
                "-c",
                plugin_config_file,
                "-u",
                user,
                "--password",
                password,
                "-d",
                directory,
            ],
        )

        assert result.exit_code == 1
        assert result.output == (
            "API request failed with HTTP Status 401"
            "\nDetails: Invalid username or password."
            "\nAction: Try with a different set of credentials."
            "\n")
        mock_download_logs.assert_called_once_with(engine, plugin_config_file,
                                                   user, password, directory)
Exemplo n.º 9
0
 def upload_plugin(self, name, content):
     if content.get("discoveryDefinition") is None:
         raise exceptions.HttpError(
             200,
             {
                 "type":
                 "APIError",
                 "details":
                 'The schema "repositorySchema" in the'
                 ' "DiscoveryDefinition" cannot be updated because'
                 " there exist objects that conform to the original"
                 " schema.",
                 "action":
                 "Delete the following objects and try again:"
                 " db_centos75_157_2,db_centos75_157_1,"
                 "db_centos75_157_0",
                 "id":
                 "exception.toolkit.cannot.update.schema",
             },
         )
     self.uploaded_files[name] = content