mirror of
https://ak-git.vectorsigma.ru/terghalin/metalcheck-cli.git
synced 2026-02-21 17:40:31 +09:00
99 lines
3.2 KiB
Python
99 lines
3.2 KiB
Python
import click
|
|
import requests
|
|
|
|
@click.group()
|
|
@click.pass_context
|
|
def metal_nodes(ctx):
|
|
"""Commands for managing Metal Nodes."""
|
|
pass
|
|
|
|
@metal_nodes.command("list")
|
|
@click.pass_context
|
|
def list_command(ctx):
|
|
"""List all metal nodes."""
|
|
base_url = ctx.obj["BASE_URL"]
|
|
list_metal_nodes(base_url)
|
|
|
|
@metal_nodes.command("add")
|
|
@click.pass_context
|
|
def add_command(ctx):
|
|
"""Add a new metal node."""
|
|
base_url = ctx.obj["BASE_URL"]
|
|
add_metal_node(base_url)
|
|
|
|
@metal_nodes.command("delete")
|
|
@click.pass_context
|
|
def delete_command(ctx):
|
|
"""Delete a metal node."""
|
|
base_url = ctx.obj["BASE_URL"]
|
|
delete_metal_node(base_url)
|
|
|
|
def list_metal_nodes(base_url):
|
|
"""List all metal nodes."""
|
|
metal_url = f"{base_url}/metal/data"
|
|
try:
|
|
response = requests.get(metal_url)
|
|
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(base_url):
|
|
"""Add a new metal node."""
|
|
metal_url = f"{base_url}/metal/data"
|
|
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(metal_url, 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(base_url):
|
|
"""Delete a metal node."""
|
|
metal_url = f"{base_url}/metal/data"
|
|
try:
|
|
node_id = click.prompt("Enter the ID of the metal node to delete", type=int)
|
|
response = requests.delete(f"{metal_url}/{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}")
|