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

Commit d9dfbe7

Browse files
f-alizadaFarhad Alizada
andauthored
fix: Gateway extraction for single-api name parameter (#855)
* Gateway extraction for single-api name parameter Co-authored-by: Farhad Alizada <falizada@microsoft.com>
1 parent e8694f8 commit d9dfbe7

15 files changed

+350
-45
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,5 @@ namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clien
1313
public interface IGatewayClient
1414
{
1515
Task<List<GatewayTemplateResource>> GetAllAsync(ExtractorParameters extractorParameters);
16-
17-
Task<bool> DoesApiReferenceGatewayAsync(string singleApiName, string gatewayName, ExtractorParameters extractorParameters);
1816
}
1917
}

src/ArmTemplates/Common/API/Clients/Gateway/GatewayClient.cs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,27 @@
44
// --------------------------------------------------------------------------
55

66
using System.Collections.Generic;
7-
using System.Linq;
87
using System.Net.Http;
98
using System.Threading.Tasks;
109
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Abstractions;
1110
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Constants;
12-
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Extensions;
1311
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Gateway;
1412
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;
1513
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Utilities.DataProcessors.Absctraction;
16-
using Microsoft.Extensions.Logging;
1714

1815
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Gateway
1916
{
2017
public class GatewayClient : ApiClientBase, IGatewayClient
2118
{
2219
const string GetAllGatewaysRequest = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/gateways?api-version={4}";
2320

24-
readonly ILogger<GatewayClient> logger;
25-
readonly IApisClient apisClient;
2621
readonly ITemplateResourceDataProcessor<GatewayTemplateResource> templateResourceDataProcessor;
2722

2823
public GatewayClient(
2924
IHttpClientFactory httpClientFactory,
30-
ILogger<GatewayClient> logger,
31-
IApisClient apisClient,
3225
ITemplateResourceDataProcessor<GatewayTemplateResource> templateResourceDataProcessor
3326
) : base(httpClientFactory)
3427
{
35-
this.logger = logger;
36-
this.apisClient = apisClient;
3728
this.templateResourceDataProcessor = templateResourceDataProcessor;
3829
}
3930

@@ -49,21 +40,5 @@ public async Task<List<GatewayTemplateResource>> GetAllAsync(ExtractorParameters
4940
return gatewatTemplateResources;
5041
}
5142

52-
/// <summary>
53-
/// Checks whether a given single API is referenced by a gateway
54-
/// </summary>
55-
/// <returns>true, if api references a gateway</returns>
56-
public async Task<bool> DoesApiReferenceGatewayAsync(string singleApiName, string gatewayName, ExtractorParameters extractorParameters)
57-
{
58-
var gatewayApis = await this.apisClient.GetAllLinkedToGatewayAsync(gatewayName, extractorParameters);
59-
60-
if (gatewayApis.IsNullOrEmpty())
61-
{
62-
this.logger.LogDebug("Did not find any api linked to the gateway");
63-
return false;
64-
}
65-
66-
return gatewayApis.Any(gatewayApi => gatewayApi.Name == singleApiName);
67-
}
6843
}
6944
}

src/ArmTemplates/Common/API/Utils/ApiClientUtils.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,27 @@
44
// --------------------------------------------------------------------------
55

66
using System.Collections.Generic;
7+
using System.Linq;
78
using System.Threading.Tasks;
89
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Abstractions;
10+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Gateway;
11+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Exceptions;
12+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Extensions;
13+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Apis;
914
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;
15+
using Microsoft.Extensions.Logging;
1016

