Skip to content

Commit

Permalink
Add support for connections via cds bind
Browse files Browse the repository at this point in the history
  • Loading branch information
jung-thomas committed May 20, 2022
1 parent a14b48e commit 8a59c18
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ default-*.json

/secrets
yarn.lock

# added by cds bind
.cdsrc-private.json
7 changes: 7 additions & 0 deletions CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
[
{
"date": "2022-05-20",
"version": "3.202205.4",
"Changed": [
"Add connection support for [`cds bind`](https://cap.cloud.sap/docs/advanced/hybrid-testing#bind-to-cloud-services) and the resulting `.cdsrc-private.json`. Note this will make each command take a few seconds longer as credentials are no longer stored locally but looked up from cf or k8s dynamically with each command"
]
},
{
"date": "2022-05-12",
"version": "3.202205.3",
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/).

## [3.202205.4] - 2022-05-20

**Changed**

- Add connection support for [`cds bind`](https://cap.cloud.sap/docs/advanced/hybrid-testing#bind-to-cloud-services) and the resulting `.cdsrc-private.json`. Note this will make each command take a few seconds longer as credentials are no longer stored locally but looked up from cf or k8s dynamically with each command

## [3.202205.3] - 2022-05-12

**Changed**
Expand Down
1 change: 1 addition & 0 deletions _i18n/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ requestedVersion=Requested Node.js Version: {0}
versionCheckFail=Node.js version wanted: {0}, but found: {1}
warning=Warning!
cds-dk=To use the openAPI feature you must install @sap/cds-dk as a separate global module
cds-dk2=To use the cds bind feature for connections, you must install @sap/cds-dk as a separate global module
gui.tableType=Table Type
gui.fields=Fields
gui.constraints=Constraints
Expand Down
2 changes: 1 addition & 1 deletion bin/openDBExplorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function handler (argv) {
export async function getDBX(prompts) {
base.debug('getDBX')
try {
let options = conn.getConnOptions(prompts)
let options = await conn.getConnOptions(prompts)

const host = options.hana.host
let dbxURL = ''
Expand Down
8 changes: 4 additions & 4 deletions bin/systemInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function sysInfo(prompts) {
}

case OUTPUTS.ENV: {
environmentOutput(prompts)
await environmentOutput(prompts)
break
}
case OUTPUTS.DBX: {
Expand Down Expand Up @@ -76,14 +76,14 @@ export async function basicOutput() {
base.outputTable(results)
}

export function environmentOutput(prompts) {
export async function environmentOutput(prompts) {
prompts.disableVerbose = true
console.log(conn.getConnOptions(prompts))
console.log(await conn.getConnOptions(prompts))
}

export async function dbxOutput(prompts) {
prompts.disableVerbose = true
let connDetails = conn.getConnOptions(prompts)
let connDetails = await conn.getConnOptions(prompts)
const dbStatus = await base.createDBConnection()
const dbVersion = await dbInspect.getHANAVersion(dbStatus)
const unknown = base.bundle.getText("hc.unknown")
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hana-cli",
"version": "3.202205.3",
"version": "3.202205.4",
"description": "HANA Developer Command Line Interface",
"main": "index.js",
"bin": {
Expand Down
60 changes: 52 additions & 8 deletions utils/connections.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import * as path from 'path'
import dotenv from 'dotenv'
import { homedir } from 'os'
import * as xsenv from '@sap/xsenv'
import cds from '@sap/cds'
// @ts-ignore
const LOG = cds.log('bind')
import { createRequire } from 'module'
const require = createRequire(import.meta.url)


/**
* Check parameter folders to see if the input file exists there
Expand Down Expand Up @@ -89,6 +95,15 @@ export function getEnv() {
return getFileCheckParents(`.env`)
}

/**
* Check current and parent directories for a .cdsrc-private.json
* @returns {string} - the file path if found
*/
export function getCdsrcPrivate() {
base.debug('getCdsrcPrivate')
return getFileCheckParents(`.cdsrc-private.json`)
}

/**
* Resolve Environment by deciding which option between default-env and default-env-admin we should take
* @param {*} options
Expand All @@ -108,9 +123,9 @@ export function resolveEnv(options) {
/**
* Get Connection Options from input prompts
* @param {object} prompts - input prompts
* @returns {object} connection options
* @returns {Promise<object>} connection options
*/
export function getConnOptions(prompts) {
export async function getConnOptions(prompts) {
base.debug('getConnOptions')
delete process.env.VCAP_SERVICES
let envFile
Expand All @@ -120,12 +135,41 @@ export function getConnOptions(prompts) {
envFile = getDefaultEnvAdmin()
}

//No Admin option or no default-env-admin.json file found - try for .env
//No Admin option or no default-env-admin.json file found - try for .cdsrc-private.json and cds bind
if (!envFile) {
let dotEnvFile = getEnv()
dotenv.config({ path: dotEnvFile })
let cdsrcPrivate = getCdsrcPrivate()

//No Admin option or no default-env-admin.json file found - try for .env
if (!cdsrcPrivate) {
let dotEnvFile = getEnv()
dotenv.config({ path: dotEnvFile })
} else {
try {
const data = fs.readFileSync(cdsrcPrivate,
{ encoding: 'utf8', flag: 'r' })
const object = JSON.parse(data)
const resolveBinding = require('@sap/cds-dk/lib/bind/bindingResolver').resolver(LOG)
let resolvedService = await resolveBinding(null, object.requires['[hybrid]'].db.binding)
let options = { hana: resolvedService.credentials }
options.hana.pooling = true
base.debug(options)
base.debug(base.bundle.getText("connectionFile"))
base.debug(`.cdsrc-private.json`)
if (base.verboseOutput(prompts)) { console.log(`${base.bundle.getText("connFile2")} ${`.cdsrc-private.json`} \n`) }
return (options)
}
catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
// Re-throw not "Module not found" errors
throw e
}
throw base.bundle.getText("cds-dk2")
}
}
}



//No .env File found or it doesn't contain a VCAP_SERVICES - try other options
base.debug(process.env.VCAP_SERVICES)
base.debug(envFile)
Expand Down Expand Up @@ -185,10 +229,10 @@ export function getConnOptions(prompts) {
}

/**
* Create Databse Connection
* Create Database Connection
* @param {object} prompts - input prompt values
* @param {boolean} directConnect - Direct Connection parameters are supplied in prompts
* @returns {Promise<object>} HANA DB conneciton of type hdb
* @returns {Promise<object>} HANA DB connection of type hdb
*/
export async function createConnection(prompts, directConnect = false) {
base.debug('createConnection')
Expand All @@ -198,7 +242,7 @@ export async function createConnection(prompts, directConnect = false) {
if (directConnect) {
options.hana = prompts
} else {
options = getConnOptions(prompts)
options = await getConnOptions(prompts)
}

base.debug(`In Create Connection`)
Expand Down

0 comments on commit 8a59c18

Please sign in to comment.