Create SupportObserver data provider

This commit is contained in:
Hawk Foreste 2018-03-20 14:43:36 -07:00
Родитель ea86c88838
Коммит c8153633ad
5 изменённых файлов: 199 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Diagnostics.DataProviders
{
[DataSourceConfiguration("SupportObserver")]
public class SupportObserverDataProviderConfiguration : IDataProviderConfiguration
{
public SupportObserverDataProviderConfiguration()
{
}
/// <summary>
/// Client Id
/// </summary>
[ConfigurationName("ClientId")]
public string ClientId { get; set; }
/// <summary>
/// App Key
/// </summary>
[ConfigurationName("AppKey")]
public string AppKey { get; set; }
/// <summary>
/// Uri for SupportObserverResourceAAD app
/// </summary>
public string ResourceUri { get { return "https://microsoft.onmicrosoft.com/SupportObserverResourceApp"; } }
public void PostInitialize()
{
//no op
}
}
}

Просмотреть файл

@ -11,10 +11,12 @@ namespace Diagnostics.DataProviders
private OperationDataCache _cache = new OperationDataCache();
public KustoDataProvider Kusto;
public SupportObserverDataProvider Observer;
public DataProviders(DataSourcesConfiguration configuration)
{
Kusto = new KustoDataProvider(_cache, configuration.KustoConfiguration);
Observer = new SupportObserverDataProvider(_cache, configuration.SupportObserverConfiguration);
}
}
}

Просмотреть файл

@ -0,0 +1,29 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace Diagnostics.DataProviders
{
public interface ISupportObserverDataProvider
{
Task<IEnumerable<string>> GetSiteHostNames(string siteName);
Task<string> GetSiteResourceGroupName(string siteName);
Task<IEnumerable<Dictionary<string, string>>> GetSitesInResourceGroup(string subscriptionName, string resourceGroupName);
Task<IEnumerable<Dictionary<string, string>>> GetServerFarmsInResourceGroup(string subscriptionName, string resourceGroupName);
Task<IEnumerable<Dictionary<string, string>>> GetCertificatesInResourceGroup(string subscriptionName, string resourceGroupName);
Task<string> GetWebspaceResourceGroupName(string subscriptionId, string webSpaceName);
Task<string> GetServerFarmWebspaceName(string subscriptionId, string serverFarm);
Task<string> GetSiteWebSpaceName(string subscriptionId, string siteName);
Task<IEnumerable<Dictionary<string, string>>> GetSitesInServerFarm(string subscriptionId, string serverFarmName);
Task<JObject> GetAppServiceEnvironmentDetails(string hostingEnvironmentName);
Task<IEnumerable<object>> GetAppServiceEnvironmentDeployments(string hostingEnvironmentName);
Task<JObject> GetAdminSitesBySiteName(string stampName, string siteName);
Task<JObject> GetAdminSitesByHostName(string stampName, string[] hostNames);
Task<string> GetStorageVolumeForSite(string stampName, string siteName);
Task<JObject> GetRuntimeSiteSlotMap(string siteName);
Task<JObject> GetRuntimeSiteSlotMap(string stampName, string siteName);
}
}

Просмотреть файл

@ -0,0 +1,131 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Newtonsoft.Json.Linq;
namespace Diagnostics.DataProviders
{
public class SupportObserverDataProvider : DiagnosticDataProvider, ISupportObserverDataProvider
{
private readonly SupportObserverDataProviderConfiguration _configuration;
private static AuthenticationContext _authContext;
private static ClientCredential _aadCredentials;
private readonly HttpClient _httpClient;
public SupportObserverDataProvider(OperationDataCache cache, SupportObserverDataProviderConfiguration configuration) : base(cache)
{
_configuration = configuration;
_httpClient = new HttpClient();
_httpClient.BaseAddress = new Uri("https://support-bay-api.azurewebsites.net/observer");
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public Task<JObject> GetAdminSitesByHostName(string stampName, string[] hostNames)
{
throw new NotImplementedException();
}
public Task<JObject> GetAdminSitesBySiteName(string stampName, string siteName)
{
throw new NotImplementedException();
}
public Task<IEnumerable<object>> GetAppServiceEnvironmentDeployments(string hostingEnvironmentName)
{
throw new NotImplementedException();
}
public Task<JObject> GetAppServiceEnvironmentDetails(string hostingEnvironmentName)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Dictionary<string, string>>> GetCertificatesInResourceGroup(string subscriptionName, string resourceGroupName)
{
throw new NotImplementedException();
}
public Task<JObject> GetRuntimeSiteSlotMap(string siteName)
{
throw new NotImplementedException();
}
public Task<JObject> GetRuntimeSiteSlotMap(string stampName, string siteName)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Dictionary<string, string>>> GetServerFarmsInResourceGroup(string subscriptionName, string resourceGroupName)
{
throw new NotImplementedException();
}
public Task<string> GetServerFarmWebspaceName(string subscriptionId, string serverFarm)
{
throw new NotImplementedException();
}
public async Task<IEnumerable<string>> GetSiteHostNames(string siteName)
{
var response = await GetObserverResource($"/sites/{siteName}/hostnames?api-version=2.0");
var hostnames = response.Split(new char[] { ',' });
return hostnames;
}
public Task<string> GetSiteResourceGroupName(string siteName)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Dictionary<string, string>>> GetSitesInResourceGroup(string subscriptionName, string resourceGroupName)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Dictionary<string, string>>> GetSitesInServerFarm(string subscriptionId, string serverFarmName)
{
throw new NotImplementedException();
}
public Task<string> GetSiteWebSpaceName(string subscriptionId, string siteName)
{
throw new NotImplementedException();
}
public Task<string> GetStorageVolumeForSite(string stampName, string siteName)
{
throw new NotImplementedException();
}
public Task<string> GetWebspaceResourceGroupName(string subscriptionId, string webSpaceName)
{
throw new NotImplementedException();
}
private async Task<string> GetObserverResource(string url)
{
var request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await GetAccessToken());
var response = await _httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadAsStringAsync();
return result;
}
private async Task<string> GetAccessToken()
{
if (_authContext == null)
{
_aadCredentials = new ClientCredential(_configuration.ClientId, _configuration.AppKey);
_authContext = new AuthenticationContext("https://login.microsoftonline.com/microsoft.onmicrosoft.com", TokenCache.DefaultShared);
}
var authResult = await _authContext.AcquireTokenAsync(_configuration.ResourceUri, _aadCredentials);
return authResult.AccessToken;
}
}
}

Просмотреть файл

@ -9,5 +9,6 @@ namespace Diagnostics.DataProviders
public class DataSourcesConfiguration
{
public KustoDataProviderConfiguration KustoConfiguration { get; set; }
public SupportObserverDataProviderConfiguration SupportObserverConfiguration { get; set; }
}
}