1117
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Utils
1218
{
1319
public class ApiClientUtils: IApiClientUtils
1420
{
1521
readonly IApisClient apisClient;
16-
17-
public ApiClientUtils(IApisClient apisClient)
22+
readonly ILogger<ApiClientUtils> logger;
23+
24+
public ApiClientUtils(IApisClient apisClient, ILogger<ApiClientUtils> logger)
1825
{
1926
this.apisClient = apisClient;
27+
this.logger = logger;
2028
}
2129

2230
public async Task<Dictionary<string, List<string>>> GetAllAPIsDictionaryByVersionSetName(ExtractorParameters extractorParameters)
@@ -52,5 +60,31 @@ public async Task<Dictionary<string, List<string>>> GetAllAPIsDictionaryByVersio
5260

5361
return apiDictionary;
5462
}
63+
64+
public async Task<ApiTemplateResource> GetSingleApi(string apiName, ExtractorParameters extractorParameters)
65+
{
66+
var serviceApi = await this.apisClient.GetSingleAsync(apiName, extractorParameters);
67+
68+
if (serviceApi is null)
69+
{
70+
throw new ServiceApiNotFoundException($"ServiceApi with name '{apiName}' not found");
71+
}
72+
return serviceApi;
73+
}
74+
75+
public async Task<bool> DoesApiReferenceGatewayAsync(string singleApiName, string gatewayName, ExtractorParameters extractorParameters)
76+
{
77+
var gatewayApis = await this.apisClient.GetAllLinkedToGatewayAsync(gatewayName, extractorParameters);
78+
79+
if (gatewayApis.IsNullOrEmpty())
80+
{
81+
this.logger.LogDebug("Did not find any api linked to the gateway");
82+
return false;
83+
}
84+
85+
var serviceApi = await this.GetSingleApi(singleApiName, extractorParameters);
86+
87+
return gatewayApis.Any(gatewayApi => gatewayApi.Name == serviceApi.Name);
88+
}
5589
}
5690
}

src/ArmTemplates/Common/API/Utils/IApiClientUtils.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55

66
using System.Collections.Generic;
77
using System.Threading.Tasks;
8+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Apis;
89
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;
910

1011
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Utils
1112
{
1213
public interface IApiClientUtils
1314
{
1415
Task<Dictionary<string, List<string>>> GetAllAPIsDictionaryByVersionSetName(ExtractorParameters extractorParameters);
16+
17+
Task<ApiTemplateResource> GetSingleApi(string apiName, ExtractorParameters extractorParameters);
18+
19+
Task<bool> DoesApiReferenceGatewayAsync(string singleApiName, string gatewayName, ExtractorParameters extractorParameters);
1520
}
1621
}

src/ArmTemplates/Extractor/EntityExtractors/GatewayApiExtractor.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
using System.Linq;
99
using System.Threading.Tasks;
1010
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Abstractions;
11+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Utils;
1112
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Constants;
13+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Exceptions;
1214
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Extensions;
1315
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Abstractions;
1416
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Apis;
@@ -26,17 +28,20 @@ public class GatewayApiExtractor : IGatewayApiExtractor
2628
readonly ITemplateBuilder templateBuilder;
2729
readonly IGatewayClient gatewayClient;
2830
readonly IApisClient apisClient;
31+
readonly IApiClientUtils apiClientUtils;
2932

3033
public GatewayApiExtractor(
3134
ILogger<GatewayApiExtractor> logger,
3235
ITemplateBuilder templateBuilder,
3336
IGatewayClient gatewayClient,
34-
IApisClient apisClient)
37+
IApisClient apisClient,
38+
IApiClientUtils apiClientUtils)
3539
{
3640
this.logger = logger;
3741
this.templateBuilder = templateBuilder;
3842
this.gatewayClient = gatewayClient;
3943
this.apisClient = apisClient;
44+
this.apiClientUtils = apiClientUtils;
4045
}
4146

