@gradientedge/cdk-utils 9.62.0 → 9.64.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -216,17 +216,130 @@ class AzureApiManagementManager {
216
216
  (0, utils_1.createAzureTfOutput)(`${id}-${operation.displayName}-${operation.method}-apimOperationOperationId`, scope, apimOperation.operationId);
217
217
  (0, utils_1.createAzureTfOutput)(`${id}-${operation.displayName}-${operation.method}-apimOperationFriendlyUniqueId`, scope, apimOperation.friendlyUniqueId);
218
218
  (0, utils_1.createAzureTfOutput)(`${id}-${operation.displayName}-${operation.method}-apimOperationId`, scope, apimOperation.id);
219
- if (props.policyXmlContent) {
220
- const apimOperationPolicy = new api_management_api_operation_policy_1.ApiManagementApiOperationPolicy(scope, `${id}-apim-api-operation-policy-${operation.displayName}-${operation.method}`, {
221
- apiManagementName: apiManagementApi.apiManagementName,
222
- resourceGroupName: apiManagementApi.resourceGroupName,
223
- apiName: apiManagementApi.name,
224
- operationId: apimOperation.operationId,
225
- xmlContent: props.policyXmlContent,
226
- });
227
- (0, utils_1.createAzureTfOutput)(`${id}-${operation.displayName}-${operation.method}-apimOperationPolicyFriendlyUniqueId`, scope, apimOperationPolicy.friendlyUniqueId);
228
- (0, utils_1.createAzureTfOutput)(`${id}-${operation.displayName}-${operation.method}-apimOperationPolicyId`, scope, apimOperationPolicy.id);
219
+ // Define Caching Policy if enabled
220
+ let cacheInboundPolicy = '';
221
+ let cacheOutboundPolicy = '';
222
+ if (props.caching?.enabled === true) {
223
+ if (operation.method === 'get') {
224
+ cacheInboundPolicy = `<!-- Generate a comprehensive custom cache key (without query params or Accept header) -->
225
+ <set-variable name="customCacheKey" value="@{
226
+ // Instance identification
227
+
228
+ // API identification
229
+ string apiName = context.Api.Name.Replace(" ", "").ToLower();
230
+ string apiVersion = context.Api.Version ?? "v1";
231
+
232
+ // Full path construction (without query parameters)
233
+ string fullPath = context.Request.Url.Path.ToLower();
234
+
235
+ // Construct final cache key (no Accept header needed for JSON-only APIs)
236
+ return $"{apiName}:{apiVersion}:{fullPath}";
237
+ }" />
238
+ <set-variable name="bypassCache" value="@(context.Request.Headers.GetValueOrDefault("X-Bypass-Cache", "false").ToLower())" />
239
+
240
+ <choose>
241
+ <when condition="@((string)context.Variables["bypassCache"] != "true")">
242
+ <!-- Attempt to retrieve cached response -->
243
+ <cache-lookup-value key="@((string)context.Variables["customCacheKey"])" variable-name="cachedResponse" />
244
+
245
+ <!-- If cache hit, return cached response -->
246
+ <choose>
247
+ <when condition="@(context.Variables.ContainsKey("cachedResponse"))">
248
+ <return-response>
249
+ <set-status code="200" reason="OK" />
250
+ <set-header name="Content-Type" exists-action="override">
251
+ <value>application/json</value>
252
+ </set-header>
253
+ <set-header name="X-Apim-Cache-Status" exists-action="override">
254
+ <value>HIT</value>
255
+ </set-header>
256
+ <set-header name="X-Apim-Cache-Key" exists-action="override">
257
+ <value>@((string)context.Variables["customCacheKey"])</value>
258
+ </set-header>
259
+ <set-body>@((string)context.Variables["cachedResponse"])</set-body>
260
+ </return-response>
261
+ </when>
262
+ </choose>
263
+ </when>
264
+ </choose>`;
265
+ cacheOutboundPolicy = `<choose>
266
+ <when condition="@((string)context.Variables["bypassCache"] != "true")">
267
+ <!-- Store the response body in cache -->
268
+ <choose>
269
+ <when condition="@(context.Response.StatusCode == 200)">
270
+ <cache-store-value key="@((string)context.Variables["customCacheKey"])" value="@(context.Response.Body.As<string>(preserveContent: true))" duration="${props.caching.ttl ?? 900}" />
271
+ <!-- Add cache status header -->
272
+ <set-header name="X-Apim-Cache-Status" exists-action="override">
273
+ <value>MISS</value>
274
+ </set-header>
275
+ </when>
276
+ </choose>
277
+ <!-- Add debug headers -->
278
+ <set-header name="X-Apim-Cache-Key" exists-action="override">
279
+ <value>@((string)context.Variables["customCacheKey"])</value>
280
+ </set-header>
281
+ <set-header name="X-Apim-API-Name" exists-action="override">
282
+ <value>@(context.Api.Name)</value>
283
+ </set-header>
284
+ </when>
285
+ </choose>`;
286
+ }
287
+ if (operation.method === 'post') {
288
+ cacheInboundPolicy = `<!-- Generate a comprehensive custom cache key (without query params or Accept header) -->
289
+ <set-variable name="customCacheKey" value="@{
290
+ // Instance identification
291
+
292
+ // API identification
293
+ string apiName = context.Api.Name.Replace(" ", "").ToLower();
294
+ string apiVersion = context.Api.Version ?? "v1";
295
+
296
+ // Full path construction (without query parameters)
297
+ string fullPath = context.Request.Url.Path.ToLower();
298
+
299
+ // Construct final cache key (no Accept header needed for JSON-only APIs)
300
+ return $"{apiName}:{apiVersion}:{fullPath}";
301
+ }" />
302
+ <set-variable name="clearCache" value="@(context.Request.Headers.GetValueOrDefault("X-Apim-Clear-Cache", "false").ToLower())" />
303
+
304
+ <!-- Allow admin to clear specific cache entries -->
305
+ <choose>
306
+ <when condition="@((string)context.Variables["clearCache"] == "true")">
307
+ <cache-remove-value key="@((string)context.Variables["customCacheKey"])" />
308
+ <return-response>
309
+ <set-status code="200" reason="OK" />
310
+ <set-body>Cache entry removed successfully</set-body>
311
+ </return-response>
312
+ </when>
313
+ </choose>`;
314
+ }
229
315
  }
316
+ const policyXmlContent = `<policies>
317
+ <inbound>
318
+ <base />
319
+ ${cacheInboundPolicy}
320
+ ${props.commonInboundPolicyXml ?? ''}
321
+ </inbound>
322
+ <backend>
323
+ <base />
324
+ </backend>
325
+ <outbound>
326
+ <base />
327
+ ${cacheOutboundPolicy ?? ''}
328
+ ${props.commonOutboundPolicyXml ?? ''}
329
+ </outbound>
330
+ <on-error>
331
+ <base />
332
+ </on-error>
333
+ </policies>`;
334
+ const apimOperationPolicy = new api_management_api_operation_policy_1.ApiManagementApiOperationPolicy(scope, `${id}-apim-api-operation-policy-${operation.displayName}-${operation.method}`, {
335
+ apiManagementName: apiManagementApi.apiManagementName,
336
+ resourceGroupName: apiManagementApi.resourceGroupName,
337
+ apiName: apiManagementApi.name,
338
+ operationId: apimOperation.operationId,
339
+ xmlContent: policyXmlContent,
340
+ });
341
+ (0, utils_1.createAzureTfOutput)(`${id}-${operation.displayName}-${operation.method}-apimOperationPolicyFriendlyUniqueId`, scope, apimOperationPolicy.friendlyUniqueId);
342
+ (0, utils_1.createAzureTfOutput)(`${id}-${operation.displayName}-${operation.method}-apimOperationPolicyId`, scope, apimOperationPolicy.id);
230
343
  });
