Skip to content

Commit 8d094fe

Browse files
authored
feature expose skip state refresh on plan phase to our CLI (#104)
1 parent 766ce6c commit 8d094fe

File tree

10 files changed

+73
-15
lines changed

10 files changed

+73
-15
lines changed

node/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ For sensitive environment variables, use:
115115
- Usage: `--requiresApproval <true/false>`
116116
- Alias: `-a`
117117

118+
### Skip State Refresh
119+
> Disable automatic state refresh on 'plan -destroy' phase.
120+
- Usage: `--skipStateRefresh <true/false>`
121+
- Alias: `-z`
122+
118123
### Targets (Partial Apply)
119124
> A list of resources to explicitly include in the deployment, delimited by a comma. Format is "resource1,resource2,resource3"
120125
- Usage: `--targets`
@@ -143,6 +148,7 @@ https://docs.env0.com/reference
143148
You can compile and run the CLI from source by cloning the repo from Github and then running the following:
144149
```bash
145150
# clone the repo from github
151+
# make sure env0 lib is not installed globally
146152
yarn install
147153
yarn link # link your local copy of the CLI to your terminal path
148154
```

node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@env0/cli",
3-
"version": "1.0.16",
3+
"version": "1.0.17",
44
"description": "env0 CLI",
55
"repository": {
66
"type": "git",

node/src/commands/deploy.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ const deploy = async (options, variables) => {
3636
deployment = environment.latestDeploymentLog;
3737
} else {
3838
assertNoWorkspaceNameChanges(options, environment);
39-
const optionsWithoutWorkspace = { ...options }
40-
delete optionsWithoutWorkspace[WORKSPACE_NAME]
39+
const optionsWithoutWorkspace = { ...options };
40+
delete optionsWithoutWorkspace[WORKSPACE_NAME];
4141
deployment = await deployUtils.deployEnvironment(environment, optionsWithoutWorkspace, configurationChanges);
4242
}
4343

node/src/commands/destroy.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const DeployUtils = require('../lib/deploy-utils');
22
const { options } = require('../config/constants');
33
const _ = require('lodash');
4-
const { convertStringToBoolean } = require('../lib/general-utils');
4+
const { convertStringToBoolean, removeEmptyValuesFromObj } = require('../lib/general-utils');
55

6-
const { PROJECT_ID, ENVIRONMENT_NAME, REQUIRES_APPROVAL } = options;
6+
const { PROJECT_ID, ENVIRONMENT_NAME, REQUIRES_APPROVAL, SKIP_STATE_REFRESH } = options;
77

88
const assertEnvironmentExists = environment => {
99
if (!environment) {
@@ -25,7 +25,10 @@ const destroy = async options => {
2525
await deployUtils.updateEnvironment(environment, { requiresApproval: requiresApproval });
2626
}
2727

28-
const deployment = await deployUtils.destroyEnvironment(environment);
28+
const params = removeEmptyValuesFromObj({
29+
[SKIP_STATE_REFRESH]: options[SKIP_STATE_REFRESH]
30+
});
31+
const deployment = await deployUtils.destroyEnvironment(environment, params);
2932

3033
status = await deployUtils.pollDeploymentStatus(deployment);
3134

node/src/config/arguments.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
TERRAFORM_VARIABLES,
1313
SENSITIVE_ENVIRONMENT_VARIABLES,
1414
REVISION,
15+
SKIP_STATE_REFRESH,
1516
REQUIRES_APPROVAL,
1617
TARGETS
1718
} = options;
@@ -119,6 +120,13 @@ const argumentsMap = {
119120
group: ['deploy'],
120121
description: 'Your git revision, can be a branch tag or a commit hash. Default value "master" '
121122
},
123+
[SKIP_STATE_REFRESH]: {
124+
name: SKIP_STATE_REFRESH,
125+
alias: 'z',
126+
type: String,
127+
group: ['destroy'],
128+
description: 'Disable automatic state refresh on plan destroy phase'
129+
},
122130
[TARGETS]: {
123131
name: TARGETS,
124132
alias: 't',

node/src/config/commands.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { argumentsMap, allArguments, baseArguments } = require('./arguments');
22
const { options } = require('./constants');
33

4-
const { API_KEY, API_SECRET, ORGANIZATION_ID, PROJECT_ID, ENVIRONMENT_NAME, BLUEPRINT_ID, REQUIRES_APPROVAL } = options;
4+
const { API_KEY, API_SECRET, ORGANIZATION_ID, PROJECT_ID, ENVIRONMENT_NAME, BLUEPRINT_ID, SKIP_STATE_REFRESH, REQUIRES_APPROVAL } = options;
55

66
const commands = {
77
deploy: {
@@ -14,7 +14,7 @@ const commands = {
1414
]
1515
},
1616
destroy: {
17-
options: [...baseArguments, argumentsMap[REQUIRES_APPROVAL]],
17+
options: [...baseArguments, argumentsMap[REQUIRES_APPROVAL], argumentsMap[SKIP_STATE_REFRESH]],
1818
help: [
1919
{
2020
desc: 'Destroys an environment',

node/src/config/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const options = {
1010
TERRAFORM_VARIABLES: 'terraformVariables',
1111
SENSITIVE_ENVIRONMENT_VARIABLES: 'sensitiveEnvironmentVariables',
1212
REVISION: 'revision',
13+
SKIP_STATE_REFRESH: 'skipStateRefresh',
1314
REQUIRES_APPROVAL: 'requiresApproval',
1415
TARGETS: 'targets'
1516
};

node/src/lib/deploy-utils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,12 @@ class DeployUtils {
7676
});
7777
}
7878

79-
async destroyEnvironment(environment) {
79+
async destroyEnvironment(environment, params) {
8080
logger.info('Starting to destroy environment...');
8181

82-
return await apiClient.callApi('post', `environments/${environment.id}/destroy`);
82+
return await apiClient.callApi('post', `environments/${environment.id}/destroy`, {
83+
params
84+
});
8385
}
8486

8587
async writeDeploymentStepLog(deploymentLogId, stepName) {

node/tests/commands/destroy.spec.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jest.mock('../../src/lib/deploy-utils');
1212

1313
const mockDeployment = { id: 'id0' };
1414

15-
const { ENVIRONMENT_NAME, REQUIRES_APPROVAL } = options;
15+
const { ENVIRONMENT_NAME, REQUIRES_APPROVAL, SKIP_STATE_REFRESH } = options;
1616

1717
describe('destroy', () => {
1818
beforeEach(() => {
@@ -35,11 +35,11 @@ describe('destroy', () => {
3535

3636
await destroy({});
3737

38-
expect(mockDestroyEnvironment).toBeCalledWith(mockEnvironment);
38+
expect(mockDestroyEnvironment).toHaveBeenCalledWith(mockEnvironment, expect.anything());
3939
expect(mockPollDeploymentStatus).toBeCalledWith(mockDeployment);
4040
});
4141

42-
it('should fail when environment doesnt exist', async () => {
42+
it("should fail when environment doesn't exist", async () => {
4343
const mockEnvironmentName = 'environment0';
4444
mockGetEnvironment.mockResolvedValue(undefined);
4545

@@ -49,6 +49,21 @@ describe('destroy', () => {
4949
);
5050
});
5151

52+
describe('skipStateRefresh argument', () => {
53+
it.each`
54+
options
55+
${{ [SKIP_STATE_REFRESH]: 'true' }}
56+
${{ [SKIP_STATE_REFRESH]: 'false' }}
57+
${{}}
58+
`('should call destroyEnvironment with skipStateRefresh Option, options=$options', async ({ options }) => {
59+
const mockEnvironment = { id: 'something', name: 'someone' };
60+
mockGetEnvironment.mockResolvedValue(mockEnvironment);
61+
62+
await destroy(options);
63+
expect(mockDestroyEnvironment).toBeCalledWith(expect.anything(), options);
64+
});
65+
});
66+
5267
describe('requires approval argument', () => {
5368
it.each`
5469
option | existing
@@ -74,7 +89,7 @@ describe('destroy', () => {
7489

7590
await destroy({ [REQUIRES_APPROVAL]: option });
7691

77-
expect(mockUpdateEnvironment).toBeCalledWith(expect.anything(), { requiresApproval: expected });
92+
expect(mockUpdateEnvironment).toBeCalledWith(expect.anything(), { [REQUIRES_APPROVAL]: expected });
7893
});
7994
});
8095
});

node/tests/lib/deploy-utils.spec.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ jest.mock('../../src/lib/api-client', () =>
1010
}))
1111
);
1212

13-
const { BLUEPRINT_ID, REVISION, REQUIRES_APPROVAL, TARGETS, ENVIRONMENT_NAME, PROJECT_ID } = options;
13+
const {
14+
BLUEPRINT_ID,
15+
REVISION,
16+
SKIP_STATE_REFRESH,
17+
REQUIRES_APPROVAL,
18+
TARGETS,
19+
ENVIRONMENT_NAME,
20+
PROJECT_ID
21+
} = options;
1422

1523
const mockDeploymentId = 'deployment0';
1624
const mockDeployment = { id: mockDeploymentId };
@@ -45,6 +53,21 @@ describe('deploy utils', () => {
4553
});
4654
});
4755

56+
describe('skip automatic state refresh', () => {
57+
it.each`
58+
params
59+
${{ [SKIP_STATE_REFRESH]: 'true' }}
60+
${{}}
61+
`(`should call the api with the right query params param=$params`, async ({ params }) => {
62+
mockCallApi.mockResolvedValue([]);
63+
64+
await deployUtils.destroyEnvironment({ id: mockDeploymentId }, params);
65+
expect(mockCallApi).toBeCalledWith('post', `environments/${mockDeploymentId}/destroy`, {
66+
params
67+
});
68+
});
69+
});
70+
4871
describe('write deployment steps', () => {
4972
it('should call api', async () => {
5073
mockCallApi.mockResolvedValue([]);

0 commit comments

Comments
 (0)