From 6ab32a164d8d4c52c65f4bec824782ff9e6e0ee4 Mon Sep 17 00:00:00 2001 From: Saket Agrahari <saket.agrahari@geant.org> Date: Mon, 12 May 2025 12:40:50 +0100 Subject: [PATCH 1/3] update to add output to file for import_edge_port --- gso/cli/imports.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/gso/cli/imports.py b/gso/cli/imports.py index 66c864594..e46eef8aa 100644 --- a/gso/cli/imports.py +++ b/gso/cli/imports.py @@ -10,10 +10,6 @@ from typing import Self, TypeVar import typer import yaml -from orchestrator.db import db -from orchestrator.services.processes import start_process -from orchestrator.services.products import get_product_by_id -from orchestrator.types import SubscriptionLifecycle from pydantic import BaseModel, NonNegativeInt, ValidationError, field_validator, model_validator from pydantic_forms.types import UUIDstr from sqlalchemy.exc import SQLAlchemyError @@ -55,6 +51,10 @@ from gso.utils.types.ip_address import ( ) from gso.utils.types.virtual_identifiers import VC_ID, VLAN_ID from gso.workflows.l3_core_service.shared import L3_CREAT_IMPORTED_WF_MAP, L3_IMPORT_WF_MAP, L3ProductNameType +from orchestrator.db import db +from orchestrator.services.processes import start_process +from orchestrator.services.products import get_product_by_id +from orchestrator.types import SubscriptionLifecycle app: typer.Typer = typer.Typer() IMPORT_WAIT_MESSAGE = "Waiting for the dust to settle before importing new products..." @@ -540,18 +540,32 @@ def import_opengear(filepath: str = common_filepath_option) -> None: @app.command() -def import_edge_port(filepath: str = common_filepath_option) -> None: +def import_edge_port(filepath: str, output_dir: str | None = None) -> None: """Import Edge Port into GSO.""" successfully_imported_data = [] data = _read_data(Path(filepath)) + # Save successfully imported data to a file if output_dir is provided + if output_dir: + output_file = Path(output_dir) / "successful_imported_ep.json" + output_file.parent.mkdir(parents=True, exist_ok=True) # Ensure the directory exists + + # Load existing data if the file exists + if output_file.exists(): + with output_file.open("r", encoding="utf-8") as f: + existing_data = json.load(f) + else: + existing_data = {} + for edge_port in data: typer.echo(f"Importing Edge Port {edge_port["name"]} on {edge_port["node"]}. ") try: + key = f"{edge_port["name"]}@{edge_port["node"]}" edge_port["node"] = _get_router_subscription_id(edge_port["node"]) initial_data = EdgePortImportModel(**edge_port) start_process("create_imported_edge_port", [initial_data.model_dump()]) successfully_imported_data.append(edge_port["name"]) typer.echo(f"Successfully imported Edge Port {edge_port["name"]} on {edge_port["node"]}.") + existing_data[key] = edge_port["node"] except ValidationError as e: typer.echo(f"Validation error: {e}") @@ -570,6 +584,11 @@ def import_edge_port(filepath: str = common_filepath_option) -> None: for item in successfully_imported_data: typer.echo(f"- {item}") + # Write the updated map back to the file + with output_file.open("w", encoding="utf-8") as f: + json.dump(existing_data, f, indent=4) + typer.echo(f"Successfully imported edge ports saved to {output_file}") + @app.command() def import_iptrunks(filepath: str = common_filepath_option) -> None: -- GitLab From 3334e2aa883e5bd4bb16f1cf51e990db1b27a35e Mon Sep 17 00:00:00 2001 From: Saket Agrahari <saket.agrahari@geant.org> Date: Mon, 12 May 2025 11:45:49 +0000 Subject: [PATCH 2/3] tox changes --- gso/cli/imports.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gso/cli/imports.py b/gso/cli/imports.py index e46eef8aa..37888fd2a 100644 --- a/gso/cli/imports.py +++ b/gso/cli/imports.py @@ -10,6 +10,10 @@ from typing import Self, TypeVar import typer import yaml +from orchestrator.db import db +from orchestrator.services.processes import start_process +from orchestrator.services.products import get_product_by_id +from orchestrator.types import SubscriptionLifecycle from pydantic import BaseModel, NonNegativeInt, ValidationError, field_validator, model_validator from pydantic_forms.types import UUIDstr from sqlalchemy.exc import SQLAlchemyError @@ -51,10 +55,6 @@ from gso.utils.types.ip_address import ( ) from gso.utils.types.virtual_identifiers import VC_ID, VLAN_ID from gso.workflows.l3_core_service.shared import L3_CREAT_IMPORTED_WF_MAP, L3_IMPORT_WF_MAP, L3ProductNameType -from orchestrator.db import db -from orchestrator.services.processes import start_process -from orchestrator.services.products import get_product_by_id -from orchestrator.types import SubscriptionLifecycle app: typer.Typer = typer.Typer() IMPORT_WAIT_MESSAGE = "Waiting for the dust to settle before importing new products..." -- GitLab From 60c89a077bf7717a52a548ae38e77f9986e25507 Mon Sep 17 00:00:00 2001 From: Saket Agrahari <saket.agrahari@geant.org> Date: Mon, 12 May 2025 14:26:15 +0000 Subject: [PATCH 3/3] fixing error --- gso/cli/imports.py | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/gso/cli/imports.py b/gso/cli/imports.py index 37888fd2a..84370b569 100644 --- a/gso/cli/imports.py +++ b/gso/cli/imports.py @@ -540,24 +540,17 @@ def import_opengear(filepath: str = common_filepath_option) -> None: @app.command() -def import_edge_port(filepath: str, output_dir: str | None = None) -> None: +def import_edge_port( + filepath: str = typer.Option(default="data.json", help="Path to the file"), + output_dir: str | None = None, +) -> None: """Import Edge Port into GSO.""" successfully_imported_data = [] data = _read_data(Path(filepath)) - # Save successfully imported data to a file if output_dir is provided - if output_dir: - output_file = Path(output_dir) / "successful_imported_ep.json" - output_file.parent.mkdir(parents=True, exist_ok=True) # Ensure the directory exists - - # Load existing data if the file exists - if output_file.exists(): - with output_file.open("r", encoding="utf-8") as f: - existing_data = json.load(f) - else: - existing_data = {} + ep_map = {} for edge_port in data: - typer.echo(f"Importing Edge Port {edge_port["name"]} on {edge_port["node"]}. ") + typer.echo(f"Importing Edge Port {edge_port["name"]} on {edge_port["node"]}.") try: key = f"{edge_port["name"]}@{edge_port["node"]}" edge_port["node"] = _get_router_subscription_id(edge_port["node"]) @@ -565,7 +558,7 @@ def import_edge_port(filepath: str, output_dir: str | None = None) -> None: start_process("create_imported_edge_port", [initial_data.model_dump()]) successfully_imported_data.append(edge_port["name"]) typer.echo(f"Successfully imported Edge Port {edge_port["name"]} on {edge_port["node"]}.") - existing_data[key] = edge_port["node"] + ep_map[key] = edge_port["node"] except ValidationError as e: typer.echo(f"Validation error: {e}") @@ -584,9 +577,22 @@ def import_edge_port(filepath: str, output_dir: str | None = None) -> None: for item in successfully_imported_data: typer.echo(f"- {item}") + if output_dir: + output_file = Path(output_dir) / "successful_imported_ep.json" + output_file.parent.mkdir(parents=True, exist_ok=True) # Ensure the directory exists + + # Load existing data if the file exists + existing_data = {} + if output_file.exists(): + with output_file.open("r", encoding="utf-8") as f: + existing_data = json.load(f) + + # Merge existing data with ep_map + ep_map = {**existing_data, **ep_map} + # Write the updated map back to the file with output_file.open("w", encoding="utf-8") as f: - json.dump(existing_data, f, indent=4) + json.dump(ep_map, f, indent=4) typer.echo(f"Successfully imported edge ports saved to {output_file}") -- GitLab