Skip to content

Commit

Permalink
Merge pull request #12846 from RakhithaRR/AI-8455
Browse files Browse the repository at this point in the history
Remove the capability to change the subscription availability when cross-tenant subscription is disabled
  • Loading branch information
RakhithaRR authored Feb 17, 2025
2 parents 9cde9a9 + 8951bbc commit 62d4ae4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public enum ExceptionCodes implements ErrorHandler {
API_PRODUCT_NOT_FOUND(900360, "API Product Not Found", 404, "Requested API Product with id '%s' not found"),
SUB_ORGANIZATION_NOT_IDENTIFIED(900361, "User's Organization Not Identified", 403, "User's Organization is not identified"),
CANNOT_CREATE_API_VERSION(900362, "New API Version cannot be created from a different provider", 409, "Initial provider of an API must be preserved in all versions of that API"),
INTERNAL_ERROR_WHILE_UPDATING_API(900363, "Internal Server Error occurred while updating the API", 500, "Internal Server Error. '%s'"),
ERROR_WHILE_UPDATING_MANDATORY_PROPERTIES(903010, "Error while updating required properties", 400, "Error while updating required properties."),

//Lifecycle related codes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ private static API prepareForUpdateApi(API originalAPI, APIDTO apiDtoToUpdate, A
apiToUpdate.setUUID(originalAPI.getUUID());
apiToUpdate.setOrganization(originalAPI.getOrganization());
validateScopes(apiToUpdate);
validateSubscriptionAvailability(originalAPI, apiToUpdate);
apiToUpdate.setThumbnailUrl(originalAPI.getThumbnailUrl());
if (apiDtoToUpdate.getKeyManagers() instanceof List) {
apiToUpdate.setKeyManagers((List<String>) apiDtoToUpdate.getKeyManagers());
Expand Down Expand Up @@ -2634,6 +2635,7 @@ public static APIProduct updateApiProduct(APIProduct originalAPIProduct, APIProd
}

APIProduct product = APIMappingUtil.fromDTOtoAPIProduct(apiProductDtoToUpdate, username);
validateSubscriptionAvailabilityForProduct(originalAPIProduct, product);
product.setState(originalAPIProduct.getState());
//We do not allow to modify provider,name,version and uuid. Set the origial value
APIProductIdentifier productIdentifier = originalAPIProduct.getId();
Expand Down Expand Up @@ -2771,6 +2773,58 @@ public static APIProduct addAPIProductWithGeneratedSwaggerDefinition(APIProductD
return createdProduct;
}

/**
* Validate subscription availability when cross tenant subscription is disabled.
*
* @param originalAPI Original API
* @param apiToUpdate API to be updated
* @throws APIManagementException If an error occurs while validating availability
*/
public static void validateSubscriptionAvailability(API originalAPI, API apiToUpdate)
throws APIManagementException {
if (originalAPI.getSubscriptionAvailability() != null && apiToUpdate.getSubscriptionAvailability() != null
&& !APIUtil.isCrossTenantSubscriptionsEnabled()) {
if (!originalAPI.getSubscriptionAvailability()
.equalsIgnoreCase(apiToUpdate.getSubscriptionAvailability())) {
if (!APIConstants.SUBSCRIPTION_TO_CURRENT_TENANT
.equalsIgnoreCase(apiToUpdate.getSubscriptionAvailability())) {
throw new APIManagementException(
ExceptionCodes.from(ExceptionCodes.INTERNAL_ERROR_WHILE_UPDATING_API,
"Cannot set Subscription Availability to "
+ apiToUpdate.getSubscriptionAvailability().toUpperCase()
+ " when Cross Tenant Subscription is disabled"));
}
}
}
}

/**
* Validate subscription availability when cross tenant subscription is disabled.
*
* @param originalProduct Original API Product
* @param productToUpdate API Product to be updated
* @throws APIManagementException If an error occurs while validating availability
*/
public static void validateSubscriptionAvailabilityForProduct(APIProduct originalProduct,
APIProduct productToUpdate)
throws APIManagementException {
if (originalProduct.getSubscriptionAvailability() != null
&& productToUpdate.getSubscriptionAvailability() != null
&& !APIUtil.isCrossTenantSubscriptionsEnabled()) {
if (!originalProduct.getSubscriptionAvailability()
.equalsIgnoreCase(productToUpdate.getSubscriptionAvailability())) {
if (!APIConstants.SUBSCRIPTION_TO_CURRENT_TENANT
.equalsIgnoreCase(productToUpdate.getSubscriptionAvailability())) {
throw new APIManagementException(
ExceptionCodes.from(ExceptionCodes.INTERNAL_ERROR_WHILE_UPDATING_API,
"Cannot set Subscription Availability to "
+ productToUpdate.getSubscriptionAvailability().toUpperCase()
+ " when Cross Tenant Subscription is disabled"));
}
}
}
}

private static void validateApiLifeCycleForApiProducts(API api) throws APIManagementException {
String status = api.getStatus();

Expand Down

0 comments on commit 62d4ae4

Please sign in to comment.