231
344
  return apiManagementApi;
232
345
  }
@@ -3,7 +3,6 @@ import { ApiManagementBackendConfig } from '@cdktf/provider-azurerm/lib/api-mana
3
3
  import { ApiManagementCustomDomainConfig } from '@cdktf/provider-azurerm/lib/api-management-custom-domain';
4
4
  import { ApiManagementApiConfig } from '@cdktf/provider-azurerm/lib/api-management-api';
5
5
  import { ApiManagementApiOperationConfig } from '@cdktf/provider-azurerm/lib/api-management-api-operation';
6
- import { ApiManagementApiOperationPolicyConfig } from '@cdktf/provider-azurerm/lib/api-management-api-operation-policy';
7
6
  export interface ApiManagementProps extends ApiManagementConfig {
8
7
  }
9
8
  export interface ApiManagementBackendProps extends ApiManagementBackendConfig {
@@ -12,9 +11,17 @@ export interface ApiManagementBackendProps extends ApiManagementBackendConfig {
12
11
  export interface ApiManagementCustomDomainProps extends ApiManagementCustomDomainConfig {
13
12
  }
14
13
  export interface ApiManagementApiProps extends ApiManagementApiConfig {
15
- operations: ApiManagementApiOperationConfig[];
16
- policyXmlContent?: ApiManagementApiOperationPolicyConfig['xmlContent'];
14
+ operations: ApiManagementApiOperationProps[];
15
+ commonInboundPolicyXml: string;
16
+ commonOutboundPolicyXml: string;
17
+ caching?: ApiManagementApiCaching;
17
18
  }
18
19
  export interface ApiManagementV2Props extends ApiManagementConfig {
19
20
  body: any;
20
21
  }
22
+ export interface ApiManagementApiOperationProps extends ApiManagementApiOperationConfig {
23
+ }
24
+ export interface ApiManagementApiCaching {
25
+ enabled: boolean;
26
+ ttl?: number;
27
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gradientedge/cdk-utils",
3
- "version": "9.62.0",
3
+ "version": "9.64.0",
4
4
  "description": "Utilities for AWS CDK provisioning",
5
5
  "main": "dist/index.js",
6
6
  "engines": {
@@ -272,30 +272,147 @@ export class AzureApiManagementManager {
272
272
  )
273
273
  createAzureTfOutput(`${id}-${operation.displayName}-${operation.method}-apimOperationId`, scope, apimOperation.id)
274
274
 
275
- if (props.policyXmlContent) {
276
- const apimOperationPolicy = new ApiManagementApiOperationPolicy(
277
- scope,
278
- `${id}-apim-api-operation-policy-${operation.displayName}-${operation.method}`,
279
- {
280
- apiManagementName: apiManagementApi.apiManagementName,
281
- resourceGroupName: apiManagementApi.resourceGroupName,
282
- apiName: apiManagementApi.name,
283
- operationId: apimOperation.operationId,
284
- xmlContent: props.policyXmlContent,
285
- }
286
- )
287
-
288
- createAzureTfOutput(
289
- `${id}-${operation.displayName}-${operation.method}-apimOperationPolicyFriendlyUniqueId`,
290
- scope,
291
- apimOperationPolicy.friendlyUniqueId
292
- )
293
- createAzureTfOutput(
294
- `${id}-${operation.displayName}-${operation.method}-apimOperationPolicyId`,
295
- scope,
296
- apimOperationPolicy.id
297
- )
275
+ // Define Caching Policy if enabled
276
+ let cacheInboundPolicy = ''
277
+ let cacheOutboundPolicy = ''
278
+
279
+ if (props.caching?.enabled === true) {
280
+ if (operation.method === 'get') {
281
+ cacheInboundPolicy = `<!-- Generate a comprehensive custom cache key (without query params or Accept header) -->
282
+ <set-variable name="customCacheKey" value="@{
283
+ // Instance identification
284
+
285
+ // API identification
286
+ string apiName = context.Api.Name.Replace(" ", "").ToLower();
287
+ string apiVersion = context.Api.Version ?? "v1";
288
+
289
+ // Full path construction (without query parameters)
290
+ string fullPath = context.Request.Url.Path.ToLower();
291
+
292
+ // Construct final cache key (no Accept header needed for JSON-only APIs)
293
+ return $"{apiName}:{apiVersion}:{fullPath}";
294
+ }" />
295
+ <set-variable name="bypassCache" value="@(context.Request.Headers.GetValueOrDefault("X-Bypass-Cache", "false").ToLower())" />
296
+
297
+ <choose>
298
+ <when condition="@((string)context.Variables["bypassCache"] != "true")">
299
+ <!-- Attempt to retrieve cached response -->
300
+ <cache-lookup-value key="@((string)context.Variables["customCacheKey"])" variable-name="cachedResponse" />
301
+
302
+ <!-- If cache hit, return cached response -->
303
+ <choose>
304
+ <when condition="@(context.Variables.ContainsKey("cachedResponse"))">
305
+ <return-response>
306
+ <set-status code="200" reason="OK" />
307
+ <set-header name="Content-Type" exists-action="override">
308
+ <value>application/json</value>
309
+ </set-header>
310
+ <set-header name="X-Apim-Cache-Status" exists-action="override">
311
+ <value>HIT</value>
312
+ </set-header>
313
+ <set-header name="X-Apim-Cache-Key" exists-action="override">
314
+ <value>@((string)context.Variables["customCacheKey"])</value>
315
+ </set-header>
316
+ <set-body>@((string)context.Variables["cachedResponse"])</set-body>
317
+ </return-response>
318
+ </when>
319
+ </choose>
320
+ </when>
321
+ </choose>`
322
+ cacheOutboundPolicy = `<choose>
323
+ <when condition="@((string)context.Variables["bypassCache"] != "true")">
324
+ <!-- Store the response body in cache -->
325
+ <choose>
326
+ <when condition="@(context.Response.StatusCode == 200)">
327
+ <cache-store-value key="@((string)context.Variables["customCacheKey"])" value="@(context.Response.Body.As<string>(preserveContent: true))" duration="${props.caching.ttl ?? 900}" />
328
+ <!-- Add cache status header -->
329
+ <set-header name="X-Apim-Cache-Status" exists-action="override">
330
+ <value>MISS</value>
331
+ </set-header>
332
+ </when>
333
+ </choose>
334
+ <!-- Add debug headers -->
335
+ <set-header name="X-Apim-Cache-Key" exists-action="override">
336
+ <value>@((string)context.Variables["customCacheKey"])</value>
337
+ </set-header>
338
+ <set-header name="X-Apim-API-Name" exists-action="override">
339
+ <value>@(context.Api.Name)</value>
340
+ </set-header>
341
+ </when>
342
+ </choose>`
343
+ }
344
+
345
+ if (operation.method === 'post') {
346
+ cacheInboundPolicy = `<!-- Generate a comprehensive custom cache key (without query params or Accept header) -->
347
+ <set-variable name="customCacheKey" value="@{
348
+ // Instance identification
349
+
350
+ // API identification
351
+ string apiName = context.Api.Name.Replace(" ", "").ToLower();
352
+ string apiVersion = context.Api.Version ?? "v1";
353
+
354
+ // Full path construction (without query parameters)
355
+ string fullPath = context.Request.Url.Path.ToLower();
356
+
357
+ // Construct final cache key (no Accept header needed for JSON-only APIs)
358
+ return $"{apiName}:{apiVersion}:{fullPath}";
359
+ }" />
360
+ <set-variable name="clearCache" value="@(context.Request.Headers.GetValueOrDefault("X-Apim-Clear-Cache", "false").ToLower())" />
361
+
362
+ <!-- Allow admin to clear specific cache entries -->
363
+ <choose>
364
+ <when condition="@((string)context.Variables["clearCache"] == "true")">
365
+ <cache-remove-value key="@((string)context.Variables["customCacheKey"])" />
366
+ <return-response>
367
+ <set-status code="200" reason="OK" />
368
+ <set-body>Cache entry removed successfully</set-body>
369
+ </return-response>
370
+ </when>
371
+ </choose>`
372
+ }
298
373
  }
374
+
375
+ const policyXmlContent = `<policies>
376
+ <inbound>
377
+ <base />
378
+ ${cacheInboundPolicy}
379
+ ${props.commonInboundPolicyXml ?? ''}
380
+ </inbound>
381
+ <backend>
382
+ <base />
383
+ </backend>
384
+ <outbound>
385
+ <base />
386
+ ${cacheOutboundPolicy ?? ''}
387
+ ${props.commonOutboundPolicyXml ?? ''}
388
+ </outbound>
389
+ <on-error>
390
+ <base />
391
+ </on-error>
392
+ </policies>`
393
+
394
+ const apimOperationPolicy = new ApiManagementApiOperationPolicy(
395
+ scope,
396
+ `${id}-apim-api-operation-policy-${operation.displayName}-${operation.method}`,
397
+ {
398
+ apiManagementName: apiManagementApi.apiManagementName,
399
+ resourceGroupName: apiManagementApi.resourceGroupName,
400
+ apiName: apiManagementApi.name,
401
+ operationId: apimOperation.operationId,
402
+ xmlContent: policyXmlContent,
403
+ }
404
+ )
405
+
406
+ createAzureTfOutput(
407
+ `${id}-${operation.displayName}-${operation.method}-apimOperationPolicyFriendlyUniqueId`,
408
+ scope,
409
+ apimOperationPolicy.friendlyUniqueId
410
+ )
411
+ createAzureTfOutput(
412
+ `${id}-${operation.displayName}-${operation.method}-apimOperationPolicyId`,
413
+ scope,
414
+ apimOperationPolicy.id
415
+ )
299
416
  })
300
417
 
301
418
  return apiManagementApi
@@ -3,7 +3,6 @@ import { ApiManagementBackendConfig } from '@cdktf/provider-azurerm/lib/api-mana
3
3
  import { ApiManagementCustomDomainConfig } from '@cdktf/provider-azurerm/lib/api-management-custom-domain'
4
4
  import { ApiManagementApiConfig } from '@cdktf/provider-azurerm/lib/api-management-api'
5
5
  import { ApiManagementApiOperationConfig } from '@cdktf/provider-azurerm/lib/api-management-api-operation'
6
- import { ApiManagementApiOperationPolicyConfig } from '@cdktf/provider-azurerm/lib/api-management-api-operation-policy'
7
6
 
8
7
  export interface ApiManagementProps extends ApiManagementConfig {}
9
8
 
@@ -14,10 +13,19 @@ export interface ApiManagementBackendProps extends ApiManagementBackendConfig {
14
13
  export interface ApiManagementCustomDomainProps extends ApiManagementCustomDomainConfig {}
15
14
 
16
15
  export interface ApiManagementApiProps extends ApiManagementApiConfig {
17
- operations: ApiManagementApiOperationConfig[]
18
- policyXmlContent?: ApiManagementApiOperationPolicyConfig['xmlContent']
16
+ operations: ApiManagementApiOperationProps[]
17
+ commonInboundPolicyXml: string
18
+ commonOutboundPolicyXml: string
19
+ caching?: ApiManagementApiCaching
19
20
  }
20
21
 
21
22
  export interface ApiManagementV2Props extends ApiManagementConfig {
22
23
  body: any
23
24
  }
25
+
26
+ export interface ApiManagementApiOperationProps extends ApiManagementApiOperationConfig {}
27
+
28
+ export interface ApiManagementApiCaching {
29
+ enabled: boolean
30
+ ttl?: number
31
+ }