Skip to content
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

feat(api): validate minio connection #86

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion docs/src/installing/ametnes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ data-center, in AWS, GCP or Azure.

Nesis is available on the Ametnes Platform and can be deployed in your kubernetes cluster wherever you host it.

The first step is to setup your kubernetes cluster as an Ametnes Application Location. See these detailed <a href="https://cloud.ametnes.com/docs/concepts/data-service-location/" target="_blank">instructions</a>.
The first step is to set up your kubernetes cluster as an Ametnes Application Location. See these detailed <a href="https://cloud.ametnes.com/docs/concepts/data-service-location/" target="_blank">instructions</a>.

## Create the service

Expand Down
17 changes: 17 additions & 0 deletions nesis/api/core/document_loaders/minio.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,23 @@ def validate_connection_info(connection: Dict[str, Any]) -> Dict[str, Any]:
assert not isblank(
connection.get("dataobjects")
), "One or more buckets must be supplied"

endpoint = connection.get("endpoint")
dataobjects = connection.get("dataobjects")

endpoint_parts = endpoint.split("://")
client = Minio(
endpoint=endpoint_parts[1].split("/")[0],
access_key=connection.get("user"),
secret_key=connection.get("password"),
secure=endpoint_parts[0] == "https",
)

try:
list(client.list_objects(dataobjects.split(",")[0], recursive=True))
except:
raise ValueError(f"Failed to connect to minio instance {endpoint}")

return {
key: val
for key, val in connection.items()
Expand Down
32 changes: 32 additions & 0 deletions nesis/api/tests/core/document_loaders/test_minio.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,35 @@ def test_fetch_documents(
"self_link": "https://s3.endpoint/buckets/SomeName",
},
)


@mock.patch("nesis.api.core.document_loaders.minio.Minio")
def test_validate_connection_info(
minio_instance: mock.MagicMock, cache: mock.MagicMock, session: Session
) -> None:

connection = {
"endpoint": "https://s3.endpoint",
"user": "",
"password": "",
"dataobjects": "bucketname",
}

# Test missing endpoint
with pytest.raises(AssertionError) as ex_info:
minio.validate_connection_info(connection={})
assert "An endpoint must be supplied" in str(ex_info)

# Test missing bucket
with pytest.raises(AssertionError) as ex_info:
minio.validate_connection_info(connection={"endpoint": "some.endpoint"})
assert "One or more buckets must be supplied" in str(ex_info)

# Test connection to minio
minio_client = mock.MagicMock()
minio_instance.return_value = minio_client
minio_client.list_objects.side_effect = Exception("Connection failed")

with pytest.raises(ValueError) as ex_info:
minio.validate_connection_info(connection=connection)
assert "Failed to connect to minio instance" in str(ex_info)
Loading