Esempio n. 1
0
def test_expand_environment_variables_in_connection():
    conn_str = "{}://cluster('clustername').database('dbname')".format(TEST_URI_SCHEMA_NAME)
    os.environ['KQL_DATABASE'] = conn_str
    assert Parser.parse("$KQL_DATABASE {}".format(query1), empty_config) == \
            {'connection': conn_str,
            'kql': query1,
            'options': default_options}
Esempio n. 2
0
def test_parse_kusto_socket_connection_dsn():
    conn_str = "{}://username($USERNAME).password($PASSWORD).cluster('clustername').database('dbname')".format(TEST_URI_SCHEMA_NAME)
    result_conn_str = "{}://username('michael').password('michael123').cluster('clustername').database('dbname')".format(TEST_URI_SCHEMA_NAME)
    assert Parser.parse("{0} {1}".format(conn_str, query1), empty_config) == \
            {'connection': result_conn_str,
            'kql': query1,
            'options': default_options}
Esempio n. 3
0
def test_parse_kql_only():
    parsed = Parser.parse(query1, empty_config)
    print(parsed)
    assert parsed == \
           {'connection': "",
            'kql': query1,
            'options': default_options}
Esempio n. 4
0
def test_parse_kusto_socket_connection_with_credentials():
    conn_str = "{}://username('username').password('password').cluster('clustername').database('dbname')".format(
        TEST_URI_SCHEMA_NAME)
    assert Parser.parse("{0} {1}".format(conn_str, query1), empty_config, TEST_ENGINE, {}) == \
           {'connection': conn_str,
            'kql': query1,
            'options': default_options}
Esempio n. 5
0
def test_parse_kusto_socket_connection_with_env_credentials():
    conn_str = "{}://username($USERNAME).password($PASSWORD).cluster('clustername').database('dbname')".format(
        TEST_URI_SCHEMA_NAME)
    result_conn_str = "{}://username('michael').password('michael123').cluster('clustername').database('dbname')".format(
        TEST_URI_SCHEMA_NAME)
    os.environ['USERNAME'] = "******"
    os.environ['PASSWORD'] = "******"
    assert Parser.parse("{0} {1}".format(conn_str, query1), empty_config, TEST_ENGINE, {}) == \
           {'connection': result_conn_str,
            'kql': query1,
            'options': default_options}
Esempio n. 6
0
def test_parse_kusto_socket_connection():
    conn_str = "{}://cluster('clustername').database('dbname')".format(TEST_URI_SCHEMA_NAME)
    assert Parser.parse("{0} {1}".format(conn_str, query1), empty_config) == \
           {'connection': conn_str,
            'kql': query1,
            'options': default_options}
Esempio n. 7
0
def test_parse_with_kql():
    assert Parser.parse("dbname@clustername {}".format(query1),
                 empty_config) == \
           {'connection': "dbname@clustername",
            'kql': query1,
            'options': default_options}
Esempio n. 8
0
def test_parse_no_kql():
    assert Parser.parse("dbname@clustername", empty_config) == \
           {'connection': "dbname@clustername",
            'kql': '',
            'options': default_options}
Esempio n. 9
0
    def execute(self, line, cell="", local_ns={}):
        """Query Kusto or ApplicationInsights using kusto query language (kql). Repository specified by a connect string.

        Magic Syntax::

            %%kql <connection-string>
            <KQL statement>
            # Note: establish connection and query.

            %%kql <established-connection-reference>
            <KQL statemnt>
            # Note: query using an established connection.

            %%kql
            <KQL statement>
            # Note: query using current established connection.

            %kql <KQL statment>
            # Note: single line query using current established connection.

            %kql <connection-string>
            # Note: established connection only.


        Connection string Syntax::

            kusto://username('<username>).password(<password>).cluster(<cluster>).database(<database>')

            appinsights://appid(<appid>).appkey(<appkey>)

            loganalytics://workspace(<workspaceid>).appkey(<appkey>)

            %<connectionStringVariable>%
            # Note: connection string is taken from the environment variable.

            [<sectionName>]
            # Note: connection string is built from the dsn file settings, section <sectionName>. 
            #       The dsn filename value is taken from configuartion value Kqlmagic.dsn_filename.

            # Note: if password or appkey component is missing, user will be prompted.
            # Note: connection string doesn't have to include all components, see examples below.
            # Note: substring of the form $name or ${name} in windows also %name%, are replaced by environment variables if exist.


        Examples::

            %%kql kusto://username('myName').password('myPassword').cluster('myCluster').database('myDatabase')
            <KQL statement>
            # Note: establish connection to kusto and submit query.

            %%kql myDatabase@myCluster
            <KQL statement>
            # Note: submit query using using an established kusto connection to myDatabase database at cluster myCluster.

            %%kql appinsights://appid('myAppid').appkey('myAppkey')
            <KQL statement>
            # Note: establish connection to ApplicationInsights and submit query.

            %%kql myAppid@appinsights
            <KQL statement>
            # Note: submit query using established ApplicationInsights connection to myAppid.

            %%kql loganalytics://workspace('myWorkspaceid').appkey('myAppkey')
            <KQL statement>
            # Note: establish connection to LogAnalytics and submit query.

            %%kql myWorkspaceid@loganalytics
            <KQL statement>
            # Note: submit query using established LogAnalytics connection to myWorkspaceid.

            %%kql
            <KQL statement>
            # Note: submit query using current established connection.

            %kql <KQL statement>
            # Note: submit single line query using current established connection.

            %%kql kusto://cluster('myCluster').database('myDatabase')
            <KQL statement>
            # Note: establish connection to kusto using current username and password to form the full connection string and submit query.

            %%kql kusto://database('myDatabase')
            <KQL statement>
            # Note: establish connection to kusto using current username, password and cluster to form the full connection string and submit query.

            %kql kusto://username('myName').password('myPassword').cluster('myCluster')
            # Note: set current (default) username, passsword and cluster to kusto.

            %kql kusto://username('myName').password('myPassword')
            # Note: set current (default) username and password to kusto.

            %kql kusto://cluster('myCluster')
            # Note set current (default) cluster to kusto.
        """

        set_logger(Logger(None, create_log_context()))

        # save globals and locals so they can be referenced in bind vars
        user_ns = self.shell.user_ns.copy()
        user_ns.update(local_ns)

        logger().debug("To Parsed: \n\rline: {}\n\rcell:\n\r{}".format(
            line, cell))
        try:
            parsed = None
            parsed_queries = Parser.parse("%s\n%s" % (line, cell), self)
            logger().debug("Parsed: {}".format(parsed_queries))
            result = None
            for parsed in parsed_queries:
                result = self.execute_query(parsed, user_ns)
            return result
        except Exception as e:
            if parsed:
                if parsed["options"].get("short_errors", self.short_errors):
                    Display.showDangerMessage(str(e))
                    return None
            elif self.short_errors:
                Display.showDangerMessage(str(e))
                return None
            raise