Skip to content

Commit

Permalink
Sonar code complexity fix
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudhprasad-sap committed Jun 26, 2024
1 parent 3215c18 commit 969e4df
Showing 1 changed file with 38 additions and 43 deletions.
81 changes: 38 additions & 43 deletions bin/cap-op-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Mustache = require('mustache')
const { ask, mergeObj, isCAPOperatorChart } = require('../lib/util')

const isCli = require.main === module
const SUPPORTED = {'generate-runtime-values': ['--via-prompts', '--via-input-yaml']}
const SUPPORTED = { 'generate-runtime-values': ['--via-prompts', '--via-input-yaml'] }

async function capOperatorPlugin(cmd, option, inputYamlPath) {

Expand All @@ -18,7 +18,7 @@ async function capOperatorPlugin(cmd, option, inputYamlPath) {
if (option === '--via-input-yaml' && !inputYamlPath) return _usage(`Input yaml path is missing.`)

if (cmd === 'generate-runtime-values') await generateRuntimeValues(option, inputYamlPath)
} catch(e) {
} catch (e) {
if (isCli) {
console.error(e.message)
process.exit(1)
Expand Down Expand Up @@ -62,7 +62,7 @@ async function generateRuntimeValues(option, inputYamlPath) {
let answerStruct = {}
const { appName, appDescription } = getAppDetails()

if(option === '--via-input-yaml' && inputYamlPath) {
if (option === '--via-input-yaml' && inputYamlPath) {

answerStruct = yaml.parse(await cds.utils.read(cds.utils.path.join(cds.root, inputYamlPath)))

Expand All @@ -73,12 +73,12 @@ async function generateRuntimeValues(option, inputYamlPath) {
const questions = [
['Enter app name for deployment: ', appName, true],
['Enter CAP Operator subdomain (In kyma cluster it is "cap-op" by default): ', 'cap-op', true],
['Enter your cluster shoot domain: ','', true],
['Enter your global account ID: ','', true],
['Enter your provider subdomain: ','', true],
['Enter your provider tenant ID: ','', true],
['Enter your HANA database instance ID: ','', false],
['Enter your image pull secrets: ','', false]
['Enter your cluster shoot domain: ', '', true],
['Enter your global account ID: ', '', true],
['Enter your provider subdomain: ', '', true],
['Enter your provider tenant ID: ', '', true],
['Enter your HANA database instance ID: ', '', false],
['Enter your image pull secrets: ', '', false]
]

const answerKeys = [
Expand All @@ -95,60 +95,43 @@ async function generateRuntimeValues(option, inputYamlPath) {

const valuesYaml = yaml.parse(await cds.utils.read(cds.utils.path.join(cds.root, 'chart/values.yaml')))

const runtimeValuesYaml = yaml.parse(Mustache.render( await cds.utils.read(cds.utils.path.join(__dirname, '../files/runtime-values.yaml.hbs')), answerStruct))
let runtimeValuesYaml = yaml.parse(Mustache.render(await cds.utils.read(cds.utils.path.join(__dirname, '../files/runtime-values.yaml.hbs')), answerStruct))

if(!answerStruct['imagePullSecret'])
if (!answerStruct['imagePullSecret'])
delete runtimeValuesYaml['imagePullSecrets']

runtimeValuesYaml['workloads'] = {}
for (const [workloadKey, workloadDetails] of Object.entries(valuesYaml.workloads)) {
if (workloadDetails.deploymentDefinition)
runtimeValuesYaml['workloads'][workloadKey] = { "deploymentDefinition": {"env": workloadDetails.deploymentDefinition.env ?? [] }}
else if (workloadDetails.jobDefinition)
runtimeValuesYaml['workloads'][workloadKey] = { "jobDefinition": {"env": workloadDetails.jobDefinition.env ?? [] }}

const cdsConfigHana = Mustache.render('{"requires":{"cds.xt.DeploymentService":{"hdi":{"create":{"database_id":"{{hanaInstanceId}}"}}}}}', answerStruct)

if (workloadDetails.deploymentDefinition && workloadDetails.deploymentDefinition.type === 'CAP' && answerStruct['hanaInstanceId']) {
const index = runtimeValuesYaml['workloads'][workloadKey]['deploymentDefinition']['env'].findIndex(e => e.name === 'CDS_CONFIG')
if (index > -1) {
// Get existing CDS_CONFIG and merge with new CDS_CONFIG for HANA
const existingCdsConfigJson = JSON.parse(runtimeValuesYaml['workloads'][workloadKey]['deploymentDefinition']['env'][index].value)
const mergedCdsConfig = mergeObj(existingCdsConfigJson, JSON.parse(cdsConfigHana))
runtimeValuesYaml['workloads'][workloadKey] = workloadDetails.deploymentDefinition
? { "deploymentDefinition": { "env": workloadDetails.deploymentDefinition.env ?? [] } }
: { "jobDefinition": { "env": workloadDetails.jobDefinition.env ?? [] } }

runtimeValuesYaml['workloads'][workloadKey]['deploymentDefinition']['env'][index] = {name: 'CDS_CONFIG', value: JSON.stringify(mergedCdsConfig) }
} else
runtimeValuesYaml['workloads'][workloadKey]['deploymentDefinition']['env'].push({name: 'CDS_CONFIG', value: cdsConfigHana })
}
const cdsConfigHana = Mustache.render('{"requires":{"cds.xt.DeploymentService":{"hdi":{"create":{"database_id":"{{hanaInstanceId}}"}}}}}', answerStruct)

if (workloadDetails.jobDefinition && workloadDetails.jobDefinition.type === 'TenantOperation' && answerStruct['hanaInstanceId']) {
const index = runtimeValuesYaml['workloads'][workloadKey]['jobDefinition']['env'].findIndex(e => e.name === 'CDS_CONFIG')
if (index > -1) {
// Get existing CDS_CONFIG and merge with new CDS_CONFIG for HANA
const existingCdsConfigJson = JSON.parse(runtimeValuesYaml['workloads'][workloadKey]['jobDefinition']['env'][index].value)
const mergedCdsConfig = mergeObj(existingCdsConfigJson, JSON.parse(cdsConfigHana))
if (workloadDetails?.deploymentDefinition?.type === 'CAP' && answerStruct['hanaInstanceId'])
updateCdsConfigEnv(runtimeValuesYaml, workloadKey, 'deploymentDefinition', cdsConfigHana)

runtimeValuesYaml['workloads'][workloadKey]['jobDefinition']['env'][index] = {name: 'CDS_CONFIG', value: JSON.stringify(mergedCdsConfig)}
} else
runtimeValuesYaml['workloads'][workloadKey]['jobDefinition']['env'].push({name: 'CDS_CONFIG', value: cdsConfigHana })
if (workloadDetails?.jobDefinition?.type === 'TenantOperation' && answerStruct['hanaInstanceId']) {
updateCdsConfigEnv(runtimeValuesYaml, workloadKey, 'jobDefinition', cdsConfigHana)
}

if (workloadDetails.deploymentDefinition && workloadDetails.deploymentDefinition.type === 'Router') {
if (workloadDetails?.deploymentDefinition?.type === 'Router') {
const index = runtimeValuesYaml['workloads'][workloadKey]['deploymentDefinition']['env'].findIndex(e => e.name === 'TENANT_HOST_PATTERN')
if (index > -1)
runtimeValuesYaml['workloads'][workloadKey]['deploymentDefinition']['env'][index] = {name: 'TENANT_HOST_PATTERN', value: '^(.*).'+ answerStruct["appName"] + '.' + answerStruct["clusterDomain"]}
runtimeValuesYaml['workloads'][workloadKey]['deploymentDefinition']['env'][index] = { name: 'TENANT_HOST_PATTERN', value: '^(.*).' + answerStruct["appName"] + '.' + answerStruct["clusterDomain"] }
else
runtimeValuesYaml['workloads'][workloadKey]['deploymentDefinition']['env'].push({name: 'TENANT_HOST_PATTERN', value: '^(.*).'+ answerStruct["appName"] + '.' + answerStruct["clusterDomain"]})
runtimeValuesYaml['workloads'][workloadKey]['deploymentDefinition']['env'].push({ name: 'TENANT_HOST_PATTERN', value: '^(.*).' + answerStruct["appName"] + '.' + answerStruct["clusterDomain"] })
}
}

// remove workload definition where env is empty
for (const [workloadKey, workloadDetails] of Object.entries(runtimeValuesYaml.workloads)) {
if (workloadDetails.deploymentDefinition && workloadDetails.deploymentDefinition.env && workloadDetails.deploymentDefinition.env.length === 0) {
if (workloadDetails?.deploymentDefinition?.env.length === 0) {
delete runtimeValuesYaml['workloads'][workloadKey]
}

if (workloadDetails.jobDefinition && workloadDetails.jobDefinition.env && workloadDetails.jobDefinition.env.length === 0) {
if (workloadDetails?.jobDefinition?.env.length === 0) {
delete runtimeValuesYaml['workloads'][workloadKey]
}
}
Expand All @@ -157,15 +140,27 @@ async function generateRuntimeValues(option, inputYamlPath) {
console.log("Generated 'runtime-values.yaml' file in the 'chart' folder.")
}

function updateCdsConfigEnv(runtimeValuesYaml, workloadKey, workloadDefintion, cdsConfigHana) {
const index = runtimeValuesYaml['workloads'][workloadKey][workloadDefintion]['env'].findIndex(e => e.name === 'CDS_CONFIG')
if (index > -1) {
// Get existing CDS_CONFIG and merge with new CDS_CONFIG for HANA
const existingCdsConfigJson = JSON.parse(runtimeValuesYaml['workloads'][workloadKey][workloadDefintion]['env'][index].value)
const mergedCdsConfig = mergeObj(existingCdsConfigJson, JSON.parse(cdsConfigHana))

runtimeValuesYaml['workloads'][workloadKey][workloadDefintion]['env'][index] = { name: 'CDS_CONFIG', value: JSON.stringify(mergedCdsConfig) }
} else
runtimeValuesYaml['workloads'][workloadKey][workloadDefintion]['env'].push({ name: 'CDS_CONFIG', value: cdsConfigHana })
}

function getAppDetails() {
const { name, description } = JSON.parse(cds.utils.fs.readFileSync(cds.utils.path.join(cds.root, 'package.json')))
const segments = (name ?? this.appName).trim().replace(/@/g, '').split('/').map(encodeURIComponent)
return { appName: segments[segments.length - 1], appDescription: description }
}

if (isCli) {
const [, , cmd, option, inputYamlPath ] = process.argv
;(async () => await capOperatorPlugin(cmd, option, inputYamlPath ?? undefined))()
const [, , cmd, option, inputYamlPath] = process.argv;
(async () => await capOperatorPlugin(cmd, option, inputYamlPath ?? undefined))()
}

module.exports = { capOperatorPlugin }

0 comments on commit 969e4df

Please sign in to comment.