update(app): refactor commands
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
import click
|
||||
import requests
|
||||
|
||||
BASE_URL = "http://localhost:8000/export" # Backend URL for exporting data
|
||||
|
||||
@click.command()
|
||||
@click.option(
|
||||
"--format",
|
||||
@@ -11,15 +9,20 @@ BASE_URL = "http://localhost:8000/export" # Backend URL for exporting data
|
||||
help="Specify the export format: yaml or json. Default is json.",
|
||||
)
|
||||
@click.argument("output", required=False, type=click.Path(writable=True))
|
||||
def export_data(format, output):
|
||||
@click.pass_context
|
||||
def export_data(ctx, format, output):
|
||||
"""
|
||||
Export Metal Check data in the specified format (yaml or json).
|
||||
|
||||
If an OUTPUT file is provided, the data will be saved to the file.
|
||||
Otherwise, it will be printed to the console.
|
||||
"""
|
||||
|
||||
base_url = ctx.obj["BASE_URL"]
|
||||
export_url = f"{base_url}/export"
|
||||
|
||||
try:
|
||||
response = requests.get(f"{BASE_URL}?format={format}")
|
||||
response = requests.get(f"{export_url}?format={format}")
|
||||
if response.status_code == 200:
|
||||
data = response.text
|
||||
if output:
|
||||
@@ -33,4 +36,3 @@ def export_data(format, output):
|
||||
click.echo(f"Error: {response.status_code} - {response.text}")
|
||||
except requests.RequestException as e:
|
||||
click.echo(f"Error: {e}")
|
||||
|
||||
|
||||
@@ -1,33 +1,31 @@
|
||||
import click
|
||||
import requests
|
||||
|
||||
BASE_URL = "http://localhost:8000/k8s" # Backend URL for Kubernetes API
|
||||
|
||||
@click.group()
|
||||
def kubernetes_nodes():
|
||||
@click.pass_context
|
||||
def kubernetes_nodes(ctx):
|
||||
"""Commands for managing Kubernetes Nodes."""
|
||||
pass
|
||||
|
||||
@kubernetes_nodes.command("list")
|
||||
def list_command():
|
||||
handle_command("list")
|
||||
|
||||
@kubernetes_nodes.command("think")
|
||||
def think_command():
|
||||
handle_command("think")
|
||||
|
||||
|
||||
def handle_command(action):
|
||||
"""Handle commands related to Kubernetes nodes."""
|
||||
if action == "list":
|
||||
list_kubernetes_nodes()
|
||||
elif action == "analyze":
|
||||
analyze_kubernetes_cluster()
|
||||
|
||||
def list_kubernetes_nodes():
|
||||
@click.pass_context
|
||||
def list_command(ctx):
|
||||
"""List all Kubernetes nodes."""
|
||||
base_url = ctx.obj["BASE_URL"]
|
||||
list_kubernetes_nodes(base_url)
|
||||
|
||||
@kubernetes_nodes.command("analyze")
|
||||
@click.pass_context
|
||||
def analyze_command(ctx):
|
||||
"""Request an AI analysis of the Kubernetes cluster."""
|
||||
base_url = ctx.obj["BASE_URL"]
|
||||
analyze_kubernetes_cluster(base_url)
|
||||
|
||||
def list_kubernetes_nodes(base_url):
|
||||
"""List all Kubernetes nodes."""
|
||||
k8s_url = f"{base_url}/k8s/data"
|
||||
try:
|
||||
response = requests.get(f"{BASE_URL}/data")
|
||||
response = requests.get(k8s_url)
|
||||
if response.status_code == 200:
|
||||
nodes = response.json().get("nodes", [])
|
||||
click.echo("\n📦 Kubernetes Nodes:")
|
||||
@@ -43,10 +41,11 @@ def list_kubernetes_nodes():
|
||||
except requests.RequestException as e:
|
||||
click.echo(f"Error: {e}")
|
||||
|
||||
def analyze_kubernetes_cluster():
|
||||
def analyze_kubernetes_cluster(base_url):
|
||||
"""Request an AI analysis of the Kubernetes cluster."""
|
||||
analyze_url = f"{base_url}/think/k8s"
|
||||
try:
|
||||
response = requests.get(f"{BASE_URL}/think/k8s")
|
||||
response = requests.get(analyze_url)
|
||||
if response.status_code == 200:
|
||||
summary = response.json().get("summary", "No analysis provided.")
|
||||
click.echo("\n🤖 AI Analysis of Kubernetes Cluster:")
|
||||
|
||||
@@ -1,38 +1,38 @@
|
||||
import click
|
||||
import requests
|
||||
|
||||
BASE_URL = "http://localhost:8000/metal" # Backend URL for Metal Nodes API
|
||||
|
||||
@click.group()
|
||||
def metal_nodes():
|
||||
@click.pass_context
|
||||
def metal_nodes(ctx):
|
||||
"""Commands for managing Metal Nodes."""
|
||||
pass
|
||||
|
||||
@metal_nodes.command("list")
|
||||
def list_command():
|
||||
handle_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")
|
||||
def add_command():
|
||||
handle_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")
|
||||
def delete_command():
|
||||
handle_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 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():
|
||||
def list_metal_nodes(base_url):
|
||||
"""List all metal nodes."""
|
||||
metal_url = f"{base_url}/metal/data"
|
||||
try:
|
||||
response = requests.get(f"{BASE_URL}/data")
|
||||
response = requests.get(metal_url)
|
||||
if response.status_code == 200:
|
||||
metal_nodes = response.json().get("metal_nodes", [])
|
||||
click.echo("\n🖥️ Metal Nodes:")
|
||||
@@ -47,8 +47,9 @@ def list_metal_nodes():
|
||||
except requests.RequestException as e:
|
||||
click.echo(f"Error: {e}")
|
||||
|
||||
def add_metal_node():
|
||||
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")
|
||||
@@ -72,7 +73,7 @@ def add_metal_node():
|
||||
}
|
||||
|
||||
# Send the POST request to the backend
|
||||
response = requests.post(f"{BASE_URL}/data", json=data)
|
||||
response = requests.post(metal_url, json=data)
|
||||
|
||||
if response.status_code in [200, 201]:
|
||||
response_data = response.json()
|
||||
@@ -83,11 +84,12 @@ def add_metal_node():
|
||||
except requests.RequestException as e:
|
||||
click.echo(f"Error: {e}")
|
||||
|
||||
def delete_metal_node():
|
||||
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"{BASE_URL}/data/{node_id}")
|
||||
response = requests.delete(f"{metal_url}/{node_id}")
|
||||
if response.status_code == 200:
|
||||
click.echo("✅ Metal node deleted successfully!")
|
||||
else:
|
||||
|
||||
@@ -1,38 +1,38 @@
|
||||
import click
|
||||
import requests
|
||||
|
||||
BASE_URL = "http://localhost:8000/vm" # Backend URL for Virtual Machines API
|
||||
|
||||
@click.group()
|
||||
def virtual_machines():
|
||||
@click.pass_context
|
||||
def virtual_machines(ctx):
|
||||
"""Commands for managing Virtual Machines."""
|
||||
pass
|
||||
|
||||
@virtual_machines.command("list")
|
||||
def list_command():
|
||||
handle_command("list")
|
||||
@click.pass_context
|
||||
def list_command(ctx):
|
||||
"""List all virtual machines."""
|
||||
base_url = ctx.obj["BASE_URL"]
|
||||
list_virtual_machines(base_url)
|
||||
|
||||
@virtual_machines.command("add")
|
||||
def add_command():
|
||||
handle_command("add")
|
||||
@click.pass_context
|
||||
def add_command(ctx):
|
||||
"""Add a new virtual machine."""
|
||||
base_url = ctx.obj["BASE_URL"]
|
||||
add_virtual_machine(base_url)
|
||||
|
||||
@virtual_machines.command("delete")
|
||||
def delete_command():
|
||||
handle_command("delete")
|
||||
@click.pass_context
|
||||
def delete_command(ctx):
|
||||
"""Delete a virtual machine."""
|
||||
base_url = ctx.obj["BASE_URL"]
|
||||
delete_virtual_machine(base_url)
|
||||
|
||||
def handle_command(action):
|
||||
"""Handle commands related to Virtual Machines."""
|
||||
if action == "list":
|
||||
list_virtual_machines()
|
||||
elif action == "add":
|
||||
add_virtual_machine()
|
||||
elif action == "delete":
|
||||
delete_virtual_machine()
|
||||
|
||||
def list_virtual_machines():
|
||||
def list_virtual_machines(base_url):
|
||||
"""List all virtual machines."""
|
||||
vm_url = f"{base_url}/vm/data"
|
||||
try:
|
||||
response = requests.get(f"{BASE_URL}/data")
|
||||
response = requests.get(vm_url)
|
||||
if response.status_code == 200:
|
||||
virtual_machines = response.json().get("virtual_machines", [])
|
||||
click.echo("\n💻 Virtual Machines:")
|
||||
@@ -47,8 +47,9 @@ def list_virtual_machines():
|
||||
except requests.RequestException as e:
|
||||
click.echo(f"Error: {e}")
|
||||
|
||||
def add_virtual_machine():
|
||||
def add_virtual_machine(base_url):
|
||||
"""Add a new virtual machine."""
|
||||
vm_url = f"{base_url}/vm/data"
|
||||
try:
|
||||
name = click.prompt("Name")
|
||||
location = click.prompt("Location")
|
||||
@@ -66,7 +67,7 @@ def add_virtual_machine():
|
||||
"type": vm_type,
|
||||
}
|
||||
|
||||
response = requests.post(f"{BASE_URL}/data", json=data)
|
||||
response = requests.post(vm_url, json=data)
|
||||
if response.status_code in [200, 201]:
|
||||
response_data = response.json()
|
||||
message = response_data.get("message", "Virtual machine added successfully!")
|
||||
@@ -76,11 +77,12 @@ def add_virtual_machine():
|
||||
except requests.RequestException as e:
|
||||
click.echo(f"Error: {e}")
|
||||
|
||||
def delete_virtual_machine():
|
||||
def delete_virtual_machine(base_url):
|
||||
"""Delete a virtual machine."""
|
||||
vm_url = f"{base_url}/vm/data"
|
||||
try:
|
||||
vm_id = click.prompt("Enter the ID of the virtual machine to delete", type=int)
|
||||
response = requests.delete(f"{BASE_URL}/data/{vm_id}")
|
||||
response = requests.delete(f"{vm_url}/{vm_id}")
|
||||
if response.status_code == 200:
|
||||
click.echo("✅ Virtual machine deleted successfully!")
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user