Skip to content

Commit

Permalink
- Fix issue #442
Browse files Browse the repository at this point in the history
  • Loading branch information
rathnapandi committed Oct 26, 2024
1 parent 37788d1 commit 91212aa
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<QuotaRestriction> 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<QuotaRestriction> 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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"quotas": {
"systemQuota": {
},
"applicationQuota": {
}
}
}

0 comments on commit 91212aa

Please sign in to comment.