fix(manager-api): fix status glitch on client unregister
MODIFY
manager/api/core/src/main/java/io/apiman/manager/api/core/util/EntityValidator.java
manager/api/core/src/main/java/io/apiman/manager/api/core/util/EntityValidator.java
@@ -88,13 +88,21 @@ public class EntityValidator implements IApiValidator, IClientValidator {
@Override
public ClientStatus determineStatus(ClientVersionBean cvb, List<ContractBean> contracts) {
- if (contracts.isEmpty()) {
- return ClientStatus.Created;
- }
+ ClientStatus currentStatus = cvb.getStatus();
+
boolean anyAwaitingApproval = contracts.stream().anyMatch(c -> c.getStatus() == ContractStatus.AwaitingApproval);
if (anyAwaitingApproval) {
return ClientStatus.AwaitingApproval;
}
+ // If already registered, then continue to be registered (indicates some number of contracts are still active on gateway).
+ if (currentStatus == ClientStatus.Registered) {
+ return ClientStatus.Registered;
+ }
+ // No contracts and not registered, then just created state
+ if (contracts.isEmpty()) {
+ return ClientStatus.Created;
+ }
+ // Ready to be published
return ClientStatus.Ready;
}

MODIFY
manager/api/rest-impl/src/main/java/io/apiman/manager/api/service/ActionService.java
manager/api/rest-impl/src/main/java/io/apiman/manager/api/service/ActionService.java
@@ -511,13 +511,22 @@ public class ActionService implements DataAccessUtilMixin {
throw ExceptionFactory.actionException(Messages.i18n.format("ClientNotFound")); //$NON-NLS-1$
}
try {
- contractBeans = query.getClientContracts(orgId, clientId, clientVersion);
+ contractBeans = query.getClientContracts(orgId, clientId, clientVersion)
+ .stream()
+ .peek(c -> {
+ if (c.getStatus() != ContractStatus.Created) {
+ LOGGER.debug("Will not try to delete contract {0} from gateway(s) as it is not in 'Created' state", c);
+ }
+ })
+ .filter(c -> c.getStatus() == ContractStatus.Created)
+ .collect(Collectors.toList());
+
} catch (StorageException e) {
throw ExceptionFactory.actionException(Messages.i18n.format("ClientNotFound"), e); //$NON-NLS-1$
}
- // Validate that it's ok to perform this action - client must be Ready.
- if (versionBean.getStatus() != ClientStatus.Registered) {
+ // Validate that it's ok to perform this action - client must be either registered or awaiting approval (or there's nothing to unregister)
+ if (versionBean.getStatus() != ClientStatus.Registered && versionBean.getStatus() != ClientStatus.AwaitingApproval) {
throw ExceptionFactory.actionException(Messages.i18n.format("InvalidClientStatus")); //$NON-NLS-1$
}
@@ -532,8 +541,7 @@ public class ActionService implements DataAccessUtilMixin {
try {
Map<String, IGatewayLink> links = new HashMap<>();
for (ContractSummaryBean contractBean : contractBeans) {
- ApiVersionBean svb = storage.getApiVersion(contractBean.getApiOrganizationId(),
- contractBean.getApiId(), contractBean.getApiVersion());
+ ApiVersionBean svb = storage.getApiVersion(contractBean.getApiOrganizationId(), contractBean.getApiId(), contractBean.getApiVersion());
Set<ApiGatewayBean> gateways = svb.getGateways();
if (gateways == null) {
throw new PublishingException("No gateways specified for API: " + svb.getApi().getName()); //$NON-NLS-1$

MODIFY
manager/api/rest-impl/src/main/java/io/apiman/manager/api/service/ContractService.java
manager/api/rest-impl/src/main/java/io/apiman/manager/api/service/ContractService.java
@@ -263,7 +263,7 @@ public class ContractService implements DataAccessUtilMixin {
private void deleteContractsInternal(String organizationId, String clientId, String clientVersion, List<ContractBean> allContracts, List<ContractBean> contractsToDelete)
throws Exception {
Preconditions.checkArgument(allContracts.size() > 0, "Must have at least 1 contract if you want to delete");
- Preconditions.checkArgument(allContracts.size() > 0, "Must nominate at least 1 contract to delete");
+ Preconditions.checkArgument(contractsToDelete.size() > 0, "Must nominate at least 1 contract to delete");
for (ContractBean contract : contractsToDelete) {
Long contractId = contract.getId();
if (!contract.getClient().getClient().getOrganization().getId().equals(organizationId)) {

fix(manager-api): fix status glitch on client unregister