Baby steps on code refactoring
This commit is contained in:
parent
1ee556eb4e
commit
1a3450f6f9
|
|
@ -4,26 +4,29 @@ using System.Diagnostics;
|
|||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using Newtonsoft.Json;
|
||||
using AS1024.CommunityDocumentationPage.Interfaces;
|
||||
using AS1024.CommunityDocumentationPage.DIScopes;
|
||||
|
||||
namespace AS1024.CommunityDocumentationPage.Controllers
|
||||
{
|
||||
public class HomeController : Controller
|
||||
{
|
||||
private readonly ILogger<HomeController> _logger;
|
||||
private readonly IBgpCommunityDocumentation _documentation;
|
||||
|
||||
private IConfiguration Configuration { get; }
|
||||
|
||||
public HomeController(ILogger<HomeController> logger,
|
||||
IConfiguration configuration)
|
||||
IConfiguration configuration, IBgpCommunityDocumentation documentation)
|
||||
{
|
||||
_logger = logger;
|
||||
Configuration = configuration;
|
||||
_documentation = documentation;
|
||||
}
|
||||
|
||||
public async Task<IActionResult> IndexAsync()
|
||||
{
|
||||
var results = await ReadAPI(Configuration["APIKey"]);
|
||||
|
||||
var results = await _documentation.GetBgpCommunities();
|
||||
var filtered = results.Results.Where(b => b.Name.StartsWith(Configuration["ASN"]))
|
||||
.ToList();
|
||||
|
||||
|
|
@ -39,7 +42,7 @@ namespace AS1024.CommunityDocumentationPage.Controllers
|
|||
client.DefaultRequestHeaders.Clear();
|
||||
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", token);
|
||||
var result = await client.GetAsync(BuildNetBoxURI());
|
||||
var result = await client.GetAsync(BuildNetBoxURI().AbsoluteUri);
|
||||
var stringResult = await result.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<RouteTargets>(stringResult);
|
||||
} catch
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
using AS1024.CommunityDocumentationPage.Interfaces;
|
||||
using AS1024.CommunityDocumentationPage.Models;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace AS1024.CommunityDocumentationPage.DIScopes
|
||||
{
|
||||
public class NetboxBgpCommunityDocumentation : IBgpCommunityDocumentation
|
||||
{
|
||||
private readonly IConfiguration configuration;
|
||||
|
||||
public string DcimName => "netbox";
|
||||
private HttpClient client;
|
||||
|
||||
public NetboxBgpCommunityDocumentation(IConfiguration configuration)
|
||||
{
|
||||
this.configuration = configuration;
|
||||
client = new HttpClient();
|
||||
}
|
||||
|
||||
public async Task<RouteTargets> GetBgpCommunities()
|
||||
{
|
||||
client.DefaultRequestHeaders.Clear();
|
||||
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", configuration["APIKey"]);
|
||||
var result = await client.GetAsync(BuildNetBoxURI().AbsoluteUri);
|
||||
var stringResult = await result.Content.ReadAsStringAsync();
|
||||
#pragma warning disable CS8603 // Possible null reference return.
|
||||
return JsonConvert.DeserializeObject<RouteTargets>(stringResult);
|
||||
#pragma warning restore CS8603 // Possible null reference return.
|
||||
}
|
||||
|
||||
protected Uri BuildNetBoxURI()
|
||||
{
|
||||
var endUrl = new UriBuilder
|
||||
{
|
||||
Path = "/api/ipam/route-targets",
|
||||
Host = configuration["NetBoxHost"],
|
||||
Scheme = "https"
|
||||
};
|
||||
|
||||
return endUrl.Uri;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
namespace AS1024.CommunityDocumentationPage.DIScopes
|
||||
{
|
||||
public static class ValidateBgpCommunity
|
||||
{
|
||||
/// <summary>
|
||||
/// validates if this is a valid standard BGP Community
|
||||
/// </summary>
|
||||
/// <param name="Community">Community string represented as NNN:XXX</param>
|
||||
/// <returns>true if it's a valid bgp community, false if it isn't</returns>
|
||||
public static bool IsValidBgpCommunity(this string Community)
|
||||
{
|
||||
var splitStrings =
|
||||
Community.Split(":", StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
for (int i = 0; i < 1; i++)
|
||||
{
|
||||
if (!int.TryParse(splitStrings[i], out int communityParsed)
|
||||
&& communityParsed > 65536)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
using AS1024.CommunityDocumentationPage.Models;
|
||||
|
||||
namespace AS1024.CommunityDocumentationPage.Interfaces
|
||||
{
|
||||
/// <summary>
|
||||
/// Abstraction layer on obtaining BGP communities regardless of DCIM system
|
||||
/// </summary>
|
||||
public interface IBgpCommunityDocumentation
|
||||
{
|
||||
/// <summary>
|
||||
/// Read Only field on what the constructor grabs it's data from
|
||||
/// </summary>
|
||||
string DcimName { get; }
|
||||
/// <summary>
|
||||
/// Gets a list of BGP Communities
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task<RouteTargets> GetBgpCommunities();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
namespace AS1024.CommunityDocumentationPage.Models
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class BgpCommunity
|
||||
{
|
||||
/// <summary>
|
||||
/// Community Tag
|
||||
/// </summary>
|
||||
public string CommunityTag { get; set; }
|
||||
/// <summary>
|
||||
/// Type of BGP Community
|
||||
/// </summary>
|
||||
public CommunityType BgpCommunityType { get; set; }
|
||||
/// <summary>
|
||||
/// What the community does
|
||||
/// </summary>
|
||||
public string CommunityDescription { get; set; }
|
||||
}
|
||||
|
||||
public enum CommunityType
|
||||
{
|
||||
/// <summary>
|
||||
/// 32 bit community
|
||||
/// </summary>
|
||||
Community,
|
||||
/// <summary>
|
||||
/// Extended community with RT
|
||||
/// </summary>
|
||||
ExtendedCommunity,
|
||||
/// <summary>
|
||||
/// Large BGP Community
|
||||
/// </summary>
|
||||
LargeCommunity
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,13 @@
|
|||
using AS1024.CommunityDocumentationPage.DIScopes;
|
||||
using AS1024.CommunityDocumentationPage.Interfaces;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddControllersWithViews();
|
||||
|
||||
builder.Services.AddTransient<IBgpCommunityDocumentation, NetboxBgpCommunityDocumentation>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
|
|
|
|||
Loading…
Reference in New Issue