Skip to content
Snippets Groups Projects

Draft: update to add output to file for import_edge_port

Open Saket Agrahari requested to merge NAT-1154-import-edge-port-update into develop
1 file
+ 27
2
Compare changes
  • Side-by-side
  • Inline
+ 27
2
@@ -540,18 +540,25 @@ def import_opengear(filepath: str = common_filepath_option) -> None:
@@ -540,18 +540,25 @@ def import_opengear(filepath: str = common_filepath_option) -> None:
@app.command()
@app.command()
def import_edge_port(filepath: str = common_filepath_option) -> 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."""
"""Import Edge Port into GSO."""
successfully_imported_data = []
successfully_imported_data = []
data = _read_data(Path(filepath))
data = _read_data(Path(filepath))
 
ep_map = {}
 
for edge_port in data:
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:
try:
 
key = f"{edge_port["name"]}@{edge_port["node"]}"
edge_port["node"] = _get_router_subscription_id(edge_port["node"])
edge_port["node"] = _get_router_subscription_id(edge_port["node"])
initial_data = EdgePortImportModel(**edge_port)
initial_data = EdgePortImportModel(**edge_port)
start_process("create_imported_edge_port", [initial_data.model_dump()])
start_process("create_imported_edge_port", [initial_data.model_dump()])
successfully_imported_data.append(edge_port["name"])
successfully_imported_data.append(edge_port["name"])
typer.echo(f"Successfully imported Edge Port {edge_port["name"]} on {edge_port["node"]}.")
typer.echo(f"Successfully imported Edge Port {edge_port["name"]} on {edge_port["node"]}.")
 
ep_map[key] = edge_port["node"]
except ValidationError as e:
except ValidationError as e:
typer.echo(f"Validation error: {e}")
typer.echo(f"Validation error: {e}")
@@ -570,6 +577,24 @@ def import_edge_port(filepath: str = common_filepath_option) -> None:
@@ -570,6 +577,24 @@ def import_edge_port(filepath: str = common_filepath_option) -> None:
for item in successfully_imported_data:
for item in successfully_imported_data:
typer.echo(f"- {item}")
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(ep_map, f, indent=4)
 
typer.echo(f"Successfully imported edge ports saved to {output_file}")
 
@app.command()
@app.command()
def import_iptrunks(filepath: str = common_filepath_option) -> None:
def import_iptrunks(filepath: str = common_filepath_option) -> None:
Loading