Skip to content

Commit d0a93ac

Browse files
committed
feat(variables): support service-specific environment variables. fixes #268
1 parent f812762 commit d0a93ac

File tree

7 files changed

+341
-51
lines changed

7 files changed

+341
-51
lines changed

README.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -402,19 +402,28 @@ ARGUMENTS
402402
ENVIRONMENTID the environment id
403403
404404
OPTIONS
405-
-d, --delete=delete variables/secrets to delete
406-
-j, --json output in json format
407-
-p, --programId=programId the programId. if not specified, defaults to 'cloudmanager_programid' config value
408-
-s, --secret=secret secret values in KEY VALUE format
409-
-v, --variable=variable variable values in KEY VALUE format
410-
-y, --yaml output in yaml format
411-
--imsContextName=imsContextName the alternate IMS context name to use instead of aio-cli-plugin-cloudmanager
405+
-d, --delete=delete variables/secrets to delete
406+
-j, --json output in json format
407+
-p, --programId=programId the programId. if not specified, defaults to 'cloudmanager_programid' config value
408+
-s, --secret=secret secret values in KEY VALUE format
409+
-v, --variable=variable variable values in KEY VALUE format
410+
-y, --yaml output in yaml format
411+
--authorDelete=authorDelete variables/secrets to delete for author service
412+
--authorSecret=authorSecret secret values in KEY VALUE format for author service
413+
--authorVariable=authorVariable variable values in KEY VALUE format for author service
414+
--imsContextName=imsContextName the alternate IMS context name to use instead of aio-cli-plugin-cloudmanager
412415
413-
--jsonFile=jsonFile if set, read variables from a JSON array provided as a file; variables set through
414-
--variable or --secret flag will take precedence
416+
--jsonFile=jsonFile if set, read variables from a JSON array provided as a file; variables set through
417+
--variable or --secret flag will take precedence
415418
416-
--jsonStdin if set, read variables from a JSON array provided as standard input; variables set
417-
through --variable or --secret flag will take precedence
419+
--jsonStdin if set, read variables from a JSON array provided as standard input; variables set
420+
through --variable or --secret flag will take precedence
421+
422+
--publishDelete=publishDelete variables/secrets to delete for publish service
423+
424+
--publishSecret=publishSecret secret values in KEY VALUE format for publish service
425+
426+
--publishVariable=publishVariable variable values in KEY VALUE format for publish service
418427
419428
ALIASES
420429
$ aio cloudmanager:set-environment-variables

src/base-environment-variables-command.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ const { initSdk, sanitizeEnvironmentId } = require('./cloudmanager-helpers')
1414
const BaseVariablesCommand = require('./base-variables-command')
1515

