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

fix: Gateway extraction for single-api name parameter #855

Merged
merged 3 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@ namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clien
public interface IGatewayClient
{
Task<List<GatewayTemplateResource>> GetAllAsync(ExtractorParameters extractorParameters);

Task<bool> DoesApiReferenceGatewayAsync(string singleApiName, string gatewayName, ExtractorParameters extractorParameters);
}
}
25 changes: 0 additions & 25 deletions src/ArmTemplates/Common/API/Clients/Gateway/GatewayClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,27 @@
// --------------------------------------------------------------------------

using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Abstractions;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Constants;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Extensions;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Gateway;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Utilities.DataProcessors.Absctraction;
using Microsoft.Extensions.Logging;

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

readonly ILogger<GatewayClient> logger;
readonly IApisClient apisClient;
readonly ITemplateResourceDataProcessor<GatewayTemplateResource> templateResourceDataProcessor;

public GatewayClient(
IHttpClientFactory httpClientFactory,
ILogger<GatewayClient> logger,
IApisClient apisClient,
ITemplateResourceDataProcessor<GatewayTemplateResource> templateResourceDataProcessor
) : base(httpClientFactory)
{
this.logger = logger;
this.apisClient = apisClient;
this.templateResourceDataProcessor = templateResourceDataProcessor;
}

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

/// <summary>
/// Checks whether a given single API is referenced by a gateway
/// </summary>
/// <returns>true, if api references a gateway</returns>
public async Task<bool> DoesApiReferenceGatewayAsync(string singleApiName, string gatewayName, ExtractorParameters extractorParameters)
{
var gatewayApis = await this.apisClient.GetAllLinkedToGatewayAsync(gatewayName, extractorParameters);

if (gatewayApis.IsNullOrEmpty())
{
this.logger.LogDebug("Did not find any api linked to the gateway");
return false;
}

return gatewayApis.Any(gatewayApi => gatewayApi.Name == singleApiName);
}
}
}
38 changes: 36 additions & 2 deletions src/ArmTemplates/Common/API/Utils/ApiClientUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,27 @@
// --------------------------------------------------------------------------

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Abstractions;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Gateway;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Exceptions;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Extensions;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Apis;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;
using Microsoft.Extensions.Logging;

namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Utils
{
public class ApiClientUtils: IApiClientUtils
{
readonly IApisClient apisClient;

public ApiClientUtils(IApisClient apisClient)
readonly ILogger<ApiClientUtils> logger;

public ApiClientUtils(IApisClient apisClient, ILogger<ApiClientUtils> logger)
{
this.apisClient = apisClient;
this.logger = logger;
}

public async Task<Dictionary<string, List<string>>> GetAllAPIsDictionaryByVersionSetName(ExtractorParameters extractorParameters)
Expand Down Expand Up @@ -52,5 +60,31 @@ public ApiClientUtils(IApisClient apisClient)

return apiDictionary;
}

public async Task<ApiTemplateResource> GetsingleApi(string apiName, ExtractorParameters extractorParameters)
f-alizada marked this conversation as resolved.
Show resolved Hide resolved
{
var serviceApi = await this.apisClient.GetSingleAsync(apiName, extractorParameters);

if (serviceApi is null)
{
throw new ServiceApiNotFoundException($"ServiceApi with name '{apiName}' not found");
}
return serviceApi;
}

public async Task<bool> DoesApiReferenceGatewayAsync(string singleApiName, string gatewayName, ExtractorParameters extractorParameters)
{
var gatewayApis = await this.apisClient.GetAllLinkedToGatewayAsync(gatewayName, extractorParameters);

if (gatewayApis.IsNullOrEmpty())
{
this.logger.LogDebug("Did not find any api linked to the gateway");
return false;
}

var serviceApi = await this.GetsingleApi(singleApiName, extractorParameters);

return gatewayApis.Any(gatewayApi => gatewayApi.Name == serviceApi.Name);
}
}
}
5 changes: 5 additions & 0 deletions src/ArmTemplates/Common/API/Utils/IApiClientUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@

using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Apis;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;

namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Utils
{
public interface IApiClientUtils
{
Task<Dictionary<string, List<string>>> GetAllAPIsDictionaryByVersionSetName(ExtractorParameters extractorParameters);

Task<ApiTemplateResource> GetsingleApi(string apiName, ExtractorParameters extractorParameters);

Task<bool> DoesApiReferenceGatewayAsync(string singleApiName, string gatewayName, ExtractorParameters extractorParameters);
}
}
19 changes: 12 additions & 7 deletions src/ArmTemplates/Extractor/EntityExtractors/GatewayApiExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Abstractions;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Utils;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Constants;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Exceptions;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Extensions;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Abstractions;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Apis;
Expand All @@ -26,17 +28,20 @@ public class GatewayApiExtractor : IGatewayApiExtractor
readonly ITemplateBuilder templateBuilder;
readonly IGatewayClient gatewayClient;
readonly IApisClient apisClient;
readonly IApiClientUtils apiClientUtils;

