diff --git a/src/lib/azure/services/api-management/main.ts b/src/lib/azure/services/api-management/main.ts index 394ece19..59860f2b 100644 --- a/src/lib/azure/services/api-management/main.ts +++ b/src/lib/azure/services/api-management/main.ts @@ -4,7 +4,6 @@ import { DataAzurermApiManagementConfig, } from '@cdktf/provider-azurerm/lib/data-azurerm-api-management' import { ApiManagementCustomDomain } from '@cdktf/provider-azurerm/lib/api-management-custom-domain' -import { ApiManagementBackend } from '@cdktf/provider-azurerm/lib/api-management-backend' import { ApiManagement } from '@cdktf/provider-azurerm/lib/api-management' import { ApiManagementApi } from '@cdktf/provider-azurerm/lib/api-management-api' import { ApiManagementApiOperation } from '@cdktf/provider-azurerm/lib/api-management-api-operation' @@ -20,7 +19,6 @@ import { ApiManagementProps, ApiManagementBackendProps, ApiManagementApiProps, - ApiManagementV2Props, ApiManagementCustomDomainProps, } from './types' import _ from 'lodash' @@ -122,82 +120,6 @@ export class AzureApiManagementManager { return apiManagement } - /** - * @summary Method to create a new api management - * @param id scoped id of the resource - * @param scope scope in which this resource is defined - * @param props api management properties - * @see [CDKTF Api management Module]{@link https://github.com/cdktf/cdktf-provider-azurerm/blob/main/docs/apiManagement.typescript.md} - */ - public createApiManagementv2( - id: string, - scope: CommonAzureConstruct, - props: ApiManagementV2Props, - applicationInsightsKey?: ApiManagementLoggerApplicationInsights['instrumentationKey'] - ) { - if (!props) throw `Props undefined for ${id}` - - const resourceGroup = new DataAzurermResourceGroup(scope, `${id}-am-rg`, { - name: scope.props.resourceGroupName - ? scope.resourceNameFormatter.format(scope.props.resourceGroupName) - : `${props.resourceGroupName}`, - }) - - if (!resourceGroup) throw `Resource group undefined for ${id}` - - const apiManagement = new Resource(scope, `${id}-am`, { - type: 'Microsoft.ApiManagement/service@2024-05-01', - name: scope.resourceNameFormatter.format(props.name, scope.props.resourceNameOptions?.apiManagement), - location: resourceGroup.location, - parentId: resourceGroup.id, - - body: { - properties: { - apiVersionConstraint: { - minApiVersion: '2019-12-01', - }, - publisherName: props.publisherName, - publisherEmail: props.publisherEmail, - }, - sku: { - capacity: 1, - name: props.skuName, - }, - }, - - responseExportValues: ['*'], - - identity: [ - { - type: 'SystemAssigned', - }, - ], - - ignoreMissingProperty: true, - ignoreCasing: true, - schemaValidationEnabled: false, - - lifecycle: props.lifecycle, - }) - - if (applicationInsightsKey) { - new ApiManagementLogger(scope, `${id}-am-logger`, { - name: scope.resourceNameFormatter.format(props.name, scope.props.resourceNameOptions?.apiManagementLogger), - resourceGroupName: resourceGroup.name, - apiManagementName: apiManagement.name, - applicationInsights: { - instrumentationKey: applicationInsightsKey, - }, - }) - } - - createAzureTfOutput(`${id}-apiManagementName`, scope, apiManagement.name) - createAzureTfOutput(`${id}-apiManagementFriendlyUniqueId`, scope, apiManagement.friendlyUniqueId) - createAzureTfOutput(`${id}-apiManagementId`, scope, apiManagement.id) - - return apiManagement - } - /** * @summary Method to create a new api management backend * @param id scoped id of the resource @@ -301,12 +223,13 @@ export class AzureApiManagementManager { createAzureTfOutput(`${id}-${operation.displayName}-${operation.method}-apimOperationId`, scope, apimOperation.id) // Define Caching Policy if enabled - let cacheInboundPolicy = '' - let cacheOutboundPolicy = '' + let cacheSetVariablePolicy = '' + let cacheInvalidateInboundPolicy = '' + let cacheSetInboundPolicy = '' + let cacheSetOutboundPolicy = '' - if (props.caching?.enabled === true) { - if (operation.method === 'get') { - cacheInboundPolicy = ` + if (operation.caching) { + cacheSetVariablePolicy = ` - + return $"{apiName}:{apiVersion}:{fullPath}:{query}"; + }" />` - + if (operation.caching.enableCacheSet) { + cacheSetInboundPolicy = ` @@ -347,12 +273,12 @@ export class AzureApiManagementManager { ` - cacheOutboundPolicy = ` + cacheSetOutboundPolicy = ` - + MISS @@ -370,26 +296,8 @@ export class AzureApiManagementManager { ` } - if (operation.method === 'post') { - cacheInboundPolicy = ` - - - + if (operation.caching.enableCacheInvalidation) { + cacheInvalidateInboundPolicy = ` @@ -408,11 +316,14 @@ export class AzureApiManagementManager { if (props.rateLimit && scope.props.subscriptionId) { rateLimitPolicy = `` } + const policyXmlContent = ` ${rateLimitPolicy} - ${cacheInboundPolicy} + ${cacheSetVariablePolicy} + ${cacheInvalidateInboundPolicy} + ${cacheSetInboundPolicy} ${props.commonInboundPolicyXml ?? ''} @@ -420,7 +331,7 @@ export class AzureApiManagementManager { - ${cacheOutboundPolicy ?? ''} + ${cacheSetOutboundPolicy} ${props.commonOutboundPolicyXml ?? ''} diff --git a/src/lib/azure/services/api-management/types.ts b/src/lib/azure/services/api-management/types.ts index 341ba8bb..c57d6991 100644 --- a/src/lib/azure/services/api-management/types.ts +++ b/src/lib/azure/services/api-management/types.ts @@ -18,18 +18,16 @@ export interface ApiManagementApiProps extends ApiManagementApiConfig { operations: ApiManagementApiOperationProps[] commonInboundPolicyXml: string commonOutboundPolicyXml: string - caching?: ApiManagementApiCaching rateLimit?: ApiManagementApiRateLimit } -export interface ApiManagementV2Props extends ApiManagementConfig { - body: any +export interface ApiManagementApiOperationProps extends ApiManagementApiOperationConfig { + caching?: ApiManagementApiCaching } -export interface ApiManagementApiOperationProps extends ApiManagementApiOperationConfig {} - export interface ApiManagementApiCaching { - enabled: boolean + enableCacheSet?: boolean + enableCacheInvalidation?: boolean ttlInSecs?: number } diff --git a/src/test/azure/services/api-management-manager.test.ts b/src/test/azure/services/api-management-manager.test.ts index 91dd859b..5cf09986 100644 --- a/src/test/azure/services/api-management-manager.test.ts +++ b/src/test/azure/services/api-management-manager.test.ts @@ -256,7 +256,7 @@ describe('TestAzureApiManagementConstruct', () => { '${azurerm_api_management_api_operation.test-api-management-dev-apim-api-operation-test-get.operation_id}', resource_group_name: '${azurerm_api_management_api.test-api-management-dev-am-api.resource_group_name}', xml_content: - '\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n ', + '\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n ', }) }) })