@gradientedge/cdk-utils 10.6.0 → 10.8.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.
Files changed (200) hide show
  1. package/dist/app/api-destined-function/src/lib/lambda.d.ts +1 -0
  2. package/dist/app/api-destined-function/src/lib/lambda.d.ts.map +1 -0
  3. package/dist/app/api-destined-function/src/lib/lambda.js +1 -0
  4. package/dist/app/api-destined-function/src/lib/lambda.js.map +1 -0
  5. package/dist/app/api-destined-function/tsconfig.tsbuildinfo +1 -0
  6. package/dist/src/lib/aws/services/cloudwatch/main.d.ts +11 -11
  7. package/dist/src/lib/azure/common/constants.d.ts +74 -0
  8. package/dist/src/lib/azure/common/constants.js +77 -0
  9. package/dist/src/lib/azure/common/construct.d.ts +15 -3
  10. package/dist/src/lib/azure/common/construct.js +40 -4
  11. package/dist/src/lib/azure/common/stack.d.ts +3 -2
  12. package/dist/src/lib/azure/common/stack.js +25 -42
  13. package/dist/src/lib/azure/common/types.d.ts +13 -2
  14. package/dist/src/lib/azure/construct/event-handler/index.d.ts +2 -0
  15. package/dist/src/lib/azure/construct/event-handler/index.js +2 -0
  16. package/dist/src/lib/azure/construct/event-handler/main.d.ts +20 -0
  17. package/dist/src/lib/azure/construct/event-handler/main.js +139 -0
  18. package/dist/src/lib/azure/construct/event-handler/types.d.ts +35 -0
  19. package/dist/src/lib/azure/construct/event-handler/types.js +1 -0
  20. package/dist/src/lib/azure/construct/function-app/index.d.ts +2 -0
  21. package/dist/src/lib/azure/construct/function-app/index.js +2 -0
  22. package/dist/src/lib/azure/construct/function-app/main.d.ts +51 -0
  23. package/dist/src/lib/azure/construct/function-app/main.js +297 -0
  24. package/dist/src/lib/azure/construct/function-app/types.d.ts +33 -0
  25. package/dist/src/lib/azure/construct/function-app/types.js +1 -0
  26. package/dist/src/lib/azure/construct/index.d.ts +6 -0
  27. package/dist/src/lib/azure/construct/index.js +6 -0
  28. package/dist/src/lib/azure/construct/rest-api/index.d.ts +2 -0
  29. package/dist/src/lib/azure/construct/rest-api/index.js +2 -0
  30. package/dist/src/lib/azure/construct/rest-api/main.d.ts +20 -0
  31. package/dist/src/lib/azure/construct/rest-api/main.js +172 -0
  32. package/dist/src/lib/azure/construct/rest-api/types.d.ts +25 -0
  33. package/dist/src/lib/azure/construct/rest-api/types.js +1 -0
  34. package/dist/src/lib/azure/construct/rest-api-function/index.d.ts +2 -0
  35. package/dist/src/lib/azure/construct/rest-api-function/index.js +2 -0
  36. package/dist/src/lib/azure/construct/rest-api-function/main.d.ts +19 -0
  37. package/dist/src/lib/azure/construct/rest-api-function/main.js +255 -0
  38. package/dist/src/lib/azure/construct/rest-api-function/types.d.ts +29 -0
  39. package/dist/src/lib/azure/construct/rest-api-function/types.js +1 -0
  40. package/dist/src/lib/azure/construct/rest-api-with-cache/index.d.ts +2 -0
  41. package/dist/src/lib/azure/construct/rest-api-with-cache/index.js +2 -0
  42. package/dist/src/lib/azure/construct/rest-api-with-cache/main.d.ts +12 -0
  43. package/dist/src/lib/azure/construct/rest-api-with-cache/main.js +56 -0
  44. package/dist/src/lib/azure/construct/rest-api-with-cache/types.d.ts +13 -0
  45. package/dist/src/lib/azure/construct/rest-api-with-cache/types.js +1 -0
  46. package/dist/src/lib/azure/construct/site-with-webapp/index.d.ts +2 -0
  47. package/dist/src/lib/azure/construct/site-with-webapp/index.js +2 -0
  48. package/dist/src/lib/azure/construct/site-with-webapp/main.d.ts +19 -0
  49. package/dist/src/lib/azure/construct/site-with-webapp/main.js +135 -0
  50. package/dist/src/lib/azure/construct/site-with-webapp/types.d.ts +30 -0
  51. package/dist/src/lib/azure/construct/site-with-webapp/types.js +1 -0
  52. package/dist/src/lib/azure/index.d.ts +1 -0
  53. package/dist/src/lib/azure/index.js +1 -0
  54. package/dist/src/lib/azure/services/api-management/main.d.ts +83 -5
  55. package/dist/src/lib/azure/services/api-management/main.js +114 -148
  56. package/dist/src/lib/azure/services/api-management/types.d.ts +22 -1
  57. package/dist/src/lib/azure/services/app-configuration/main.d.ts +13 -1
  58. package/dist/src/lib/azure/services/app-configuration/main.js +21 -2
  59. package/dist/src/lib/azure/services/app-service/main.d.ts +5 -2
  60. package/dist/src/lib/azure/services/app-service/main.js +12 -4
  61. package/dist/src/lib/azure/services/application-insights/main.d.ts +15 -4
  62. package/dist/src/lib/azure/services/application-insights/main.js +27 -6
  63. package/dist/src/lib/azure/services/application-insights/types.d.ts +4 -1
  64. package/dist/src/lib/azure/services/authorisation/constants.d.ts +13 -0
  65. package/dist/src/lib/azure/services/authorisation/constants.js +14 -0
  66. package/dist/src/lib/azure/services/authorisation/index.d.ts +3 -0
  67. package/dist/src/lib/azure/services/authorisation/index.js +3 -0
  68. package/dist/src/lib/azure/services/authorisation/main.d.ts +84 -0
  69. package/dist/src/lib/azure/services/authorisation/main.js +120 -0
  70. package/dist/src/lib/azure/services/authorisation/types.d.ts +3 -0
  71. package/dist/src/lib/azure/services/authorisation/types.js +1 -0
  72. package/dist/src/lib/azure/services/cosmosdb/constants.d.ts +8 -0
  73. package/dist/src/lib/azure/services/cosmosdb/constants.js +10 -0
  74. package/dist/src/lib/azure/services/cosmosdb/index.d.ts +1 -0
  75. package/dist/src/lib/azure/services/cosmosdb/index.js +1 -0
  76. package/dist/src/lib/azure/services/cosmosdb/main.d.ts +47 -5
  77. package/dist/src/lib/azure/services/cosmosdb/main.js +77 -8
  78. package/dist/src/lib/azure/services/cosmosdb/types.d.ts +3 -1
  79. package/dist/src/lib/azure/services/dns/main.d.ts +9 -4
  80. package/dist/src/lib/azure/services/dns/main.js +12 -8
  81. package/dist/src/lib/azure/services/eventgrid/main.d.ts +12 -7
  82. package/dist/src/lib/azure/services/eventgrid/main.js +18 -19
  83. package/dist/src/lib/azure/services/function/main.d.ts +16 -3
  84. package/dist/src/lib/azure/services/function/main.js +122 -8
  85. package/dist/src/lib/azure/services/function/types.d.ts +3 -4
  86. package/dist/src/lib/azure/services/index.d.ts +3 -0
  87. package/dist/src/lib/azure/services/index.js +3 -0
  88. package/dist/src/lib/azure/services/key-vault/main.d.ts +22 -2
  89. package/dist/src/lib/azure/services/key-vault/main.js +28 -3
  90. package/dist/src/lib/azure/services/key-vault/types.d.ts +3 -2
  91. package/dist/src/lib/azure/services/monitor/main.d.ts +3 -1
  92. package/dist/src/lib/azure/services/monitor/main.js +3 -2
  93. package/dist/src/lib/azure/services/operational-insights/main.d.ts +13 -2
  94. package/dist/src/lib/azure/services/operational-insights/main.js +17 -3
  95. package/dist/src/lib/azure/services/operational-insights/types.d.ts +3 -1
  96. package/dist/src/lib/azure/services/portal/error.d.ts +5 -0
  97. package/dist/src/lib/azure/services/portal/error.js +10 -0
  98. package/dist/src/lib/azure/services/portal/index.d.ts +4 -0
  99. package/dist/src/lib/azure/services/portal/index.js +4 -0
  100. package/dist/src/lib/azure/services/portal/main.d.ts +33 -0
  101. package/dist/src/lib/azure/services/portal/main.js +51 -0
  102. package/dist/src/lib/azure/services/portal/renderer.d.ts +11 -0
  103. package/dist/src/lib/azure/services/portal/renderer.js +156 -0
  104. package/dist/src/lib/azure/services/portal/types.d.ts +40 -0
  105. package/dist/src/lib/azure/services/portal/types.js +1 -0
  106. package/dist/src/lib/azure/services/redis/main.d.ts +4 -2
  107. package/dist/src/lib/azure/services/redis/main.js +3 -2
  108. package/dist/src/lib/azure/services/redis/types.d.ts +1 -1
  109. package/dist/src/lib/azure/services/resource-group/main.d.ts +10 -1
  110. package/dist/src/lib/azure/services/resource-group/main.js +17 -3
  111. package/dist/src/lib/azure/services/security-center/index.d.ts +2 -0
  112. package/dist/src/lib/azure/services/security-center/index.js +2 -0
  113. package/dist/src/lib/azure/services/security-center/main.d.ts +31 -0
  114. package/dist/src/lib/azure/services/security-center/main.js +33 -0
  115. package/dist/src/lib/azure/services/security-center/types.d.ts +3 -0
  116. package/dist/src/lib/azure/services/security-center/types.js +1 -0
  117. package/dist/src/lib/azure/services/servicebus/main.d.ts +28 -22
  118. package/dist/src/lib/azure/services/servicebus/main.js +31 -26
  119. package/dist/src/lib/azure/services/servicebus/types.d.ts +4 -4
  120. package/dist/src/lib/azure/services/storage/main.d.ts +26 -5
  121. package/dist/src/lib/azure/services/storage/main.js +51 -9
  122. package/dist/src/lib/azure/services/storage/types.d.ts +7 -2
  123. package/dist/src/lib/cloudflare/common/construct.js +1 -1
  124. package/dist/src/lib/cloudflare/common/stack.d.ts +2 -2
  125. package/dist/src/lib/cloudflare/common/stack.js +25 -25
  126. package/dist/src/lib/cloudflare/services/worker/main.js +8 -1
  127. package/dist/src/lib/cloudflare/services/worker/types.d.ts +1 -0
  128. package/package.json +26 -23
  129. package/src/lib/azure/common/constants.ts +83 -0
  130. package/src/lib/azure/common/construct.ts +50 -7
  131. package/src/lib/azure/common/stack.ts +26 -43
  132. package/src/lib/azure/common/types.ts +14 -2
  133. package/src/lib/azure/construct/event-handler/index.ts +2 -0
  134. package/src/lib/azure/construct/event-handler/main.ts +183 -0
  135. package/src/lib/azure/construct/event-handler/types.ts +49 -0
  136. package/src/lib/azure/construct/function-app/index.ts +2 -0
  137. package/src/lib/azure/construct/function-app/main.ts +410 -0
  138. package/src/lib/azure/construct/function-app/types.ts +45 -0
  139. package/src/lib/azure/construct/index.ts +6 -0
  140. package/src/lib/azure/construct/rest-api/index.ts +2 -0
  141. package/src/lib/azure/construct/rest-api/main.ts +205 -0
  142. package/src/lib/azure/construct/rest-api/types.ts +33 -0
  143. package/src/lib/azure/construct/rest-api-function/index.ts +2 -0
  144. package/src/lib/azure/construct/rest-api-function/main.ts +297 -0
  145. package/src/lib/azure/construct/rest-api-function/types.ts +39 -0
  146. package/src/lib/azure/construct/rest-api-with-cache/index.ts +2 -0
  147. package/src/lib/azure/construct/rest-api-with-cache/main.ts +75 -0
  148. package/src/lib/azure/construct/rest-api-with-cache/types.ts +15 -0
  149. package/src/lib/azure/construct/site-with-webapp/index.ts +2 -0
  150. package/src/lib/azure/construct/site-with-webapp/main.ts +160 -0
  151. package/src/lib/azure/construct/site-with-webapp/types.ts +33 -0
  152. package/src/lib/azure/index.ts +1 -0
  153. package/src/lib/azure/services/api-management/main.ts +168 -152
  154. package/src/lib/azure/services/api-management/types.ts +30 -1
  155. package/src/lib/azure/services/app-configuration/main.ts +29 -2
  156. package/src/lib/azure/services/app-service/main.ts +23 -4
  157. package/src/lib/azure/services/application-insights/main.ts +46 -7
  158. package/src/lib/azure/services/application-insights/types.ts +6 -2
  159. package/src/lib/azure/services/authorisation/constants.ts +13 -0
  160. package/src/lib/azure/services/authorisation/index.ts +3 -0
  161. package/src/lib/azure/services/authorisation/main.ts +202 -0
  162. package/src/lib/azure/services/authorisation/types.ts +3 -0
  163. package/src/lib/azure/services/cosmosdb/constants.ts +9 -0
  164. package/src/lib/azure/services/cosmosdb/index.ts +1 -0
  165. package/src/lib/azure/services/cosmosdb/main.ts +158 -8
  166. package/src/lib/azure/services/cosmosdb/types.ts +3 -0
  167. package/src/lib/azure/services/dns/main.ts +33 -8
  168. package/src/lib/azure/services/eventgrid/main.ts +41 -23
  169. package/src/lib/azure/services/function/main.ts +155 -9
  170. package/src/lib/azure/services/function/types.ts +3 -4
  171. package/src/lib/azure/services/index.ts +3 -0
  172. package/src/lib/azure/services/key-vault/main.ts +47 -4
  173. package/src/lib/azure/services/key-vault/types.ts +4 -4
  174. package/src/lib/azure/services/monitor/main.ts +5 -2
  175. package/src/lib/azure/services/operational-insights/main.ts +30 -4
  176. package/src/lib/azure/services/operational-insights/types.ts +3 -1
  177. package/src/lib/azure/services/portal/error.ts +12 -0
  178. package/src/lib/azure/services/portal/index.ts +4 -0
  179. package/src/lib/azure/services/portal/main.ts +81 -0
  180. package/src/lib/azure/services/portal/renderer.ts +182 -0
  181. package/src/lib/azure/services/portal/types.ts +45 -0
  182. package/src/lib/azure/services/redis/main.ts +10 -3
  183. package/src/lib/azure/services/redis/types.ts +1 -1
  184. package/src/lib/azure/services/resource-group/main.ts +34 -3
  185. package/src/lib/azure/services/security-center/index.ts +2 -0
  186. package/src/lib/azure/services/security-center/main.ts +42 -0
  187. package/src/lib/azure/services/security-center/types.ts +3 -0
  188. package/src/lib/azure/services/servicebus/main.ts +61 -30
  189. package/src/lib/azure/services/servicebus/types.ts +4 -4
  190. package/src/lib/azure/services/storage/main.ts +91 -9
  191. package/src/lib/azure/services/storage/types.ts +11 -2
  192. package/src/lib/cloudflare/common/construct.ts +1 -1
  193. package/src/lib/cloudflare/common/stack.ts +25 -25
  194. package/src/lib/cloudflare/services/worker/main.ts +10 -1
  195. package/src/lib/cloudflare/services/worker/types.ts +8 -1
  196. package/dist/lib/lambda.d.ts +0 -17
  197. package/dist/lib/lambda.d.ts.map +0 -1
  198. package/dist/lib/lambda.js +0 -28
  199. package/dist/lib/lambda.js.map +0 -1
  200. package/dist/tsconfig.tsbuildinfo +0 -1
