2018-12-17 17:03:28 +01:00
|
|
|
package org.gcube.application.perform.service.engine.utils;
|
|
|
|
|
2019-01-09 16:09:29 +01:00
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
2018-12-17 17:03:28 +01:00
|
|
|
import org.gcube.application.perform.service.engine.model.DatabaseConnectionDescriptor;
|
|
|
|
import org.gcube.application.perform.service.engine.model.ISQueryDescriptor;
|
2019-01-09 16:09:29 +01:00
|
|
|
import org.gcube.application.perform.service.engine.model.InternalException;
|
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
|
|
|
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
|
|
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2018-12-17 17:03:28 +01:00
|
|
|
|
2019-01-09 16:09:29 +01:00
|
|
|
@Slf4j
|
2018-12-17 17:03:28 +01:00
|
|
|
public class ISUtils {
|
|
|
|
|
|
|
|
|
2019-01-09 16:09:29 +01:00
|
|
|
public static DatabaseConnectionDescriptor queryForDatabase(ISQueryDescriptor desc) throws InternalException {
|
|
|
|
|
|
|
|
log.debug("Querying for Service Endpoints {} ",desc,ScopeUtils.getCurrentScope());
|
|
|
|
|
|
|
|
SimpleQuery query = queryFor(ServiceEndpoint.class);
|
|
|
|
|
|
|
|
if(desc.getCategory()!=null)
|
|
|
|
query.addCondition("$resource/Profile/Category/text() eq '"+desc.getCategory()+"'");
|
|
|
|
|
|
|
|
if(desc.getPlatformName()!=null)
|
|
|
|
query.addCondition("$resource/Profile/Platform/Name/text() eq '"+desc.getPlatformName()+"'");
|
|
|
|
|
|
|
|
if(desc.getResourceName()!=null)
|
|
|
|
query.addCondition("$resource/Profile/Name/text() eq '"+desc.getCategory()+"'");
|
|
|
|
|
|
|
|
|
|
|
|
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
|
|
|
|
|
|
|
|
List<ServiceEndpoint> found=client.submit(query);
|
|
|
|
|
|
|
|
if(found.size()==0) throw new InternalException("Unable to find Service Endpoint "+desc+" in "+ScopeUtils.getCurrentScope());
|
|
|
|
if(found.size()>1) log.warn("Multiple Endpoints "+desc+" found in "+ScopeUtils.getCurrentScope());
|
|
|
|
AccessPoint point= found.get(0).profile().accessPoints().iterator().next();
|
|
|
|
|
|
|
|
String url="jdbc:postgresql://"+point.address()+"/"+point.name();
|
|
|
|
|
|
|
|
DatabaseConnectionDescriptor toReturn= new DatabaseConnectionDescriptor(point.username(), url, CommonUtils.decryptString(point.password()));
|
|
|
|
log.debug("Going to use DB : "+toReturn);
|
|
|
|
|
|
|
|
return toReturn;
|
2018-12-17 17:03:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|