From 1a3450f6f961085de15305e5618068c9b8478846 Mon Sep 17 00:00:00 2001 From: Jeff Leung Date: Sat, 7 Jan 2023 23:56:22 -0800 Subject: [PATCH] Baby steps on code refactoring --- .../Controllers/HomeController.cs | 11 +++-- .../NetboxBgpCommunityDocumentation.cs | 47 +++++++++++++++++++ .../DIScopes/ValidateBgpCommunity.cs | 26 ++++++++++ .../Interfaces/BgpCommunityDocumentation.cs | 20 ++++++++ .../Models/BgpCommunity.cs | 37 +++++++++++++++ AS1024.CommunityDocumentationPage/Program.cs | 5 ++ 6 files changed, 142 insertions(+), 4 deletions(-) create mode 100644 AS1024.CommunityDocumentationPage/DIScopes/NetboxBgpCommunityDocumentation.cs create mode 100644 AS1024.CommunityDocumentationPage/DIScopes/ValidateBgpCommunity.cs create mode 100644 AS1024.CommunityDocumentationPage/Interfaces/BgpCommunityDocumentation.cs create mode 100644 AS1024.CommunityDocumentationPage/Models/BgpCommunity.cs diff --git a/AS1024.CommunityDocumentationPage/Controllers/HomeController.cs b/AS1024.CommunityDocumentationPage/Controllers/HomeController.cs index 59028f8..1589d63 100644 --- a/AS1024.CommunityDocumentationPage/Controllers/HomeController.cs +++ b/AS1024.CommunityDocumentationPage/Controllers/HomeController.cs @@ -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 _logger; + private readonly IBgpCommunityDocumentation _documentation; private IConfiguration Configuration { get; } public HomeController(ILogger logger, - IConfiguration configuration) + IConfiguration configuration, IBgpCommunityDocumentation documentation) { _logger = logger; Configuration = configuration; + _documentation = documentation; } public async Task 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(stringResult); } catch diff --git a/AS1024.CommunityDocumentationPage/DIScopes/NetboxBgpCommunityDocumentation.cs b/AS1024.CommunityDocumentationPage/DIScopes/NetboxBgpCommunityDocumentation.cs new file mode 100644 index 0000000..86254ef --- /dev/null +++ b/AS1024.CommunityDocumentationPage/DIScopes/NetboxBgpCommunityDocumentation.cs @@ -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 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(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; + } + } +} diff --git a/AS1024.CommunityDocumentationPage/DIScopes/ValidateBgpCommunity.cs b/AS1024.CommunityDocumentationPage/DIScopes/ValidateBgpCommunity.cs new file mode 100644 index 0000000..c74d670 --- /dev/null +++ b/AS1024.CommunityDocumentationPage/DIScopes/ValidateBgpCommunity.cs @@ -0,0 +1,26 @@ +namespace AS1024.CommunityDocumentationPage.DIScopes +{ + public static class ValidateBgpCommunity + { + /// + /// validates if this is a valid standard BGP Community + /// + /// Community string represented as NNN:XXX + /// true if it's a valid bgp community, false if it isn't + 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; + } + } +} diff --git a/AS1024.CommunityDocumentationPage/Interfaces/BgpCommunityDocumentation.cs b/AS1024.CommunityDocumentationPage/Interfaces/BgpCommunityDocumentation.cs new file mode 100644 index 0000000..3af56cd --- /dev/null +++ b/AS1024.CommunityDocumentationPage/Interfaces/BgpCommunityDocumentation.cs @@ -0,0 +1,20 @@ +using AS1024.CommunityDocumentationPage.Models; + +namespace AS1024.CommunityDocumentationPage.Interfaces +{ + /// + /// Abstraction layer on obtaining BGP communities regardless of DCIM system + /// + public interface IBgpCommunityDocumentation + { + /// + /// Read Only field on what the constructor grabs it's data from + /// + string DcimName { get; } + /// + /// Gets a list of BGP Communities + /// + /// + Task GetBgpCommunities(); + } +} diff --git a/AS1024.CommunityDocumentationPage/Models/BgpCommunity.cs b/AS1024.CommunityDocumentationPage/Models/BgpCommunity.cs new file mode 100644 index 0000000..fbbd4bf --- /dev/null +++ b/AS1024.CommunityDocumentationPage/Models/BgpCommunity.cs @@ -0,0 +1,37 @@ +namespace AS1024.CommunityDocumentationPage.Models +{ + /// + /// + /// + public class BgpCommunity + { + /// + /// Community Tag + /// + public string CommunityTag { get; set; } + /// + /// Type of BGP Community + /// + public CommunityType BgpCommunityType { get; set; } + /// + /// What the community does + /// + public string CommunityDescription { get; set; } + } + + public enum CommunityType + { + /// + /// 32 bit community + /// + Community, + /// + /// Extended community with RT + /// + ExtendedCommunity, + /// + /// Large BGP Community + /// + LargeCommunity + } +} diff --git a/AS1024.CommunityDocumentationPage/Program.cs b/AS1024.CommunityDocumentationPage/Program.cs index 3155fc3..b9452b9 100644 --- a/AS1024.CommunityDocumentationPage/Program.cs +++ b/AS1024.CommunityDocumentationPage/Program.cs @@ -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(); + var app = builder.Build(); // Configure the HTTP request pipeline.