-
Notifications
You must be signed in to change notification settings - Fork 14
/
plugin.ts
123 lines (119 loc) · 6.93 KB
/
plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { BaseIngredient, IngredientManager } from "@azbake/core"
import { ARMHelper } from "@azbake/arm-helper"
import { ApiManagementClient } from "@azure/arm-apimanagement"
import { ApplicationInsightsManagementClient } from '@azure/arm-appinsights'
import ApimTemplate from "./api-management.json"
import { ApimBaseUtil } from "./functions"
import * as idx from "./index"
export class ApimBase extends BaseIngredient {
public async Execute(): Promise<void> {
try {
this._logger.log(`API Manamgement: Base Logging - ${this._ingredient.properties.source}`)
let aiClient = new ApplicationInsightsManagementClient(this._ctx.AuthToken, this._ctx.Environment.authentication.subscriptionId);
let client = new ApiManagementClient(this._ctx.AuthToken, this._ctx.Environment.authentication.subscriptionId)
let util = IngredientManager.getIngredientFunction("coreutils", this._ctx)
const helper = new ARMHelper(this._ctx);
let params = await helper.BakeParamsToARMParamsAsync(this._name, this._ingredient.properties.parameters)
let serviceName = params["apiManagementServiceName"].value
//Import deployment parameters
let properties = params["properties"]
delete params["properties"]
let loggerProps = params["logger"]
delete params["logger"]
params = await helper.ConfigureDiagnostics(params);
//Deploy primary ARM template
await helper.DeployTemplate(this._name, ApimTemplate, params, await util.resource_group())
//Deploy named Key/Value pairs
if (properties) {
let keys = Object.keys(properties.value)
let apimRg = await util.resource_group() || ""
let item = ""
for (let i = 0; i < keys.length; i++) {
item = keys[i]
let subProps = properties.value[item]
let name = item
let propId = name.charAt(0).toLowerCase() + name.slice(1)
let tags = subProps["tags"] || ""
let isSecret = subProps["isSecret"] || false
let value = subProps["key"] || ""
await client.property.createOrUpdate(apimRg, serviceName, propId, {
displayName: name,
id: propId,
name: name,
tags: tags,
secret: isSecret,
value: value
}
).then((result) => {
if (result.eTag && result.displayName == name && result.value == value && result.secret == isSecret) {
this._logger.log(`property: Deployed=${name}`)
}
else {
throw `property: Deployment Failed=${name}`
}
});
}
}
//Create Logger Connection to Application Insights
if (loggerProps) {
let keys = Object.keys(loggerProps.value)
for (let i = 0; i < keys.length; i++) {
let item = keys[i]
let loggerVal = loggerProps.value[item] || ""
let aiRg = loggerVal.resourceGroup || await util.resource_group()
let apimRg = await util.resource_group() || ""
let aiName = item || ""
let currentLoggerCreds: any
if (loggerVal.type == "applicationInsights") {
this._logger.log(`logger: Getting instrumentation key from '${aiName}' in resource group '${aiRg}'`)
let response = await aiClient.components.get(aiRg, aiName)
let aiKey: string = ""
if (response.instrumentationKey) {
aiKey = response.instrumentationKey || ""
}
await client.logger.createOrUpdate(apimRg, serviceName, aiName, {
credentials: { instrumentationKey: aiKey },
loggerType: 'applicationInsights'
}).then((result) => {
if (result.eTag && result.name == aiName && result.credentials.instrumentationKey && result.loggerType == loggerVal.type) {
this._logger.log(`logger: Deployed ${result.loggerType} connection for '${aiName}' on '${serviceName}'`)
currentLoggerCreds = result.credentials.instrumentationKey.replace(/{{|}}/ig, "")
}
else {
throw `logger: ${aiName} was not deployed correctly`
}
});
//Clean logger keys
if (loggerVal.clean == undefined || loggerVal.value.clean) {
let result = await client.property.listByService(apimRg, serviceName) || ""
let propEtag = ""
for (let i = 0; i < result.length; i++) {
let id = result[i].name || ""
let displayName = result[i].displayName || ""
let currentLogKey = ""
let propTags = result[i].tags || []
if (displayName != currentLoggerCreds && displayName.match(/Logger.Credentials-.*/) && result[i].value == aiKey) {
await client.property.getEntityTag(apimRg, serviceName, id).then((result) => { propEtag = result.eTag })
await client.property.deleteMethod(apimRg, serviceName, id, propEtag)
.then((result) => {
this._logger.log(`logger: Removed old key - ${displayName}: ${result._response.status == 200}`)
})
.catch((failure) => {
this._logger.log(`logger: failed to remove AppInsights key: ${displayName}`)
})
}
}
}
}
else if (loggerVal.value.type == "azureEventHub") {
this._logger.log(`Logger: EventHub functionality is yet to be implemented`)
}
}
}
} catch (error) {
this._logger.error('deployment failed: ' + error)
throw error
}
}
}
//https://docs.microsoft.com/en-us/javascript/api/azure-arm-apimanagement/propertycontract?view=azure-node-latest