use of org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException in project carbon-apimgt by wso2.
the class ServiceDiscovererKubernetes method initImpl.
/**
* Initializes OpenShiftClient (extended KubernetesClient) and sets the necessary parameters
*
* @param implParameters implementation parameters added by the super class #initImpl(java.util.Map) method
* @throws ServiceDiscoveryException if an error occurs while initializing the client
*/
@Override
public void initImpl(Map<String, String> implParameters) throws ServiceDiscoveryException {
try {
setClient(new DefaultOpenShiftClient(buildConfig(implParameters)));
} catch (KubernetesClientException | APIMgtDAOException e) {
String msg = "Error occurred while creating Kubernetes client";
throw new ServiceDiscoveryException(msg, e, ExceptionCodes.ERROR_INITIALIZING_SERVICE_DISCOVERY);
} catch (ArrayIndexOutOfBoundsException e) {
String msg = "Error occurred while reading filtering criteria from the configuration";
throw new ServiceDiscoveryException(msg, e, ExceptionCodes.ERROR_INITIALIZING_SERVICE_DISCOVERY);
}
includeClusterIP = Boolean.parseBoolean(implParameters.get(INCLUDE_CLUSTER_IPS));
includeExternalNameTypeServices = Boolean.parseBoolean(implParameters.get(INCLUDE_EXTERNAL_NAME_SERVICES));
}
use of org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException in project carbon-apimgt by wso2.
the class ServiceDiscovererKubernetesTestCase method testInitWhileExternalTokenFileNameNotGiven.
@Test(description = "Test init method when external service account token file name is NOT given")
public void testInitWhileExternalTokenFileNameNotGiven() throws Exception {
OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class);
ServiceDiscovererKubernetes sdKubernetes = new ServiceDiscovererKubernetes();
sdKubernetes.setClient(openShiftClient);
try {
sdKubernetes.initImpl(createImplParametersMap(""));
} catch (ServiceDiscoveryException e) {
// since pod's token is then searched, this is exception msg we get
Assert.assertEquals(e.getCause().getMessage(), "Error while reading file /var/run/secrets/kubernetes.io/serviceaccount/token");
}
}
use of org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException in project carbon-apimgt by wso2.
the class ServiceDiscovererKubernetesTestCase method testInitWhileExternalTokenFileNameGiven.
@Test(description = "Test init method with external service account token file name")
public void testInitWhileExternalTokenFileNameGiven() throws Exception {
OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class);
ServiceDiscovererKubernetes sdKubernetes = new ServiceDiscovererKubernetes();
sdKubernetes.setClient(openShiftClient);
try {
sdKubernetes.initImpl(createImplParametersMap("TestK8Token"));
} catch (ServiceDiscoveryException e) {
Assert.assertEquals(e.getCause().getMessage(), "File to decrypt does not exist");
}
}
use of org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException in project carbon-apimgt by wso2.
the class APIPublisherImpl method discoverServiceEndpoints.
/**
* Discover and return a list of service endpoints, of all systems that were included in the config
*
* @return {@code List<Endpoint>} list of all service endpoints
* @throws APIManagementException If an error occurred while discovering services
*/
@Override
public List<Endpoint> discoverServiceEndpoints() throws APIManagementException {
List<Endpoint> discoveredEndpointList = new ArrayList();
try {
ServiceDiscoveryConfigurations serviceDiscoveryConfig = ServiceDiscoveryConfigBuilder.getServiceDiscoveryConfiguration();
// If service discovery not enabled - do not proceed
if (!serviceDiscoveryConfig.isServiceDiscoveryEnabled()) {
log.error("Service Discovery not enabled");
return discoveredEndpointList;
}
List<ServiceDiscoveryImplConfig> implConfigList = serviceDiscoveryConfig.getImplementationsList();
for (ServiceDiscoveryImplConfig implConfig : implConfigList) {
// Every implConfig has two elements. The implClass and the implParameters.
/* Get the implClass instance */
String implClassName = implConfig.getImplClass();
Class implClass = Class.forName(implClassName);
ServiceDiscoverer serviceDiscoverer = (ServiceDiscoverer) implClass.newInstance();
/* Pass the implParameters to the above instance */
serviceDiscoverer.init(implConfig.getImplParameters());
/*
* The .init() method above sets the filtering parameters (if provided)
* to the ServiceDiscoverer impl class instance.
*
* Let's check whether those filtering parameters : "namespace" and/or "criteria" are set,
* and call ServiceDiscoverer Impl class's #listServices method accordingly
*/
String namespaceFilter = serviceDiscoverer.getNamespaceFilter();
Map<String, String> criteriaFilter = serviceDiscoverer.getCriteriaFilter();
List<Endpoint> subDiscoveredEndpointList;
if (namespaceFilter == null && criteriaFilter == null) {
// both not set
subDiscoveredEndpointList = serviceDiscoverer.listServices();
} else if (namespaceFilter != null && criteriaFilter != null) {
// both set
subDiscoveredEndpointList = serviceDiscoverer.listServices(namespaceFilter, criteriaFilter);
} else if (namespaceFilter != null) {
// only "namespace" is set
subDiscoveredEndpointList = serviceDiscoverer.listServices(namespaceFilter);
} else {
// remaining -> only "criteria" is set
subDiscoveredEndpointList = serviceDiscoverer.listServices(criteriaFilter);
}
if (subDiscoveredEndpointList != null) {
discoveredEndpointList.addAll(subDiscoveredEndpointList);
}
}
} catch (ServiceDiscoveryException e) {
String msg = "Error while Discovering Service Endpoints";
throw new APIManagementException(msg, e, e.getErrorHandler());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
String msg = "Error while Loading Service Discovery Impl Class";
throw new APIManagementException(msg, e, ExceptionCodes.ERROR_LOADING_SERVICE_DISCOVERY_IMPL_CLASS);
}
return discoveredEndpointList;
}
Aggregations