Update code to properly format the AS Tags
This commit is contained in:
parent
7b6794e7e9
commit
9c80ad14f3
|
|
@ -8,15 +8,16 @@ namespace AS1024.CommunityDocumentationPage.Controllers
|
|||
public class HomeController : Controller
|
||||
{
|
||||
private readonly ILogger<HomeController> _logger;
|
||||
private readonly IBgpCommunityDocumentation _documentation;
|
||||
private readonly IBgpCommunity _bgpCommunity;
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
public HomeController(ILogger<HomeController> logger,
|
||||
IConfiguration configuration, IBgpCommunityDocumentation documentation)
|
||||
IConfiguration configuration,
|
||||
IBgpCommunity bgpCommunity)
|
||||
{
|
||||
_logger = logger;
|
||||
_configuration = configuration;
|
||||
_documentation = documentation;
|
||||
_bgpCommunity = bgpCommunity;
|
||||
}
|
||||
|
||||
[ResponseCache(Duration = 3600)]
|
||||
|
|
@ -24,11 +25,10 @@ namespace AS1024.CommunityDocumentationPage.Controllers
|
|||
{
|
||||
try
|
||||
{
|
||||
RouteTargets results = await _documentation.GetBgpCommunities();
|
||||
List<Result> filtered = results.Results.Where(b => b.Name.StartsWith(_configuration["ASN"]))
|
||||
.ToList();
|
||||
var result =
|
||||
await _bgpCommunity.GetCommunities();
|
||||
|
||||
return View(filtered);
|
||||
return View(result);
|
||||
} catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Failed to obtain data\n{ex}");
|
||||
|
|
|
|||
|
|
@ -0,0 +1,57 @@
|
|||
using AS1024.CommunityDocumentationPage.Interfaces;
|
||||
using AS1024.CommunityDocumentationPage.Models;
|
||||
using Newtonsoft.Json;
|
||||
using System.Net.Http.Headers;
|
||||
|
||||
namespace AS1024.CommunityDocumentationPage.DIScopes
|
||||
{
|
||||
public class NetBoxBgpCommunities : IBgpCommunity
|
||||
{
|
||||
protected readonly IConfiguration _configuration;
|
||||
public NetBoxBgpCommunities(IConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
}
|
||||
string IBgpCommunity.DcimName => "netbox";
|
||||
|
||||
public async Task<ICollection<CommunityTag>> GetCommunities()
|
||||
{
|
||||
var Tags = new List<CommunityTag>();
|
||||
using HttpClient client = new HttpClient();
|
||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", _configuration["APIKey"]);
|
||||
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
HttpResponseMessage result = await client.GetAsync(BuildNetBoxURI());
|
||||
string stringResult = await result.Content.ReadAsStringAsync();
|
||||
var temp =
|
||||
JsonConvert.DeserializeObject<RouteTargets>(stringResult);
|
||||
|
||||
foreach (var route in temp.Results)
|
||||
{
|
||||
var community = new CommunityTag
|
||||
{
|
||||
ASN = int.Parse(route.Name.Split(':')[0]),
|
||||
CommunityValue = int.Parse(route.Name.Split(':')[1]),
|
||||
Description = route.Description,
|
||||
Tag = route.Tags
|
||||
};
|
||||
Tags.Add(community);
|
||||
}
|
||||
|
||||
return Tags.OrderBy(b => b.ASN)
|
||||
.ThenBy(b => b.CommunityValue)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
protected Uri BuildNetBoxURI()
|
||||
{
|
||||
UriBuilder endUrl = new UriBuilder
|
||||
{
|
||||
Path = "/api/ipam/route-targets/",
|
||||
Host = _configuration["NetBoxHost"],
|
||||
Scheme = "https"
|
||||
};
|
||||
|
||||
return endUrl.Uri;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -17,4 +17,19 @@ namespace AS1024.CommunityDocumentationPage.Interfaces
|
|||
/// <returns></returns>
|
||||
Task<RouteTargets> GetBgpCommunities();
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public interface IBgpCommunity
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
string DcimName { get; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task<ICollection<CommunityTag>> GetCommunities();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,4 +35,13 @@
|
|||
public string slug { get; set; }
|
||||
public string color { get; set; }
|
||||
}
|
||||
|
||||
public class CommunityTag
|
||||
{
|
||||
public int ASN { get; set; }
|
||||
public int CommunityValue { get; set; }
|
||||
public string FormattedVaule => $"{this.ASN}:{this.CommunityValue}";
|
||||
public string? Description { get; set; }
|
||||
public IList<Tag>? Tag { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ var builder = WebApplication.CreateBuilder(args);
|
|||
builder.Services.AddControllersWithViews();
|
||||
|
||||
builder.Services.AddTransient<IBgpCommunityDocumentation, NetboxBgpCommunityDocumentation>();
|
||||
builder.Services.AddTransient<IBgpCommunity, NetBoxBgpCommunities>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
@using Microsoft.Extensions.DependencyInjection
|
||||
@inject IConfiguration Configuration
|
||||
@model List<AS1024.CommunityDocumentationPage.Models.Result>
|
||||
@model List<AS1024.CommunityDocumentationPage.Models.CommunityTag>
|
||||
@{
|
||||
ViewData["Title"] = "BGP Communities";
|
||||
}
|
||||
|
|
@ -20,10 +20,10 @@
|
|||
</thead>
|
||||
@foreach (var item in Model) {
|
||||
<tr>
|
||||
<td scope="row">@item.Name</td>
|
||||
<td scope="row">@item.FormattedVaule</td>
|
||||
<td scope="row">@item.Description</td>
|
||||
<td scope="row">
|
||||
@foreach (var tags in item.Tags) {
|
||||
@foreach (var tags in item.Tag) {
|
||||
<div class="d-inline-flex p-2 text-white" style="background-color:#@tags.color">@tags.display</div>
|
||||
}
|
||||
</td>
|
||||
|
|
|
|||
Loading…
Reference in New Issue