Skip to content

Commit

Permalink
Merge pull request #12445 from chashikajw/fix-import-app-issue
Browse files Browse the repository at this point in the history
Fix subscription update flow when importing existing application
  • Loading branch information
chashikajw authored Jun 25, 2024
2 parents c0ce919 + 13a072e commit 3500770
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ public SubscriptionResponse addSubscription(ApiTypeWrapper apiTypeWrapper, Strin

WorkflowResponse workflowResponse = null;
String tenantAwareUsername = MultitenantUtils.getTenantAwareUsername(userId);
checkSubscriptionAllowed(apiTypeWrapper);
checkSubscriptionAllowed(apiTypeWrapper, apiTypeWrapper.getTier());
int subscriptionId;
if (APIConstants.PUBLISHED.equals(state) || APIConstants.PROTOTYPED.equals(state)) {
subscriptionId = apiMgtDAO.addSubscription(apiTypeWrapper, application,
Expand Down Expand Up @@ -1058,7 +1058,7 @@ public SubscriptionResponse updateSubscription(ApiTypeWrapper apiTypeWrapper, St
apiContext = api.getContext();
apiOrgId = api.getOrganization();
}
checkSubscriptionAllowed(apiTypeWrapper);
checkSubscriptionAllowed(apiTypeWrapper, requestedThrottlingPolicy);
WorkflowResponse workflowResponse = null;
int subscriptionId;
if (APIConstants.PUBLISHED.equals(state) || APIConstants.PROTOTYPED.equals(state)) {
Expand Down Expand Up @@ -4484,7 +4484,7 @@ private Map<String, String> getHostWithSchemeMappingForLabelWS(List<Label> gatew
* subscription, this will throw an instance of APIMgtAuthorizationFailedException
* with the reason as the message
*/
private void checkSubscriptionAllowed(ApiTypeWrapper apiTypeWrapper)
private void checkSubscriptionAllowed(ApiTypeWrapper apiTypeWrapper, String requestedThrottlingPolicy)
throws APIManagementException {

Set<Tier> tiers;
Expand Down Expand Up @@ -4542,17 +4542,17 @@ private void checkSubscriptionAllowed(ApiTypeWrapper apiTypeWrapper)
List<String> allowedTierList = new ArrayList<>();
while (iterator.hasNext()) {
Tier t = iterator.next();
if (t.getName() != null && (t.getName()).equals(apiTypeWrapper.getTier())) {
if (t.getName() != null && (t.getName()).equals(requestedThrottlingPolicy)) {
isTierAllowed = true;
}
allowedTierList.add(t.getName());
}
if (!isTierAllowed) {
String msg =
"Tier " + apiTypeWrapper.getTier() + " is not allowed for API/API Product " + apiTypeWrapper + ". Only "
"Tier " + requestedThrottlingPolicy + " is not allowed for API/API Product " + apiTypeWrapper + ". Only "
+ Arrays.toString(allowedTierList.toArray()) + " Tiers are allowed.";
throw new APIManagementException(msg, ExceptionCodes.from(ExceptionCodes.SUBSCRIPTION_TIER_NOT_ALLOWED,
apiTypeWrapper.getTier(), username));
requestedThrottlingPolicy, username));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class ImportUtils {
private static final Log log = LogFactory.getLog(ImportUtils.class);
Expand Down Expand Up @@ -152,28 +154,90 @@ public static APIKey getAPIKeyFromApplicationKeyDTO(ApplicationKeyDTO applicatio
* @throws UserStoreException if an error occurs while checking whether the tenant domain exists
*/
public static List<APIIdentifier> importSubscriptions(Set<ExportedSubscribedAPI> subscribedAPIs, String userId,
Application application, Boolean update, APIConsumer apiConsumer, String organization)
Application application, Boolean update,
APIConsumer apiConsumer, String organization)
throws APIManagementException,
UserStoreException {
List<APIIdentifier> skippedAPIList = new ArrayList<>();
// removing existing subscribed apis

// Create a map of the imported subscriptions. The key is a combination of apiName and version
Map<String, ExportedSubscribedAPI> importedSubscriptionMap = subscribedAPIs.stream()
.collect(Collectors.toMap(
api -> api.getApiId().getApiName() + "_" + api.getApiId().getVersion(),
api -> api));

// If update flag is set, remove the existing subscriptions that are not in the imported list
if (update) {
Subscriber subscriber = apiConsumer.getSubscriber(userId);
Set<SubscribedAPI> currentSubscribedAPIs = apiConsumer.getSubscribedAPIs(subscriber, application.getName(),
application.getGroupId());
for (SubscribedAPI subscribedAPI : currentSubscribedAPIs) {
apiConsumer.removeSubscription(subscribedAPI.getAPIIdentifier(), userId, application.getId(),
application.getGroupId(), application.getOrganization());
Set<SubscribedAPI> currentSubscribedAPIs = apiConsumer.getSubscribedAPIs(subscriber,
application.getName(), application.getGroupId());

for (SubscribedAPI existingSubscribedAPI : currentSubscribedAPIs) {
String existingSubscriptionKey = existingSubscribedAPI.getIdentifier().getName() + "_" +
existingSubscribedAPI.getIdentifier().getVersion();
APIIdentifier existingApi = existingSubscribedAPI.getAPIIdentifier();
if (importedSubscriptionMap.containsKey(existingSubscriptionKey)) {

// Update the tier if the existing tier is different from the imported tier
if (!existingSubscribedAPI.getTier().getName().equals(
importedSubscriptionMap.get(existingSubscriptionKey).getThrottlingPolicy())) {


String tenantDomain = MultitenantUtils
.getTenantDomain(APIUtil.replaceEmailDomainBack(existingApi.getProviderName()));
if (!StringUtils.isEmpty(tenantDomain) && APIUtil.isTenantAvailable(tenantDomain)) {
String uuidFromIdentifier = ApiMgtDAO.getInstance().getUUIDFromIdentifier(existingApi,
tenantDomain);
if (StringUtils.isNotEmpty(uuidFromIdentifier)) {
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(
uuidFromIdentifier, tenantDomain);
// Tier of the imported subscription
String targetTier = importedSubscriptionMap.get(existingSubscriptionKey).
getThrottlingPolicy();
// Checking whether the target tier is available
if (isTierAvailable(targetTier, apiTypeWrapper) && apiTypeWrapper.getStatus() != null
&& APIConstants.PUBLISHED.equals(apiTypeWrapper.getStatus())) {

apiConsumer.updateSubscription(apiTypeWrapper, userId, application,
existingSubscribedAPI.getUUID(), existingSubscribedAPI.getTier().getName(),
targetTier);
} else {
log.error("Failed to import Subscription as API/API Product "
+ existingApi.getName() + "-" + existingApi.getVersion() +
" as one or more tiers may be unavailable or the API/API Product may " +
"not have been published ");
}
} else {
log.error("Failed to import Subscription as API " + existingApi.getName() + "-" +
existingApi.getVersion() + " is not available");
}
} else {
log.error("Failed to import Subscription as Tenant domain: " + tenantDomain +
" is not available");
}
}

// Remove the existing subscription from the imported list
importedSubscriptionMap.remove(existingSubscriptionKey);
} else {
// Remove the subscription if it is not in the imported list
apiConsumer.removeSubscription(existingApi, userId, application.getId(), application.getGroupId(),
organization);

}
}
}
for (ExportedSubscribedAPI subscribedAPI : subscribedAPIs) {

for (String importedSubscriptionKey : importedSubscriptionMap.keySet()) {
ExportedSubscribedAPI subscribedAPI = importedSubscriptionMap.get(importedSubscriptionKey);
APIIdentifier apiIdentifier = subscribedAPI.getApiId();
String tenantDomain = MultitenantUtils
.getTenantDomain(APIUtil.replaceEmailDomainBack(apiIdentifier.getProviderName()));
if (!StringUtils.isEmpty(tenantDomain) && APIUtil.isTenantAvailable(tenantDomain)) {
String uuidFromIdentifier = ApiMgtDAO.getInstance().getUUIDFromIdentifier(apiIdentifier, tenantDomain);
if (StringUtils.isNotEmpty(uuidFromIdentifier)) {
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(uuidFromIdentifier, tenantDomain);
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(uuidFromIdentifier,
tenantDomain);
// Tier of the imported subscription
String targetTier = subscribedAPI.getThrottlingPolicy();
// Checking whether the target tier is available
Expand All @@ -191,7 +255,8 @@ public static List<APIIdentifier> importSubscriptions(Set<ExportedSubscribedAPI>
}
} else {
log.error("Failed to import Subscription as API/API Product "
+ apiIdentifier.getName() + "-" + apiIdentifier.getVersion() + " as one or more tiers may "
+ apiIdentifier.getName() + "-" + apiIdentifier.getVersion() + " as one or more " +
"tiers may "
+ "be unavailable or the API/API Product may not have been published ");
skippedAPIList.add(subscribedAPI.getApiId());
}
Expand Down

0 comments on commit 3500770

Please sign in to comment.