@sap/cds-dk
Version:
Command line client and development toolkit for the SAP Cloud Application Programming Model
90 lines (77 loc) • 4.41 kB
JavaScript
const { readProject } = require('../../projectReader')
const { merge, sort } = require('../../merge')
const { srv4, approuter, mtxSidecar4 } = require('../../registries/mta')
const cds = require('../../../cds')
const { read, write } = cds.utils
// const mvn = require('../../mvn')
module.exports = class IasTemplate extends require('../../plugin') {
static help() {
return 'authentication via IAS'
}
static hasInProduction(env) {
// REVISIT: Ugly, find more generic solution
return env.requires?.auth?.kind === 'ias' ||
cds.cli.argv?.[0]?.includes('ias') || cds.cli.argv?.[0]?.includes('ams') ||
cds.cli.options?.add?.has('ias') || cds.cli.options?.add?.has('ams') || false
}
async run() {
const project = readProject()
const { configFile, isNodejs } = project
await merge(__dirname, 'files/package.json.hbs').into(configFile, { project, forceOverwrite: true })
if (isNodejs) await sort('package.json', 'dependencies')
// if (project.isJava) await mvn.add('ias')
}
async combine() {
const project = readProject()
const { isJava, hasMta, hasCfManifest, hasHelm, hasHelmUnifiedRuntime, hasApprouter, hasMultitenancy, srvPath } = project
if (hasMultitenancy) {
const conf = (await read('mtx/sidecar/package.json')) ?? {}
conf.cds ??= {}
const profile = conf.cds.profile
if (profile) delete conf.cds.profile
conf.cds.profiles ??= profile ? [profile] : []
if (!conf.cds.profiles.includes('subscription-manager')) {
conf.cds.profiles.push('subscription-manager')
}
await write('mtx/sidecar/package.json', conf, { spaces: 2 })
await merge(__dirname, 'files/sidecar.package.json.hbs').into('mtx/sidecar/package.json')
}
if (hasMta) {
const srv = srv4(srvPath)
const identity = { in: 'resources', where: { 'parameters.service': 'identity' } }
const srvIdentityBinding = { in: [srv, 'requires'], where: { 'parameters.config.app-identifier': 'srv' } }
const srvCertUrlApi = { in: [srv, 'provides'], where: { 'name': 'srv-api' } }
const additions = [srv, identity, srvIdentityBinding, srvCertUrlApi]
const overwrites = []
if (hasApprouter) {
additions.push(approuter)
additions.push({ in: [approuter, 'requires'], where: { 'parameters.config.app-identifier': 'approuter' }})
const srvApi = { in: [approuter, 'requires'], where: { 'name': 'srv-api' } }
additions.push(srvApi)
overwrites.push({ item: [srvApi, 'properties.url'], withValue: '~{srv-cert-url}' })
}
if (hasMultitenancy) {
const mtxSidecar = mtxSidecar4(isJava ? 'mtx/sidecar' : 'gen/mtx/sidecar')
const mtxIdentityBinding = { in: [mtxSidecar, 'requires'], where: { 'parameters.config.app-identifier': 'mtx' } }
additions.push(mtxSidecar, mtxIdentityBinding)
}
await merge(__dirname, 'files/mta.yaml.hbs').into('mta.yaml', { project, additions, overwrites })
}
if (hasHelm || hasHelmUnifiedRuntime) {
const identity = { in: 'dependencies', where: { 'alias': 'identity' } }
await merge(__dirname, 'files/Chart.yaml.hbs').into('chart/Chart.yaml', { project, additions: [identity] })
await merge(__dirname, 'files/values.yaml.hbs').into('chart/values.yaml', { project })
// TODO: MTX, approuter
}
if (hasCfManifest) {
await this.#combineWithCfManifest(project) // REVISIT: we shouldn't overly promote cf-manifest
}
}
async #combineWithCfManifest(project) {
const srv = { in: 'applications', where: { 'name': `${project.appName}-srv` } }
const identityBinding = { in: 'services', where: { 'parameters.app-identifier': 'srv' } }
await merge(__dirname, 'files/manifest.yml.hbs').into('manifest.yml', { project, additions: [srv, identityBinding] })
const identity = { in: 'create-services', where: { 'broker': 'identity' } }
await merge(__dirname, 'files/services-manifest.yml.hbs').into('services-manifest.yml', { project, additions: [identity] })
}
}