use of org.wso2.ballerinalang.compiler.semantics.model.Scope in project carbon-apimgt by wso2.
the class APIPublisherImpl method addApiFromDefinition.
/**
* Create api from Definition
*
* @param apiDefinition API definition stream.
* @return UUID of the added API.
* @throws APIManagementException If failed to add the API.
*/
@Override
public String addApiFromDefinition(InputStream apiDefinition) throws APIManagementException {
try {
String apiDefinitionString = IOUtils.toString(apiDefinition);
validateScope(apiDefinitionString);
API.APIBuilder apiBuilder = apiDefinitionFromSwagger20.generateApiFromSwaggerResource(getUsername(), apiDefinitionString);
Map<String, String> scopes = apiDefinitionFromSwagger20.getScopesFromSecurityDefinition(apiDefinitionString);
for (Map.Entry<String, String> scopeEntry : scopes.entrySet()) {
getKeyManager().registerScope(new Scope(scopeEntry.getKey(), scopeEntry.getValue()));
}
apiBuilder.corsConfiguration(new CorsConfiguration());
apiBuilder.apiDefinition(apiDefinitionString);
addAPI(apiBuilder);
return apiBuilder.getId();
} catch (IOException e) {
throw new APIManagementException("Couldn't Generate ApiDefinition from file", ExceptionCodes.API_DEFINITION_MALFORMED);
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.Scope in project carbon-apimgt by wso2.
the class APIPublisherImpl method deleteAPI.
/**
* Delete an API
*
* @param identifier UUID of the API.
* @throws APIManagementException if failed to remove the API
*/
@Override
public void deleteAPI(String identifier) throws APIManagementException {
APIGateway gateway = getApiGateway();
try {
if (getAPISubscriptionCountByAPI(identifier) == 0) {
API api = getAPIbyUUID(identifier);
// Checks whether the user has required permissions to delete the API
verifyUserPermissionsToDeleteAPI(getUsername(), api);
String apiWfStatus = api.getWorkflowStatus();
API.APIBuilder apiBuilder = new API.APIBuilder(api);
// Delete API in gateway
gateway.deleteAPI(api);
if (log.isDebugEnabled()) {
log.debug("API : " + api.getName() + " has been successfully removed from the gateway");
}
if (!getApiDAO().isAPIVersionsExist(api.getName())) {
Map<String, String> scopeMap = apiDefinitionFromSwagger20.getScopesFromSecurityDefinition(getApiSwaggerDefinition(identifier));
for (String scope : scopeMap.keySet()) {
try {
getKeyManager().deleteScope(scope);
} catch (KeyManagementException e) {
// We ignore the error due to if scope get deleted from other end that will affect to delete
// api.
log.warn("Scope couldn't delete from Key Manager", e);
}
}
}
getApiDAO().deleteAPI(identifier);
// Deleting API specific endpoints
if (api.getEndpoint() != null) {
for (Map.Entry<String, Endpoint> entry : api.getEndpoint().entrySet()) {
Endpoint endpoint = entry.getValue();
if (APIMgtConstants.API_SPECIFIC_ENDPOINT.equals(endpoint.getApplicableLevel())) {
deleteEndpoint(endpoint.getId());
}
}
}
getApiLifecycleManager().removeLifecycle(apiBuilder.getLifecycleInstanceId());
APIUtils.logDebug("API with id " + identifier + " was deleted successfully.", log);
if (APILCWorkflowStatus.PENDING.toString().equals(apiWfStatus)) {
cleanupPendingTaskForAPIStateChange(identifier);
}
// 'API_M Functions' related code
// Create a payload with event specific details
Map<String, String> eventPayload = new HashMap<>();
eventPayload.put(APIMgtConstants.FunctionsConstants.API_ID, api.getId());
eventPayload.put(APIMgtConstants.FunctionsConstants.API_NAME, api.getName());
eventPayload.put(APIMgtConstants.FunctionsConstants.API_VERSION, api.getVersion());
eventPayload.put(APIMgtConstants.FunctionsConstants.API_PROVIDER, api.getProvider());
eventPayload.put(APIMgtConstants.FunctionsConstants.API_DESCRIPTION, api.getDescription());
// This will notify all the EventObservers(Asynchronous)
ObserverNotifier observerNotifier = new ObserverNotifier(Event.API_DELETION, getUsername(), ZonedDateTime.now(ZoneOffset.UTC), eventPayload, this);
ObserverNotifierThreadPool.getInstance().executeTask(observerNotifier);
} else {
throw new ApiDeleteFailureException("API with " + identifier + " already have subscriptions");
}
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while deleting the API with id " + identifier;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
} catch (LifecycleException e) {
String errorMsg = "Error occurred while Disassociating the API with Lifecycle id " + identifier;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, ExceptionCodes.APIMGT_LIFECYCLE_EXCEPTION);
} catch (GatewayException e) {
String message = "Error occurred while deleting API with id - " + identifier + " from gateway";
log.error(message, e);
throw new APIManagementException(message, ExceptionCodes.GATEWAY_EXCEPTION);
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.Scope in project carbon-apimgt by wso2.
the class DefaultScopeRegistrationImplTest method testUpdateScopeWhileAuthorizationServerThrowsInternalError.
@Test
public void testUpdateScopeWhileAuthorizationServerThrowsInternalError() {
DefaultScopeRegistrationServiceStub defaultScopeRegistrationServiceStub = Mockito.mock(DefaultScopeRegistrationServiceStub.class);
DefaultScopeRegistrationImpl defaultScopeRegistration = new DefaultScopeRegistrationImpl(defaultScopeRegistrationServiceStub);
ScopeInfo scopeInfo = new ScopeInfo();
scopeInfo.setDescription("cde");
scopeInfo.setDisplayName("abc");
Scope scope = new Scope();
scope.setName("abc");
scope.setDescription("cde");
Mockito.when(defaultScopeRegistrationServiceStub.updateScope(Mockito.any(ScopeInfo.class), Mockito.anyString())).thenReturn(Response.builder().status(500).headers(new HashMap<>()).build());
try {
defaultScopeRegistration.updateScope(scope);
} catch (KeyManagementException e) {
Assert.assertEquals(e.getErrorHandler().getErrorCode(), 900967);
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.Scope in project carbon-apimgt by wso2.
the class DefaultScopeRegistrationImplTest method testRetrievescope.
@Test
public void testRetrievescope() throws KeyManagementException {
DefaultScopeRegistrationServiceStub defaultScopeRegistrationServiceStub = Mockito.mock(DefaultScopeRegistrationServiceStub.class);
DefaultScopeRegistrationImpl defaultScopeRegistration = new DefaultScopeRegistrationImpl(defaultScopeRegistrationServiceStub);
ScopeInfo scopeInfo = new ScopeInfo();
scopeInfo.setName("abc");
scopeInfo.setDescription("cde");
Mockito.when(defaultScopeRegistrationServiceStub.getScopeByName(scopeInfo.getName())).thenReturn(Response.builder().status(200).headers(new HashMap<>()).body(new Gson().toJson(scopeInfo), feign.Util.UTF_8).build());
Scope retrievedScope = defaultScopeRegistration.getScopeByName(scopeInfo.getName());
Assert.assertEquals(scopeInfo.getName(), retrievedScope.getName());
Assert.assertEquals(scopeInfo.getDescription(), retrievedScope.getDescription());
Assert.assertEquals(Collections.emptyList(), retrievedScope.getBindings());
}
use of org.wso2.ballerinalang.compiler.semantics.model.Scope in project carbon-apimgt by wso2.
the class DefaultScopeRegistrationImplTest method testRetrieveScopeWithBindings.
@Test
public void testRetrieveScopeWithBindings() throws KeyManagementException {
DefaultScopeRegistrationServiceStub defaultScopeRegistrationServiceStub = Mockito.mock(DefaultScopeRegistrationServiceStub.class);
DefaultScopeRegistrationImpl defaultScopeRegistration = new DefaultScopeRegistrationImpl(defaultScopeRegistrationServiceStub);
ScopeInfo scopeInfo = new ScopeInfo();
scopeInfo.setName("abc");
scopeInfo.setDescription("cde");
List<String> list = new ArrayList<>();
list.add("apim:api_create");
scopeInfo.setBindings(list);
Mockito.when(defaultScopeRegistrationServiceStub.getScopeByName(scopeInfo.getName())).thenReturn(Response.builder().status(200).headers(new HashMap<>()).body(new Gson().toJson(scopeInfo), feign.Util.UTF_8).build());
Scope retrievedScope = defaultScopeRegistration.getScopeByName(scopeInfo.getName());
Assert.assertEquals(scopeInfo.getName(), retrievedScope.getName());
Assert.assertEquals(scopeInfo.getDescription(), retrievedScope.getDescription());
Assert.assertEquals(list, retrievedScope.getBindings());
}
Aggregations