From 15eb4d35320c2ba9babaa946fbecb34a9ec393b2 Mon Sep 17 00:00:00 2001 From: Tim Nguyen Date: Wed, 9 Mar 2022 15:44:07 -0500 Subject: [PATCH] fix: Return badRequest if trying to terminate an environment that has already been terminated (#946) --- .../__tests__/environment-sc-service.test.js | 87 +++++++++++++++++++ .../service-catalog/environment-sc-service.js | 15 ++++ 2 files changed, 102 insertions(+) diff --git a/addons/addon-base-raas/packages/base-raas-services/lib/environment/service-catalog/__tests__/environment-sc-service.test.js b/addons/addon-base-raas/packages/base-raas-services/lib/environment/service-catalog/__tests__/environment-sc-service.test.js index fc39e3cfb8..dbf60a7d9b 100644 --- a/addons/addon-base-raas/packages/base-raas-services/lib/environment/service-catalog/__tests__/environment-sc-service.test.js +++ b/addons/addon-base-raas/packages/base-raas-services/lib/environment/service-catalog/__tests__/environment-sc-service.test.js @@ -35,6 +35,7 @@ const SettingsServiceMock = require('@aws-ee/base-services/lib/settings/env-sett jest.mock('@aws-ee/base-services/lib/audit/audit-writer-service'); const AuditServiceMock = require('@aws-ee/base-services/lib/audit/audit-writer-service'); +const environmentScStatus = require('../environent-sc-status-enum'); jest.mock('../../environment-authz-service.js'); const EnvironmentAuthZServiceMock = require('../../environment-authz-service.js'); @@ -1392,6 +1393,92 @@ Quisque egestas, eros nec feugiat venenatis, lorem turpis placerat tortor, ullam } }); + it('should fail because the environment is already terminated', async () => { + // BUILD + const requestContext = { + principal: { + isExternalUser: false, + }, + }; + const existingEnv = { + id: 'abc', + name: 'exampleName', + envTypeId: 'exampleETI', + envTypeConfigId: 'exampleETCI', + status: environmentScStatus.TERMINATED, + }; + + service.mustFind = jest.fn().mockResolvedValueOnce(existingEnv); + + // OPERATE + try { + await service.delete(requestContext, { id: existingEnv.id }); + expect.hasAssertions(); + } catch (err) { + // CHECK + expect(service.boom.is(err, 'badRequest')).toBe(true); + expect(err.message).toContain(`Workspace '${existingEnv.id}' has already been terminated`); + } + }); + + it('should fail because the environment is being terminated', async () => { + // BUILD + const requestContext = { + principal: { + isExternalUser: false, + }, + }; + const existingEnv = { + id: 'abc', + name: 'exampleName', + envTypeId: 'exampleETI', + envTypeConfigId: 'exampleETCI', + status: environmentScStatus.TERMINATING, + }; + + service.mustFind = jest.fn().mockResolvedValueOnce(existingEnv); + + // OPERATE + try { + await service.delete(requestContext, { id: existingEnv.id }); + expect.hasAssertions(); + } catch (err) { + // CHECK + expect(service.boom.is(err, 'badRequest')).toBe(true); + expect(err.message).toContain(`Workspace '${existingEnv.id}' is already being terminated`); + } + }); + + it('should fail because the environment is in termination failed status', async () => { + // BUILD + const requestContext = { + principal: { + isExternalUser: false, + }, + }; + const existingEnv = { + id: 'abc', + name: 'exampleName', + envTypeId: 'exampleETI', + envTypeConfigId: 'exampleETCI', + status: environmentScStatus.TERMINATING_FAILED, + }; + + service.mustFind = jest.fn().mockResolvedValueOnce(existingEnv); + + // OPERATE + try { + await service.delete(requestContext, { id: existingEnv.id }); + expect.hasAssertions(); + } catch (err) { + // CHECK + expect(service.boom.is(err, 'badRequest')).toBe(true); + expect(err.message).toContain( + `Workspace '${existingEnv.id}' can not be terminated while in ${environmentScStatus.TERMINATING_FAILED} status`, + ); + } + }); + it('should fail because the workflow failed to trigger', async () => { // BUILD const requestContext = { diff --git a/addons/addon-base-raas/packages/base-raas-services/lib/environment/service-catalog/environment-sc-service.js b/addons/addon-base-raas/packages/base-raas-services/lib/environment/service-catalog/environment-sc-service.js index 211b0c9e94..170a5ec350 100644 --- a/addons/addon-base-raas/packages/base-raas-services/lib/environment/service-catalog/environment-sc-service.js +++ b/addons/addon-base-raas/packages/base-raas-services/lib/environment/service-catalog/environment-sc-service.js @@ -979,6 +979,21 @@ class EnvironmentScService extends Service { existingEnvironment, ); + if (existingEnvironment.status === environmentScStatus.TERMINATED) { + throw this.boom.badRequest(`Workspace '${id}' has already been terminated`, true); + } + + if (existingEnvironment.status === environmentScStatus.TERMINATING) { + throw this.boom.badRequest(`Workspace '${id}' is already being terminated`, true); + } + + if (existingEnvironment.status === environmentScStatus.TERMINATING_FAILED) { + throw this.boom.badRequest( + `Workspace '${id}' can not be terminated while in ${environmentScStatus.TERMINATING_FAILED} status`, + true, + ); + } + await this.update(requestContext, { id, rev: existingEnvironment.rev,