Skip to content

Commit

Permalink
fixes #51: change to pipeline insertion script
Browse files Browse the repository at this point in the history
fixes #51: Move insert_new_version_pipeline to pipelines_version_insertion.py
  • Loading branch information
Maxence Guindon committed Apr 8, 2024
1 parent 1289291 commit 65b2268
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 40 deletions.
2 changes: 2 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@
"endpoints": None,
"pipelines": {},
}

app = Quart(__name__)
app = cors(app, allow_origin="*", allow_methods=["GET", "POST", "OPTIONS"])
app.config["MAX_CONTENT_LENGTH"] = 200 * 1024 * 1024 # 200MB


@app.post("/del")
Expand Down
37 changes: 0 additions & 37 deletions azure_storage/azure_storage_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,40 +276,3 @@ async def get_pipeline_info(

except (ValueError, GetBlobError, PipelineNotFoundError) as error:
raise PipelineNotFoundError(f"This version {pipeline_version} was not found") from error


def insert_new_version_pipeline(
pipelines_json: dict,
connection_string: str,
pipeline_container_name: str
) -> bool:
"""
Inserts a new version of a pipeline JSON into an Azure Blob Storage
container.
Args:
pipelines_json (dict): The JSON data of the pipeline. connection_string
(str): The connection string for the Azure Blob Storage account.
pipeline_container_name (str): The name of the container where the
pipeline JSON will be uploaded.
Returns:
bool: True if the pipeline JSON was successfully uploaded, False
otherwise.
"""
try:
blob_service_client = BlobServiceClient.from_connection_string(
connection_string
)

container_client = blob_service_client.get_container_client(
pipeline_container_name
)

json_name = "{}/{}.json".format("pipelines", pipelines_json.get("version"))
container_client.upload_blob(
json_name, json.dumps(pipelines_json, indent=4), overwrite=True)
return "The pipeline was successfully uploaded to the blob storage"

except ValueError as error:
raise ConnectionStringError(error.args[0]) from error
51 changes: 48 additions & 3 deletions pipelines_version_insertion.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import json

import azure_storage.azure_storage_api as azure_storage_api
from azure.storage.blob import BlobServiceClient
from azure.core.exceptions import ResourceExistsError

from sys import argv
from cryptography.fernet import Fernet
Expand All @@ -19,11 +20,48 @@ class PipelineInsertionError(Exception):
pass


def insert_new_version_pipeline(
pipelines_json: dict,
connection_string: str,
pipeline_container_name: str
) -> bool:
"""
Inserts a new version of a pipeline JSON into an Azure Blob Storage
container.
Args:
pipelines_json (dict): The JSON data of the pipeline. connection_string
(str): The connection string for the Azure Blob Storage account.
pipeline_container_name (str): The name of the container where the
pipeline JSON will be uploaded.
Returns:
bool: True if the pipeline JSON was successfully uploaded, False
otherwise.
"""
try:
blob_service_client = BlobServiceClient.from_connection_string(
connection_string
)

container_client = blob_service_client.get_container_client(
pipeline_container_name
)

json_name = "{}/{}.json".format("pipelines", pipelines_json.get("version"))
container_client.upload_blob(
json_name, json.dumps(pipelines_json, indent=4), overwrite=False)
return "The pipeline was successfully uploaded to the blob storage"

except (ValueError, ResourceExistsError) as error:
raise ConnectionStringError(error.args[0]) from error


def pipeline_insertion(json_path:str):

if not os.path.exists(json_path):
raise PipelineInsertionError(
"The file does not exist, please check the file path")
f"The file does not exist, please check the file path\n provided path{json_path}")

if json_path.split(".")[-1] != "json":
raise PipelineInsertionError(
Expand All @@ -37,6 +75,12 @@ def pipeline_insertion(json_path:str):

pipelines_json = json.loads(pipelines_json)

if not isinstance(pipelines_json, dict):
raise PipelineInsertionError(
f"The file must contain a dictionary with the following keys: version, date, pipelines, models \
\n instead provided a {type(pipelines_json)}")


for model in pipelines_json["models"]:
# crypting endopoint
endpoint = model["endpoint"].encode()
Expand All @@ -45,7 +89,7 @@ def pipeline_insertion(json_path:str):
api_key = model["api_key"].encode()
model["api_key"] = cipher_suite.encrypt(api_key).decode()

return azure_storage_api.insert_new_version_pipeline(
return insert_new_version_pipeline(
pipelines_json, CONNECTION_STRING, BLOB_STORAGE_ACCOUNT_NAME)

except (ConnectionStringError) as error:
Expand All @@ -59,6 +103,7 @@ def main():
print(pipeline_insertion(json_path))
except (IndexError, PipelineInsertionError) as error:
if isinstance(error, IndexError):
# Add the given path
print("Please provide the path to the json file as an argument")

if isinstance(error, PipelineInsertionError):
Expand Down

0 comments on commit 65b2268

Please sign in to comment.