From b6d44ea5cfcdfebe07083e25d5120038cee2a96f Mon Sep 17 00:00:00 2001 From: SanketD92 Date: Mon, 26 Jul 2021 15:07:29 -0400 Subject: [PATCH] fix: prevent redundant looping --- .../lib/controllers/environment-sc-controller.js | 1 + .../service-catalog/environment-sc-cidr-service.js | 2 +- .../environment-sc-connection-service.js | 9 ++++++--- .../service-catalog/environment-sc-service.js | 14 ++------------ 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/addons/addon-base-raas/packages/base-raas-rest-api/lib/controllers/environment-sc-controller.js b/addons/addon-base-raas/packages/base-raas-rest-api/lib/controllers/environment-sc-controller.js index 224471b175..ecf1b386cc 100644 --- a/addons/addon-base-raas/packages/base-raas-rest-api/lib/controllers/environment-sc-controller.js +++ b/addons/addon-base-raas/packages/base-raas-rest-api/lib/controllers/environment-sc-controller.js @@ -30,6 +30,7 @@ async function configure(context) { const [environmentScService] = await context.service(['environmentScService']); const result = await environmentScService.mustFind(requestContext, { id }); + await environmentScService.verifyAppStreamConfig(requestContext, result.projectId); res.status(200).json(result); }), ); diff --git a/addons/addon-base-raas/packages/base-raas-services/lib/environment/service-catalog/environment-sc-cidr-service.js b/addons/addon-base-raas/packages/base-raas-services/lib/environment/service-catalog/environment-sc-cidr-service.js index cc4bb6c41d..e063d6fb10 100644 --- a/addons/addon-base-raas/packages/base-raas-services/lib/environment/service-catalog/environment-sc-cidr-service.js +++ b/addons/addon-base-raas/packages/base-raas-services/lib/environment/service-catalog/environment-sc-cidr-service.js @@ -102,7 +102,7 @@ class EnvironmentScCidrService extends Service { ); // Verify environment is linked to an AppStream project when application has AppStream enabled - await environmentScService.verifyAppStreamConfig(requestContext, id); + await environmentScService.verifyAppStreamConfig(requestContext, existingEnvironment.projectId); await lockService.tryWriteLockAndRun({ id: `${id}-CidrUpdate` }, async () => { // Calculate diff and update CIDR ranges in ingress rules diff --git a/addons/addon-base-raas/packages/base-raas-services/lib/environment/service-catalog/environment-sc-connection-service.js b/addons/addon-base-raas/packages/base-raas-services/lib/environment/service-catalog/environment-sc-connection-service.js index 9322188b57..edf84e2508 100644 --- a/addons/addon-base-raas/packages/base-raas-services/lib/environment/service-catalog/environment-sc-connection-service.js +++ b/addons/addon-base-raas/packages/base-raas-services/lib/environment/service-catalog/environment-sc-connection-service.js @@ -146,7 +146,8 @@ class EnvironmentScConnectionService extends Service { } // Verify environment is linked to an AppStream project when application has AppStream enabled - await environmentScService.verifyAppStreamConfig(requestContext, envId); + const { projectId } = await environmentScService.mustFind(requestContext, { id: envId }); + await environmentScService.verifyAppStreamConfig(requestContext, projectId); if (_.toLower(_.get(connection, 'type', '')) === 'sagemaker') { const sagemaker = await environmentScService.getClientSdkWithEnvMgmtRole( @@ -241,7 +242,8 @@ class EnvironmentScConnectionService extends Service { await validationService.ensureValid(sshConnectionInfo, sshConnectionInfoSchema); // Verify environment is linked to an AppStream project when application has AppStream enabled - await environmentScService.verifyAppStreamConfig(requestContext, envId); + const { projectId } = await environmentScService.mustFind(requestContext, { id: envId }); + await environmentScService.verifyAppStreamConfig(requestContext, projectId); // The following will succeed only if the user has permissions to access the specified environment const connection = await this.mustFindConnection(requestContext, envId, connectionId); @@ -313,7 +315,8 @@ class EnvironmentScConnectionService extends Service { ]); // Verify environment is linked to an AppStream project when application has AppStream enabled - await environmentScService.verifyAppStreamConfig(requestContext, envId); + const { projectId } = await environmentScService.mustFind(requestContext, { id: envId }); + await environmentScService.verifyAppStreamConfig(requestContext, projectId); // The following will succeed only if the user has permissions to access the specified environment // and connection 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 8362698d97..28cf7d555f 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 @@ -344,9 +344,6 @@ class EnvironmentScService extends Service { await this.assertAuthorized(requestContext, { action: 'get-sc', conditions: [this._allowAuthorized] }, result); } - // Verify environment is linked to an AppStream project when application has AppStream enabled - await this.verifyAppStreamConfig(requestContext, id); - const env = this._fromDbToDataObject(result); // We only check for the ingress rules of a successfully provisioned environment not in failure state @@ -470,10 +467,9 @@ class EnvironmentScService extends Service { return dbResult; } - async verifyAppStreamConfig(requestContext, envId) { + async verifyAppStreamConfig(requestContext, projectId) { // If the AppStream feature is enabled, verify the project linked to the environment has it configured if (this.isAppStreamEnabled) { - const { projectId } = await this.mustFind(requestContext, { id: envId }); const projectService = await this.service('projectService'); // The isAppStreamConfigured attribute value will be returned by project service. indexId field is enough for filtering const { isAppStreamConfigured } = await projectService.mustFind(requestContext, { @@ -505,7 +501,7 @@ class EnvironmentScService extends Service { ); // Verify environment is linked to an AppStream project when application has AppStream enabled - await this.verifyAppStreamConfig(requestContext, existingEnvironment.id); + await this.verifyAppStreamConfig(requestContext, existingEnvironment.projectId); const by = _.get(requestContext, 'principalIdentifier.uid'); const { id, rev } = environment; @@ -659,9 +655,6 @@ class EnvironmentScService extends Service { existingEnvironment, ); - // Verify environment is linked to an AppStream project when application has AppStream enabled - await this.verifyAppStreamConfig(requestContext, existingEnvironment.id); - const { status, outputs, projectId } = existingEnvironment; // expected environment run state based on operation @@ -900,9 +893,6 @@ class EnvironmentScService extends Service { existingEnvironment, ); - // Verify environment is linked to an AppStream project when application has AppStream enabled - await this.verifyAppStreamConfig(requestContext, existingEnvironment.id); - await this.update(requestContext, { id, rev: existingEnvironment.rev,