-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add env-configs:configure command (#183)
Co-authored-by: Shashwat Khanna <Shashwat.Khanna@nuance.com>
- Loading branch information
Showing
6 changed files
with
286 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright 2022, Nuance, Inc. and its contributors. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in | ||
* the LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {FlagOutput} from '@oclif/core/lib/interfaces' | ||
import makeDebug from 'debug' | ||
import chalk from 'chalk' | ||
|
||
import * as MixFlags from '../../utils/flags' | ||
import MixCommand from '../../utils/base/mix-command' | ||
import {MixClient, MixResponse} from '../../mix/types' | ||
import {EnvConfigsConfigureParams} from '../../mix/api/env-configs-types' | ||
import {configureEnvConfigWithGeo, configureEnvConfigWithoutGeo} from '../../mix/api/env-configs' | ||
|
||
const debug = makeDebug('mix:commands:env-configs:configure') | ||
|
||
export default class EnvConfigsList extends MixCommand { | ||
static description = `Configure an environment configuration | ||
Environment configurations provide default values either for the project as a whole | ||
or for a specific environment geography. If an environment geography doest not have | ||
a default for a specific configuration, then the default for the project is used. | ||
Using this command with only the 'project' flag configures the project-level default value | ||
for the given configuration label. Using this command with the 'env' and 'env-geo' flags | ||
in addition to the 'project' flag configures the default value for the given configuration | ||
label targeting the specified environment geography. | ||
` | ||
|
||
static examples = [ | ||
'Configure an environment configuration project default', | ||
'mix env-configs:configure -P 1922 --label=GRAMMAR_BASE_PATH --value=https://www.example.com/grammars', | ||
'Configure an environment configuration for a specific environment geography', | ||
'mix env-configs:configure -P 1922 --env=1923 --env-geo=9 --label=GRAMMAR_BASE_PATH --value=https://www.example.com/grammars', | ||
] | ||
|
||
static flags = { | ||
project: MixFlags.projectFlag, | ||
env: { | ||
...MixFlags.envIDFlag, | ||
dependsOn: ['env-geo'], | ||
}, | ||
'env-geo': { | ||
...MixFlags.envGeoIDFlag, | ||
dependsOn: ['env'], | ||
}, | ||
label: MixFlags.labelFlag, | ||
value: MixFlags.valueFlag, | ||
} | ||
|
||
async buildRequestParameters(options: Partial<FlagOutput>): Promise<EnvConfigsConfigureParams> { | ||
debug('buildRequestParameters()') | ||
const { | ||
env, | ||
'env-geo': envGeoId, | ||
label, | ||
project, | ||
value, | ||
} = options | ||
|
||
return { | ||
envId: env, | ||
envGeoId, | ||
label, | ||
projectId: project, | ||
value, | ||
} | ||
} | ||
|
||
doRequest(client: MixClient, params: EnvConfigsConfigureParams): Promise<MixResponse> { | ||
debug('doRequest()') | ||
const {envId} = params | ||
|
||
if (envId) { | ||
return configureEnvConfigWithGeo(client, params) | ||
} | ||
|
||
return configureEnvConfigWithoutGeo(client, params) | ||
} | ||
|
||
outputHumanReadable() { | ||
debug('outputHumanReadable()') | ||
|
||
this.log() | ||
if (this.options.env) { | ||
this.log(`Environment configuration ${this.options.label} configured successfully for project ${chalk.cyan(this.options.project)}`) | ||
this.log(`In environment geography ${chalk.cyan(this.options['env-geo'])} of environment ${chalk.cyan(this.options.env)}`) | ||
} else { | ||
this.log(`Environment configuration ${this.options.label} default configured successfully for project ${chalk.cyan(this.options.project)}`) | ||
} | ||
|
||
this.log() | ||
} | ||
|
||
setRequestActionMessage(_options: any) { | ||
debug('setRequestActionMessage()') | ||
|
||
this.requestActionMessage = `Configuring environment configuration ${this.options.label} for project ${this.options.project}` | ||
if (this.options.env) { | ||
this.requestActionMessage += ` in environment geography ${this.options['env-geo']} of environment ${this.options.env}` | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Copyright 2022, Nuance, Inc. and its contributors. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in | ||
* the LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import { expect, test } from "@oclif/test"; | ||
|
||
const testData = require("../../test-data"); | ||
const serverURL = `https://${testData.server}`; | ||
|
||
describe("env-configs:configure command", () => { | ||
let project = 1; | ||
let envId = 1001; | ||
let envGeoId = 9; | ||
let label = "GRAMMAR_BASE_PATH"; | ||
let value = "http://www.test.com/"; | ||
|
||
test | ||
.env(testData.env) | ||
.nock(serverURL, (api) => | ||
api | ||
.put(`/v4/projects/${project}/env-configs/${label}`, `"${value}"`) | ||
.reply(200) | ||
) | ||
.stdout() | ||
.command([ | ||
"env-configs:configure", | ||
`--project=${project}`, | ||
`--label=${label}`, | ||
`--value=${value}`, | ||
]) | ||
.it("configures default environment configuration", (ctx) => { | ||
const [firstLine, secondLine] = ctx.stdout.split("\n"); | ||
expect(secondLine).to.contain(`configured successfully`); | ||
}); | ||
|
||
test | ||
.env(testData.env) | ||
.nock(serverURL, (api) => | ||
api | ||
.put( | ||
`/v4/environments/${envId}/geographies/${envGeoId}/configs/${label}`, | ||
`"${value}"` | ||
) | ||
.query({ | ||
projectId: project, | ||
}) | ||
.reply(200) | ||
) | ||
.stdout() | ||
.command([ | ||
"env-configs:configure", | ||
`--project=${project}`, | ||
`--env=${envId}`, | ||
`--env-geo=${envGeoId}`, | ||
`--label=${label}`, | ||
`--value=${value}`, | ||
]) | ||
.it("configures environment configuration with env and env-geo", (ctx) => { | ||
const [firstLine, secondLine] = ctx.stdout.split("\n"); | ||
expect(secondLine).to.contain(`configured successfully`); | ||
}); | ||
|
||
test | ||
.env(testData.env) | ||
.stderr() | ||
.command([ | ||
"env-configs:configure", | ||
`--project=${project}`, | ||
`--env=${envId}`, | ||
`--label=${label}`, | ||
`--value=${value}`, | ||
]) | ||
.catch((ctx) => { | ||
expect(ctx.message).to.contain( | ||
`All of the following must be provided when using --env: --env-geo` | ||
); | ||
}) | ||
.it("fails to configure environment configuration with env but no env-geo"); | ||
|
||
test | ||
.env(testData.env) | ||
.stderr() | ||
.command([ | ||
"env-configs:configure", | ||
`--project=${project}`, | ||
`--env-geo=${envGeoId}`, | ||
`--label=${label}`, | ||
`--value=${value}`, | ||
]) | ||
.catch((ctx) => { | ||
expect(ctx.message).to.contain( | ||
`All of the following must be provided when using --env-geo: --env` | ||
); | ||
}) | ||
.it("fails to configure environment configuration with env-geo but no env"); | ||
|
||
test | ||
.env(testData.env) | ||
.stderr() | ||
.command([ | ||
"env-configs:configure", | ||
`--label=${label}`, | ||
`--value=${value}`, | ||
]) | ||
.catch((ctx) => { | ||
expect(ctx.message).to.contain( | ||
`Missing required flag project` | ||
); | ||
}) | ||
.it("fails to configure environment configuration with no project ID"); | ||
|
||
test | ||
.env(testData.env) | ||
.stderr() | ||
.command([ | ||
"env-configs:configure", | ||
`--project=ok123`, | ||
`--label=${label}`, | ||
`--value=${value}`, | ||
]) | ||
.catch((ctx) => { | ||
expect(ctx.message).to.contain( | ||
'Expected an integer' | ||
); | ||
}) | ||
.it("fails to configure environment configuration with invalid project ID"); | ||
}); |