Skip to content

Commit 3f4abf7

Browse files
[Fix] Improve validation for service parameters in MtaTransformer and correct error logging in util (#87)
1 parent 1c577b9 commit 3f4abf7

3 files changed

Lines changed: 40 additions & 35 deletions

File tree

bin/cap-op-plugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async function capOperatorPlugin(cmd, option, yamlPath) {
3636
if (option === '--with-input-yaml' && !yamlPath)
3737
return _usage(`Input yaml path is missing.`)
3838

39-
if (option === '--with-input-yaml' && !yamlPath && cds.utils.exists(cds.utils.path.join(cds.root, yamlPath)))
39+
if (option === '--with-input-yaml' && yamlPath && !cds.utils.exists(cds.utils.path.join(cds.root, yamlPath)))
4040
return _usage(`Input yaml path ${yamlPath} does not exist.`)
4141

4242
await generateRuntimeValues(option, yamlPath)
@@ -46,7 +46,7 @@ async function capOperatorPlugin(cmd, option, yamlPath) {
4646
if (option === '--with-runtime-yaml' && !yamlPath)
4747
return _usage(`Input runtime yaml path is missing.`)
4848

49-
if (option === '--with-runtime-yaml' && !yamlPath && cds.utils.exists(cds.utils.path.join(cds.root, yamlPath)))
49+
if (option === '--with-runtime-yaml' && yamlPath && !cds.utils.exists(cds.utils.path.join(cds.root, yamlPath)))
5050
return _usage(`Input runtime yaml path ${yamlPath} does not exist.`)
5151

5252
await convertToconfigurableTemplateChart(option, yamlPath)

lib/mta-transformer.js

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ module.exports = class MtaTransformer {
2323

2424
async readMtaExtensions() {
2525
let mtaExtensions = []
26-
for(let file of this.mtaExtensionPaths) {
27-
let mtaExt = cds.parse.yaml(await cds.utils.read(cds.utils.path.join(cds.root, file)))
28-
if (mtaExt && mtaExt["_schema-version"].split(".")[0] === this.mta["_schema-version"].split(".")[0] && mtaExt["extends"] === this.mta["ID"]) {
29-
mtaExtensions.push(mtaExt)
30-
} else {
31-
console.log(`⚠️ ${file} is not a valid mta extension file. _schema major version or extends doesn't match. Skipping...`)
32-
}
26+
for (let file of this.mtaExtensionPaths) {
27+
let mtaExt = cds.parse.yaml(await cds.utils.read(cds.utils.path.join(cds.root, file)))
28+
if (mtaExt && mtaExt["_schema-version"].split(".")[0] === this.mta["_schema-version"].split(".")[0] && mtaExt["extends"] === this.mta["ID"]) {
29+
mtaExtensions.push(mtaExt)
30+
} else {
31+
console.log(`⚠️ ${file} is not a valid mta extension file. _schema major version or extends doesn't match. Skipping...`)
32+
}
3333
}
3434
return mtaExtensions
35-
}
35+
}
3636

3737
async getMta() {
3838
this.mta = cds.parse.yaml(await cds.utils.read(join(cds.root, this.mtaFilePath)))
@@ -41,7 +41,7 @@ module.exports = class MtaTransformer {
4141

4242
this.mergedMta = this.merge(this.mta, this.mtaExtensions)
4343

44-
if (this.mergedMta.parameters){
44+
if (this.mergedMta.parameters) {
4545
this.mergedMta.modules = replacePlaceholders(this.mergedMta.modules, this.mergedMta.parameters)
4646
this.mergedMta.resources = replacePlaceholders(this.mergedMta.resources, this.mergedMta.parameters)
4747
}
@@ -78,7 +78,7 @@ module.exports = class MtaTransformer {
7878
merge(mta, mtaExtensions) {
7979
let mergedMta = mta
8080

81-
for(let mtaExtension of mtaExtensions) {
81+
for (let mtaExtension of mtaExtensions) {
8282
// Merge parameters
8383
mergedMta.parameters = mergeObj(mergedMta.parameters, mtaExtension.parameters)
8484

@@ -94,21 +94,26 @@ module.exports = class MtaTransformer {
9494
}
9595

9696
async getServiceInstances() {
97-
if(this.serviceInstances.size !== 0) return this.serviceInstances
97+
if (this.serviceInstances.size !== 0) return this.serviceInstances
9898

99-
if(!this.mergedMta) await this.getMta()
99+
if (!this.mergedMta) await this.getMta()
100100

101101
let managedSvcs = this.mergedMta?.resources.filter(r => r.type === 'org.cloudfoundry.managed-service') || []
102-
for(let svc of managedSvcs) {
103-
this.serviceInstances.set(
104-
svc.parameters["service"] && svc.parameters["service-plan"] ? convertHypenNameToCamelcase(svc.parameters["service"] + "-" +svc.parameters["service-plan"]) : null,
105-
{
106-
name: svc.name,
107-
serviceOfferingName: svc.parameters["service"] ? svc.parameters["service"] : null,
108-
servicePlanName: svc.parameters["service-plan"] ? svc.parameters["service-plan"] : null,
109-
parameters: svc.parameters["config"] ? svc.parameters["config"] : {}
110-
}
111-
)
102+
for (let svc of managedSvcs) {
103+
try {
104+
this.serviceInstances.set(
105+
convertHypenNameToCamelcase(svc.parameters["service"] + "-" + svc.parameters["service-plan"]),
106+
{
107+
name: svc.name,
108+
serviceOfferingName: svc.parameters["service"],
109+
servicePlanName: svc.parameters["service-plan"],
110+
parameters: svc.parameters["config"] || {}
111+
}
112+
)
113+
} catch (err) {
114+
console.warn(`⚠️ Error processing service instance ${svc.name} - ${err.message}. Skipping...`)
115+
continue
116+
}
112117
}
113118

114119
// for xsuaa service instance, remove oauth2-configuration -> redirect-uris as it is filled by service-instance.yaml template
@@ -151,7 +156,7 @@ module.exports = class MtaTransformer {
151156
async getServiceBindings() {
152157
if (this.serviceBindings.size !== 0) return this.serviceBindings
153158

154-
if (this.workloads.size === 0 ) await this.getWorkloads()
159+
if (this.workloads.size === 0) await this.getWorkloads()
155160

156161
return this.serviceBindings
157162
}
@@ -181,9 +186,9 @@ module.exports = class MtaTransformer {
181186
serviceInstanceName: consumedBTPService.name,
182187
}
183188
if (this.serviceBindings.has(serviceBindingNameKey) &&
184-
md5(JSON.stringify(this.serviceBindings.get(serviceBindingNameKey))) !== md5(JSON.stringify(serviceBindingValue))) {
189+
md5(JSON.stringify(this.serviceBindings.get(serviceBindingNameKey))) !== md5(JSON.stringify(serviceBindingValue))) {
185190

186-
let serviceBindingNewName = consumedBTPService.name +"-"+ module.name + this.serviceBindingNameSuffix
191+
let serviceBindingNewName = consumedBTPService.name + "-" + module.name + this.serviceBindingNameSuffix
187192
let serviceBindingNewNameKey = convertHypenNameToCamelcase(serviceBindingNewName)
188193
this.serviceBindings.set(serviceBindingNewNameKey, {
189194
name: serviceBindingNewName,
@@ -194,14 +199,14 @@ module.exports = class MtaTransformer {
194199
})
195200
workloadServiceBindings.push(serviceBindingNewName)
196201
} else if (this.serviceBindings.has(serviceBindingNameKey) &&
197-
md5(JSON.stringify(this.serviceBindings.get(serviceBindingNameKey))) === md5(JSON.stringify(serviceBindingValue))) {
202+
md5(JSON.stringify(this.serviceBindings.get(serviceBindingNameKey))) === md5(JSON.stringify(serviceBindingValue))) {
198203

199204
workloadServiceBindings.push(serviceBindingName)
200205
continue
201206
}
202207
else {
203208
workloadServiceBindings.push(serviceBindingName)
204-
this.serviceBindings.set(serviceBindingNameKey,serviceBindingValue)
209+
this.serviceBindings.set(serviceBindingNameKey, serviceBindingValue)
205210
}
206211
}
207212
return workloadServiceBindings
@@ -212,7 +217,7 @@ module.exports = class MtaTransformer {
212217
for (const [key, value] of Object.entries(properties)) {
213218
env.push({
214219
name: key,
215-
value: typeof value === 'string'? value : JSON.stringify(value)
220+
value: typeof value === 'string' ? value : JSON.stringify(value)
216221
})
217222
}
218223
return env
@@ -231,7 +236,7 @@ module.exports = class MtaTransformer {
231236
}
232237
if (module.path?.includes("gen/mtx/sidecar") || module.type === 'com.sap.application.content') {
233238
workload.jobDefinition = {
234-
type: module.type === 'com.sap.application.content'? "Content": "TenantOperation",
239+
type: module.type === 'com.sap.application.content' ? "Content" : "TenantOperation",
235240
image: null
236241
}
237242
} else if (module.path?.includes("gen/srv")) {
@@ -255,9 +260,9 @@ module.exports = class MtaTransformer {
255260
// delete existing TENANT_HOST_PATTERN if any; Handled in cap-operator-cro.yaml template
256261
if (workload.deploymentDefinition?.type == "Router") {
257262
if (workload.deploymentDefinition.env) {
258-
const index = workload.deploymentDefinition.env.findIndex(e => e.name === "TENANT_HOST_PATTERN")
259-
if (index != -1)
260-
workload.deploymentDefinition.env.splice(index, 1)
263+
const index = workload.deploymentDefinition.env.findIndex(e => e.name === "TENANT_HOST_PATTERN")
264+
if (index != -1)
265+
workload.deploymentDefinition.env.splice(index, 1)
261266
}
262267
}
263268

lib/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function extractAndReplaceHanaInstanceId(env, valuesYaml) {
137137
env[index].value = JSON.stringify(cdsConfig)
138138
}
139139
} catch (err) {
140-
console.warn(`Failed to parse CDS_CONFIG: ${error.message}`)
140+
console.warn(`Failed to parse CDS_CONFIG: ${err.message}`)
141141
}
142142
}
143143

0 commit comments

Comments
 (0)