wip
This commit is contained in:
parent
2b36992be1
commit
25087d055c
16 changed files with 1394 additions and 816 deletions
|
@ -1,6 +1,6 @@
|
|||
from flask import Blueprint, render_template, redirect, url_for, request, flash, jsonify
|
||||
from flask_login import login_required
|
||||
from app.core.models import Subnet, Server, App
|
||||
from flask_login import login_required, current_user
|
||||
from app.core.models import Subnet, Server, App, Location
|
||||
from app.core.extensions import db
|
||||
from app.scripts.ip_scanner import scan
|
||||
import ipaddress
|
||||
|
@ -36,32 +36,24 @@ def ipam_home():
|
|||
@login_required
|
||||
def subnet_new():
|
||||
"""Create a new subnet"""
|
||||
# Get all locations for the dropdown
|
||||
locations = Location.query.filter_by(user_id=current_user.id).all()
|
||||
|
||||
if request.method == "POST":
|
||||
cidr = request.form.get("cidr")
|
||||
location = request.form.get("location")
|
||||
location_id = request.form.get("location_id")
|
||||
auto_scan = request.form.get("auto_scan") == "on"
|
||||
|
||||
# Basic validation
|
||||
if not cidr or not location:
|
||||
flash("Please fill in all required fields", "danger")
|
||||
return render_template("ipam/subnet_form.html", title="New Subnet")
|
||||
|
||||
# Validate CIDR format
|
||||
try:
|
||||
ipaddress.ip_network(cidr, strict=False)
|
||||
except ValueError:
|
||||
flash("Invalid CIDR format", "danger")
|
||||
return render_template("ipam/subnet_form.html", title="New Subnet")
|
||||
|
||||
# Check if CIDR already exists
|
||||
if Subnet.query.filter_by(cidr=cidr).first():
|
||||
flash("Subnet already exists", "danger")
|
||||
return render_template("ipam/subnet_form.html", title="New Subnet")
|
||||
|
||||
# Create new subnet with JSON string for active_hosts, not a Python list
|
||||
if not cidr or not location_id:
|
||||
flash("CIDR notation and location are required", "danger")
|
||||
return render_template("ipam/subnet_form.html", title="New Subnet", locations=locations)
|
||||
|
||||
# Create new subnet
|
||||
subnet = Subnet(
|
||||
cidr=cidr,
|
||||
location=location,
|
||||
location_id=location_id,
|
||||
user_id=current_user.id,
|
||||
active_hosts=json.dumps([]), # Convert empty list to JSON string
|
||||
last_scanned=None,
|
||||
auto_scan=auto_scan,
|
||||
|
@ -73,7 +65,7 @@ def subnet_new():
|
|||
flash("Subnet created successfully", "success")
|
||||
return redirect(url_for("ipam.subnet_view", subnet_id=subnet.id))
|
||||
|
||||
return render_template("ipam/subnet_form.html", title="New Subnet")
|
||||
return render_template("ipam/subnet_form.html", title="New Subnet", locations=locations)
|
||||
|
||||
|
||||
@bp.route("/subnet/<int:subnet_id>")
|
||||
|
@ -267,43 +259,39 @@ def subnet_create_ajax():
|
|||
return jsonify({"success": False, "error": str(e)})
|
||||
|
||||
|
||||
@bp.route("/location/<location>")
|
||||
@bp.route("/location/<int:location_id>")
|
||||
@login_required
|
||||
def location_overview(location):
|
||||
def location_overview(location_id):
|
||||
"""View all subnets and servers in a specific location"""
|
||||
# Get all subnets in this location
|
||||
subnets = Subnet.query.filter_by(location=location).all()
|
||||
|
||||
# Get servers in these subnets
|
||||
servers = []
|
||||
for subnet in subnets:
|
||||
subnet_servers = Server.query.filter_by(subnet_id=subnet.id).all()
|
||||
servers.extend(subnet_servers)
|
||||
# Get the location (ensure it belongs to the current user)
|
||||
location = Location.query.filter_by(id=location_id, user_id=current_user.id).first_or_404()
|
||||
|
||||
# Create a hierarchical structure
|
||||
hierarchy = {
|
||||
'subnets': [],
|
||||
'standalone_servers': []
|
||||
'locations': {
|
||||
location.name: {
|
||||
'id': location.id,
|
||||
'description': location.description,
|
||||
'subnets': [],
|
||||
'standalone_servers': []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Organize subnets and their servers
|
||||
for subnet in subnets:
|
||||
for subnet in location.subnets:
|
||||
subnet_data = {
|
||||
'id': subnet.id,
|
||||
'cidr': subnet.cidr,
|
||||
'location': subnet.location,
|
||||
'servers': []
|
||||
}
|
||||
|
||||
# Only add description if it exists as an attribute
|
||||
if hasattr(subnet, 'description'):
|
||||
subnet_data['description'] = subnet.description
|
||||
|
||||
for server in subnet.servers:
|
||||
server_data = {
|
||||
'id': server.id,
|
||||
'hostname': server.hostname,
|
||||
'ip_address': server.ip_address,
|
||||
'description': server.description,
|
||||
'apps': []
|
||||
}
|
||||
|
||||
|
@ -311,17 +299,59 @@ def location_overview(location):
|
|||
app_data = {
|
||||
'id': app.id,
|
||||
'name': app.name,
|
||||
'ports': app.ports
|
||||
'url': app.url,
|
||||
'ports': []
|
||||
}
|
||||
|
||||
for port in app.ports:
|
||||
port_data = {
|
||||
'id': port.id,
|
||||
'number': port.port_number,
|
||||
'protocol': port.protocol,
|
||||
'description': port.description
|
||||
}
|
||||
app_data['ports'].append(port_data)
|
||||
|
||||
server_data['apps'].append(app_data)
|
||||
|
||||
subnet_data['servers'].append(server_data)
|
||||
|
||||
hierarchy['subnets'].append(subnet_data)
|
||||
hierarchy['locations'][location.name]['subnets'].append(subnet_data)
|
||||
|
||||
# Add standalone servers
|
||||
for server in location.standalone_servers:
|
||||
server_data = {
|
||||
'id': server.id,
|
||||
'hostname': server.hostname,
|
||||
'ip_address': server.ip_address,
|
||||
'description': server.description,
|
||||
'apps': []
|
||||
}
|
||||
|
||||
for app in server.apps:
|
||||
app_data = {
|
||||
'id': app.id,
|
||||
'name': app.name,
|
||||
'url': app.url,
|
||||
'ports': []
|
||||
}
|
||||
|
||||
for port in app.ports:
|
||||
port_data = {
|
||||
'id': port.id,
|
||||
'number': port.port_number,
|
||||
'protocol': port.protocol,
|
||||
'description': port.description
|
||||
}
|
||||
app_data['ports'].append(port_data)
|
||||
|
||||
server_data['apps'].append(app_data)
|
||||
|
||||
hierarchy['locations'][location.name]['standalone_servers'].append(server_data)
|
||||
|
||||
return render_template(
|
||||
"dashboard/overview.html",
|
||||
title=f"{location} Overview",
|
||||
"ipam/location_overview.html",
|
||||
title=f"{location.name} Overview",
|
||||
hierarchy=hierarchy,
|
||||
location=location
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue