Integrate with APIM dev-portal APIs

pull/142/head
Dharmakeerthi Lasantha 1 year ago
parent b5012d9681
commit 6e0fe2e26e

@ -84,8 +84,10 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
try { try {
APIConsumer apiConsumer = API_MANAGER_FACTORY.getAPIConsumer(username); APIConsumer apiConsumer = API_MANAGER_FACTORY.getAPIConsumer(username);
Application application = null; // todo:apim - apiConsumer.getApplicationsByName(username, applicationName, ""); Application application = null; // todo:apim - apiConsumer.getApplicationsByName(username, applicationName, "");
// curl -k -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" "https://localhost:9443/api/am/devportal/v3/applications?query=CalculatorApp"
if (application != null) { if (application != null) {
// todo:apim - apiConsumer.removeApplication(application, username); // todo:apim - apiConsumer.removeApplication(application, username);
//curl -k -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" -X DELETE "https://localhost:9443/api/am/devportal/v3/applications/896658a0-b4ee-4535-bbfa-806c894a4015"
} }
} catch (APIManagementException e) { } catch (APIManagementException e) {
throw new APIManagerException("Failed to remove api application : " + applicationName, e); throw new APIManagerException("Failed to remove api application : " + applicationName, e);
@ -102,6 +104,37 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
String keyType, String username, boolean isAllowedAllDomains, String validityTime) String keyType, String username, boolean isAllowedAllDomains, String validityTime)
throws APIManagerException { throws APIManagerException {
/*
todo - Modify generateAndRetrieveApplicationKeys
Check the existence of the API application.
if Application is not exists
Create the Application
If super tenants
Get set of tagged APIs
If the tenant domain is not super tenant
Get set of tagged APIs from super tenant space
If new Application
Subscribed to tagged APIs
Else
Get all subscribed APIs of application
Filter out APIs and subscribed to APIs which can be subscribed
Filter -> Use set of tagged APis
Remove already subscribed APIs from the set
Subscribed to remaining APIs
Get Application keys from application
If API keys are there return API keys
Otherwise, Generate Application Keys and return them
*/
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
if (StringUtils.isEmpty(username)) { if (StringUtils.isEmpty(username)) {
username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername() + "@" + tenantDomain; username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername() + "@" + tenantDomain;
@ -109,15 +142,20 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
try { try {
APIConsumer apiConsumer = API_MANAGER_FACTORY.getAPIConsumer(username); APIConsumer apiConsumer = API_MANAGER_FACTORY.getAPIConsumer(username);
Application application = null; // todo:apim - apiConsumer.getApplicationsByName(username, applicationName, ""); Application application = null; // todo:apim - apiConsumer.getApplicationsByName(username, applicationName, "");
// cnt rm
// // curl -k -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" "https://localhost:9443/api/am/devportal/v3/applications?query=CalculatorApp"
int applicationId = 0; int applicationId = 0;
Subscriber subscriber = null; Subscriber subscriber = null;
if (application == null) { if (application == null) {
subscriber = null; // todo:apim - apiConsumer.getSubscriber(username); subscriber = null; // todo:apim - apiConsumer.getSubscriber(username);
// cnt rm
if (subscriber == null) { if (subscriber == null) {
// create subscriber // create subscriber
// todo:apim - apiConsumer.addSubscriber(username, ""); // todo:apim - apiConsumer.addSubscriber(username, "");
// cnt rm
subscriber = null; // todo:apim - apiConsumer.getSubscriber(username); subscriber = null; // todo:apim - apiConsumer.getSubscriber(username);
// cnt rm
} }
//create application //create application
application = new Application(applicationName, subscriber); application = new Application(applicationName, subscriber);
@ -125,23 +163,30 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
application.setGroupId(""); application.setGroupId("");
application.setTokenType("OAUTH"); application.setTokenType("OAUTH");
// todo:apim - apiConsumer.addApplication(application, username); // todo:apim - apiConsumer.addApplication(application, username);
// cnt rm
application = null; // todo:apim - apiConsumer.getApplicationsByName(username, applicationName, ""); application = null; // todo:apim - apiConsumer.getApplicationsByName(username, applicationName, "");
// cnt rm
} else { } else {
subscriber = null; // todo:apim - apiConsumer.getSubscriber(username); subscriber = null; // todo:apim - apiConsumer.getSubscriber(username);
// cnt rm
} }
Set<SubscribedAPI> subscribedAPIs = Set<SubscribedAPI> subscribedAPIs =
null; // todo:apim - apiConsumer.getSubscribedAPIs(subscriber, applicationName, ""); null; // todo:apim - apiConsumer.getSubscribedAPIs(subscriber, applicationName, "");
//curl -k -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" "https://localhost:9443/api/am/devportal/v3/subscriptions?apiId=02e658e7-71c7-4b1d-a623-be145b789340"
// cnt rm
log.info("Already subscribed API count: " + subscribedAPIs.size()); log.info("Already subscribed API count: " + subscribedAPIs.size());
// subscribe to apis. // subscribe to apis.
Set<String> tempApiIds = new HashSet<>();
APIConsumer apiConsumerAPIPublishedTenant = apiConsumer; APIConsumer apiConsumerAPIPublishedTenant = apiConsumer;
if (tags != null && tags.length > 0) { if (tags != null && tags.length > 0) {
for (String tag : tags) { for (String tag : tags) {
boolean startedTenantFlow = false; boolean startedTenantFlow = false;
Set<API> apisWithTag = null; // todo:apim - apiConsumer.getAPIsWithTag(tag, tenantDomain); Set<API> apisWithTag = null; // todo:apim - apiConsumer.getAPIsWithTag(tag, tenantDomain);
// curl -k "https://localhost:9443/api/am/devportal/v3/apis"
// cnt rm
/** /**
* From APIM 4.0.0, APIs published in the super tenant can only be listed by * From APIM 4.0.0, APIs published in the super tenant can only be listed by
@ -165,11 +210,13 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
} }
apisWithTag = null; // todo:apim - apiConsumerAPIPublishedTenant.getAPIsWithTag(tag, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); apisWithTag = null; // todo:apim - apiConsumerAPIPublishedTenant.getAPIsWithTag(tag, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
// cnt rm
startedTenantFlow = true; startedTenantFlow = true;
} }
Set<ApiTypeWrapper> apiTypeWrapperList = new HashSet<>(); Set<ApiTypeWrapper> apiTypeWrapperList = new HashSet<>();
if (apisWithTag != null && apisWithTag.size() > 0) { if (apisWithTag != null && apisWithTag.size() > 0) {
Set<String> tempApiIds = new HashSet<>();
for (API apiInfo : apisWithTag) { for (API apiInfo : apisWithTag) {
String id = apiInfo.getId().getProviderName().replace("@", "-AT-") String id = apiInfo.getId().getProviderName().replace("@", "-AT-")
+ "-" + apiInfo.getId().getName() + "-" + apiInfo.getId().getVersion(); + "-" + apiInfo.getId().getName() + "-" + apiInfo.getId().getVersion();
@ -177,6 +224,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
if (subscribedAPIs.size() > 0) { if (subscribedAPIs.size() > 0) {
for (SubscribedAPI subscribedAPI : subscribedAPIs) { for (SubscribedAPI subscribedAPI : subscribedAPIs) {
// todo:apim // todo:apim
// cnt rm
// if (String.valueOf(subscribedAPI.getApiId().toString()).equals(id)) { // if (String.valueOf(subscribedAPI.getApiId().toString()).equals(id)) {
// subscriptionExist = true; // subscriptionExist = true;
// break; // break;
@ -220,6 +268,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
*/ */
for (ApiTypeWrapper apiTypeWrapper : apiTypeWrapperList) { for (ApiTypeWrapper apiTypeWrapper : apiTypeWrapperList) {
// todo:apim - apiConsumer.addSubscription(apiTypeWrapper, username, application); // todo:apim - apiConsumer.addSubscription(apiTypeWrapper, username, application);
// cnt rm
} }
} }
} }
@ -250,8 +299,8 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
APIAdmin apiAdmin = new APIAdminImpl(); APIAdmin apiAdmin = new APIAdminImpl();
String keyManagerId = null; String keyManagerId = null;
try { try {
List<KeyManagerConfigurationDTO> keyManagerConfigurations = null; // todo:apim - apiAdmin List<KeyManagerConfigurationDTO> keyManagerConfigurations = null; // todo:apim -
// .getKeyManagerConfigurationsByTenant(tenantDomain); // apiAdmin.getKeyManagerConfigurationsByTenant(tenantDomain);
if (keyManagerConfigurations != null) { if (keyManagerConfigurations != null) {
for (KeyManagerConfigurationDTO keyManagerConfigurationDTO : keyManagerConfigurations) { for (KeyManagerConfigurationDTO keyManagerConfigurationDTO : keyManagerConfigurations) {
keyManagerId = keyManagerConfigurationDTO.getUuid(); keyManagerId = keyManagerConfigurationDTO.getUuid();
@ -283,6 +332,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe
} }
throw new APIManagerException("Failed to generate keys for tenant: " + tenantDomain); throw new APIManagerException("Failed to generate keys for tenant: " + tenantDomain);
// todo:apim - commected as it says never throw since we commented apim calls above // todo:apim - commected as it says never throw since we commented apim calls above
// cnt rm
// } catch (APIManagementException e) { // } catch (APIManagementException e) {
} catch (Exception e) { } catch (Exception e) {
throw new APIManagerException("Failed to create api application for tenant: " + tenantDomain, e); throw new APIManagerException("Failed to create api application for tenant: " + tenantDomain, e);

@ -436,6 +436,8 @@ public class KeyMgtServiceImpl implements KeyMgtService {
APIManagerFactory apiManagerFactory = APIManagerFactory.getInstance(); APIManagerFactory apiManagerFactory = APIManagerFactory.getInstance();
APIConsumer apiConsumer = apiManagerFactory.getAPIConsumer(owner); APIConsumer apiConsumer = apiManagerFactory.getAPIConsumer(owner);
return null; // todo:apim - apiConsumer.getApplicationsByName(owner, applicationName, ""); return null; // todo:apim - apiConsumer.getApplicationsByName(owner, applicationName, "");
// // curl -k -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" "https://localhost:9443/api/am/devportal/v3/applications?query=CalculatorApp"
} catch (APIManagementException e) { } catch (APIManagementException e) {
msg = "Error while trying to retrieve the application"; msg = "Error while trying to retrieve the application";
log.error(msg); log.error(msg);

@ -239,8 +239,8 @@ public class APIPublisherServiceImpl implements APIPublisherService {
// need remove the local scope and add as a shared scope // need remove the local scope and add as a shared scope
if (!publisherRESTAPIServices.isSharedScopeNameExists(apiApplicationKey, accessTokenInfo, if (!publisherRESTAPIServices.isSharedScopeNameExists(apiApplicationKey, accessTokenInfo,
apiScope.getKey())) { apiScope.getKey())) {
//todo:apim //resolve- todo:apim- resolve
// if (apiProvider.isScopeKeyAssignedLocally(apiIdentifier, apiScope.getKey(), tenantId)) { // if (apiProvider.isScopeKeyAssignedLocally(apiIdentifier, apiScope.getKey(), tenantId)) {
if (true) { if (true) {
// collect scope to move as shared scopes // collect scope to move as shared scopes
scopesToMoveAsSharedScopes.add(apiScope); scopesToMoveAsSharedScopes.add(apiScope);

Loading…
Cancel
Save