public GatewayApiExtractor(
ILogger<GatewayApiExtractor> logger,
ITemplateBuilder templateBuilder,
IGatewayClient gatewayClient,
IApisClient apisClient)
IApisClient apisClient,
IApiClientUtils apiClientUtils)
{
this.logger = logger;
this.templateBuilder = templateBuilder;
this.gatewayClient = gatewayClient;
this.apisClient = apisClient;
this.apiClientUtils = apiClientUtils;
}

public async Task<Template<GatewayApiTemplateResources>> GenerateGatewayApiTemplateAsync(
Expand Down Expand Up @@ -67,10 +72,14 @@ public class GatewayApiExtractor : IGatewayApiExtractor

if (!string.IsNullOrEmpty(singleApiName))
{
// inluding only apis with singleApiName
var apis = gatewayApis.Where(x => x.Name == singleApiName);
var serviceApi = await this.apiClientUtils.GetsingleApi(singleApiName, extractorParameters);

// inluding only api with singleApiName
f-alizada marked this conversation as resolved.
Show resolved Hide resolved
var apis = gatewayApis.Where(x => x.Name == serviceApi.Name).ToList();
f-alizada marked this conversation as resolved.
Show resolved Hide resolved
if (!apis.IsNullOrEmpty())
{
//apis contains only one api
apis[0].Name = singleApiName;
var gatewayApiResources = this.GenerateGatewayApiTemplateResources(gateway.Name, apis);
gatewayApiTemplate.TypedResources.GatewayApis.AddRange(gatewayApiResources);
}
Expand Down Expand Up @@ -98,7 +107,6 @@ public class GatewayApiExtractor : IGatewayApiExtractor

List<GatewayApiTemplateResource> GenerateGatewayApiTemplateResources(string gatewayName, IEnumerable<ApiTemplateResource> apis)
{
string dependsOn = null;
var templateResources = new List<GatewayApiTemplateResource>();
foreach (var api in apis)
{
Expand All @@ -108,14 +116,11 @@ List<GatewayApiTemplateResource> GenerateGatewayApiTemplateResources(string gate
gatewayApiTemplateResource.Name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{gatewayName}/{api.Name}')]";
gatewayApiTemplateResource.ApiVersion = GlobalConstants.ApiVersion;
gatewayApiTemplateResource.Scale = null;
gatewayApiTemplateResource.DependsOn = string.IsNullOrEmpty(dependsOn) ? Array.Empty<string>() : new[] { dependsOn };
gatewayApiTemplateResource.Properties = new GatewayApiProperties
{
ProvisioningState = "created"
};

dependsOn = $"[resourceId('Microsoft.ApiManagement/service/gateways/apis', parameters('{ParameterNames.ApimServiceName}'), '{gatewayName}', '{api.Name}')]";
f-alizada marked this conversation as resolved.
Show resolved Hide resolved

templateResources.Add(gatewayApiTemplateResource);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System.Threading.Tasks;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Abstractions;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Utils;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Constants;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Extensions;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Abstractions;
Expand All @@ -21,15 +22,18 @@ public class GatewayExtractor : IGatewayExtractor
readonly ILogger<GatewayExtractor> logger;
readonly ITemplateBuilder templateBuilder;
readonly IGatewayClient gatewayClient;
readonly IApiClientUtils apiClientUtils;

public GatewayExtractor(
ILogger<GatewayExtractor> logger,
ITemplateBuilder templateBuilder,
IGatewayClient gatewayClient)
IGatewayClient gatewayClient,
IApiClientUtils apiClientUtils)
{
this.logger = logger;
this.templateBuilder = templateBuilder;
this.gatewayClient = gatewayClient;
this.apiClientUtils = apiClientUtils;
}

public async Task<Template<GatewayTemplateResources>> GenerateGatewayTemplateAsync(string singleApiName, ExtractorParameters extractorParameters)
Expand Down Expand Up @@ -59,7 +63,7 @@ public async Task<Template<GatewayTemplateResources>> GenerateGatewayTemplateAsy
}
else
{
var doesApiReferenceGateway = await this.gatewayClient.DoesApiReferenceGatewayAsync(singleApiName, gateway.OriginalName, extractorParameters);
var doesApiReferenceGateway = await this.apiClientUtils.DoesApiReferenceGatewayAsync(singleApiName, gateway.OriginalName, extractorParameters);
if (doesApiReferenceGateway)
{
gatewayTemplate.TypedResources.Gateways.Add(gateway);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public async Task GenerateAPIVersionSetTemplates_RaisesError_GivenApiVersionSetN
var responseFileLocation = Path.Combine(MockClientUtils.ApiClientJsonResponsesPath, "ApiManagementListApis_success_response.json");
var mockedApisClient = await MockApisClient.GetMockedHttpApiClient(new MockClientConfiguration(responseFileLocation: responseFileLocation));

var apiClientUtils = new ApiClientUtils(mockedApisClient);
var apiClientUtils = new ApiClientUtils(mockedApisClient, this.GetTestLogger<ApiClientUtils>());

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

var apiClientUtils = new ApiClientUtils(mockedApisClient);
var apiClientUtils = new ApiClientUtils(mockedApisClient, this.GetTestLogger<ApiClientUtils>());

var mockapiExtractor = new Mock<IApiExtractor>(MockBehavior.Strict);
mockapiExtractor
Expand Down
Loading