add(app): create python application
This commit is contained in:
96
cli/commands/metal.py
Normal file
96
cli/commands/metal.py
Normal file
@@ -0,0 +1,96 @@
|
||||
import click
|
||||
import requests
|
||||
|
||||
BASE_URL = "http://localhost:8000/metal" # Backend URL for Metal Nodes API
|
||||
|
||||
@click.group()
|
||||
def metal_nodes():
|
||||
"""Commands for managing Metal Nodes."""
|
||||
pass
|
||||
|
||||
@metal_nodes.command("list")
|
||||
def list_command():
|
||||
handle_command("list")
|
||||
|
||||
@metal_nodes.command("add")
|
||||
def add_command():
|
||||
handle_command("add")
|
||||
|
||||
@metal_nodes.command("delete")
|
||||
def delete_command():
|
||||
handle_command("delete")
|
||||
|
||||
def handle_command(action):
|
||||
"""Handle commands related to Metal Nodes."""
|
||||
if action == "list":
|
||||
list_metal_nodes()
|
||||
elif action == "add":
|
||||
add_metal_node()
|
||||
elif action == "delete":
|
||||
delete_metal_node()
|
||||
|
||||
def list_metal_nodes():
|
||||
"""List all metal nodes."""
|
||||
try:
|
||||
response = requests.get(f"{BASE_URL}/data")
|
||||
if response.status_code == 200:
|
||||
metal_nodes = response.json().get("metal_nodes", [])
|
||||
click.echo("\n🖥️ Metal Nodes:")
|
||||
for node in metal_nodes:
|
||||
click.echo(
|
||||
f"ID: {node[0]}, Name: {node[1]}, Location: {node[2]}, "
|
||||
f"Vendor: {node[3]}, CPU: {node[4]}, Memory: {node[5]}, "
|
||||
f"Storage: {node[6]}, Time on Duty: {node[7]}"
|
||||
)
|
||||
else:
|
||||
click.echo(f"Error: {response.status_code} - {response.text}")
|
||||
except requests.RequestException as e:
|
||||
click.echo(f"Error: {e}")
|
||||
|
||||
def add_metal_node():
|
||||
"""Add a new metal node."""
|
||||
try:
|
||||
# Gather inputs from the prompt
|
||||
name = click.prompt("Name")
|
||||
location = click.prompt("Location")
|
||||
vendor = click.prompt("Vendor")
|
||||
cpu = click.prompt("CPU (cores)", type=int)
|
||||
memory = click.prompt("Memory (GB)")
|
||||
storage = click.prompt("Storage (e.g., 1TB SSD)")
|
||||
time_on_duty = click.prompt("Time on Duty (hours)", type=int)
|
||||
initial_cost = click.prompt("Initial Cost ($)", type=float)
|
||||
|
||||
data = {
|
||||
"name": name,
|
||||
"location": location,
|
||||
"vendor": vendor,
|
||||
"cpu": cpu,
|
||||
"memory": memory,
|
||||
"storage": storage,
|
||||
"time_on_duty": time_on_duty,
|
||||
"initial_cost": initial_cost,
|
||||
}
|
||||
|
||||
# Send the POST request to the backend
|
||||
response = requests.post(f"{BASE_URL}/data", json=data)
|
||||
|
||||
if response.status_code in [200, 201]:
|
||||
response_data = response.json()
|
||||
message = response_data.get("message", "Metal node added successfully!")
|
||||
click.echo(f"✅ {message}")
|
||||
else:
|
||||
click.echo(f"Error: {response.status_code} - {response.text}")
|
||||
except requests.RequestException as e:
|
||||
click.echo(f"Error: {e}")
|
||||
|
||||
def delete_metal_node():
|
||||
"""Delete a metal node."""
|
||||
try:
|
||||
node_id = click.prompt("Enter the ID of the metal node to delete", type=int)
|
||||
response = requests.delete(f"{BASE_URL}/data/{node_id}")
|
||||
if response.status_code == 200:
|
||||
click.echo("✅ Metal node deleted successfully!")
|
||||
else:
|
||||
click.echo(f"Error: {response.status_code} - {response.text}")
|
||||
except requests.RequestException as e:
|
||||
click.echo(f"Error: {e}")
|
||||
Reference in New Issue
Block a user