From 91212aa16d1fe75adefa8fee536d8df903287eae Mon Sep 17 00:00:00 2001 From: rathnapandi Date: Sat, 26 Oct 2024 15:34:13 -0700 Subject: [PATCH] - Fix issue #442 --- CHANGELOG.md | 6 +++ .../apim/setup/APIManagerSettingsApp.java | 49 +++++++++---------- .../apim/setup/APIManagerSettingsAppTest.java | 9 ++++ .../setup/adapter/remove-global-quotas.json | 8 +++ 4 files changed, 47 insertions(+), 25 deletions(-) create mode 100644 modules/settings/src/test/resources/com/axway/apim/setup/adapter/remove-global-quotas.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 8447cb167..85abcc8b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +# [1.14.8] In progress + +## Fixed +- Removing existing GLOBAL quotas in Api Manager(See issue [#442](https://github.com/Axway-API-Management-Plus/apim-cli/issues/442)) + + # [1.14.7] 2024-10-24 ## Fixed - NullPointerException: Cannot invoke "com.axway.apim.api.model.APIMethod.getName() (See issue [#512](https://github.com/Axway-API-Management-Plus/apim-cli/issues/512)) diff --git a/modules/settings/src/main/java/com/axway/apim/setup/APIManagerSettingsApp.java b/modules/settings/src/main/java/com/axway/apim/setup/APIManagerSettingsApp.java index 7f204ca0d..956c03e0f 100644 --- a/modules/settings/src/main/java/com/axway/apim/setup/APIManagerSettingsApp.java +++ b/modules/settings/src/main/java/com/axway/apim/setup/APIManagerSettingsApp.java @@ -26,8 +26,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Iterator; - public class APIManagerSettingsApp implements APIMCLIServiceProvider { private static final Logger LOG = LoggerFactory.getLogger(APIManagerSettingsApp.class); @@ -58,7 +56,7 @@ public static int exportConfig(String[] args) { try { params = (APIManagerSetupExportParams) APIManagerSetupExportCLIOptions.create(args).getParams(); } catch (AppException e) { - LOG.error("Error {}", e.getMessage()); + LOG.error("Error processing get", e); return e.getError().getCode(); } APIManagerSettingsApp app = new APIManagerSettingsApp(); @@ -194,43 +192,44 @@ public void upsertGlobalSystemQuota(Quotas quotas) throws AppException { APIManagerAdapter adapter = APIManagerAdapter.getInstance(); APIManagerQuotaAdapter quotaAdapter = adapter.getQuotaAdapter(); QuotaRestriction systemQuotaRestriction = quotas.getSystemQuota(); - if (systemQuotaRestriction != null) { + APIQuota systemQuota = quotaAdapter.getDefaultQuota(APIManagerQuotaAdapter.Quota.SYSTEM_DEFAULT); + if (systemQuotaRestriction != null && systemQuotaRestriction.getApi() != null) { // Updating quota LOG.debug("Updating System Global Quota : {}", systemQuotaRestriction); - APIQuota systemQuota = quotaAdapter.getDefaultQuota(APIManagerQuotaAdapter.Quota.SYSTEM_DEFAULT); if (systemQuota.getRestrictions() != null) { - for (Iterator iterator = systemQuota.getRestrictions().iterator(); iterator.hasNext(); ) { - QuotaRestriction quotaRestriction = iterator.next(); - if (quotaRestriction.getApi().equals("*")) { - iterator.remove(); - LOG.debug("Removing exiting System Global Quota for update"); - } - } + removeExistingGlobalQuota(systemQuota); // Remove exising quota to update systemQuota.getRestrictions().add(systemQuotaRestriction); } - quotaAdapter.saveQuota(systemQuota, APIManagerQuotaAdapter.Quota.SYSTEM_DEFAULT.getQuotaId()); - LOG.debug("System Global Quota is updated"); + + }else { // Removing system quota + if (systemQuota.getRestrictions() != null) { + removeExistingGlobalQuota(systemQuota); + } } + quotaAdapter.saveQuota(systemQuota, APIManagerQuotaAdapter.Quota.SYSTEM_DEFAULT.getQuotaId()); + LOG.debug("System Global Quota is updated"); + } + + public void removeExistingGlobalQuota( APIQuota systemQuota) { + systemQuota.getRestrictions().removeIf(quotaRestriction -> quotaRestriction.getApi().equals("*")); } public void upsertGlobalApplicationQuota(Quotas quotas) throws AppException { APIManagerAdapter adapter = APIManagerAdapter.getInstance(); APIManagerQuotaAdapter quotaAdapter = adapter.getQuotaAdapter(); QuotaRestriction applicationQuotaRestriction = quotas.getApplicationQuota(); - if (applicationQuotaRestriction != null) { + APIQuota applicationQuota = quotaAdapter.getDefaultQuota(APIManagerQuotaAdapter.Quota.APPLICATION_DEFAULT); + if (applicationQuotaRestriction != null && applicationQuotaRestriction.getApi() != null) { LOG.debug("Updating Application Global Quota : {}", applicationQuotaRestriction); - APIQuota applicationQuota = quotaAdapter.getDefaultQuota(APIManagerQuotaAdapter.Quota.APPLICATION_DEFAULT); if (applicationQuota.getRestrictions() != null) { - for (Iterator iterator = applicationQuota.getRestrictions().iterator(); iterator.hasNext(); ) { - QuotaRestriction quotaRestriction = iterator.next(); - if (quotaRestriction.getApi().equals("*")) { - iterator.remove(); - LOG.debug("Removing exiting Application Global Quota for update"); - } - } + removeExistingGlobalQuota(applicationQuota); applicationQuota.getRestrictions().add(applicationQuotaRestriction); } - quotaAdapter.saveQuota(applicationQuota, APIManagerQuotaAdapter.Quota.APPLICATION_DEFAULT.getQuotaId()); - LOG.debug("Application Global Quota is updated"); + }else { + if (applicationQuota.getRestrictions() != null) { + removeExistingGlobalQuota(applicationQuota); + } } + quotaAdapter.saveQuota(applicationQuota, APIManagerQuotaAdapter.Quota.APPLICATION_DEFAULT.getQuotaId()); + LOG.debug("Application Global Quota is updated"); } } diff --git a/modules/settings/src/test/java/com/axway/apim/setup/APIManagerSettingsAppTest.java b/modules/settings/src/test/java/com/axway/apim/setup/APIManagerSettingsAppTest.java index 27a627d80..bbe6d44ed 100644 --- a/modules/settings/src/test/java/com/axway/apim/setup/APIManagerSettingsAppTest.java +++ b/modules/settings/src/test/java/com/axway/apim/setup/APIManagerSettingsAppTest.java @@ -65,4 +65,13 @@ public void importGlobalQuotas() { int returnCode = APIManagerSettingsApp.importConfig(args); Assert.assertEquals(returnCode, 0); } + + @Test + public void removeGlobalQuotas() { + ClassLoader classLoader = this.getClass().getClassLoader(); + String configFile = classLoader.getResource("com/axway/apim/setup/adapter/global-quotas.json").getFile(); + String[] args = {"-h", "localhost", "-c", configFile, "-type", "globalquotas"}; + int returnCode = APIManagerSettingsApp.importConfig(args); + Assert.assertEquals(returnCode, 0); + } } diff --git a/modules/settings/src/test/resources/com/axway/apim/setup/adapter/remove-global-quotas.json b/modules/settings/src/test/resources/com/axway/apim/setup/adapter/remove-global-quotas.json new file mode 100644 index 000000000..6b693693d --- /dev/null +++ b/modules/settings/src/test/resources/com/axway/apim/setup/adapter/remove-global-quotas.json @@ -0,0 +1,8 @@ +{ + "quotas": { + "systemQuota": { + }, + "applicationQuota": { + } + } +}