4247
public async Task<Template<GatewayApiTemplateResources>> GenerateGatewayApiTemplateAsync(
@@ -67,10 +72,14 @@ public async Task<Template<GatewayApiTemplateResources>> GenerateGatewayApiTempl
6772

6873
if (!string.IsNullOrEmpty(singleApiName))
6974
{
70-
// inluding only apis with singleApiName
71-
var apis = gatewayApis.Where(x => x.Name == singleApiName);
75+
var serviceApi = await this.apiClientUtils.GetSingleApi(singleApiName, extractorParameters);
76+
77+
// including only api with singleApiName
78+
var apis = gatewayApis.Where(x => x.Name == serviceApi.Name).ToList();
7279
if (!apis.IsNullOrEmpty())
7380
{
81+
//apis contains only one api
82+
apis[0].Name = singleApiName;
7483
var gatewayApiResources = this.GenerateGatewayApiTemplateResources(gateway.Name, apis);
7584
gatewayApiTemplate.TypedResources.GatewayApis.AddRange(gatewayApiResources);
7685
}
@@ -98,7 +107,6 @@ public async Task<Template<GatewayApiTemplateResources>> GenerateGatewayApiTempl
98107

99108
List<GatewayApiTemplateResource> GenerateGatewayApiTemplateResources(string gatewayName, IEnumerable<ApiTemplateResource> apis)
100109
{
101-
string dependsOn = null;
102110
var templateResources = new List<GatewayApiTemplateResource>();
103111
foreach (var api in apis)
104112
{
@@ -108,14 +116,11 @@ List<GatewayApiTemplateResource> GenerateGatewayApiTemplateResources(string gate
108116
gatewayApiTemplateResource.Name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{gatewayName}/{api.Name}')]";
109117
gatewayApiTemplateResource.ApiVersion = GlobalConstants.ApiVersion;
110118
gatewayApiTemplateResource.Scale = null;
111-
gatewayApiTemplateResource.DependsOn = string.IsNullOrEmpty(dependsOn) ? Array.Empty<string>() : new[] { dependsOn };
112119
gatewayApiTemplateResource.Properties = new GatewayApiProperties
113120
{
114121
ProvisioningState = "created"
115122
};
116123

117-
dependsOn = $"[resourceId('Microsoft.ApiManagement/service/gateways/apis', parameters('{ParameterNames.ApimServiceName}'), '{gatewayName}', '{api.Name}')]";
118-
119124
templateResources.Add(gatewayApiTemplateResource);
120125
}
121126

src/ArmTemplates/Extractor/EntityExtractors/GatewayExtractor.cs

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

66
using System.Threading.Tasks;
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.Extensions;
1011
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Abstractions;
@@ -21,15 +22,18 @@ public class GatewayExtractor : IGatewayExtractor
2122
readonly ILogger<GatewayExtractor> logger;
2223
readonly ITemplateBuilder templateBuilder;
2324
readonly IGatewayClient gatewayClient;
25+
readonly IApiClientUtils apiClientUtils;
2426

2527
public GatewayExtractor(
2628
ILogger<GatewayExtractor> logger,
2729
ITemplateBuilder templateBuilder,
28-
IGatewayClient gatewayClient)
30+
IGatewayClient gatewayClient,
31+
IApiClientUtils apiClientUtils)
2932
{
3033
this.logger = logger;
3134
this.templateBuilder = templateBuilder;
3235
this.gatewayClient = gatewayClient;
36+
this.apiClientUtils = apiClientUtils;
3337
}
3438

3539
public async Task<Template<GatewayTemplateResources>> GenerateGatewayTemplateAsync(string singleApiName, ExtractorParameters extractorParameters)
@@ -59,7 +63,7 @@ public async Task<Template<GatewayTemplateResources>> GenerateGatewayTemplateAsy
5963
}
6064
else
6165
{
62-
var doesApiReferenceGateway = await this.gatewayClient.DoesApiReferenceGatewayAsync(singleApiName, gateway.OriginalName, extractorParameters);
66+
var doesApiReferenceGateway = await this.apiClientUtils.DoesApiReferenceGatewayAsync(singleApiName, gateway.OriginalName, extractorParameters);
6367
if (doesApiReferenceGateway)
6468
{
6569
gatewayTemplate.TypedResources.Gateways.Add(gateway);

tests/ArmTemplates.Tests/Extractor/Scenarios/ApiExtractorByVersionSetNameTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public async Task GenerateAPIVersionSetTemplates_RaisesError_GivenApiVersionSetN
6363
var responseFileLocation = Path.Combine(MockClientUtils.ApiClientJsonResponsesPath, "ApiManagementListApis_success_response.json");
6464
var mockedApisClient = await MockApisClient.GetMockedHttpApiClient(new MockClientConfiguration(responseFileLocation: responseFileLocation));
6565

66-
var apiClientUtils = new ApiClientUtils(mockedApisClient);
66+
var apiClientUtils = new ApiClientUtils(mockedApisClient, this.GetTestLogger<ApiClientUtils>());
6767

6868
var extractorExecutor = ExtractorExecutor.BuildExtractorExecutor(
6969
this.GetTestLogger<ExtractorExecutor>(),
@@ -89,7 +89,7 @@ public async Task GenerateAPIVersionSetTemplates_GeneratesApiTemplates()
8989
var responseFileLocation = Path.Combine(MockClientUtils.ApiClientJsonResponsesPath, "ApiManagementListApis_ExpandVersionSet_success_response.json");
9090
var mockedApisClient = await MockApisClient.GetMockedHttpApiClient(new MockClientConfiguration(responseFileLocation: responseFileLocation));
9191

92-
var apiClientUtils = new ApiClientUtils(mockedApisClient);
92+
var apiClientUtils = new ApiClientUtils(mockedApisClient, this.GetTestLogger<ApiClientUtils>());
9393

9494
var mockapiExtractor = new Mock<IApiExtractor>(MockBehavior.Strict);
9595
mockapiExtractor

0 commit comments

Comments
 (0)