@@ -0,0 +1,205 @@
1
+ import { HostnameType, LoggerType } from '@pulumi/azure-native/apimanagement/index.js'
2
+ import { getComponentOutput, GetComponentResult } from '@pulumi/azure-native/applicationinsights/index.js'
3
+ import { getVaultOutput } from '@pulumi/azure-native/keyvault/index.js'
4
+ import * as pulumi from '@pulumi/pulumi'
5
+ import { Output } from '@pulumi/pulumi'
6
+ import { CommonAzureConstruct } from '../../common/index.js'
7
+ import { RoleDefinitionId } from '../../services/index.js'
8
+ import { AzureApi, AzureRestApiProps } from './types.js'
9
+
10
+ export class AzureRestApi extends CommonAzureConstruct {
11
+ props: AzureRestApiProps
12
+ api: AzureApi
13
+ applicationInsights: Output<GetComponentResult>
14
+
15
+ constructor(id: string, props: AzureRestApiProps) {
16
+ super(id, props)
17
+ this.props = props
18
+ this.id = id
19
+ }
20
+
21
+ public initResources() {
22
+ this.createResourceGroup()
23
+ this.resolveApiKeyVault()
24
+ this.resolveCommonLogAnalyticsWorkspace()
25
+ this.resolveApplicationInsights()
26
+ this.createApiManagement()
27
+ this.createNamespaceSecretRole()
28
+ this.createNamespaceSecret()
29
+ this.createSubscriptionKeySecret()
30
+ this.createApiManagementLogger()
31
+ this.createApiDiagnostic()
32
+ this.createDiagnosticLog()
33
+ }
34
+
35
+ protected resolveApiKeyVault() {
36
+ this.api.authKeyVault = getVaultOutput({
37
+ vaultName: this.props.apiAuthKeyVault.name,
38
+ resourceGroupName: this.props.apiAuthKeyVault.resourceGroupName,
39
+ })
40
+ }
41
+
42
+ protected resolveApplicationInsights() {
43
+ if (!this.props.commonApplicationInsights || !this.props.commonApplicationInsights.resourceName) return
44
+
45
+ this.applicationInsights = getComponentOutput({
46
+ resourceName: this.props.commonApplicationInsights.resourceName,
47
+ resourceGroupName: this.props.commonApplicationInsights.resourceGroupName,
48
+ })
49
+ }
50
+
51
+ protected createApiManagement() {
52
+ if (this.props.apiManagement.useExistingApiManagement) {
53
+ if (this.props.apiManagement.apiStackName) {
54
+ const apiStack = new pulumi.StackReference(this.props.apiManagement.apiStackName)
55
+ this.api.id = apiStack.getOutput('apiId')
56
+ this.api.name = apiStack.getOutput('apiName')
57
+ this.api.resourceGroupName = apiStack.getOutput('apiResourceGroupName')
58
+ }
59
+ } else {
60
+ let hostnameConfigurations
61
+ if (this.props.apiManagement.certificateKeyVaultId) {
62
+ hostnameConfigurations = [
63
+ {
64
+ hostName: `api-${this.props.locationConfig?.[this.props.location].name}.${this.props.domainName}`,
65
+ keyVaultId: this.props.apiManagement.certificateKeyVaultId,
66
+ type: HostnameType.Management,
67
+ },
68
+ ]
69
+ }
70
+
71
+ this.api.apim = this.apiManagementManager.createApiManagementService(
72
+ this.id,
73
+ this,
74
+ {
75
+ ...this.props.apiManagement,
76
+ serviceName: this.props.stackName,
77
+ location: this.resourceGroup.location,
78
+ resourceGroupName: this.resourceGroup.name,
79
+ hostnameConfigurations,
80
+ },
81
+ undefined,
82
+ undefined,
83
+ { protect: true }
84
+ )
85
+ this.api.id = this.api.apim.id
86
+ this.api.name = this.api.apim.name
87
+ this.api.resourceGroupName = this.resourceGroup.name
88
+
89
+ if (this.props.apiManagement.certificateKeyVaultId) {
90
+ this.authorisationManager.createRoleAssignment(`${this.id}-kv-role`, this, {
91
+ principalId: this.api.apim.identity.apply(identity => identity?.principalId ?? ''),
92
+ roleDefinitionId: RoleDefinitionId.KEY_VAULT_CERTIFICATE_USER,
93
+ scope: this.props.apiManagement.certificateKeyVaultId,
94
+ })
95
+ }
96
+ }
97
+
98
+ this.registerOutputs({
99
+ apiId: this.api.id,
100
+ apiName: this.api.name,
101
+ apiResourceGroupName: this.api.resourceGroupName,
102
+ })
103
+ }
104
+
105
+ protected createNamespaceSecretRole() {
106
+ if (this.props.apiManagement.useExistingApiManagement) return
107
+
108
+ this.api.namedValueRoleAssignment = this.authorisationManager.createRoleAssignment(
109
+ `${this.id}-key-vault-role-api-namespace`,
110
+ this,
111
+ {
112
+ principalId: this.api.apim.identity.apply(identity => identity?.principalId ?? ''),
113
+ roleDefinitionId: `/subscriptions/${this.props.subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/4633458b-17de-408a-b874-0445c86b69e6`,
114
+ scope: this.api.authKeyVault.id,
115
+ }
116
+ )
117
+ }
118
+
119
+ protected createNamespaceSecret() {
120
+ if (this.props.apiManagement.useExistingApiManagement) return
121
+
122
+ this.api.namedValueSecret = this.keyVaultManager.createKeyVaultSecret(
123
+ `${this.id}-key-vault-api-namespace-secret`,
124
+ this,
125
+ {
126
+ vaultName: this.api.authKeyVault.name,
127
+ secretName: `${this.applicationInsights.name}-${this.props.stackName}-key`,
128
+ resourceGroupName: this.resourceGroup.name,
129
+ properties: {
130
+ value: this.applicationInsights.instrumentationKey,
131
+ },
132
+ }
133
+ )
134
+ }
135
+
136
+ protected createSubscriptionKeySecret() {
137
+ if (this.props.apiManagement.useExistingApiManagement) return
138
+
139
+ const apiManagementSubscription = this.apiManagementManager.createSubscription(this.id, this, {
140
+ serviceName: this.api.apim.name,
141
+ resourceGroupName: this.resourceGroup.name,
142
+ displayName: 'all-apis',
143
+ state: 'active',
144
+ allowTracing: false,
145
+ scope: '', // todo
146
+ })
147
+
148
+ this.keyVaultManager.createKeyVaultSecret(`${this.id}-key-vault-api-subscription-key-secret`, this, {
149
+ vaultName: this.api.authKeyVault.name,
150
+ secretName: `${this.props.stackName}-subscription-key`,
151
+ resourceGroupName: this.resourceGroup.name,
152
+ properties: {
153
+ value: apiManagementSubscription.primaryKey.apply(key => key ?? ''),
154
+ },
155
+ })
156
+ }
157
+
158
+ protected createApiManagementLogger() {
159
+ if (this.props.apiManagement.useExistingApiManagement) return
160
+
161
+ const apiAppNamedValue = this.apiManagementManager.createNamedValue(`${this.id}-am-nv`, this, {
162
+ displayName: this.applicationInsights.name,
163
+ resourceGroupName: this.resourceGroup.name,
164
+ serviceName: this.api.apim.name,
165
+ namedValueId: `${this.applicationInsights.name}-key`,
166
+ secret: true,
167
+ keyVault: {
168
+ secretIdentifier: this.api.namedValueSecret.id,
169
+ },
170
+ })
171
+
172
+ this.api.logger = this.apiManagementManager.createLogger(`${this.id}-am-logger`, this, {
173
+ resourceGroupName: this.resourceGroup.name,
174
+ serviceName: this.api.apim.name,
175
+ resourceId: this.applicationInsights.id,
176
+ loggerType: LoggerType.ApplicationInsights,
177
+ credentials: {
178
+ instrumentationKey: `{{${apiAppNamedValue.displayName}}}`,
179
+ },
180
+ })
181
+ }
182
+
183
+ protected createApiDiagnostic() {
184
+ if (this.props.apiManagement.useExistingApiManagement) return
185
+
186
+ this.apiManagementManager.createApiDiagnostic(`${this.id}-all-apis`, this, {
187
+ ...this.props.apiManagementDiagnostic,
188
+ apiId: this.api.apim.id,
189
+ resourceGroupName: this.resourceGroup.name,
190
+ serviceName: this.api.apim.name,
191
+ loggerId: this.api.logger.id,
192
+ })
193
+ }
194
+
195
+ protected createDiagnosticLog() {
196
+ if (this.props.apiManagement.useExistingApiManagement) return
197
+
198
+ this.monitorManager.createMonitorDiagnosticSettings(`${this.id}-apim-diagnostic`, this, {
199
+ ...this.props.apiManagementDiagnosticSettings,
200
+ name: `${this.props.stackName}-api-management`,
201
+ resourceUri: this.api.apim.id,
202
+ workspaceId: this.commonLogAnalyticsWorkspace.id,
203
+ })
204
+ }
205
+ }
@@ -0,0 +1,33 @@
1
+ import { ApiManagementService, Logger } from '@pulumi/azure-native/apimanagement/index.js'
2
+ import { RoleAssignment } from '@pulumi/azure-native/authorization/index.js'
3
+ import { GetVaultResult, Secret } from '@pulumi/azure-native/keyvault/index.js'
4
+ import { Input, Output } from '@pulumi/pulumi'
5
+ import {
6
+ ApiDiagnosticProps,
7
+ ApiManagementProps,
8
+ CommonAzureStackProps,
9
+ MonitorDiagnosticSettingProps,
10
+ } from '../../index.js'
11
+
12
+ export interface ApiAuthKeyVault {
13
+ name: string
14
+ resourceGroupName: string
15
+ }
16
+
17
+ export interface AzureRestApiProps extends CommonAzureStackProps {
18
+ apiAuthKeyVault: ApiAuthKeyVault
19
+ apiManagement: ApiManagementProps
20
+ apiManagementDiagnostic: ApiDiagnosticProps
21
+ apiManagementDiagnosticSettings: MonitorDiagnosticSettingProps
22
+ }
23
+
24
+ export interface AzureApi {
25
+ id: Input<string>
26
+ name: Input<string>
27
+ resourceGroupName: Input<string>
28
+ authKeyVault: Output<GetVaultResult>
29
+ apim: ApiManagementService
30
+ namedValueSecret: Secret
31
+ namedValueRoleAssignment: RoleAssignment
32
+ logger: Logger
33
+ }
@@ -0,0 +1,2 @@
1
+ export * from './main.js'
2
+ export * from './types.js'
@@ -0,0 +1,297 @@
1
+ import { HostnameType, NamedValue } from '@pulumi/azure-native/apimanagement/index.js'
2
+ import { getVaultOutput } from '@pulumi/azure-native/keyvault/index.js'
3
+ import { listWebAppHostKeysOutput } from '@pulumi/azure-native/web/index.js'
4
+ import * as pulumi from '@pulumi/pulumi'
5
+ import _ from 'lodash'
6
+ import { ApiManagementApiOperationProps, RoleDefinitionId } from '../../services/index.js'
7
+ import { AzureFunctionApp } from '../function-app/index.js'
8
+ import { AzureApiFunction, AzureRestApiFunctionProps } from './types.js'
9
+
10
+ export class AzureRestApiFunction extends AzureFunctionApp {
11
+ props: AzureRestApiFunctionProps
12
+ api: AzureApiFunction
13
+
14
+ constructor(id: string, props: AzureRestApiFunctionProps) {
15
+ super(id, props)
16
+ this.props = props
17
+ this.id = id
18
+ }
19
+
20
+ public initResources() {
21
+ this.createResourceGroup()
22
+ this.resolveCommonLogAnalyticsWorkspace()
23
+ this.resolveApplicationInsights()
24
+ this.createAppServicePlan()
25
+ this.createdParsedAppConfigurations()
26
+ this.createAppConfiguration()
27
+ this.createAppConfigurations()
28
+ this.createStorageAccount()
29
+ this.createStorageDeploymentContainer()
30
+ this.createStorageContainer()
31
+ this.createDataStorageAccount()
32
+ this.createDataStorageContainer()
33
+ this.generateStorageContainerSas()
34
+ this.createFunctionHosts()
35
+ this.createCodePackage()
36
+ this.createFunctionAppSiteConfig()
37
+ this.createFunctionApp()
38
+ this.createRoleAssignments()
39
+ this.resolveApiKeyVault()
40
+ this.createNamespaceSecret()
41
+ this.createApiManagement()
42
+ this.createApiManagementNamespace()
43
+ this.createApiManagementRoutes()
44
+ this.createCorsPolicy()
45
+ this.createFunctionDashboard()
46
+ }
47
+
48
+ protected resolveApiKeyVault() {
49
+ this.api.authKeyVault = getVaultOutput({
50
+ vaultName: this.props.apiAuthKeyVault.name,
51
+ resourceGroupName: this.props.apiAuthKeyVault.resourceGroupName,
52
+ })
53
+ }
54
+
55
+ protected createNamespaceSecret() {
56
+ if (!this.props.apiManagement.useExistingApiManagement) return
57
+
58
+ const functionDefaultKey = listWebAppHostKeysOutput({
59
+ name: this.app.name,
60
+ resourceGroupName: this.resourceGroup.name,
61
+ })
62
+
63
+ this.api.namedValueSecret = this.keyVaultManager.createKeyVaultSecret(
64
+ `${this.id}-key-vault-api-namespace-secret`,
65
+ this,
66
+ {
67
+ vaultName: this.api.authKeyVault.name,
68
+ secretName: pulumi.interpolate`${this.app.name}key`,
69
+ resourceGroupName: this.resourceGroup.name,
70
+ properties: {
71
+ value: functionDefaultKey.functionKeys?.apply(keys => keys?.['default'] ?? ''),
72
+ },
73
+ }
74
+ )
75
+ }
76
+
77
+ protected createApiManagement() {
78
+ if (this.props.apiManagement.useExistingApiManagement) {
79
+ if (this.props.apiManagement.apiStackName) {
80
+ const apiStack = new pulumi.StackReference(this.props.apiManagement.apiStackName)
81
+ this.api.id = apiStack.getOutput('apiId')
82
+ this.api.name = apiStack.getOutput('apiName')
83
+ this.api.resourceGroupName = apiStack.getOutput('apiResourceGroupName')
84
+ }
85
+ } else {
86
+ let hostnameConfigurations
87
+ if (this.props.apiManagement.certificateKeyVaultId) {
88
+ hostnameConfigurations = [
89
+ {
90
+ hostName: `api-${this.props.locationConfig?.[this.props.location].name}.${this.props.domainName}`,
91
+ keyVaultId: this.props.apiManagement.certificateKeyVaultId,
92
+ type: HostnameType.Management,
93
+ },
94
+ ]
95
+ }
96
+
97
+ this.api.apim = this.apiManagementManager.createApiManagementService(
98
+ this.id,
99
+ this,
100
+ {
101
+ ...this.props.apiManagement,
102
+ serviceName: this.props.stackName,
103
+ location: this.resourceGroup.location,
104
+ resourceGroupName: this.resourceGroup.name,
105
+ hostnameConfigurations,
106
+ },
107
+ undefined,
108
+ undefined,
109
+ { protect: true }
110
+ )
111
+ this.api.id = this.api.apim.id
112
+ this.api.name = this.api.apim.name
113
+ this.api.resourceGroupName = this.resourceGroup.name
114
+
115
+ if (this.props.apiManagement.certificateKeyVaultId) {
116
+ this.authorisationManager.createRoleAssignment(`${this.id}-kv-role`, this, {
117
+ principalId: this.api.apim.identity.apply(identity => identity?.principalId ?? ''),
118
+ roleDefinitionId: RoleDefinitionId.KEY_VAULT_CERTIFICATE_USER,
119
+ scope: this.props.apiManagement.certificateKeyVaultId,
120
+ })
121
+ }
122
+ }
123
+ }
124
+
125
+ protected createApiManagementNamespace() {
126
+ this.api.namedValue = new NamedValue(`${this.id}-am-nv`, {
127
+ displayName: this.app.name,
128
+ keyVault: {
129
+ secretIdentifier: this.api.namedValueSecret.id,
130
+ },
131
+ resourceGroupName: this.api.resourceGroupName,
132
+ secret: true,
133
+ serviceName: this.api.name,
134
+ })
135
+
136
+ this.api.backend = this.apiManagementManager.createBackend(this.id, this, {
137
+ ...this.props.apiManagementBackend,
138
+ title: this.props.stackName,
139
+ resourceGroupName: this.api.resourceGroupName,
140
+ serviceName: this.api.name,
141
+ url: pulumi.interpolate`https://${this.app.name}.azurewebsites.net/${this.props.apiManagementBackend.backendUrlPath}`,
142
+ resourceId: pulumi.interpolate`https://management.azure.com/subscriptions/${this.props.subscriptionId}/resourceGroups/${this.resourceGroup.name}/providers/Microsoft.Web/sites/${this.app.name}`,
143
+ credentials: {
144
+ header: {
145
+ 'x-functions-key': [`{{${this.api.namedValue.name}}}`],
146
+ },
147
+ },
148
+ })
149
+ }
150
+
151
+ protected createApiManagementRoutes() {
152
+ this.api.managementApi = this.apiManagementManager.createApi(`${this.id}-apim-api`, this, {
153
+ ...this.props.apiManagementApi,
154
+ displayName: this.props.apiManagementApi.displayName ?? this.props.stackName,
155
+ serviceName: this.api.name,
156
+ resourceGroupName: this.api.resourceGroupName,
157
+ isCurrent: this.props.apiManagementApi.isCurrent ?? true,
158
+ protocols: this.props.apiManagementApi.protocols ?? ['https'],
159
+ })
160
+
161
+ _.forEach(this.props.apiManagementApi.operations, operation => {
162
+ this.createApiOperation(operation)
163
+ this.createApiOperationCachePolicy(operation)
164
+ })
165
+ }
166
+
167
+ protected createApiOperation(operation: ApiManagementApiOperationProps) {
168
+ this.api.apiOperations[operation.displayName.toString()] = this.apiManagementManager.createOperation(
169
+ `${this.id}-apim-api-apim-api-operation-${operation.displayName}-${operation.method}`,
170
+ this,
171
+ {
172
+ operationId: `${operation.displayName}-${operation.method}`,
173
+ method: operation.method.toString().toUpperCase(),
174
+ serviceName: this.api.name,
175
+ resourceGroupName: this.api.resourceGroupName,
176
+ apiId: this.api.id,
177
+ displayName: operation.displayName,
178
+ urlTemplate: operation.urlTemplate,
179
+ templateParameters: operation.templateParameters,
180
+ }
181
+ )
182
+ }
183
+
184
+ protected createApiOperationCachePolicy(operation: ApiManagementApiOperationProps) {
185
+ if (!operation.caching || !operation.caching.enableCacheSet) return
186
+
187
+ this.apiManagementManager.createOperationPolicy(
188
+ `${this.id}-apim-api-operation-policy-${operation.displayName}-${operation.method}`,
189
+ this,
190
+ {
191
+ apiId: this.api.id,
192
+ resourceGroupName: this.api.resourceGroupName,
193
+ serviceName: this.api.name,
194
+ operationId: `${operation.displayName}-${operation.method}`,
195
+ value: `
196
+ <policies>
197
+ <policies>
198
+ <inbound>
199
+ <base />
200
+ ${this.props.apiManagementApi.cacheSetInboundPolicy}
201
+ </inbound>
202
+ <backend>
203
+ <base />
204
+ </backend>
205
+ <outbound>
206
+ <base />
207
+ ${this.props.apiManagementApi.cacheSetOutboundPolicy}
208
+ </outbound>
209
+ <on-error>
210
+ <base />
211
+ </on-error>
212
+ </policies>`.replace(/\n[ \t]*\n/g, '\n'), // move to utils
213
+ }
214
+ )
215
+ }
216
+
217
+ protected createCorsPolicy() {
218
+ if (!this.props.apiManagementCors?.enableCors) return
219
+
220
+ const allowedOrigins: string[] = []
221
+ if (this.props.apiManagementCors.allowedOrigins) {
222
+ _.forEach(this.props.apiManagementCors.allowedOrigins, (origin: string) => {
223
+ allowedOrigins.push(`<origin>${origin}</origin>`)
224
+ })
225
+ } else if (this.props.apiManagementCors.originSubdomain) {
226
+ _.forEach(this.props.locales, (locale: string) => {
227
+ allowedOrigins.push(
228
+ `<origin>https://${this.props.apiManagementCors?.originSubdomain}-${locale}.${this.props.domainName}</origin>`
229
+ )
230
+ })
231
+ }
232
+
233
+ const allowedHeaders: string[] = []
234
+ _.forEach(this.props.apiManagementCors.allowedHeaders, (header: string) => {
235
+ allowedHeaders.push(`<header>${header}</header>`)
236
+ })
237
+
238
+ const allowedMethods: string[] = []
239
+ _.forEach(this.props.apiManagementCors.allowedMethods, (method: string) => {
240
+ allowedMethods.push(`<method>${method}</method>`)
241
+ })
242
+
243
+ this.api.corsPolicyXmlContent = `
244
+ <cors allow-credentials="${this.props.apiManagementCors.allowCredentials}">
245
+ <allowed-origins>
246
+ ${allowedOrigins.toString().replaceAll(',', '')}
247
+ </allowed-origins>
248
+ <allowed-methods>
249
+ ${allowedMethods.toString().replaceAll(',', '')}
250
+ </allowed-methods>
251
+ <allowed-headers>
252
+ ${allowedHeaders.toString().replaceAll(',', '')}
253
+ </allowed-headers>
254
+ </cors>`.replace(/\n[ \t]*\n/g, '\n') // move to utils
255
+ }
256
+
257
+ protected createApiPolicy() {
258
+ const policyXmlContent = pulumi.interpolate`
259
+ <policies>
260
+ <inbound>
261
+ <base />
262
+ ${this.api.corsPolicyXmlContent ?? ''}
263
+ <set-backend-service backend-id="${this.api.backend.name}" />
264
+ <set-header name="traceparent" exists-action="override">
265
+ <value>@(context.Request.Headers.GetValueOrDefault("traceparent", ""))</value>
266
+ </set-header>
267
+ </inbound>
268
+ <backend>
269
+ <base />
270
+ </backend>
271
+ <outbound>
272
+ <base />
273
+ <set-header name="traceparent" exists-action="override">
274
+ <value>@(context.Request.Headers.GetValueOrDefault("traceparent", ""))</value>
275
+ </set-header>
276
+ </outbound>
277
+ <on-error>
278
+ <base />
279
+ </on-error>
280
+ </policies>`
281
+
282
+ this.apiManagementManager.createPolicy(`${this.id}-apim-api-policy`, this, {
283
+ serviceName: this.api.name,
284
+ apiId: this.api.id,
285
+ resourceGroupName: this.api.resourceGroupName,
286
+ value: policyXmlContent.apply(xml => xml.replace(/\n[ \t]*\n/g, '\n')),
287
+ })
288
+ }
289
+
290
+ protected dashboardVariables(): Record<string, any> {
291
+ const variables = super.dashboardVariables()
292
+ return {
293
+ ...variables,
294
+ apimName: this.api.name,
295
+ }
296
+ }
297
+ }
@@ -0,0 +1,39 @@
1
+ import { Api, ApiOperation, Backend, NamedValue } from '@pulumi/azure-native/apimanagement/index.js'
2
+ import {
3
+ ApiManagementApiProps,
4
+ ApiManagementBackendProps,
5
+ ApiManagementProps,
6
+ ApplicationInsightsProps,
7
+ AzureApi,
8
+ AzureFunctionAppProps,
9
+ AzureRestApiProps,
10
+ } from '../../index.js'
11
+
12
+ export interface ApiManagementRestApiProps extends ApiManagementProps {
13
+ useExistingApiManagement: boolean
14
+ }
15
+
16
+ export interface ApiManagementCors {
17
+ enableCors: boolean
18
+ allowCredentials: boolean
19
+ allowedMethods: string[]
20
+ allowedHeaders: string[]
21
+ allowedOrigins?: string[]
22
+ originSubdomain?: string
23
+ }
24
+
25
+ export interface AzureRestApiFunctionProps extends AzureRestApiProps, AzureFunctionAppProps {
26
+ apiManagementBackend: ApiManagementBackendProps
27
+ apiManagementApi: ApiManagementApiProps
28
+ apiManagementApplicationInsights?: ApplicationInsightsProps
29
+ apiManagement: ApiManagementRestApiProps
30
+ apiManagementCors?: ApiManagementCors
31
+ }
32
+
33
+ export interface AzureApiFunction extends AzureApi {
34
+ corsPolicyXmlContent?: string
35
+ apiOperations: { [operation: string]: ApiOperation }
36
+ managementApi: Api
37
+ backend: Backend
38
+ namedValue: NamedValue
39
+ }
@@ -0,0 +1,2 @@
1
+ export * from './main.js'
2
+ export * from './types.js'
@@ -0,0 +1,75 @@
1
+ import { AzureRestApi } from '../rest-api/main.js'
2
+ import { AzureApiWithCache, AzureRestApiWithCacheProps } from './types.js'
3
+
4
+ export class AzureRestApiWithCache extends AzureRestApi {
5
+ props: AzureRestApiWithCacheProps
6
+ declare api: AzureApiWithCache
7
+
8
+ constructor(id: string, props: AzureRestApiWithCacheProps) {
9
+ super(id, props)
10
+ this.props = props
11
+ this.id = id
12
+ }
13
+
14
+ public initResources() {
15
+ super.initResources()
16
+ this.createRedisCache()
17
+ this.createRedisCacheSecret()
18
+ this.createRedisCacheNamespace()
19
+ this.createRedisCacheApiManagement()
20
+ }
21
+
22
+ protected createRedisCache() {
23
+ this.api.redis = this.redisManager.createManagedRedis(
24
+ this.id,
25
+ this,
26
+ {
27
+ ...this.props.apiManagementManagedRedis,
28
+ name: this.props.stackName,
29
+ location: this.resourceGroup.location,
30
+ resourceGroupName: this.resourceGroup.name,
31
+ },
32
+ { ignoreChanges: ['location'] }
33
+ )
34
+ }
35
+
36
+ protected createRedisCacheSecret() {
37
+ this.api.redisNamedValueSecret = this.keyVaultManager.createKeyVaultSecret(
38
+ `${this.id}-key-vault-redis-namespace-secret`,
39
+ this,
40
+ {
41
+ vaultName: this.api.authKeyVault.name,
42
+ secretName: `${this.api.redis.name}key`,
43
+ resourceGroupName: this.resourceGroup.name,
44
+ properties: {
45
+ value: `${this.api.redis.name}:10000,password=${this.api.redis.accessKeys.primaryKey},ssl=True,abortConnect=False`,
46
+ },
47
+ },
48
+ { dependsOn: [this.api.redis, this.api.namedValueRoleAssignment] }
49
+ )
50
+ }
51
+
52
+ protected createRedisCacheNamespace() {
53
+ this.api.redisNamedValue = this.apiManagementManager.createNamedValue(`${this.id}-redis-nv`, this, {
54
+ displayName: `${this.api.redis.name}key`,
55
+ resourceGroupName: this.resourceGroup.name,
56
+ serviceName: this.api.apim.name,
57
+ namedValueId: `${this.api.redis.name}key`,
58
+ secret: true,
59
+ keyVault: {
60
+ secretIdentifier: this.api.redisNamedValueSecret.id,
61
+ },
62
+ })
63
+ }
64
+
65
+ protected createRedisCacheApiManagement() {
66
+ this.apiManagementManager.createCache(`${this.id}-am-redis-cache`, this, {
67
+ serviceName: this.api.apim.name,
68
+ connectionString: `{{${this.api.redisNamedValue.name}}}`,
69
+ cacheId: this.api.redis.id,
70
+ resourceGroupName: this.resourceGroup.name,
71
+ useFromLocation: this.api.redis.location,
72
+ description: `Redis cache for ${this.api.apim.name}`,
73
+ })
74
+ }
75
+ }
@@ -0,0 +1,15 @@
1
+ import { NamedValue } from '@pulumi/azure-native/apimanagement/index.js'
2
+ import { Secret } from '@pulumi/azure-native/keyvault/index.js'
3
+ import { Redis } from '@pulumi/azure-native/redis/index.js'
4
+ import { RedisProps } from '../../index.js'
5
+ import { AzureApi, AzureRestApiProps } from '../index.js'
6
+
7
+ export interface AzureRestApiWithCacheProps extends AzureRestApiProps {
8
+ apiManagementManagedRedis: RedisProps
9
+ }
10
+
11
+ export interface AzureApiWithCache extends AzureApi {
12
+ redis: Redis
13
+ redisNamedValueSecret: Secret
14
+ redisNamedValue: NamedValue
15
+ }
@@ -0,0 +1,2 @@
1
+ export * from './main.js'
2
+ export * from './types.js'