-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes for SSL certs, HTTP authentication, and consistent list types #26
base: main
Are you sure you want to change the base?
Changes from all commits
507f5f4
8852bbf
c3cb058
b45b880
be9a2d7
2857f0a
ff1289c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,49 @@ | |
from opensearch_schema import ErrorOutput, SuccessOutput, StoreDocumentRequest | ||
|
||
|
||
def convert_to_supported_type(value) -> typing.Dict: | ||
type_of_val = type(value) | ||
if type_of_val == list: | ||
new_list = [] | ||
for i in value: | ||
new_list.append(convert_to_supported_type(i)) | ||
Comment on lines
+15
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps, new_list = [convert_to_supported_type(v) for v in value] |
||
# A list needs to be of a consistent type or it will | ||
# not be indexible into a system like Opensearch | ||
return convert_to_homogenous_list(new_list) | ||
elif type_of_val == dict: | ||
result = {} | ||
for k in value: | ||
result[convert_to_supported_type(k)] = convert_to_supported_type(value[k]) | ||
return result | ||
Comment on lines
+22
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More Pythonic would be: result = {}
for k, v in value.items():
result[convert_to_supported_type(k)] = convert_to_supported_type(v)
return result or, even better, return {
convert_to_supported_type(k): convert_to_supported_type(v)
for k, v in value.items()
} |
||
elif type_of_val in (float, int, str, bool): | ||
return value | ||
elif isinstance(type_of_val, type(None)): | ||
return str("") | ||
else: | ||
print("Unknown type", type_of_val, "with val", str(value)) | ||
Comment on lines
+30
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need the |
||
return str(value) | ||
|
||
|
||
def convert_to_homogenous_list(input_list: list): | ||
# To make all types in list homogeneous, we upconvert them | ||
# to the least commom type. | ||
Comment on lines
+35
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Return type hint? Also, replace "commom" with "common" (although, I'm not sure that I would consider |
||
# int -> float -> str | ||
# bool + None -> str | ||
list_type = str() | ||
for j in input_list: | ||
if type(j) is dict: | ||
list_type = dict() | ||
break | ||
Comment on lines
+42
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that
|
||
if type(j) in (str, bool, type(None)): | ||
list_type = str() | ||
break | ||
elif type(j) is float: | ||
list_type = float() | ||
elif type(j) is int and type(list_type) is not float: | ||
list_type = int() | ||
return list(map(type(list_type), input_list)) | ||
|
||
|
||
@plugin.step( | ||
id="opensearch", | ||
name="OpenSearch", | ||
|
@@ -18,26 +61,33 @@ | |
def store( | ||
params: StoreDocumentRequest, | ||
) -> typing.Tuple[str, typing.Union[SuccessOutput, ErrorOutput]]: | ||
|
||
document = convert_to_supported_type(params.data) | ||
|
||
try: | ||
if params.username: | ||
opensearch = OpenSearch( | ||
hosts=params.url, basic_auth=[params.username, params.password] | ||
hosts=params.url, | ||
http_auth=(params.username, params.password), | ||
verify_certs=params.tls_verify, | ||
) | ||
# Support for servers that don't require authentication | ||
else: | ||
opensearch = OpenSearch(hosts=params.url) | ||
resp = opensearch.index(index=params.index, body=params.data) | ||
opensearch = OpenSearch( | ||
hosts=params.url, | ||
verify_certs=params.tls_verify, | ||
) | ||
Comment on lines
67
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can also do this like this: kwargs = {"hosts": params.url, "verify_certs": params.tls_verify}
if params.username:
kwargs["http_auth"] = (params.username, params.password)
opensearch = OpenSearch(**kwargs) |
||
resp = opensearch.index( | ||
index=params.index, | ||
body=document, | ||
) | ||
if resp["result"] != "created": | ||
raise Exception(f"Document status: {resp['_shards']}") | ||
|
||
return "success", SuccessOutput( | ||
f"Successfully uploaded document for index {params.index}" | ||
) | ||
except Exception as ex: | ||
return "error", ErrorOutput( | ||
f"Failed to create OpenSearch document: {ex}" | ||
) | ||
return "error", ErrorOutput(f"Failed to create OpenSearch document: {ex}") | ||
|
||
|
||
if __name__ == "__main__": | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,44 @@ def test_serialization(): | |
) | ||
) | ||
|
||
def test_convert_to_homogeneous_list(self): | ||
test_cases = [ | ||
["a", "b", "c"], # all str | ||
["a", "b", 1], # One final int to convert to str | ||
[1.0, 1, "1"], # str at end, so upconvert all to str | ||
["1", 1, 1.0], | ||
["1", 1, 1], | ||
[1, 1, "1"], | ||
[1, 1, 1], | ||
[1.0, 1, 1], | ||
[1, 1, 1.0], | ||
] | ||
# Ensure they're all homogeneous | ||
for test_case in test_cases: | ||
validate_list_items_homogeous_type( | ||
self, opensearch_plugin.convert_to_homogenous_list(test_case) | ||
) | ||
# Ensure the type matches | ||
self.assertEqual( | ||
int, type(opensearch_plugin.convert_to_homogenous_list([1, 1, 1])[0]) | ||
) | ||
self.assertEqual( | ||
float, | ||
type(opensearch_plugin.convert_to_homogenous_list([1, 1, 1.0])[0]), | ||
) | ||
self.assertEqual( | ||
str, | ||
type(opensearch_plugin.convert_to_homogenous_list([1, 1.0, "1.0"])[0]), | ||
) | ||
|
||
|
||
def validate_list_items_homogeous_type(t, input_list): | ||
if len(input_list) == 0: | ||
return # no problem with an empty list | ||
Comment on lines
+68
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't actually have a test case for a zero-length list -- I recommend either adding one or removing this otherwise useless code. |
||
expected_type = type(input_list[0]) | ||
for item in input_list: | ||
t.assertEqual(type(item), expected_type) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type hints on this signature look wrong: it should accept an
Any
and return anAny
.