Skip to content
This repository has been archived by the owner on Mar 31, 2021. It is now read-only.

Fix test failures #19

Merged
merged 1 commit into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions escli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ def config_location():
if "XDG_CONFIG_HOME" in os.environ:
return "%s/escli/" % expanduser(os.environ["XDG_CONFIG_HOME"])
elif platform.system() == "Windows":
# USERPROFILE is typically C:\Users\{username}
return "%s\\AppData\\Local\\dbcli\\escli\\" % os.getenv("USERPROFILE")
else:
return expanduser("~/.conf/escli/")
return expanduser("~/.config/escli/")


def _load_config(user_config, default_config=None):
Expand Down Expand Up @@ -69,7 +70,7 @@ def get_config(esclirc_file=None):
"""
Get config for escli.

This config comes from either existing conf in the OS, or create a conf file in the OS, and write default conf
This config comes from either existing config in the OS, or create a config file in the OS, and write default config
including in the package to it.
"""
from escli.conf import __file__ as package_root
Expand Down
29 changes: 23 additions & 6 deletions escli/esconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(self, endpoint=None, http_auth=None):
self.client = None
self.ssl_context = None
self.es_version = None
self.plugins = None
self.aws_auth = None
self.indices_list = []
self.endpoint = endpoint
Expand All @@ -54,10 +55,13 @@ def get_indices(self):
def get_aes_client(self):
service = "es"
session = boto3.Session()

credentials = session.get_credentials()
region = session.region_name
self.aws_auth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service)

if credentials is not None:
self.aws_auth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service)
else:
click.secho(message="Can not retrieve credentials, check your aws config", fg="red")

aes_client = Elasticsearch(
hosts=[{"host": str(self.endpoint), "port": 443}],
Expand All @@ -80,6 +84,11 @@ def get_open_distro_client(self):

return open_distro_client

def is_sql_plugin_installed(self, es_client):
self.plugins = es_client.cat.plugins(params={"s": "component", "v": "true"})
sql_plugin_name_list = ["opendistro-sql", "opendistro_sql"]
return any(x in self.plugins for x in sql_plugin_name_list)

def set_connection(self, is_reconnect=False):
urllib3.disable_warnings()
logging.captureWarnings(True)
Expand All @@ -93,14 +102,22 @@ def set_connection(self, is_reconnect=False):
else:
es_client = Elasticsearch([self.endpoint], verify_certs=True)

# check client, es.info() may throw ConnectionError
# check connection. check Open Distro Elasticsearch SQL plugin availability.
try:
if not self.is_sql_plugin_installed(es_client):
click.secho(
message="Must have Open Distro SQL plugin installed in your Elasticsearch "
"instance!\nCheck this out: https://github.com/opendistro-for-elasticsearch/sql",
fg="red",
)
click.echo(self.plugins)
sys.exit()

# info() may throw ConnectionError, if connection fails to establish
info = es_client.info()
es_version = info["version"]["number"]

self.es_version = info["version"]["number"]
self.client = es_client
self.get_indices()
self.es_version = es_version

except ConnectionError as error:
if is_reconnect:
Expand Down
4 changes: 2 additions & 2 deletions escli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
@click.command()
@click.argument("endpoint", default="http://localhost:9200")
@click.option("-q", "--query", "query", type=click.STRING, help="Run single query in non-interactive mode")
@click.option("-e", "--explain", "explain", is_flag=True, help="Explain sql to DSL")
@click.option("-e", "--explain", "explain", is_flag=True, help="Explain SQL to ES DSL")
@click.option(
"--esclirc",
default=config_location() + "conf",
default=config_location() + "config",
envvar="ESCLIRC",
help="Location of esclirc file.",
type=click.Path(dir_okay=False),
Expand Down
19 changes: 13 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
from setuptools import setup, find_packages

install_requirements = [
"click >= 4.1",
"prompt_toolkit >= 2.0.6",
"Pygments >= 2.0",
"cli_helpers[styles] >= 1.2.0",
"elasticsearch>=7.0.0,<8.0.0",
"click == 7.1.1",
"prompt_toolkit == 2.0.6",
"Pygments == 2.6.1",
"cli_helpers[styles] == 1.2.1",
"elasticsearch == 7.5.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason specify 7.5.1 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it is just the latest stable version of elasticsearch python client. I change it to a fixed version because I switched to use fixed version number. I believe it is also good for maintenance. For reference https://elasticsearch-py.readthedocs.io/en/master/ "For Elasticsearch 7.0 and later, use the major version 7 (7.x.y) of the library."

"pyfiglet == 0.8.post1",
"boto3 >= 1.9.181",
"boto3 == 1.9.181",
"requests-aws4auth == 0.9",
]

Expand All @@ -37,6 +37,9 @@

description = "CLI for Elasticsearch Open Distro SQL with auto-completion and syntax highlighting."

with open("README.md", "r") as fh:
long_description = fh.read()

setup(
name="escli",
author="Zhongnan",
Expand All @@ -47,12 +50,16 @@
packages=find_packages(),
package_data={"escli": ["conf/esclirc", "esliterals/esliterals.json"]},
description=description,
long_description=long_description,
long_description_content_type="text/markdown",
install_requires=install_requirements,
entry_points={"console_scripts": ["escli=escli.main:cli"]},
classifiers=[
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Operating System :: Unix",
"Operating System :: POSIX :: Linux",
"Operating System :: Microsoft :: Windows",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.4",
Expand Down
14 changes: 10 additions & 4 deletions tests/test_esconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ def test_query_nonexistent_index(self, connection):
self.load_data_to_es(connection)

expected = {
"reason": "Invalid SQL query",
"details": "no such index [non-existed]",
"reason": "Error occurred in Elasticsearch engine: no such index [non-existed]",
"details": "org.elasticsearch.index.IndexNotFoundException: no such index [non-existed]\nFor more "
"details, please send request for Json format to see the raw response from elasticsearch "
"engine.",
"type": "IndexNotFoundException",
}

Expand Down Expand Up @@ -109,11 +111,15 @@ def test_select_client(self):
od_test_executor = ESConnection(endpoint=OPEN_DISTRO_ENDPOINT, http_auth=AUTH)
aes_test_executor = ESConnection(endpoint=AES_ENDPOINT)

with mock.patch.object(od_test_executor, "get_open_distro_client") as mock_od_client:
with mock.patch.object(od_test_executor, "get_open_distro_client") as mock_od_client, mock.patch.object(
ESConnection, "is_sql_plugin_installed", return_value=True
):
od_test_executor.set_connection()
mock_od_client.assert_called()

with mock.patch.object(aes_test_executor, "get_aes_client") as mock_aes_client:
with mock.patch.object(aes_test_executor, "get_aes_client") as mock_aes_client, mock.patch.object(
ESConnection, "is_sql_plugin_installed", return_value=True
):
aes_test_executor.set_connection()
mock_aes_client.assert_called()

Expand Down