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

Commit 1f30504

Browse files
f-alizadaFarhad Alizada
andauthored
Fetch by version set name approach changed (#848)
* Fetch by version set name approach changed * Add functionality to fetch name of version sets for grouping Co-authored-by: Farhad Alizada <falizada@microsoft.com>
1 parent f879768 commit 1f30504

File tree

11 files changed

+456
-14
lines changed

11 files changed

+456
-14
lines changed

src/ArmTemplates/Commands/Executors/ExtractorExecutor.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Commands.Configurations;
77
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Abstractions;
8+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Utils;
89
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Constants;
910
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Exceptions;
1011
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Extensions;
@@ -48,6 +49,7 @@ public class ExtractorExecutor
4849

4950
readonly ILogger<ExtractorExecutor> logger;
5051
readonly IApisClient apisClient;
52+
readonly IApiClientUtils apiClientUtils;
5153

5254
readonly IApiExtractor apiExtractor;
5355
readonly IApiVersionSetExtractor apiVersionSetExtractor;
@@ -98,7 +100,8 @@ public ExtractorExecutor(
98100
ISchemaExtractor schemaExtractor,
99101
IOpenIdConnectProviderExtractor openIdConnectProviderExtractor,
100102
IPolicyFragmentsExtractor policyFragmentsExtractor,
101-
IApiReleaseExtractor apiReleaseExtractor)
103+
IApiReleaseExtractor apiReleaseExtractor,
104+
IApiClientUtils apiClientUtils)
102105
{
103106
this.logger = logger;
104107
this.apisClient = apisClient;
@@ -125,6 +128,7 @@ public ExtractorExecutor(
125128
this.openIdConnectProviderExtractor = openIdConnectProviderExtractor;
126129
this.policyFragmentsExtractor = policyFragmentsExtractor;
127130
this.apiReleaseExtractor = apiReleaseExtractor;
131+
this.apiClientUtils = apiClientUtils;
128132
}
129133

130134
/// <summary>
@@ -156,7 +160,8 @@ public static ExtractorExecutor BuildExtractorExecutor(
156160
ISchemaExtractor schemaExtractor = null,
157161
IOpenIdConnectProviderExtractor openIdConnectProviderExtractor = null,
158162
IPolicyFragmentsExtractor policyFragmentsExtractor = null,
159-
IApiReleaseExtractor apiReleaseExtractor = null)
163+
IApiReleaseExtractor apiReleaseExtractor = null,
164+
IApiClientUtils apiClientUtils = null)
160165
=> new ExtractorExecutor(
161166
logger,
162167
apisClient,
@@ -182,7 +187,8 @@ public static ExtractorExecutor BuildExtractorExecutor(
182187
schemaExtractor,
183188
openIdConnectProviderExtractor,
184189
policyFragmentsExtractor,
185-
apiReleaseExtractor);
190+
apiReleaseExtractor,
191+
apiClientUtils);
186192

