@gradientedge/cdk-utils 9.81.0 → 9.83.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.
- package/dist/src/lib/azure/common/construct.d.ts +2 -1
- package/dist/src/lib/azure/common/construct.js +2 -0
- package/dist/src/lib/azure/services/api-management/main.d.ts +2 -1
- package/dist/src/lib/azure/services/api-management/main.js +15 -5
- package/dist/src/lib/azure/services/api-management/types.d.ts +4 -0
- package/dist/src/lib/azure/services/index.d.ts +1 -0
- package/dist/src/lib/azure/services/index.js +1 -0
- package/dist/src/lib/azure/services/redis/index.d.ts +2 -0
- package/dist/src/lib/azure/services/redis/index.js +18 -0
- package/dist/src/lib/azure/services/redis/main.d.ts +30 -0
- package/dist/src/lib/azure/services/redis/main.js +57 -0
- package/dist/src/lib/azure/services/redis/types.d.ts +3 -0
- package/dist/src/lib/azure/services/redis/types.js +2 -0
- package/package.json +1 -1
- package/src/lib/azure/common/construct.ts +3 -0
- package/src/lib/azure/services/api-management/main.ts +19 -5
- package/src/lib/azure/services/api-management/types.ts +4 -0
- package/src/lib/azure/services/index.ts +1 -0
- package/src/lib/azure/services/redis/index.ts +2 -0
- package/src/lib/azure/services/redis/main.ts +59 -0
- package/src/lib/azure/services/redis/types.ts +3 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { TerraformStack } from 'cdktf';
|
|
2
2
|
import { Construct } from 'constructs';
|
|
3
|
-
import { AzureStorageManager, AzureKeyVaultManager, AzureApiManagementManager, AzureFunctionManager, AzureResourceGroupManager, AzureAppServiceManager, AzureApplicationInsightsManager, AzureAppConfigurationManager, AzureCosmosDbManager, AzureServicebusManager, AzureEventgridManager, AzureDnsManager, AzureLogAnalyticsWorkspaceManager, AzureMonitorManager } from '../services';
|
|
3
|
+
import { AzureStorageManager, AzureKeyVaultManager, AzureApiManagementManager, AzureFunctionManager, AzureResourceGroupManager, AzureAppServiceManager, AzureApplicationInsightsManager, AzureAppConfigurationManager, AzureCosmosDbManager, AzureServicebusManager, AzureEventgridManager, AzureDnsManager, AzureLogAnalyticsWorkspaceManager, AzureMonitorManager, AzureRedisManager } from '../services';
|
|
4
4
|
import { CommonAzureStackProps } from './types';
|
|
5
5
|
import { AzureResourceNameFormatter } from './resource-name-formatter';
|
|
6
6
|
export declare class CommonAzureConstruct extends TerraformStack {
|
|
@@ -18,6 +18,7 @@ export declare class CommonAzureConstruct extends TerraformStack {
|
|
|
18
18
|
keyVaultManager: AzureKeyVaultManager;
|
|
19
19
|
logAnalyticsWorkspaceManager: AzureLogAnalyticsWorkspaceManager;
|
|
20
20
|
monitorManager: AzureMonitorManager;
|
|
21
|
+
redisManager: AzureRedisManager;
|
|
21
22
|
resourceGroupManager: AzureResourceGroupManager;
|
|
22
23
|
resourceNameFormatter: AzureResourceNameFormatter;
|
|
23
24
|
servicebusManager: AzureServicebusManager;
|
|
@@ -24,6 +24,7 @@ class CommonAzureConstruct extends cdktf_1.TerraformStack {
|
|
|
24
24
|
keyVaultManager;
|
|
25
25
|
logAnalyticsWorkspaceManager;
|
|
26
26
|
monitorManager;
|
|
27
|
+
redisManager;
|
|
27
28
|
resourceGroupManager;
|
|
28
29
|
resourceNameFormatter;
|
|
29
30
|
servicebusManager;
|
|
@@ -44,6 +45,7 @@ class CommonAzureConstruct extends cdktf_1.TerraformStack {
|
|
|
44
45
|
this.keyVaultManager = new services_1.AzureKeyVaultManager();
|
|
45
46
|
this.logAnalyticsWorkspaceManager = new services_1.AzureLogAnalyticsWorkspaceManager();
|
|
46
47
|
this.monitorManager = new services_1.AzureMonitorManager();
|
|
48
|
+
this.redisManager = new services_1.AzureRedisManager();
|
|
47
49
|
this.resourceGroupManager = new services_1.AzureResourceGroupManager();
|
|
48
50
|
this.resourceNameFormatter = new resource_name_formatter_1.AzureResourceNameFormatter(this, `${id}-rnf`, props);
|
|
49
51
|
this.servicebusManager = new services_1.AzureServicebusManager();
|
|
@@ -3,6 +3,7 @@ import { ApiManagementCustomDomain } from '@cdktf/provider-azurerm/lib/api-manag
|
|
|
3
3
|
import { ApiManagement } from '@cdktf/provider-azurerm/lib/api-management';
|
|
4
4
|
import { ApiManagementApi } from '@cdktf/provider-azurerm/lib/api-management-api';
|
|
5
5
|
import { ApiManagementLoggerApplicationInsights } from '@cdktf/provider-azurerm/lib/api-management-logger';
|
|
6
|
+
import { RedisCache } from '@cdktf/provider-azurerm/lib/redis-cache';
|
|
6
7
|
import { Resource } from '../../.gen/providers/azapi/resource';
|
|
7
8
|
import { CommonAzureConstruct } from '../../common';
|
|
8
9
|
import { ApiManagementProps, ApiManagementBackendProps, ApiManagementApiProps, ApiManagementCustomDomainProps } from './types';
|
|
@@ -31,7 +32,7 @@ export declare class AzureApiManagementManager {
|
|
|
31
32
|
* @param props api management properties
|
|
32
33
|
* @see [CDKTF Api management Module]{@link https://github.com/cdktf/cdktf-provider-azurerm/blob/main/docs/apiManagement.typescript.md}
|
|
33
34
|
*/
|
|
34
|
-
createApiManagement(id: string, scope: CommonAzureConstruct, props: ApiManagementProps, applicationInsightsKey?: ApiManagementLoggerApplicationInsights['instrumentationKey']): ApiManagement;
|
|
35
|
+
createApiManagement(id: string, scope: CommonAzureConstruct, props: ApiManagementProps, applicationInsightsKey?: ApiManagementLoggerApplicationInsights['instrumentationKey'], externalRedisCache?: RedisCache): ApiManagement;
|
|
35
36
|
/**
|
|
36
37
|
* @summary Method to resolve an api management
|
|
37
38
|
* @param id scoped id of the resource
|
|
@@ -12,6 +12,7 @@ const api_management_api_1 = require("@cdktf/provider-azurerm/lib/api-management
|
|
|
12
12
|
const api_management_api_operation_1 = require("@cdktf/provider-azurerm/lib/api-management-api-operation");
|
|
13
13
|
const api_management_api_operation_policy_1 = require("@cdktf/provider-azurerm/lib/api-management-api-operation-policy");
|
|
14
14
|
const api_management_logger_1 = require("@cdktf/provider-azurerm/lib/api-management-logger");
|
|
15
|
+
const api_management_redis_cache_1 = require("@cdktf/provider-azurerm/lib/api-management-redis-cache");
|
|
15
16
|
const resource_1 = require("../../.gen/providers/azapi/resource");
|
|
16
17
|
const utils_1 = require("../../utils");
|
|
17
18
|
const lodash_1 = __importDefault(require("lodash"));
|
|
@@ -40,7 +41,7 @@ class AzureApiManagementManager {
|
|
|
40
41
|
* @param props api management properties
|
|
41
42
|
* @see [CDKTF Api management Module]{@link https://github.com/cdktf/cdktf-provider-azurerm/blob/main/docs/apiManagement.typescript.md}
|
|
42
43
|
*/
|
|
43
|
-
createApiManagement(id, scope, props, applicationInsightsKey) {
|
|
44
|
+
createApiManagement(id, scope, props, applicationInsightsKey, externalRedisCache) {
|
|
44
45
|
if (!props)
|
|
45
46
|
throw `Props undefined for ${id}`;
|
|
46
47
|
const resourceGroup = new data_azurerm_resource_group_1.DataAzurermResourceGroup(scope, `${id}-am-rg`, {
|
|
@@ -68,6 +69,15 @@ class AzureApiManagementManager {
|
|
|
68
69
|
},
|
|
69
70
|
});
|
|
70
71
|
}
|
|
72
|
+
if (externalRedisCache) {
|
|
73
|
+
new api_management_redis_cache_1.ApiManagementRedisCache(scope, `${id}-am-redis-cache`, {
|
|
74
|
+
name: scope.resourceNameFormatter.format(props.name, scope.props.resourceNameOptions?.apiManagementRedisCache),
|
|
75
|
+
apiManagementId: apiManagement.id,
|
|
76
|
+
connectionString: externalRedisCache.primaryConnectionString,
|
|
77
|
+
cacheLocation: externalRedisCache.location,
|
|
78
|
+
redisCacheId: externalRedisCache.id,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
71
81
|
(0, utils_1.createAzureTfOutput)(`${id}-apiManagementName`, scope, apiManagement.name);
|
|
72
82
|
(0, utils_1.createAzureTfOutput)(`${id}-apiManagementFriendlyUniqueId`, scope, apiManagement.friendlyUniqueId);
|
|
73
83
|
(0, utils_1.createAzureTfOutput)(`${id}-apiManagementId`, scope, apiManagement.id);
|
|
@@ -205,7 +215,7 @@ class AzureApiManagementManager {
|
|
|
205
215
|
cacheSetInboundPolicy = `<choose>
|
|
206
216
|
<when condition="@((string)context.Variables["bypassCache"] != "true")">
|
|
207
217
|
<!-- Attempt to retrieve cached response -->
|
|
208
|
-
<cache-lookup-value key="@((string)context.Variables["customCacheKey"])" variable-name="cachedResponse" />
|
|
218
|
+
<cache-lookup-value key="@((string)context.Variables["customCacheKey"])" variable-name="cachedResponse" caching-type="${operation.caching.cachingType || 'prefer-external'}" />
|
|
209
219
|
|
|
210
220
|
<!-- If cache hit, return cached response -->
|
|
211
221
|
<choose>
|
|
@@ -227,13 +237,13 @@ class AzureApiManagementManager {
|
|
|
227
237
|
</choose>
|
|
228
238
|
</when>
|
|
229
239
|
<when condition="@((string)context.Variables["bypassCache"] == "true")">
|
|
230
|
-
<cache-remove-value key="@((string)context.Variables["customCacheKey"])" />
|
|
240
|
+
<cache-remove-value key="@((string)context.Variables["customCacheKey"])" caching-type="${operation.caching.cachingType || 'prefer-external'}" />
|
|
231
241
|
</when>
|
|
232
242
|
</choose>`;
|
|
233
243
|
cacheSetOutboundPolicy = `<!-- Store the response body in cache -->
|
|
234
244
|
<choose>
|
|
235
245
|
<when condition="@(context.Response.StatusCode == 200)">
|
|
236
|
-
<cache-store-value key="@((string)context.Variables["customCacheKey"])" value="@(context.Response.Body.As<string>(preserveContent: true))" duration="${operation.caching.ttlInSecs ?? 900}" />
|
|
246
|
+
<cache-store-value key="@((string)context.Variables["customCacheKey"])" value="@(context.Response.Body.As<string>(preserveContent: true))" duration="${operation.caching.ttlInSecs ?? 900}" caching-type="${operation.caching.cachingType || 'prefer-external'}" />
|
|
237
247
|
<!-- Add cache status header -->
|
|
238
248
|
<set-header name="X-Apim-Cache-Status" exists-action="override">
|
|
239
249
|
<value>MISS</value>
|
|
@@ -253,7 +263,7 @@ class AzureApiManagementManager {
|
|
|
253
263
|
<!-- Allow admin to clear specific cache entries -->
|
|
254
264
|
<choose>
|
|
255
265
|
<when condition="@((string)context.Variables["clearCache"] == "true")">
|
|
256
|
-
<cache-remove-value key="@((string)context.Variables["customCacheKey"])" />
|
|
266
|
+
<cache-remove-value key="@((string)context.Variables["customCacheKey"])" caching-type="${operation.caching.cachingType || 'prefer-external'}" />
|
|
257
267
|
<return-response>
|
|
258
268
|
<set-status code="200" reason="OK" />
|
|
259
269
|
<set-body>Cache entry removed successfully</set-body>
|
|
@@ -3,6 +3,7 @@ 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 { ApiManagementRedisCacheConfig } from '@cdktf/provider-azurerm/lib/api-management-redis-cache';
|
|
6
7
|
export interface ApiManagementProps extends ApiManagementConfig {
|
|
7
8
|
}
|
|
8
9
|
export interface ApiManagementBackendProps extends ApiManagementBackendConfig {
|
|
@@ -25,8 +26,11 @@ export interface ApiManagementApiCaching {
|
|
|
25
26
|
enableCacheSet?: boolean;
|
|
26
27
|
enableCacheInvalidation?: boolean;
|
|
27
28
|
ttlInSecs?: number;
|
|
29
|
+
cachingType?: string;
|
|
28
30
|
}
|
|
29
31
|
export interface ApiManagementApiRateLimit {
|
|
30
32
|
calls: number;
|
|
31
33
|
renewalPeriodInSecs: number;
|
|
32
34
|
}
|
|
35
|
+
export interface ApiManagementRedisCacheProps extends ApiManagementRedisCacheConfig {
|
|
36
|
+
}
|
|
@@ -25,6 +25,7 @@ __exportStar(require("./function"), exports);
|
|
|
25
25
|
__exportStar(require("./key-vault"), exports);
|
|
26
26
|
__exportStar(require("./log-analytics-workspace"), exports);
|
|
27
27
|
__exportStar(require("./monitor"), exports);
|
|
28
|
+
__exportStar(require("./redis"), exports);
|
|
28
29
|
__exportStar(require("./resource-group"), exports);
|
|
29
30
|
__exportStar(require("./servicebus"), exports);
|
|
30
31
|
__exportStar(require("./storage"), exports);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./main"), exports);
|
|
18
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { RedisCache } from '@cdktf/provider-azurerm/lib/redis-cache';
|
|
2
|
+
import { CommonAzureConstruct } from '../../common';
|
|
3
|
+
import { RedisCacheProps } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* @classdesc Provides operations on Azure Redis
|
|
6
|
+
* - A new instance of this class is injected into {@link CommonAzureConstruct} constructor.
|
|
7
|
+
* - If a custom construct extends {@link CommonAzureConstruct}, an instance is available within the context.
|
|
8
|
+
* @example
|
|
9
|
+
* ```
|
|
10
|
+
* import { CommonAzureConstruct, CommonAzureStackProps } from '@gradientedge/cdk-utils'
|
|
11
|
+
*
|
|
12
|
+
* class CustomConstruct extends CommonAzureConstruct {
|
|
13
|
+
* constructor(parent: Construct, id: string, props: CommonAzureStackProps) {
|
|
14
|
+
* super(parent, id, props)
|
|
15
|
+
* this.props = props
|
|
16
|
+
* this.redisManager.createRedis('MyRedisCache', this, props)
|
|
17
|
+
* }
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare class AzureRedisManager {
|
|
22
|
+
/**
|
|
23
|
+
* @summary Method to create a new redis cache
|
|
24
|
+
* @param id scoped id of the resource
|
|
25
|
+
* @param scope scope in which this resource is defined
|
|
26
|
+
* @param props redis cache properties
|
|
27
|
+
* @see [CDKTF Redis Cache Module]{@link https://github.com/cdktf/cdktf-provider-azurerm/blob/main/docs/redisCache.typescript.md}
|
|
28
|
+
*/
|
|
29
|
+
createRedisCache(id: string, scope: CommonAzureConstruct, props: RedisCacheProps): RedisCache;
|
|
30
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AzureRedisManager = void 0;
|
|
4
|
+
const data_azurerm_resource_group_1 = require("@cdktf/provider-azurerm/lib/data-azurerm-resource-group");
|
|
5
|
+
const redis_cache_1 = require("@cdktf/provider-azurerm/lib/redis-cache");
|
|
6
|
+
const utils_1 = require("../../utils");
|
|
7
|
+
/**
|
|
8
|
+
* @classdesc Provides operations on Azure Redis
|
|
9
|
+
* - A new instance of this class is injected into {@link CommonAzureConstruct} constructor.
|
|
10
|
+
* - If a custom construct extends {@link CommonAzureConstruct}, an instance is available within the context.
|
|
11
|
+
* @example
|
|
12
|
+
* ```
|
|
13
|
+
* import { CommonAzureConstruct, CommonAzureStackProps } from '@gradientedge/cdk-utils'
|
|
14
|
+
*
|
|
15
|
+
* class CustomConstruct extends CommonAzureConstruct {
|
|
16
|
+
* constructor(parent: Construct, id: string, props: CommonAzureStackProps) {
|
|
17
|
+
* super(parent, id, props)
|
|
18
|
+
* this.props = props
|
|
19
|
+
* this.redisManager.createRedis('MyRedisCache', this, props)
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
class AzureRedisManager {
|
|
25
|
+
/**
|
|
26
|
+
* @summary Method to create a new redis cache
|
|
27
|
+
* @param id scoped id of the resource
|
|
28
|
+
* @param scope scope in which this resource is defined
|
|
29
|
+
* @param props redis cache properties
|
|
30
|
+
* @see [CDKTF Redis Cache Module]{@link https://github.com/cdktf/cdktf-provider-azurerm/blob/main/docs/redisCache.typescript.md}
|
|
31
|
+
*/
|
|
32
|
+
createRedisCache(id, scope, props) {
|
|
33
|
+
if (!props)
|
|
34
|
+
throw `Props undefined for ${id}`;
|
|
35
|
+
const resourceGroup = new data_azurerm_resource_group_1.DataAzurermResourceGroup(scope, `${id}-rc-rg`, {
|
|
36
|
+
name: scope.props.resourceGroupName
|
|
37
|
+
? scope.resourceNameFormatter.format(scope.props.resourceGroupName)
|
|
38
|
+
: `${props.resourceGroupName}`,
|
|
39
|
+
});
|
|
40
|
+
if (!resourceGroup)
|
|
41
|
+
throw `Resource group undefined for ${id}`;
|
|
42
|
+
const redisCache = new redis_cache_1.RedisCache(scope, `${id}-rc`, {
|
|
43
|
+
...props,
|
|
44
|
+
name: scope.resourceNameFormatter.format(props.name, scope.props.resourceNameOptions?.redisCache),
|
|
45
|
+
location: resourceGroup.location,
|
|
46
|
+
resourceGroupName: resourceGroup.name,
|
|
47
|
+
tags: props.tags ?? {
|
|
48
|
+
environment: scope.props.stage,
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
(0, utils_1.createAzureTfOutput)(`${id}-redisCacheName`, scope, redisCache.name);
|
|
52
|
+
(0, utils_1.createAzureTfOutput)(`${id}-redisCacheFriendlyUniqueId`, scope, redisCache.friendlyUniqueId);
|
|
53
|
+
(0, utils_1.createAzureTfOutput)(`${id}-redisCacheId`, scope, redisCache.id);
|
|
54
|
+
return redisCache;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.AzureRedisManager = AzureRedisManager;
|
package/package.json
CHANGED
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
AzureDnsManager,
|
|
21
21
|
AzureLogAnalyticsWorkspaceManager,
|
|
22
22
|
AzureMonitorManager,
|
|
23
|
+
AzureRedisManager,
|
|
23
24
|
} from '../services'
|
|
24
25
|
import { CommonAzureStackProps } from './types'
|
|
25
26
|
import { AzureRemoteBackend } from './constants'
|
|
@@ -40,6 +41,7 @@ export class CommonAzureConstruct extends TerraformStack {
|
|
|
40
41
|
keyVaultManager: AzureKeyVaultManager
|
|
41
42
|
logAnalyticsWorkspaceManager: AzureLogAnalyticsWorkspaceManager
|
|
42
43
|
monitorManager: AzureMonitorManager
|
|
44
|
+
redisManager: AzureRedisManager
|
|
43
45
|
resourceGroupManager: AzureResourceGroupManager
|
|
44
46
|
resourceNameFormatter: AzureResourceNameFormatter
|
|
45
47
|
servicebusManager: AzureServicebusManager
|
|
@@ -62,6 +64,7 @@ export class CommonAzureConstruct extends TerraformStack {
|
|
|
62
64
|
this.keyVaultManager = new AzureKeyVaultManager()
|
|
63
65
|
this.logAnalyticsWorkspaceManager = new AzureLogAnalyticsWorkspaceManager()
|
|
64
66
|
this.monitorManager = new AzureMonitorManager()
|
|
67
|
+
this.redisManager = new AzureRedisManager()
|
|
65
68
|
this.resourceGroupManager = new AzureResourceGroupManager()
|
|
66
69
|
this.resourceNameFormatter = new AzureResourceNameFormatter(this, `${id}-rnf`, props)
|
|
67
70
|
this.servicebusManager = new AzureServicebusManager()
|
|
@@ -12,6 +12,8 @@ import {
|
|
|
12
12
|
ApiManagementLogger,
|
|
13
13
|
ApiManagementLoggerApplicationInsights,
|
|
14
14
|
} from '@cdktf/provider-azurerm/lib/api-management-logger'
|
|
15
|
+
import { ApiManagementRedisCache } from '@cdktf/provider-azurerm/lib/api-management-redis-cache'
|
|
16
|
+
import { RedisCache } from '@cdktf/provider-azurerm/lib/redis-cache'
|
|
15
17
|
import { Resource } from '../../.gen/providers/azapi/resource'
|
|
16
18
|
import { CommonAzureConstruct } from '../../common'
|
|
17
19
|
import { createAzureTfOutput } from '../../utils'
|
|
@@ -20,6 +22,7 @@ import {
|
|
|
20
22
|
ApiManagementBackendProps,
|
|
21
23
|
ApiManagementApiProps,
|
|
22
24
|
ApiManagementCustomDomainProps,
|
|
25
|
+
ApiManagementRedisCacheProps,
|
|
23
26
|
} from './types'
|
|
24
27
|
import _ from 'lodash'
|
|
25
28
|
|
|
@@ -52,7 +55,8 @@ export class AzureApiManagementManager {
|
|
|
52
55
|
id: string,
|
|
53
56
|
scope: CommonAzureConstruct,
|
|
54
57
|
props: ApiManagementProps,
|
|
55
|
-
applicationInsightsKey?: ApiManagementLoggerApplicationInsights['instrumentationKey']
|
|
58
|
+
applicationInsightsKey?: ApiManagementLoggerApplicationInsights['instrumentationKey'],
|
|
59
|
+
externalRedisCache?: RedisCache
|
|
56
60
|
) {
|
|
57
61
|
if (!props) throw `Props undefined for ${id}`
|
|
58
62
|
|
|
@@ -84,6 +88,16 @@ export class AzureApiManagementManager {
|
|
|
84
88
|
})
|
|
85
89
|
}
|
|
86
90
|
|
|
91
|
+
if (externalRedisCache) {
|
|
92
|
+
new ApiManagementRedisCache(scope, `${id}-am-redis-cache`, {
|
|
93
|
+
name: scope.resourceNameFormatter.format(props.name, scope.props.resourceNameOptions?.apiManagementRedisCache),
|
|
94
|
+
apiManagementId: apiManagement.id,
|
|
95
|
+
connectionString: externalRedisCache.primaryConnectionString,
|
|
96
|
+
cacheLocation: externalRedisCache.location,
|
|
97
|
+
redisCacheId: externalRedisCache.id,
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
|
|
87
101
|
createAzureTfOutput(`${id}-apiManagementName`, scope, apiManagement.name)
|
|
88
102
|
createAzureTfOutput(`${id}-apiManagementFriendlyUniqueId`, scope, apiManagement.friendlyUniqueId)
|
|
89
103
|
createAzureTfOutput(`${id}-apiManagementId`, scope, apiManagement.id)
|
|
@@ -252,7 +266,7 @@ export class AzureApiManagementManager {
|
|
|
252
266
|
cacheSetInboundPolicy = `<choose>
|
|
253
267
|
<when condition="@((string)context.Variables["bypassCache"] != "true")">
|
|
254
268
|
<!-- Attempt to retrieve cached response -->
|
|
255
|
-
<cache-lookup-value key="@((string)context.Variables["customCacheKey"])" variable-name="cachedResponse" />
|
|
269
|
+
<cache-lookup-value key="@((string)context.Variables["customCacheKey"])" variable-name="cachedResponse" caching-type="${operation.caching.cachingType || 'prefer-external'}" />
|
|
256
270
|
|
|
257
271
|
<!-- If cache hit, return cached response -->
|
|
258
272
|
<choose>
|
|
@@ -274,13 +288,13 @@ export class AzureApiManagementManager {
|
|
|
274
288
|
</choose>
|
|
275
289
|
</when>
|
|
276
290
|
<when condition="@((string)context.Variables["bypassCache"] == "true")">
|
|
277
|
-
<cache-remove-value key="@((string)context.Variables["customCacheKey"])" />
|
|
291
|
+
<cache-remove-value key="@((string)context.Variables["customCacheKey"])" caching-type="${operation.caching.cachingType || 'prefer-external'}" />
|
|
278
292
|
</when>
|
|
279
293
|
</choose>`
|
|
280
294
|
cacheSetOutboundPolicy = `<!-- Store the response body in cache -->
|
|
281
295
|
<choose>
|
|
282
296
|
<when condition="@(context.Response.StatusCode == 200)">
|
|
283
|
-
<cache-store-value key="@((string)context.Variables["customCacheKey"])" value="@(context.Response.Body.As<string>(preserveContent: true))" duration="${operation.caching.ttlInSecs ?? 900}" />
|
|
297
|
+
<cache-store-value key="@((string)context.Variables["customCacheKey"])" value="@(context.Response.Body.As<string>(preserveContent: true))" duration="${operation.caching.ttlInSecs ?? 900}" caching-type="${operation.caching.cachingType || 'prefer-external'}" />
|
|
284
298
|
<!-- Add cache status header -->
|
|
285
299
|
<set-header name="X-Apim-Cache-Status" exists-action="override">
|
|
286
300
|
<value>MISS</value>
|
|
@@ -301,7 +315,7 @@ export class AzureApiManagementManager {
|
|
|
301
315
|
<!-- Allow admin to clear specific cache entries -->
|
|
302
316
|
<choose>
|
|
303
317
|
<when condition="@((string)context.Variables["clearCache"] == "true")">
|
|
304
|
-
<cache-remove-value key="@((string)context.Variables["customCacheKey"])" />
|
|
318
|
+
<cache-remove-value key="@((string)context.Variables["customCacheKey"])" caching-type="${operation.caching.cachingType || 'prefer-external'}" />
|
|
305
319
|
<return-response>
|
|
306
320
|
<set-status code="200" reason="OK" />
|
|
307
321
|
<set-body>Cache entry removed successfully</set-body>
|
|
@@ -3,6 +3,7 @@ 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 { ApiManagementRedisCacheConfig } from '@cdktf/provider-azurerm/lib/api-management-redis-cache'
|
|
6
7
|
|
|
7
8
|
export interface ApiManagementProps extends ApiManagementConfig {}
|
|
8
9
|
|
|
@@ -29,9 +30,12 @@ export interface ApiManagementApiCaching {
|
|
|
29
30
|
enableCacheSet?: boolean
|
|
30
31
|
enableCacheInvalidation?: boolean
|
|
31
32
|
ttlInSecs?: number
|
|
33
|
+
cachingType?: string
|
|
32
34
|
}
|
|
33
35
|
|
|
34
36
|
export interface ApiManagementApiRateLimit {
|
|
35
37
|
calls: number
|
|
36
38
|
renewalPeriodInSecs: number
|
|
37
39
|
}
|
|
40
|
+
|
|
41
|
+
export interface ApiManagementRedisCacheProps extends ApiManagementRedisCacheConfig {}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { DataAzurermResourceGroup } from '@cdktf/provider-azurerm/lib/data-azurerm-resource-group'
|
|
2
|
+
import { RedisCache } from '@cdktf/provider-azurerm/lib/redis-cache'
|
|
3
|
+
import { CommonAzureConstruct } from '../../common'
|
|
4
|
+
import { createAzureTfOutput } from '../../utils'
|
|
5
|
+
import { RedisCacheProps } from './types'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @classdesc Provides operations on Azure Redis
|
|
9
|
+
* - A new instance of this class is injected into {@link CommonAzureConstruct} constructor.
|
|
10
|
+
* - If a custom construct extends {@link CommonAzureConstruct}, an instance is available within the context.
|
|
11
|
+
* @example
|
|
12
|
+
* ```
|
|
13
|
+
* import { CommonAzureConstruct, CommonAzureStackProps } from '@gradientedge/cdk-utils'
|
|
14
|
+
*
|
|
15
|
+
* class CustomConstruct extends CommonAzureConstruct {
|
|
16
|
+
* constructor(parent: Construct, id: string, props: CommonAzureStackProps) {
|
|
17
|
+
* super(parent, id, props)
|
|
18
|
+
* this.props = props
|
|
19
|
+
* this.redisManager.createRedis('MyRedisCache', this, props)
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export class AzureRedisManager {
|
|
25
|
+
/**
|
|
26
|
+
* @summary Method to create a new redis cache
|
|
27
|
+
* @param id scoped id of the resource
|
|
28
|
+
* @param scope scope in which this resource is defined
|
|
29
|
+
* @param props redis cache properties
|
|
30
|
+
* @see [CDKTF Redis Cache Module]{@link https://github.com/cdktf/cdktf-provider-azurerm/blob/main/docs/redisCache.typescript.md}
|
|
31
|
+
*/
|
|
32
|
+
public createRedisCache(id: string, scope: CommonAzureConstruct, props: RedisCacheProps) {
|
|
33
|
+
if (!props) throw `Props undefined for ${id}`
|
|
34
|
+
|
|
35
|
+
const resourceGroup = new DataAzurermResourceGroup(scope, `${id}-rc-rg`, {
|
|
36
|
+
name: scope.props.resourceGroupName
|
|
37
|
+
? scope.resourceNameFormatter.format(scope.props.resourceGroupName)
|
|
38
|
+
: `${props.resourceGroupName}`,
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
if (!resourceGroup) throw `Resource group undefined for ${id}`
|
|
42
|
+
|
|
43
|
+
const redisCache = new RedisCache(scope, `${id}-rc`, {
|
|
44
|
+
...props,
|
|
45
|
+
name: scope.resourceNameFormatter.format(props.name, scope.props.resourceNameOptions?.redisCache),
|
|
46
|
+
location: resourceGroup.location,
|
|
47
|
+
resourceGroupName: resourceGroup.name,
|
|
48
|
+
tags: props.tags ?? {
|
|
49
|
+
environment: scope.props.stage,
|
|
50
|
+
},
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
createAzureTfOutput(`${id}-redisCacheName`, scope, redisCache.name)
|
|
54
|
+
createAzureTfOutput(`${id}-redisCacheFriendlyUniqueId`, scope, redisCache.friendlyUniqueId)
|
|
55
|
+
createAzureTfOutput(`${id}-redisCacheId`, scope, redisCache.id)
|
|
56
|
+
|
|
57
|
+
return redisCache
|
|
58
|
+
}
|
|
59
|
+
}
|