Skip to content

Commit 59b3b17

Browse files
committed
dev: added storageGRID endpoint validation check
1 parent 29d89f0 commit 59b3b17

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

modzy/jobs.py

+44-9
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
"""Classes for interacting with jobs."""
33

44
import logging
5+
from multiprocessing.sharedctypes import Value
56
import time
7+
import boto3
68
from datetime import datetime
79
from types import SimpleNamespace
810
from urllib.parse import urlencode
911
from ._api_object import ApiObject
1012
from ._size import human_read_to_bytes
1113
from ._util import encode_data_uri, depth, file_to_chunks, bytes_to_chunks
1214
from .error import Timeout
13-
from .models import Model
15+
from .models import Model, Models
1416
from deprecation import deprecated
1517

1618

@@ -201,6 +203,34 @@ def __fix_single_source_job(self, sources):
201203
else:
202204
return sources
203205

206+
def check_storagegrid_endpoint(self, endpoint, bucket, access_key_id, secret_access_key):
207+
208+
# establish session with aws sdk
209+
session = boto3.session.Session()
210+
# try to connect to storagegrid endpoint
211+
req_check=False
212+
try:
213+
s3 = session.resource(service_name='s3', endpoint_url=endpoint, verify=False, aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key)
214+
bucket_exists = s3.Bucket(bucket) in s3.buckets.all()
215+
if not bucket_exists:
216+
raise ValueError
217+
req_check=True
218+
new_endpoint=endpoint
219+
except Exception as e:
220+
if not str(endpoint).startswith("https://") and not str(endpoint).startswith("http://"):
221+
new_endpoint = "https://" + str(endpoint)
222+
self.check_storagegrid_endpoint(new_endpoint, bucket, access_key_id, secret_access_key)
223+
req_check = True
224+
elif str(endpoint).startswith("http://"):
225+
new_endpoint = "https://" + endpoint.split("http://")[-1]
226+
self.check_storagegrid_endpoint(new_endpoint, bucket, access_key_id, secret_access_key)
227+
req_check = True
228+
229+
if not req_check:
230+
raise ValueError("Invalid endpoint or bucket name. The endpoint param should point to a valid endpoint associated with your StorageGRID account. Confirm both your endpoint and the bucket name in your input sources dictionary are correct and try again.")
231+
232+
return new_endpoint
233+
204234
def submit_text(self, model, version, sources, explain=False):
205235
"""Submits text data for a multiple source `Job`.
206236
@@ -613,14 +643,19 @@ def submit_storagegrid(self, model, version, sources, access_key_id, secret_acce
613643
identifier = Model._coerce_identifier(model)
614644
version = str(version)
615645
access_key_id = str(access_key_id)
616-
# storageGRID endpoint must begin with "https://", so this conducts a quick test
617-
if not str(endpoint).startswith("https://") and not str(endpoint).startswith("http://"):
618-
endpoint = "https://" + str(endpoint)
619-
elif str(endpoint).startswith("http://"):
620-
endpoint = "https://" + endpoint.split("http://")[-1]
621-
else:
622-
endpoint = str(endpoint)
623-
646+
models = Models(self._api_client)
647+
sample_input_key = list(models.get_version_input_sample(identifier, version)["input"]["sources"].keys())[0]
648+
input_filename = list(models.get_version_input_sample(identifier, version)["input"]["sources"][sample_input_key].keys())[0]
649+
# validate storageGRID endpoint
650+
try:
651+
first_key = list(sources.keys())[0]
652+
if first_key == input_filename:
653+
bucket = sources[first_key]["bucket"]
654+
else:
655+
bucket = sources[first_key][input_filename]["bucket"]
656+
except Exception:
657+
raise ValueError("Invalid input sources file. Confirm your sources dictionary meets the required format (https://docs.modzy.com/docs/clientjobssubmit_netapp_storage_grid) and try again.")
658+
endpoint = self.check_storagegrid_endpoint(endpoint, bucket, access_key_id, secret_access_key)
624659
body = {
625660
"model": {
626661
"identifier": identifier,

requirements_dev.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ grpcio-reflection
2525
protobuf==3.19.4
2626
pyyaml
2727
six
28-
gcloud
28+
gcloud
29+
boto3

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
with open('HISTORY.rst') as history_file:
1212
history = history_file.read()
1313

14-
requirements = ['requests', 'python-dotenv', 'deprecation', 'protobuf==3.19.4', 'grpcio', 'google-api-python-client']
14+
requirements = ['requests', 'python-dotenv', 'deprecation', 'protobuf==3.19.4', 'grpcio', 'google-api-python-client', 'boto3']
1515

1616
# removed in 0.7.1 test_requirements = ['pytest']
1717

@@ -40,6 +40,6 @@
4040
# removed in 0.7.1 test_suite='tests',
4141
# removed in 0.7.1 tests_require=test_requirements,
4242
url='https://github.com/modzy/sdk-python',
43-
version='0.9.0.post',
43+
version='0.9.1',
4444
zip_safe=False,
4545
)

0 commit comments

Comments
 (0)