187193
public void SetExtractorParameters(ExtractorParameters extractorParameters)
188194
{
@@ -1031,13 +1037,13 @@ async Task GenerateSplitAPITemplates()
10311037
/// <summary>
10321038
/// Generates master template for each API within this version set and an extra master template to link these apis
10331039
/// </summary>
1034-
async Task GenerateAPIVersionSetTemplates()
1040+
public async Task GenerateAPIVersionSetTemplates()
10351041
{
10361042
// get api dictionary and check api version set
1037-
var apiDictionary = await this.GetAllAPIsDictionary(this.extractorParameters);
1043+
var apiDictionary = await this.apiClientUtils.GetAllAPIsDictionaryByVersionSetName(this.extractorParameters);
10381044
if (!apiDictionary.ContainsKey(this.extractorParameters.ApiVersionSetName))
10391045
{
1040-
throw new NoApiVersionSetWithSuchNameFoundException("API Version Set with this name doesn't exist");
1046+
throw new NoApiVersionSetWithSuchNameFoundException(ErrorMessages.ApiVersionDoesNotExistErrorMessage);
10411047
}
10421048
else
10431049
{
@@ -1052,7 +1058,7 @@ async Task GenerateAPIVersionSetTemplates()
10521058
}
10531059

10541060
// create master templates for this apiVersionSet
1055-
string versionSetFolder = string.Concat(this.extractorParameters.FilesGenerationRootDirectory, this.extractorParameters.FileNames.VersionSetMasterFolder);
1061+
string versionSetFolder = Path.Combine(this.extractorParameters.FilesGenerationRootDirectory, this.extractorParameters.FileNames.VersionSetMasterFolder);
10561062
Directory.CreateDirectory(versionSetFolder);
10571063
await this.GenerateTemplates(versionSetFolder, multipleApiNames: apiDictionary[this.extractorParameters.ApiVersionSetName]);
10581064

src/ArmTemplates/Common/API/Clients/Abstractions/IApisClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public interface IApisClient
1414
{
1515
Task<ApiTemplateResource> GetSingleAsync(string apiName, ExtractorParameters extractorParameters);
1616

17-
Task<List<ApiTemplateResource>> GetAllAsync(ExtractorParameters extractorParameters);
17+
Task<List<ApiTemplateResource>> GetAllAsync(ExtractorParameters extractorParameters, bool expandVersionSet = false);
1818

1919
Task<List<ApiTemplateResource>> GetAllCurrentAsync(ExtractorParameters extractorParameters);
2020

src/ArmTemplates/Common/API/Clients/Apis/ApisClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clien
1717
public class ApisClient : ApiClientBase, IApisClient
1818
{
1919
const string GetSingleApiRequest = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/apis/{4}?api-version={5}";
20-
const string GetAllApisRequest = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/apis?api-version={4}";
20+
const string GetAllApisRequest = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/apis?api-version={4}&expandApiVersionSet={5}";
2121
const string GetAllCurrentApisRequest = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/apis?api-version={4}&$filter=isCurrent";
2222
const string GetAllApisLinkedToProductRequest = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/products/{4}/apis?api-version={5}";
2323
const string GetApisLinkedToGatewayRequest = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/gateways/{4}/apis?api-version={5}";
@@ -41,12 +41,12 @@ public async Task<ApiTemplateResource> GetSingleAsync(string apiName, ExtractorP
4141
return api;
4242
}
4343

44-
public async Task<List<ApiTemplateResource>> GetAllAsync(ExtractorParameters extractorParameters)
44+
public async Task<List<ApiTemplateResource>> GetAllAsync(ExtractorParameters extractorParameters, bool expandVersionSet = false)
4545
{
4646
var (azToken, azSubId) = await this.Auth.GetAccessToken();
4747

4848
string requestUrl = string.Format(GetAllApisRequest,
49-
this.BaseUrl, azSubId, extractorParameters.ResourceGroup, extractorParameters.SourceApimName, GlobalConstants.ApiVersion);
49+
this.BaseUrl, azSubId, extractorParameters.ResourceGroup, extractorParameters.SourceApimName, GlobalConstants.ApiVersion, expandVersionSet);
5050

5151
var apis = await this.GetPagedResponseAsync<ApiTemplateResource>(azToken, requestUrl);
5252
this.apiDataProcessor.ProcessData(apis);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// --------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License.
4+
// --------------------------------------------------------------------------
5+
6+
using System.Collections.Generic;
7+
using System.Threading.Tasks;
8+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Abstractions;
9+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;
10+
11+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Utils
12+
{
13+
public class ApiClientUtils: IApiClientUtils
14+
{
15+
readonly IApisClient apisClient;
16+
17+
public ApiClientUtils(IApisClient apisClient)
18+
{
19+
this.apisClient = apisClient;
20+
}
21+
22+
public async Task<Dictionary<string, List<string>>> GetAllAPIsDictionaryByVersionSetName(ExtractorParameters extractorParameters)
23+
{
24+
// pull all apis from service
25+
var apis = await this.apisClient.GetAllAsync(extractorParameters, expandVersionSet: true);
26+
27+
// Generate apis dictionary based on all apiversionset
28+
var apiDictionary = new Dictionary<string, List<string>>();
29+
30+
foreach (var api in apis)
31+
{
32+
string apiVersionSetName = api.Properties.ApiVersionSet?.Name;
33+
34+
if (string.IsNullOrEmpty(apiVersionSetName))
35+
{
36+
continue;
37+
}
38+
39+
if (!apiDictionary.ContainsKey(apiVersionSetName))
40+
{
41+
var apiVersionSet = new List<string>
42+
{
43+
api.Name
44+
};
45+
apiDictionary[apiVersionSetName] = apiVersionSet;
46+
}
47+
else
48+
{
49+
apiDictionary[apiVersionSetName].Add(api.Name);
50+
}
51+
}
52+
53+
return apiDictionary;
54+
}
55+
}
56+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// --------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License.
4+
// --------------------------------------------------------------------------
5+
6+
using System.Collections.Generic;
7+
using System.Threading.Tasks;
8+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;
9+
10+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Utils
11+
{
12+
public interface IApiClientUtils
13+
{
14+
Task<Dictionary<string, List<string>>> GetAllAPIsDictionaryByVersionSetName(ExtractorParameters extractorParameters);
15+
}
16+
}

src/ArmTemplates/Common/Constants/ErrorMessages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ public static class ErrorMessages
99
{
1010
public const string DuplicateTagResourceNameErrorMessage = "Duplicate tag resource name found during sanitizing the display name. Please consider renaming tags: {0}, {1}. Both resulted resource name to be equal to: {2}";
1111
public const string EmptyResourceNameAfterSanitizingErrorMessage = "Sanitizing the display name '{0}' resulted empty string";
12+
public const string ApiVersionDoesNotExistErrorMessage = "API Version Set with this name doesn't exist";
1213
}
1314
}

src/ArmTemplates/Common/FileHandlers/FileNameGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static FileNames GenerateFileNames(string baseFileName)
6464
LinkedMaster = $@"{baseFileName}master.template.json",
6565
Apis = "/Apis",
6666
SplitAPIs = "/SplitAPIs",
67-
VersionSetMasterFolder = "/VersionSetMasterFolder",
67+
VersionSetMasterFolder = "VersionSetMasterFolder",
6868
RevisionMasterFolder = "/RevisionMasterFolder",
6969
GroupAPIsMasterFolder = "/MultipleApisMasterFolder",
7070
Groups = $"{baseFileName}groups.template.json",

src/ArmTemplates/ServiceExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Schemas;
4040
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.OpenIdConnectProviders;
4141
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.PolicyFragments;
42+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Utils;
4243

4344
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
4445
{
@@ -65,6 +66,7 @@ public static void AddArmTemplatesServices(this IServiceCollection services, ILo
6566
SetupExtractors(services);
6667
SetupCreators(services);
6768
SetupDataProcessors(services);
69+
SetupApiClientsUtils(services);
6870
}
6971

7072
static void SetupDataProcessors(IServiceCollection services)
@@ -169,5 +171,10 @@ static void SetupApiClients(IServiceCollection services)
169171
services.AddScoped<IOpenIdConnectProvidersClient, OpenIdConnectProviderClient>();
170172
services.AddScoped<IPolicyFragmentsClient, PolicyFragmentsClient>();
171173
}
174+
175+
static void SetupApiClientsUtils(IServiceCollection services)
176+
{
177+
services.AddScoped<IApiClientUtils, ApiClientUtils>();
178+
}
172179
}
173180
}

0 commit comments

Comments
 (0)