1616
class BaseEnvironmentVariablesCommand extends BaseVariablesCommand {
17+
outputTable (result, flags) {
18+
super.outputTable(result, flags, {
19+
service: {
20+
get: (item) => item.service || '',
21+
},
22+
})
23+
}
24+
1725
async getVariables (programId, args, imsContextName = null) {
1826
const environmentId = sanitizeEnvironmentId(args.environmentId)
1927
const sdk = await initSdk(imsContextName)

src/base-variables-command.js

Lines changed: 69 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,28 @@ const { getPipedData } = require('@adobe/aio-lib-core-config')
2222
const _ = require('lodash')
2323

2424
class BaseVariablesCommand extends Command {
25-
outputTable (result, flags) {
25+
getFlagDefs () {
26+
return {
27+
variable: {
28+
type: 'string',
29+
},
30+
secret: {
31+
type: 'secretString',
32+
},
33+
delete: {
34+
action: 'delete',
35+
},
36+
}
37+
}
38+
39+
outputTable (result, flags, extraColumns = {}) {
2640
cli.table(result, {
2741
name: {},
2842
type: {},
2943
value: {
3044
get: (item) => item.type === 'secretString' ? '****' : item.value,
3145
},
46+
...extraColumns,
3247
}, {
3348
output: getOutputFormat(flags),
3449
})
@@ -63,44 +78,55 @@ class BaseVariablesCommand extends Command {
6378

6479
async prepareVariableList (flags, currentVariablesList) {
6580
const currentVariableTypes = {}
66-
currentVariablesList.forEach(variable => (currentVariableTypes[variable.name] = variable.type))
81+
currentVariablesList.forEach(variable => {
82+
const tempName = variable.service ? `${variable.service}:${variable.name}` : variable.name
83+
currentVariableTypes[tempName] = variable.type
84+
})
6785

6886
const variables = []
69-
if (flags.variable) {
70-
// each --param flag expects two values ( a key and a value ). Multiple --parm flags can be passed
71-
// For example : aio runtime:action:create --param name "foo" --param city "bar"
72-
const parsedVariables = createKeyValueObjectFromFlag(flags.variable)
73-
for (const key in parsedVariables) {
74-
variables.push({
75-
name: key,
76-
value: parsedVariables[key],
77-
type: 'string',
78-
})
79-
}
80-
}
81-
if (flags.secret) {
82-
const parsedSecrets = createKeyValueObjectFromFlag(flags.secret)
83-
for (const key in parsedSecrets) {
84-
variables.push({
85-
name: key,
86-
value: parsedSecrets[key],
87-
type: 'secretString',
88-
})
87+
88+
const flagDefs = this.getFlagDefs()
89+
90+
Object.keys(flagDefs).forEach(flagName => {
91+
const flagDef = flagDefs[flagName]
92+
if (!flags[flagName]) {
93+
return
8994
}
90-
}
91-
if (flags.delete) {
92-
flags.delete.forEach(key => {
93-
if (currentVariableTypes[key]) {
94-
variables.push({
95-
name: key,
96-
type: currentVariableTypes[key],
97-
value: '',
95+
switch (flagDef.action) {
96+
case 'delete':
97+
flags[flagName].forEach(key => {
98+
const currentVariableKey = flagDef.service ? `${flagDef.service}:${key}` : key
99+
if (currentVariableTypes[currentVariableKey]) {
100+
const newVar = {
101+
name: key,
102+
type: currentVariableTypes[currentVariableKey],
103+
value: '',
104+
}
105+
if (flagDef.service) {
106+
newVar.service = flagDef.service
107+
}
108+
variables.push(newVar)
109+
} else {
110+
this.warn(`Variable ${key} not found. Will not try to delete.`)
111+
}
98112
})
99-
} else {
100-
this.warn(`Variable ${key} not found. Will not try to delete.`)
113+
break
114+
default: {
115+
const parsedFlag = createKeyValueObjectFromFlag(flags[flagName])
116+
for (const key in parsedFlag) {
117+
const newVar = {
118+
name: key,
119+
value: parsedFlag[key],
120+
type: flagDef.type,
121+
}
122+
if (flagDef.service) {
123+
newVar.service = flagDef.service
124+
}
125+
variables.push(newVar)
126+
}
101127
}
102-
})
103-
}
128+
}
129+
})
104130

105131
if (flags.jsonStdin) {
106132
const rawStdinData = await getPipedData()
@@ -129,18 +155,19 @@ class BaseVariablesCommand extends Command {
129155
if (!item.type) {
130156
item.type = 'string'
131157
}
132-
if (currentVariableTypes[item.name] && !item.value) {
133-
item.type = currentVariableTypes[item.name]
158+
const currentVariableKey = item.service ? `${item.service}:${item.name}` : item.name
159+
if (currentVariableTypes[currentVariableKey] && !item.value) {
160+
item.type = currentVariableTypes[currentVariableKey]
134161
}
135-
if (!variables.find(variable => variable.name === item.name)) {
162+
if (!variables.find(variable => variable.name === item.name && variables.services === item.service)) {
136163
variables.push(item)
137164
}
138165
}
139166
})
140167
}
141168
}
142169

143-
BaseVariablesCommand.setterFlags = {
170+
BaseVariablesCommand.coreSetterFlags = {
144171
variable: flags.string({
145172
char: 'v',
146173
description: 'variable values in KEY VALUE format',
@@ -156,6 +183,10 @@ BaseVariablesCommand.setterFlags = {
156183
description: 'variables/secrets to delete',
157184
multiple: true,
158185
}),
186+
}
187+
188+
BaseVariablesCommand.setterFlags = {
189+
...BaseVariablesCommand.coreSetterFlags,
159190
jsonStdin: flags.boolean({
160191
default: false,
161192
description: 'if set, read variables from a JSON array provided as standard input; variables set through --variable or --secret flag will take precedence',

src/commands/cloudmanager/environment/set-variables.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,30 @@ governing permissions and limitations under the License.
1313
const BaseEnvironmentVariablesCommand = require('../../../base-environment-variables-command')
1414
const BaseVariablesCommand = require('../../../base-variables-command')
1515
const { initSdk, sanitizeEnvironmentId } = require('../../../cloudmanager-helpers')
16+
const { flags } = require('@oclif/command')
17+
const _ = require('lodash')
1618
const commonFlags = require('../../../common-flags')
1719

20+
const services = ['author', 'publish']
21+
1822
class SetEnvironmentVariablesCommand extends BaseEnvironmentVariablesCommand {
23+
getFlagDefs () {
24+
const coreFlagDefs = super.getFlagDefs()
25+
const result = {
26+
...coreFlagDefs,
27+
}
28+
services.forEach(service => {
29+
Object.keys(coreFlagDefs).forEach(coreFlagKey => {
30+
const flagName = _.camelCase(`${service} ${coreFlagKey}`)
31+
result[flagName] = {
32+
...coreFlagDefs[coreFlagKey],
33+
service,
34+
}
35+
})
36+
})
37+
return result
38+
}
39+
1940
async run () {
2041
const { args, flags } = this.parse(SetEnvironmentVariablesCommand)
2142

@@ -41,6 +62,17 @@ SetEnvironmentVariablesCommand.flags = {
4162
...BaseVariablesCommand.setterFlags,
4263
}
4364

65+
services.forEach(service => {
66+
Object.keys(BaseVariablesCommand.coreSetterFlags).forEach(coreFlagKey => {
67+
const coreFlag = BaseVariablesCommand.coreSetterFlags[coreFlagKey]
68+
const flagName = _.camelCase(`${service} ${coreFlagKey}`)
69+
SetEnvironmentVariablesCommand.flags[flagName] = flags.string({
70+
description: `${coreFlag.description} for ${service} service`,
71+
multiple: true,
72+
})
73+
})
74+
})
75+
4476
SetEnvironmentVariablesCommand.aliases = [
4577
'cloudmanager:set-environment-variables',
4678
]

test/__mocks__/@adobe/aio-lib-cloudmanager.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ function createDefaultMock () {
8585
}, {
8686
name: 'I_AM_A_SECRET',
8787
type: 'secretString',
88+
}, {
89+
name: 'AUTHOR_SECRET',
90+
value: 'something',
91+
service: 'author',
92+
type: 'secretString',
8893
}])),
8994
setEnvironmentVariables: jest.fn(() => Promise.resolve()),
9095
getPipelineVariables: jest.fn(() => Promise.resolve([{

test/commands/environment/list-variables.test.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ test('list-environment-variables - success', async () => {
5050
cloudmanager_programid: '4',
5151
})
5252

53-
expect.assertions(7)
53+
expect.assertions(9)
5454

5555
const runResult = ListEnvironmentVariablesCommand.run(['1'])
5656
await expect(runResult instanceof Promise).toBeTruthy()
@@ -69,6 +69,17 @@ test('list-environment-variables - success', async () => {
6969
type: 'string',
7070
value: 'value',
7171
})).toBe('value')
72+
await expect(cli.table.mock.calls[0][1].service.get({
73+
name: 'KEY',
74+
type: 'string',
75+
value: 'value',
76+
})).toBe('')
77+
await expect(cli.table.mock.calls[0][1].service.get({
78+
name: 'KEY',
79+
type: 'string',
80+
value: 'value',
81+
service: 'author',
82+
})).toBe('author')
7283
})
7384

7485
test('list-environment-variables for "e" prefixed env id - success', async () => {
@@ -88,5 +99,11 @@ test('list-environment-variables for "e" prefixed env id - success', async () =>
8899
}, {
89100
name: 'I_AM_A_SECRET',
90101
type: 'secretString',
91-
}])
102+
}, {
103+
name: 'AUTHOR_SECRET',
104+
service: 'author',
105+
type: 'secretString',
106+
value: 'something',
107+
},
108+
])
92109
})

0 commit comments

Comments
 (0)