Skip to content

Commit 6e98764

Browse files
authored
breaking-change: ACNA-3833 - add Deploy Service support for some commands (#385)
1 parent abaf187 commit 6e98764

File tree

20 files changed

+356
-223
lines changed

20 files changed

+356
-223
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@
7070
},
7171
"repository": "adobe/aio-cli-plugin-runtime",
7272
"scripts": {
73-
"eslint-fix": "eslint src test e2e --fix",
74-
"posttest": "eslint src test e2e",
73+
"lint-fix": "eslint src test e2e --fix",
74+
"lint": "eslint src test e2e",
75+
"posttest": "npm run lint",
7576
"test": "npm run unit-tests",
7677
"unit-tests": "jest --ci",
7778
"prepack": "oclif manifest && oclif readme --no-aliases",

src/DeployServiceCommand.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
Copyright 2019 Adobe Inc. All rights reserved.
3+
This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License. You may obtain a copy
5+
of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
Unless required by applicable law or agreed to in writing, software distributed under
8+
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
OF ANY KIND, either express or implied. See the License for the specific language
10+
governing permissions and limitations under the License.
11+
*/
12+
13+
const { Flags } = require('@oclif/core')
14+
15+
const { PropertyDefault } = require('./properties')
16+
const runtimeLib = require('@adobe/aio-lib-runtime')
17+
const { getToken, context, CLI } = require('@adobe/aio-lib-ims')
18+
const { getCliEnv } = require('@adobe/aio-lib-env')
19+
const RuntimeBaseCommand = require('./RuntimeBaseCommand')
20+
21+
class DeployServiceCommand extends RuntimeBaseCommand {
22+
/**
23+
* Retrieves an access token for Adobe I/O CLI authentication.
24+
* This function handles both CLI and custom contexts, setting up the appropriate
25+
* authentication context and retrieving the corresponding access token.
26+
*
27+
* @async
28+
* @function getAccessToken
29+
* @param {object} [options] - Options for token retrieval
30+
* @param {string} [options.env] - The environment to use (e.g. 'prod', 'stage')
31+
* @param {boolean} [options.useCachedToken] - Whether to use a cached token instead of requesting a new one
32+
* @returns {Promise<{accessToken: string|null, env: string}>} An object containing:
33+
* - accessToken: The retrieved access token for authentication, or null if token retrieval failed
34+
* - env: The current CLI environment
35+
* @throws {Error} If token retrieval fails or context setup fails
36+
*/
37+
async getAccessToken ({ env = getCliEnv(), useCachedToken = false } = {}) {
38+
let contextName = CLI // default
39+
const currentContext = await context.getCurrent() // potential override
40+
41+
if (currentContext !== CLI) {
42+
contextName = currentContext
43+
} else {
44+
await context.setCli({ 'cli.bare-output': true }, false) // set this globally
45+
}
46+
47+
let accessToken = null
48+
if (useCachedToken) {
49+
const contextConfig = await context.get(contextName)
50+
accessToken = contextConfig?.access_token?.token
51+
} else {
52+
accessToken = await getToken(contextName)
53+
}
54+
55+
return { accessToken, env }
56+
}
57+
58+
getAuthHandler () {
59+
const env = getCliEnv()
60+
return {
61+
getAuthHeader: async () => {
62+
this.debug(`Retrieving CLI Token using env=${env}`)
63+
const { accessToken } = await this.getAccessToken({ env })
64+
65+
return `Bearer ${accessToken}`
66+
}
67+
}
68+
}
69+
70+
async setRuntimeApiHostAndAuthHandler (options) {
71+
let _options = structuredClone(options)
72+
if (!_options?.['use-runtime-auth']) {
73+
const endpoint = process.env.AIO_DEPLOY_SERVICE_URL ?? PropertyDefault.DEPLOYSERVICEURL
74+
_options = _options ?? {}
75+
_options.apihost = `${endpoint}/runtime`
76+
_options.auth_handler = this.getAuthHandler()
77+
}
78+
79+
return _options
80+
}
81+
82+
async wsk (options) {
83+
let _options = structuredClone(options)
84+
if (!_options) {
85+
_options = await super.getOptions()
86+
_options = await this.setRuntimeApiHostAndAuthHandler(_options)
87+
}
88+
return runtimeLib.init(_options)
89+
}
90+
}
91+
92+
DeployServiceCommand.flags = {
93+
...RuntimeBaseCommand.flags,
94+
'use-runtime-auth': Flags.boolean({ char: 'r', description: 'use Runtime auth [default: false]', default: false })
95+
}
96+
97+
module.exports = DeployServiceCommand

src/RuntimeBaseCommand.js

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ const debug = createDebug('aio-cli-plugin-runtime')
1818
const http = require('http')
1919
const runtimeLib = require('@adobe/aio-lib-runtime')
2020
const config = require('@adobe/aio-lib-core-config')
21-
const { getToken, context } = require('@adobe/aio-lib-ims')
22-
const { getCliEnv } = require('@adobe/aio-lib-env')
23-
const { CLI } = require('@adobe/aio-lib-ims/src/context')
2421

2522
class RuntimeBaseCommand extends Command {
2623
async getOptions () {
@@ -34,7 +31,8 @@ class RuntimeBaseCommand extends Command {
3431
apihost: flags.apihost || config.get('runtime.apihost') || properties.get('APIHOST') || PropertyDefault.APIHOST,
3532
namespace: config.get('runtime.namespace') || properties.get('NAMESPACE'),
3633
api_key: flags.auth || config.get('runtime.auth') || properties.get('AUTH'),
37-
ignore_certs: flags.insecure || config.get('runtime.insecure')
34+
ignore_certs: flags.insecure || config.get('runtime.insecure'),
35+
'use-runtime-auth': process.env.USE_RUNTIME_AUTH || flags['use-runtime-auth']
3836
}
3937

4038
// remove any null or undefined keys
@@ -66,24 +64,11 @@ class RuntimeBaseCommand extends Command {
6664
}
6765

6866
async wsk (options) {
69-
if (!options) {
70-
const authHandler = {
71-
getAuthHeader: async () => {
72-
await context.setCli({ 'cli.bare-output': true }, false) // set this globally
73-
const env = getCliEnv()
74-
console.debug(`Retrieving CLI Token using env=${env}`)
75-
const accessToken = await getToken(CLI)
76-
77-
return `Bearer ${accessToken}`
78-
}
79-
}
80-
options = await this.getOptions()
81-
if (process.env.IS_DEPLOY_SERVICE_ENABLED === 'true') {
82-
options.auth_handler = authHandler
83-
options.apihost = options.apihost ?? PropertyDefault.DEPLOYSERVICEURL
84-
}
67+
let _options = structuredClone(options)
68+
if (!_options) {
69+
_options = await this.getOptions()
8570
}
86-
return runtimeLib.init(options)
71+
return runtimeLib.init(_options)
8772
}
8873

8974
getImsOrgId () {

src/commands/runtime/action/create.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ const fs = require('fs')
1414
const { createKeyValueArrayFromFlag, createKeyValueArrayFromFile, createComponentsfromSequence, getKeyValueArrayFromMergedParameters } = require('@adobe/aio-lib-runtime').utils
1515
const { kindForFileExtension } = require('../../../kinds')
1616
const { Flags } = require('@oclif/core')
17-
const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
17+
const DeployServiceCommand = require('../../../DeployServiceCommand')
1818

19-
class ActionCreate extends RuntimeBaseCommand {
19+
class ActionCreate extends DeployServiceCommand {
2020
isUpdate () { return false }
2121

2222
async run () {
@@ -235,7 +235,8 @@ ActionCreate.args = [
235235
]
236236

237237
ActionCreate.flags = {
238-
...RuntimeBaseCommand.flags,
238+
...DeployServiceCommand.flags,
239+
239240
param: Flags.string({
240241
char: 'p',
241242
description: 'parameter values in KEY VALUE format', // help description for flag

src/commands/runtime/action/delete.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ OF ANY KIND, either express or implied. See the License for the specific languag
1010
governing permissions and limitations under the License.
1111
*/
1212

13-
const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
13+
const DeployServiceCommand = require('../../../DeployServiceCommand')
1414
const { Flags } = require('@oclif/core')
1515

16-
class ActionDelete extends RuntimeBaseCommand {
16+
class ActionDelete extends DeployServiceCommand {
1717
async run () {
1818
const { flags, args } = await this.parse(ActionDelete)
1919
const name = args.actionName
@@ -37,7 +37,7 @@ ActionDelete.args = [
3737
]
3838

3939
ActionDelete.flags = {
40-
...RuntimeBaseCommand.flags,
40+
...DeployServiceCommand.flags,
4141
json: Flags.boolean({
4242
description: 'output raw json'
4343
})

src/commands/runtime/api/create.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ OF ANY KIND, either express or implied. See the License for the specific languag
99
governing permissions and limitations under the License.
1010
*/
1111

12-
const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
12+
const DeployServiceCommand = require('../../../DeployServiceCommand')
1313
const { Flags } = require('@oclif/core')
1414
const fs = require('fs')
1515

16-
class ApiCreate extends RuntimeBaseCommand {
16+
class ApiCreate extends DeployServiceCommand {
1717
async run () {
1818
const { args, flags } = await this.parse(ApiCreate)
1919

@@ -73,7 +73,7 @@ ApiCreate.args = [
7373
]
7474

7575
ApiCreate.flags = {
76-
...RuntimeBaseCommand.flags,
76+
...DeployServiceCommand.flags,
7777
apiname: Flags.string({
7878
char: 'n',
7979
description: 'Friendly name of the API; ignored when CFG_FILE is specified (default BASE_PATH)',

src/commands/runtime/api/delete.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ OF ANY KIND, either express or implied. See the License for the specific languag
99
governing permissions and limitations under the License.
1010
*/
1111

12-
const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
12+
const DeployServiceCommand = require('../../../DeployServiceCommand')
1313
// eslint-disable-next-line no-unused-vars
1414

15-
class ApiDelete extends RuntimeBaseCommand {
15+
class ApiDelete extends DeployServiceCommand {
1616
async run () {
1717
const { args } = await this.parse(ApiDelete)
1818

@@ -49,7 +49,7 @@ ApiDelete.args = [
4949
]
5050

5151
ApiDelete.flags = {
52-
...RuntimeBaseCommand.flags
52+
...DeployServiceCommand.flags
5353
}
5454

5555
ApiDelete.description = 'delete an API'

src/commands/runtime/deploy/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class IndexCommand extends RuntimeBaseCommand {
2828
const params = getKeyValueObjectFromMergedParameters(flags.param, flags['param-file'])
2929
const options = await this.getOptions()
3030
const entities = processPackage(packages, deploymentPackages, deploymentTriggers, params, false, options)
31-
const ow = await this.wsk(options)
31+
const ow = await this.wsk()
3232
const logger = this.log
3333
await deployPackage(entities, ow, logger.bind(this), this.getImsOrgId())
3434
} catch (err) {

src/commands/runtime/deploy/sync.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ OF ANY KIND, either express or implied. See the License for the specific languag
1010
governing permissions and limitations under the License.
1111
*/
1212

13-
const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
13+
const DeployServiceCommand = require('../../../DeployServiceCommand')
1414
const { setPaths, processPackage, syncProject } = require('@adobe/aio-lib-runtime').utils
1515
const { Flags } = require('@oclif/core')
1616

17-
class DeploySync extends RuntimeBaseCommand {
17+
class DeploySync extends DeployServiceCommand {
1818
async run () {
1919
const { flags } = await this.parse(DeploySync)
2020
try {
@@ -28,8 +28,9 @@ class DeploySync extends RuntimeBaseCommand {
2828
}
2929
const params = {}
3030
const options = await this.getOptions()
31+
delete options['use-runtime-auth']
3132
const entities = processPackage(packages, deploymentPackages, deploymentTriggers, params, false, options)
32-
const ow = await this.wsk(options)
33+
const ow = await this.wsk()
3334
const logger = this.log
3435
await syncProject(components.projectName, components.manifestPath, components.manifestContent, entities, ow, logger.bind(this), this.getImsOrgId())
3536
} catch (err) {
@@ -39,7 +40,7 @@ class DeploySync extends RuntimeBaseCommand {
3940
}
4041

4142
DeploySync.flags = {
42-
...RuntimeBaseCommand.flags,
43+
...DeployServiceCommand.flags,
4344
manifest: Flags.string({
4445
char: 'm',
4546
description: 'the manifest file location' // help description for flag

src/commands/runtime/deploy/undeploy.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ OF ANY KIND, either express or implied. See the License for the specific languag
1010
governing permissions and limitations under the License.
1111
*/
1212

13-
const RuntimeBaseCommand = require('../../../RuntimeBaseCommand')
13+
const DeployServiceCommand = require('../../../DeployServiceCommand')
1414
const { getProjectEntities, undeployPackage, processPackage, setPaths } = require('@adobe/aio-lib-runtime').utils
1515
const { Flags } = require('@oclif/core')
1616

17-
class DeployUndeploy extends RuntimeBaseCommand {
17+
class DeployUndeploy extends DeployServiceCommand {
1818
async run () {
1919
const { flags } = await this.parse(DeployUndeploy)
2020
try {
2121
const options = await this.getOptions()
22-
const ow = await this.wsk(options)
22+
delete options['use-runtime-auth']
23+
const ow = await this.wsk()
2324
const logger = this.log
2425

2526
let entities
@@ -44,7 +45,7 @@ class DeployUndeploy extends RuntimeBaseCommand {
4445
}
4546

4647
DeployUndeploy.flags = {
47-
...RuntimeBaseCommand.flags,
48+
...DeployServiceCommand.flags,
4849
manifest: Flags.string({
4950
char: 'm',
5051
description: 'the manifest file location' // help description for flag

0 commit comments

Comments
 (0)