Commit 73689aab authored by Eric Dieckman's avatar Eric Dieckman
Browse files

Add firewall, vsys, firewall VLN controllers

parent ca4e674b
Loading
Loading
Loading
Loading
+41 −0
Original line number Diff line number Diff line
using Cals.Visor.Infrastructure.Contexts;
using Cals.Visor.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Cals.Visor.Api.Controllers;

[Route("palo_alto/firewalls/vlans")]
[ApiController]
public class PaloAltoFirewallVlansController : ControllerBase
{
    private readonly ApplicationDbContext _context;

    public PaloAltoFirewallVlansController(ApplicationDbContext context) => _context = context;

    // GET: /palo_alto/firewalls/vlans
    [ProducesResponseType<IEnumerable<PaloAltoFirewallVlan>>(StatusCodes.Status200OK)]
    [HttpGet(Name = "GetAllPaloAltoFirewallVlans")]
    public async Task<IActionResult> GetAllPaloAltoFirewallVlansAsync()
    {
        List<PaloAltoFirewallVlan> firewallVlans = await _context.PaloAltoFirewallVlans.ToListAsync();

        return Ok(firewallVlans);
    }

    // GET: /palo_alto/firewalls/vlans/1
    [ProducesResponseType<PaloAltoFirewallVlan>(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [HttpGet("{firewallVlanId:long}", Name = "GetPaloAltoFirewallVlanById")]
    public async Task<IActionResult> GetPaloAltoFirewallVlanByIdAsync(long firewallVlanId)
    {
        PaloAltoFirewallVlan? result = await _context.PaloAltoFirewallVlans
            .Where(e => e.FirewallVlanId == firewallVlanId)
            .FirstOrDefaultAsync();

        if (result == null)
            return NotFound();

        return Ok(result);
    }
}
+100 −0
Original line number Diff line number Diff line
using Cals.Visor.Infrastructure.Contexts;
using Cals.Visor.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Cals.Visor.Api.Controllers;

[Route("palo_alto/firewalls/vsyses")]
[ApiController]
public class PaloAltoFirewallVsysesController : ControllerBase
{
    private readonly ApplicationDbContext _context;

    public PaloAltoFirewallVsysesController(ApplicationDbContext context) => _context = context;

    // GET: /palo_alto/firewalls/vsyses
    [ProducesResponseType<IEnumerable<PaloAltoVsys>>(StatusCodes.Status200OK)]
    [HttpGet(Name = "GetAllPaloAltoFirewallVsyses")]
    public async Task<IActionResult> GetAllPaloAltoVsysesAsync()
    {
        List<PaloAltoVsys> vsyses = await _context.PaloAltoVsyses.ToListAsync();

        return Ok(vsyses);
    }

    // GET: /palo_alto/firewalls/vsyses/1
    [ProducesResponseType<PaloAltoVsys>(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [HttpGet("{vsysId:long}", Name = "GetPaloAltoFirewallVsysById")]
    public async Task<IActionResult> GetPaloAltoFirewallVsysByIdAsync(long vsysId)
    {
        PaloAltoVsys? result = await _context.PaloAltoVsyses
            .Where(e => e.VsysId == vsysId)
            .FirstOrDefaultAsync();

        if (result == null)
            return NotFound();

        return Ok(result);
    }

    // POST: /palo_alto/firewalls/1/vsyses
    [ProducesResponseType<PaloAltoVsys>(StatusCodes.Status201Created)]
    [HttpPost("~/palo_alto/firewalls/{firewallId:long}/vsyses", Name = "CreateNewPaloAltoVsys")]
    public async Task<IActionResult> CreateNewAsync(long firewallId, PaloAltoVsys data)
    {
        data.FirewallId = firewallId;
        _context.Add(data);
        await _context.SaveChangesAsync();

        return CreatedAtRoute("GetPaloAltoFirewallVsysById", new { vsysId = data.VsysId }, data);
    }

    // PUT: /palo_alto/firewalls/1/vsyses/2
    [ProducesResponseType<PaloAltoVsys>(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [HttpPut("~/palo_alto/firewalls/{firewallId:long}/vsyses/{vsysId:long}", Name = "UpdatePaloAltoVsys")]
    public async Task<IActionResult> UpdateAsync(long vsysId, long firewallId, PaloAltoVsys data)
    {
        PaloAltoVsys? entity = await _context.PaloAltoVsyses
            .Where(e => e.VsysId == vsysId)
            .FirstOrDefaultAsync();

        if (entity == null)
            return NotFound();

        entity.IsActive = data.IsActive;
        entity.Name = data.Name;
        entity.Number = data.Number;
        entity.FirewallId = firewallId;

        await _context.SaveChangesAsync();

        return Ok(entity);
    }

    // POST: /palo_alto/firewalls/2/vsyses/2/vlans
    [ProducesResponseType<PaloAltoFirewallVlan>(StatusCodes.Status201Created)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [HttpPost("{firewallId:long}/vsyses/{vsysId:long}/vlans", Name = "AssignVlanToPaloAltoVsys")]
    public async Task<IActionResult> AssignVlanToFirewallAsync(long firewallId, long vsysId, PaloAltoFirewallVlan vlan)
    {
        if (!await _context.PaloAltoFirewalls.AnyAsync(l => l.FirewallId == firewallId))
            return NotFound();

        if (!await _context.PaloAltoVsyses.AnyAsync(v => v.VsysId == vsysId))
            return NotFound();

        if (!await _context.Vlans.AnyAsync(v => v.VlanId == vlan.VlanId))
            return BadRequest();

        vlan.VsysId = vsysId;
        vlan.FirewallId = firewallId;

        _context.PaloAltoFirewallVlans.Add(vlan);
        await _context.SaveChangesAsync();

        return CreatedAtRoute("GetPaloAltoFirewallVlanById", new { firewallVlanId = vlan.FirewallVlanId }, vlan);
    }
}
+27 −5
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ using Microsoft.EntityFrameworkCore;

namespace Cals.Visor.Api.Controllers;

[Route("[controller]")]
[Route("palo_alto/firewalls")]
[ApiController]
public class PaloAltoFirewallsController : ControllerBase
{
@@ -13,7 +13,7 @@ public class PaloAltoFirewallsController : ControllerBase

    public PaloAltoFirewallsController(ApplicationDbContext context) => _context = context;

    // GET: /paloaltofirewalls
    // GET: /palo_alto/firewalls
    [ProducesResponseType<IList<PaloAltoFirewall>>(StatusCodes.Status200OK)]
    [HttpGet(Name = "GetAllPaloAltoFirewalls")]
    public async Task<IActionResult> GetAllAsync()
@@ -25,7 +25,7 @@ public class PaloAltoFirewallsController : ControllerBase
        return Ok(result);
    }

    // GET: /paloaltofirewalls/1
    // GET: /palo_alto/firewalls/1
    [ProducesResponseType<PaloAltoFirewall>(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [HttpGet("{firewallId:long}", Name = "GetPaloAltoFirewallById")]
@@ -41,7 +41,7 @@ public class PaloAltoFirewallsController : ControllerBase
        return Ok(result);
    }

    // POST: /paloaltofirewalls
    // POST: /palo_alto/firewalls
    [ProducesResponseType<PaloAltoFirewall>(StatusCodes.Status201Created)]
    [HttpPost(Name = "CreateNewPaloAltoFirewall")]
    public async Task<IActionResult> CreateNewAsync(PaloAltoFirewall data)
@@ -52,7 +52,7 @@ public class PaloAltoFirewallsController : ControllerBase
        return CreatedAtRoute("GetPaloAltoFirewallById", new { firewallId = data.FirewallId }, data);
    }

    // PUT: /paloaltofirewalls/2
    // PUT: /palo_alto/firewalls/2
    [ProducesResponseType<PaloAltoFirewall>(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [HttpPut("{firewallId:long}", Name = "UpdatePaloAltoFirewall")]
@@ -74,4 +74,26 @@ public class PaloAltoFirewallsController : ControllerBase

        return Ok(entity);
    }

    // POST: /palo_alto/firewalls/2/vlans
    [ProducesResponseType<PaloAltoFirewallVlan>(StatusCodes.Status201Created)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [HttpPost("{firewallId:long}/vlans", Name = "AssignVlanToPaloAltoFirewall")]
    public async Task<IActionResult> AssignVlanToFirewallAsync(long firewallId, PaloAltoFirewallVlan vlan)
    {
        if (!await _context.PaloAltoFirewalls.AnyAsync(l => l.FirewallId == firewallId))
            return NotFound();

        if (!await _context.Vlans.AnyAsync(v => v.VlanId == vlan.VlanId))
            return BadRequest();

        // this is for non-vsys VLANs
        vlan.VsysId = null;
        vlan.FirewallId = firewallId;

        _context.PaloAltoFirewallVlans.Add(vlan);
        await _context.SaveChangesAsync();

        return CreatedAtRoute("GetPaloAltoFirewallVlanById", new { firewallVlanId = vlan.FirewallVlanId }, vlan);
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -31,10 +31,11 @@ public class ApplicationDbContext : DbContext
    public DbSet<LocationVlan> LocationVlans => Set<LocationVlan>();
    public DbSet<PaloAltoAccessDomain> PaloAltoAccessDomains => Set<PaloAltoAccessDomain>();
    public DbSet<PaloAltoFirewall> PaloAltoFirewalls => Set<PaloAltoFirewall>();
    public DbSet<PaloAltoFirewallVlan> PaloAltoFirewallVlans => Set<PaloAltoFirewallVlan>();
    public DbSet<PaloAltoVsys> PaloAltoVsyses => Set<PaloAltoVsys>();
    public DbSet<Person> People => Set<Person>();
    public DbSet<Vlan> Vlans => Set<Vlan>();
    public DbSet<VlanUseType> VlanUseTypes => Set<VlanUseType>();
    public DbSet<PaloAltoVsys> PaloAltoVsyses => Set<PaloAltoVsys>();
    public DbSet<Subnet> Subnets => Set<Subnet>();
    public DbSet<WiscNicGroup> WiscNicGroups => Set<WiscNicGroup>();
    public DbSet<WiscNicGroupMembershipType> WiscNicGroupMembershipTypes => Set<WiscNicGroupMembershipType>();
+0 −6
Original line number Diff line number Diff line
@@ -25,9 +25,6 @@ public class PaloAltoFirewallVlanEntityTypeConfiguration : IEntityTypeConfigurat
        builder.Property(e => e.VlanId)
            .HasColumnName(ColumnNames.VlanId);

        builder.Property(e => e.AccessDomainId)
            .HasColumnName(ColumnNames.PaloAlto.AccessDomainId);

        builder.HasOne(e => e.Firewall)
            .WithMany(e => e.Vlans)
            .HasForeignKey(e => e.FirewallId);
@@ -40,9 +37,6 @@ public class PaloAltoFirewallVlanEntityTypeConfiguration : IEntityTypeConfigurat
            .WithMany()
            .HasForeignKey(e => e.VlanId);

        builder.HasOne(e => e.AccessDomain)
            .WithMany()
            .HasForeignKey(e => e.AccessDomainId);
    }
}