Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit e6a5388

Browse files
authored
backend-extractor refactoring (#670)
* backend-extractor refactoring * register BackendClient in DI * typos, renaming, review fixes * update name in copyright comment Co-authored-by: Dmitrii Korolev <dmkorolev@microsoft.com>
1 parent d96ad2b commit e6a5388

File tree

60 files changed

+643
-408
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+643
-408
lines changed

src/ArmTemplates/Commands/Executors/ExtractorExecutor.cs

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Apis;
88
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.ApiVersionSet;
99
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.AuthorizationServer;
10+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Backend;
1011
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Gateway;
1112
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.GatewayApi;
1213
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Groups;
@@ -440,6 +441,40 @@ await FileWriter.SaveAsJsonAsync(
440441
return apiTagTemplate;
441442
}
442443

444+
/// <summary>
445+
/// Generates backend templates in the desired folder
446+
/// </summary>
447+
/// <param name="singleApiName">name of API to load backend from</param>
448+
/// <param name="multipleApiNames">multiple API names to load backend from</param>
449+
/// <param name="baseFilesGenerationDirectory">name of base folder where to save output files</param>
450+
/// <returns>generated backend template</returns>
451+
public async Task<Template<BackendTemplateResources>> GenerateBackendTemplateAsync(
452+
string singleApiName,
453+
List<PolicyTemplateResource> apiPolicies,
454+
List<NamedValueTemplateResource> namedValueResources,
455+
string baseFilesGenerationDirectory)
456+
{
457+
this.logger.LogInformation("Started generation of backend template...");
458+
459+
var backendTemplate = await this.backendExtractor.GenerateBackendsTemplateAsync(
460+
singleApiName,
461+
apiPolicies,
462+
namedValueResources,
463+
baseFilesGenerationDirectory,
464+
this.extractorParameters);
465+
466+
if (backendTemplate?.HasResources() == true)
467+
{
468+
await FileWriter.SaveAsJsonAsync(
469+
backendTemplate,
470+
directory: baseFilesGenerationDirectory,
471+
fileName: this.extractorParameters.FileNames.Backends);
472+
}
473+
474+
this.logger.LogInformation("Finished generation of backend template...");
475+
return backendTemplate;
476+
}
477+
443478
/// <summary>
444479
/// Generates named-values templates in the desired folder
445480
/// </summary>
@@ -754,41 +789,25 @@ async Task GenerateTemplates(
754789
var apiTagTemplate = await this.GenerateTagApiTemplateAsync(singleApiName, multipleApiNames, baseFilesGenerationDirectory);
755790
var loggerTemplate = await this.GenerateLoggerTemplateAsync(apisToExtract, apiTemplate.TypedResources.GetAllPolicies(), baseFilesGenerationDirectory);
756791
var namedValueTemplate = await this.GenerateNamedValuesTemplateAsync(singleApiName, apiTemplate.TypedResources.GetAllPolicies(), loggerTemplate.TypedResources.Loggers, baseFilesGenerationDirectory);
792+
var backendTemplate = await this.GenerateBackendTemplateAsync(singleApiName, apiTemplate.TypedResources.GetAllPolicies(), namedValueTemplate.TypedResources.NamedValues, baseFilesGenerationDirectory);
757793
await this.GenerateGroupsTemplateAsync(baseFilesGenerationDirectory);
758-
await this.GenerateGatewayTemplateAsync(singleApiName, baseFilesGenerationDirectory);
794+
await this.GenerateGatewayTemplateAsync(singleApiName, baseFilesGenerationDirectory);
759795

760796
// not refactored
761-
List<TemplateResource> namedValueResources = namedValueTemplate.Resources.ToList();
762-
763-
var backendResult = await this.backendExtractor.GenerateBackendsARMTemplateAsync(
764-
singleApiName,
765-
apiTemplate.TypedResources.GetAllPolicies(),
766-
namedValueResources,
767-
this.extractorParameters,
768-
baseFilesGenerationDirectory);
769-
770-
// create parameters file
771797
Template templateParameters = await this.masterTemplateExtractor.CreateMasterTemplateParameterValues(
772798
apisToExtract,
773799
this.extractorParameters,
774800
this.loggerExtractor.Cache,
775801
loggerTemplate.TypedResources,
776-
backendResult.Item2,
777-
namedValueResources);
778-
779-
780-
// won't generate template when there is no resources
781-
if (!backendResult.Item1.Resources.IsNullOrEmpty())
782-
{
783-
FileWriter.WriteJSONToFile(backendResult.Item1, string.Concat(baseFilesGenerationDirectory, this.extractorParameters.FileNames.Backends));
784-
}
802+
backendTemplate.TypedResources.BackendNameParametersCache,
803+
namedValueTemplate.Resources.ToList());
785804

786805
if (this.extractorParameters.LinkedTemplatesBaseUrl != null)
787806
{
788807
// create a master template that links to all other templates
789808
Template masterTemplate = this.masterTemplateExtractor.GenerateLinkedMasterTemplate(
790809
apiTemplate, globalServicePolicyTemplate, apiVersionSetTemplate, productTemplate, productApiTemplate,
791-
apiTagTemplate, loggerTemplate, backendResult.Item1, authorizationServerTemplate, namedValueTemplate,
810+
apiTagTemplate, loggerTemplate, backendTemplate, authorizationServerTemplate, namedValueTemplate,
792811
tagTemplate, this.extractorParameters.FileNames, this.extractorParameters);
793812

794813
FileWriter.WriteJSONToFile(masterTemplate, string.Concat(baseFilesGenerationDirectory, this.extractorParameters.FileNames.LinkedMaster));
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// --------------------------------------------------------------------------
2+
// <copyright file="IBackendClient.cs" company="Microsoft">
3+
// Copyright (c) Microsoft Corporation. All rights reserved.
4+
// </copyright>
5+
// --------------------------------------------------------------------------
6+
7+
using System.Collections.Generic;
8+
using System.Threading.Tasks;
9+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Backend;
10+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;
11+
12+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Abstractions
13+
{
14+
public interface IBackendClient
15+
{
16+
Task<List<BackendTemplateResource>> GetAllAsync(ExtractorParameters extractorParameters);
17+
}
18+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// --------------------------------------------------------------------------
2+
// <copyright file="BackendClient.cs" company="Microsoft">
3+
// Copyright (c) Microsoft Corporation. All rights reserved.
4+
// </copyright>
5+
// --------------------------------------------------------------------------
6+
7+
using System.Collections.Generic;
8+
using System.Threading.Tasks;
9+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Abstractions;
10+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Backend.Responses;
11+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Constants;
12+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Backend;
13+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;
14+
15+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Backend
16+
{
17+
public class BackendClient : ApiClientBase, IBackendClient
18+
{
19+
const string GetAllBackendsRequest = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/backends?api-version={4}";
20+
21+
public async Task<List<BackendTemplateResource>> GetAllAsync(ExtractorParameters extractorParameters)
22+
{
23+
var (azToken, azSubId) = await this.Auth.GetAccessToken();
24+
25+
var requestUrl = string.Format(GetAllBackendsRequest,
26+
this.BaseUrl, azSubId, extractorParameters.ResourceGroup, extractorParameters.SourceApimName, GlobalConstants.ApiVersion);
27+
28+
var response = await this.CallApiManagementAsync<GetBackendsResponse>(azToken, requestUrl);
29+
return response.Backends;
30+
}
31+
}
32+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// --------------------------------------------------------------------------
2+
// <copyright file="GetBackendsResponse.cs" company="Microsoft">
3+
// Copyright (c) Microsoft Corporation. All rights reserved.
4+
// </copyright>
5+
// --------------------------------------------------------------------------
6+
7+
using System.Collections.Generic;
8+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Backend;
9+
using Newtonsoft.Json;
10+
11+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Backend.Responses
12+
{
13+
public class GetBackendsResponse
14+
{
15+
[JsonProperty("value")]
16+
public List<BackendTemplateResource> Backends { get; set; }
17+
18+
public int Count { get; set; }
19+
}
20+
}

src/ArmTemplates/Common/FileHandlers/FileNameGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static FileNames GenerateFileNames(string baseFileName)
2020
{
2121
ApiVersionSets = $@"{baseFileName}apiVersionSets.template.json",
2222
AuthorizationServers = $@"{baseFileName}authorizationServers.template.json",
23-
Backends = $@"\{baseFileName}backends.template.json",
23+
Backends = $@"{baseFileName}backends.template.json",
2424
GlobalServicePolicy = $@"{baseFileName}globalServicePolicy.template.json",
2525
Loggers = $@"{baseFileName}loggers.template.json",
2626
NamedValues = $@"{baseFileName}namedValues.template.json",

src/ArmTemplates/Common/FileHandlers/FileWriter.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Extensions;
2-
using Newtonsoft.Json;
32
using System.IO;
43
using System.Threading.Tasks;
54

src/ArmTemplates/Common/TemplateModels/BackendTemplateResource.cs

Lines changed: 0 additions & 69 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// --------------------------------------------------------------------------
2+
// <copyright file="BackendCredentials.cs" company="Microsoft">
3+
// Copyright (c) Microsoft Corporation. All rights reserved.
4+
// </copyright>
5+
// --------------------------------------------------------------------------
6+
7+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Backend
8+
{
9+
public class BackendCredentials
10+
{
11+
public string[] Certificate { get; set; }
12+
public object Query { get; set; }
13+
public object Header { get; set; }
14+
public BackendCredentialsAuthorization Authorization { get; set; }
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// --------------------------------------------------------------------------
2+
// <copyright file="BackendCredentialsAuthorization.cs" company="Microsoft">
3+
// Copyright (c) Microsoft Corporation. All rights reserved.
4+
// </copyright>
5+
// --------------------------------------------------------------------------
6+
7+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Backend
8+
{
9+
public class BackendCredentialsAuthorization
10+
{
11+
public string Scheme { get; set; }
12+
public string Parameter { get; set; }
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.TemplateModels
1+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Backend
22
{
33
/// <summary>
44
/// Backend Parameters for a single API
55
/// </summary>
66
public class BackendApiParameters
77
{
8-
public string resourceId { get; set; }
9-
public string url { get; set; }
10-
public string protocol { get; set; }
8+
public string ResourceId { get; set; }
9+
public string Url { get; set; }
10+
public string Protocol { get; set; }
1111
}
1212

1313
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// --------------------------------------------------------------------------
2+
// <copyright file="BackendProxy.cs" company="Microsoft">
3+
// Copyright (c) Microsoft Corporation. All rights reserved.
4+
// </copyright>
5+
// --------------------------------------------------------------------------
6+
7+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Backend
8+
{
9+
public class BackendProxy
10+
{
11+
public string Url { get; set; }
12+
public string Username { get; set; }
13+
public string Password { get; set; }
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// --------------------------------------------------------------------------
2+
// <copyright file="BackendServiceFabricCluster.cs" company="Microsoft">
3+
// Copyright (c) Microsoft Corporation. All rights reserved.
4+
// </copyright>
5+
// --------------------------------------------------------------------------
6+
7+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Backend
8+
{
9+
public class BackendServiceFabricCluster
10+
{
11+
public string ClientCertificatethumbprint { get; set; }
12+
public int MaxPartitionResolutionRetries { get; set; }
13+
public string[] ManagementEndpoints { get; set; }
14+
public string[] ServerCertificateThumbprints { get; set; }
15+
public ServerX509Names[] ServerX509Names { get; set; }
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// --------------------------------------------------------------------------
2+
// <copyright file="BackendServiceFabricProperties.cs" company="Microsoft">
3+
// Copyright (c) Microsoft Corporation. All rights reserved.
4+
// </copyright>
5+
// --------------------------------------------------------------------------
6+
7+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Backend
8+
{
9+
public class BackendServiceFabricProperties
10+
{
11+
public BackendServiceFabricCluster ServiceFabricCluster { get; set; }
12+
}
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// --------------------------------------------------------------------------
2+
// <copyright file="BackendTemplateProperties.cs" company="Microsoft">
3+
// Copyright (c) Microsoft Corporation. All rights reserved.
4+
// </copyright>
5+
// --------------------------------------------------------------------------
6+
7+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Backend
8+
{
9+
public class BackendTemplateProperties
10+
{
11+
public string Title { get; set; }
12+
public string Description { get; set; }
13+
public string ResourceId { get; set; }
14+
public BackendServiceFabricProperties Properties { get; set; }
15+
public BackendCredentials Credentials { get; set; }
16+
public BackendProxy Proxy { get; set; }
17+
public BackendTls Tls { get; set; }
18+
public string Url { get; set; }
19+
public string Protocol { get; set; }
20+
}
21+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Abstractions;
2+
3+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Backend
4+
{
5+
public class BackendTemplateResource : TemplateResource
6+
{
7+
public BackendTemplateProperties Properties { get; set; }
8+
}
9+
}

0 commit comments

Comments
 (0)