Skip to content
This repository has been archived by the owner on Aug 20, 2020. It is now read-only.

Commit

Permalink
Provide description of Azure landscape
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkerkhove committed Apr 16, 2020
1 parent eafa1e4 commit 56040fa
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;

namespace Promitor.ResourceDiscovery.Agent.Configuration
{
public class AzureLandscape
{
public string TenantId { get; set; }
public List<string> Subscriptions { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Promitor.ResourceDiscovery.Agent.Configuration
{
public class ResourceDeclaration
{
public AzureLandscape AzureLandscape { get; set; }
public List<ResourceCollection> ResourceCollections { get; set; }
}
}
18 changes: 14 additions & 4 deletions src/Promitor.ResourceDiscovery.Agent/Graph/AzureResourceGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Azure.Management.ResourceGraph;
using Microsoft.Azure.Management.ResourceGraph.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
using Promitor.ResourceDiscovery.Agent.Configuration;
using Promitor.ResourceDiscovery.Agent.Model;
Expand All @@ -12,11 +13,21 @@ namespace Promitor.ResourceDiscovery.Agent.Graph
{
public class AzureResourceGraph
{
private readonly IOptionsMonitor<ResourceDeclaration> _resourceDeclarationMonitor;
private readonly IConfiguration _configuration;
private ResourceGraphClient _graphClient;

public AzureResourceGraph(IConfiguration configuration)
public string TenantId => _resourceDeclarationMonitor.CurrentValue?.AzureLandscape?.TenantId;
public List<string> Subscriptions => _resourceDeclarationMonitor.CurrentValue?.AzureLandscape?.Subscriptions;

public AzureResourceGraph(IOptionsMonitor<ResourceDeclaration> resourceDeclarationMonitor, IConfiguration configuration)
{
Guard.NotNull(resourceDeclarationMonitor, nameof(resourceDeclarationMonitor));
Guard.NotNull(resourceDeclarationMonitor.CurrentValue, nameof(resourceDeclarationMonitor.CurrentValue));
Guard.NotNull(resourceDeclarationMonitor.CurrentValue.AzureLandscape, nameof(resourceDeclarationMonitor.CurrentValue.AzureLandscape));
Guard.NotNull(configuration, nameof(configuration));

_resourceDeclarationMonitor = resourceDeclarationMonitor;
_configuration = configuration;
}

Expand All @@ -39,7 +50,7 @@ public async Task<List<Resource>> QueryAsync(string resourceType, ResourceCriter
.LimitTo(5)
.Build();

var queryRequest = new QueryRequest(criteria.Subscriptions, query);
var queryRequest = new QueryRequest(Subscriptions, query);
var response = await graphClient.ResourcesAsync(queryRequest);

var result = response.Data as JObject;
Expand All @@ -66,11 +77,10 @@ private async Task<ResourceGraphClient> GetOrCreateClient()

private async Task<ResourceGraphClient> CreateClientAsync()
{
var tenantId = "c8819874-9e56-4e3f-b1a8-1c0325138f27";
var appId = _configuration["DISCOVERY_APPID"];
var appSecret = _configuration["DISCOVERY_APPSECRET"];

var credentials = await Authentication.GetServiceClientCredentialsAsync("https://management.core.windows.net", appId, appSecret, tenantId);
var credentials = await Authentication.GetServiceClientCredentialsAsync("https://management.core.windows.net", appId, appSecret, TenantId);
return new ResourceGraphClient(credentials);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/Promitor.ResourceDiscovery.Agent/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public void ConfigureServices(IServiceCollection services)

RestrictToJsonContentType(options);
AddEnumAsStringRepresentation(options);

});

services.AddHealthChecks();
Expand Down
2 changes: 1 addition & 1 deletion src/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ services:
context: .
dockerfile: Promitor.ResourceDiscovery.Agent/Dockerfile
volumes:
- "./resource-config.yaml:/config/resource-declaration.yaml"
- "./../tests/discovery-config.yaml:/config/resource-declaration.yaml"
27 changes: 0 additions & 27 deletions src/resource-config.yaml

This file was deleted.

54 changes: 53 additions & 1 deletion tests/discovery-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
azureLandscape:
tenantId: c8819874-9e56-4e3f-b1a8-1c0325138f27
subscriptions:
- 0329dd2a-59dc-4493-aa54-cb01cb027dc2
- 0f9d7fea-99e8-4768-8672-06a28514f77e
resourceCollections:
- name: service-bus
type: microsoft.servicebus/namespaces
Expand All @@ -24,4 +29,51 @@ resourceCollections:
tags:
app: promitor-sample
name: logic-apps
type: microsoft.logic/workflows
type: microsoft.logic/workflows
# The following are used for integration testing:
- name: no-filter
type: microsoft.logic/workflows
- name: one-resource-group-scenario
type: microsoft.logic/workflows
criteria:
resourceGroups:
- promitor-testing-resource-discovery-eu
- name: two-resource-group-scenario
type: microsoft.logic/workflows
criteria:
resourceGroups:
- promitor-testing-resource-discovery-eu
- promitor-testing-resource-discovery-us
- name: one-subscriptions-scenario
type: microsoft.logic/workflows
criteria:
subscriptions:
- 0329dd2a-59dc-4493-aa54-cb01cb027dc2
- name: two-subscriptions-scenario
type: microsoft.logic/workflows
criteria:
subscriptions:
- 0329dd2a-59dc-4493-aa54-cb01cb027dc2
- 0f9d7fea-99e8-4768-8672-06a28514f77e
- name: one-tag-scenario
type: microsoft.logic/workflows
criteria:
tags:
app: promitor-resource-discovery-tests
- name: two-tag-scenario
type: microsoft.logic/workflows
criteria:
tags:
app: promitor-resource-discovery-tests
region: europe
- name: one-region-scenario
type: microsoft.logic/workflows
criteria:
regions:
- northeurope
- name: two-region-scenario
type: microsoft.logic/workflows
criteria:
regions:
- northeurope
- westeurope

0 comments on commit 56040fa

Please sign in to comment.