Improving configuration on IS

This commit is contained in:
Luca Frosini 2022-02-22 09:34:07 +01:00
parent a7d4bc8dbb
commit eca3d130c8
4 changed files with 73 additions and 91 deletions

View File

@ -2,7 +2,7 @@
<!DOCTYPE xml> <!DOCTYPE xml>
<application mode='online'> <application mode='online'>
<name>${project.artifactId}</name> <name>${project.artifactId}</name>
<group>${serviceClass}</group> <group>${project.groupId}</group>
<version>${project.version}</version> <version>${project.version}</version>
<description>${project.description}</description> <description>${project.description}</description>
</application> </application>

View File

@ -21,7 +21,6 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<webappDirectory>${project.basedir}${file.separator}src${file.separator}main${file.separator}webapp${file.separator}WEB-INF</webappDirectory> <webappDirectory>${project.basedir}${file.separator}src${file.separator}main${file.separator}webapp${file.separator}WEB-INF</webappDirectory>
<serviceClass>data-catalogue</serviceClass>
</properties> </properties>
<scm> <scm>

View File

@ -3,6 +3,7 @@ package org.gcube.gcat.configuration;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -52,19 +53,36 @@ public class GCoreISConfigurationProxy {
public final static String DEFAULT_ORGANIZATION_PROPERTY_KEY = "DEFAULT_ORGANIZATION"; public final static String DEFAULT_ORGANIZATION_PROPERTY_KEY = "DEFAULT_ORGANIZATION";
public final static String SUPPORTED_ORGANIZATION_PROPERTY_KEY = "SUPPORTED_ORGANIZATION"; public final static String SUPPORTED_ORGANIZATION_PROPERTY_KEY = "SUPPORTED_ORGANIZATION";
public final static String API_KEY_PROPERTY_KEY = "API_KEY"; public final static String API_KEY_PROPERTY_KEY = "API_KEY";
public final static String SOLR_INDEX_ADDRESS_PROPERTY_KEY = "SOLR_INDEX_ADDRESS"; public final static String SOLR_INDEX_ADDRESS_PROPERTY_KEY = "SOLR_INDEX_ADDRESS";
public final static String SOCIAL_POST_PROPERTY_KEY = "SOCIAL_POST"; public final static String SOCIAL_POST_PROPERTY_KEY = "SOCIAL_POST";
public final static String ALERT_USERS_ON_POST_CREATION_PROPERTY_KEY = "ALERT_USERS_ON_POST_CREATION"; public final static String ALERT_USERS_ON_POST_CREATION_PROPERTY_KEY = "ALERT_USERS_ON_POST_CREATION";
public final static String MODERATION_ENABLED_KEY_PROPERTY_KEY = "MODERATION_ENABLED"; public final static String MODERATION_ENABLED_KEY_PROPERTY_KEY = "MODERATION_ENABLED";
public final static String CKAN_DB_URL_PROPERTY_KEY = "CKAN_DB_URL"; public static final Map<String, String> gCoreToConfigurationMapping;
public final static String CKAN_DB_USERNAME_PROPERTY_KEY = "CKAN_DB_USERNAME"; public static final Map<String, String> configurationToGCoreMapping;
public final static String CKAN_DB_PASSWORD_PROPERTY_KEY = "CKAN_DB_PASSWORD";
static {
gCoreToConfigurationMapping = new HashMap<>();
configurationToGCoreMapping = new HashMap<>();
gCoreToConfigurationMapping.put(API_KEY_PROPERTY_KEY, CatalogueConfiguration.SYS_ADMIN_TOKEN_KEY);
gCoreToConfigurationMapping.put(SOLR_INDEX_ADDRESS_PROPERTY_KEY, CatalogueConfiguration.SOLR_URL_KEY);
gCoreToConfigurationMapping.put(SOCIAL_POST_PROPERTY_KEY, CatalogueConfiguration.SOCIAL_POST_ENABLED_KEY);
gCoreToConfigurationMapping.put(ALERT_USERS_ON_POST_CREATION_PROPERTY_KEY, CatalogueConfiguration.NOTIFICATION_TO_USER_ENABLED_KEY);
gCoreToConfigurationMapping.put(MODERATION_ENABLED_KEY_PROPERTY_KEY, CatalogueConfiguration.MODERATION_ENABLED_KEY);
for(String key : gCoreToConfigurationMapping.keySet()) {
configurationToGCoreMapping.put(gCoreToConfigurationMapping.get(key), key);
}
}
// CKAN Instance info // CKAN Instance info
private final static String CATEGORY = "Application"; private final static String OLD_CATEGORY = "Application";
private final static String NAME = "CKanDataCatalogue"; private final static String OLD_NAME = "CKanDataCatalogue";
protected final String context; protected final String context;
protected ServiceCatalogueConfiguration catalogueConfiguration; protected ServiceCatalogueConfiguration catalogueConfiguration;
@ -81,7 +99,7 @@ public class GCoreISConfigurationProxy {
public ServiceCatalogueConfiguration getCatalogueConfiguration() { public ServiceCatalogueConfiguration getCatalogueConfiguration() {
if (catalogueConfiguration == null) { if (catalogueConfiguration == null) {
catalogueConfiguration = getCatalogueConfigurationFromIS(); catalogueConfiguration = getOLDCatalogueConfigurationFromIS();
} }
return catalogueConfiguration; return catalogueConfiguration;
} }
@ -102,11 +120,15 @@ public class GCoreISConfigurationProxy {
} }
protected ServiceCatalogueConfiguration getCatalogueConfigurationFromIS() { protected ServiceCatalogueConfiguration getCatalogueConfigurationFromIS() {
return getOLDCatalogueConfigurationFromIS();
}
protected ServiceCatalogueConfiguration getOLDCatalogueConfigurationFromIS() {
ServiceCatalogueConfiguration catalogueConfiguration = new ServiceCatalogueConfiguration(context); ServiceCatalogueConfiguration catalogueConfiguration = new ServiceCatalogueConfiguration(context);
try { try {
boolean mustBeUpdated = false; // boolean mustBeUpdated = false;
ServiceEndpoint serviceEndpoint = getServiceEndpoint(); ServiceEndpoint serviceEndpoint = getOldServiceEndpoint();
if (serviceEndpoint == null) { if (serviceEndpoint == null) {
throw new InternalServerErrorException("No CKAN configuration on IS"); throw new InternalServerErrorException("No CKAN configuration on IS");
} }
@ -127,17 +149,7 @@ public class GCoreISConfigurationProxy {
catalogueConfiguration.setEncryptedSysAdminToken(encryptedSysAdminToken); catalogueConfiguration.setEncryptedSysAdminToken(encryptedSysAdminToken);
String defaultOrganization = CatalogueConfiguration.getOrganizationName(context);; String defaultOrganization = CatalogueConfiguration.getOrganizationName(context);
if (propertyMap.containsKey(DEFAULT_ORGANIZATION_PROPERTY_KEY)) {
String org = propertyMap.get(DEFAULT_ORGANIZATION_PROPERTY_KEY).value().trim();
if(org!=null && org.compareTo("")==0) {
mustBeUpdated = true;
}else {
catalogueConfiguration.setDefaultOrganization(org);
}
}else {
mustBeUpdated = true;
}
String solrURL = null; String solrURL = null;
@ -154,8 +166,6 @@ public class GCoreISConfigurationProxy {
if (propertyMap.get(SOCIAL_POST_PROPERTY_KEY).value().trim().equalsIgnoreCase("false")) { if (propertyMap.get(SOCIAL_POST_PROPERTY_KEY).value().trim().equalsIgnoreCase("false")) {
socialPostEnabled = false; socialPostEnabled = false;
} }
}else {
mustBeUpdated = true;
} }
catalogueConfiguration.setSocialPostEnabled(socialPostEnabled); catalogueConfiguration.setSocialPostEnabled(socialPostEnabled);
@ -166,8 +176,6 @@ public class GCoreISConfigurationProxy {
.equalsIgnoreCase("true")) { .equalsIgnoreCase("true")) {
notificationToUsersEnabled = true; notificationToUsersEnabled = true;
} }
}else {
mustBeUpdated = true;
} }
catalogueConfiguration.setNotificationToUsersEnabled(notificationToUsersEnabled); catalogueConfiguration.setNotificationToUsersEnabled(notificationToUsersEnabled);
@ -176,21 +184,10 @@ public class GCoreISConfigurationProxy {
if (propertyMap.get(MODERATION_ENABLED_KEY_PROPERTY_KEY).value().trim().equalsIgnoreCase("true")) { if (propertyMap.get(MODERATION_ENABLED_KEY_PROPERTY_KEY).value().trim().equalsIgnoreCase("true")) {
moderationEnabled = true; moderationEnabled = true;
} }
}else {
mustBeUpdated = true;
} }
catalogueConfiguration.setModerationEnabled(moderationEnabled); catalogueConfiguration.setModerationEnabled(moderationEnabled);
Set<String> supportedOrganizations = null; Set<String> supportedOrganizations = getSupportedOrganizationsFromGenericResource();
if (propertyMap.containsKey(SUPPORTED_ORGANIZATION_PROPERTY_KEY)) {
String jsonArray = propertyMap.get(SUPPORTED_ORGANIZATION_PROPERTY_KEY).value();
supportedOrganizations = unmarshallSupportedOrganizations(jsonArray);
removeAllGenericResources();
}else {
supportedOrganizations = getSupportedOrganizationsFromGenericResource();
mustBeUpdated = true;
}
if (supportedOrganizations != null) { if (supportedOrganizations != null) {
catalogueConfiguration.setSupportedOrganizations(supportedOrganizations); catalogueConfiguration.setSupportedOrganizations(supportedOrganizations);
if(defaultOrganization==null) { if(defaultOrganization==null) {
@ -199,28 +196,9 @@ public class GCoreISConfigurationProxy {
} }
} }
ServiceCKANDB ckanDB = null; ServiceCKANDB ckanDB = getCKANDBFromIS();
if (propertyMap.containsKey(CKAN_DB_URL_PROPERTY_KEY)) {
String ckanDBURL = propertyMap.get(CKAN_DB_URL_PROPERTY_KEY).value().trim();
ckanDB = new ServiceCKANDB();
ckanDB.setUrl(ckanDBURL);
String ckanDBUsername = propertyMap.get(CKAN_DB_USERNAME_PROPERTY_KEY).value().trim();
ckanDB.setUsername(ckanDBUsername);
// Password is encrypted
String ckanDBPassword = propertyMap.get(CKAN_DB_PASSWORD_PROPERTY_KEY).value().trim();
ckanDB.setEncryptedPassword(ckanDBPassword);
}else {
mustBeUpdated = true;
ckanDB = getCKANDBFromIS();
}
catalogueConfiguration.setCkanDB(ckanDB); catalogueConfiguration.setCkanDB(ckanDB);
if(mustBeUpdated) {
logger.warn("The ServiceEndpoint with ID {} in context {} should be updated", serviceEndpoint.id(), context);
}
} catch (WebApplicationException e) { } catch (WebApplicationException e) {
throw e; throw e;
} catch (Exception e) { } catch (Exception e) {
@ -236,22 +214,33 @@ public class GCoreISConfigurationProxy {
* @return list of endpoints for ckan data catalogue * @return list of endpoints for ckan data catalogue
* @throws Exception * @throws Exception
*/ */
private List<ServiceEndpoint> getServiceEndpoints() { private List<ServiceEndpoint> getServiceEndpoints(String category, String name) {
SimpleQuery query = queryFor(ServiceEndpoint.class); SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Category/text() eq '" + CATEGORY + "'"); query.addCondition("$resource/Profile/Category/text() eq '" + category + "'");
query.addCondition("$resource/Profile/Name/text() eq '" + NAME + "'"); query.addCondition("$resource/Profile/Name/text() eq '" + name + "'");
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class); DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> serviceEndpoints = client.submit(query); List<ServiceEndpoint> serviceEndpoints = client.submit(query);
if(serviceEndpoints.size() == 0) {
String error = String.format("There is no %s having category '%s' and name '%s' in this context.",
ServiceEndpoint.class.getSimpleName(), category, name);
logger.error(error);
throw new InternalServerErrorException(error);
}
return serviceEndpoints; return serviceEndpoints;
} }
private ServiceEndpoint getServiceEndpoint() { /**
List<ServiceEndpoint> serviceEndpoints = getServiceEndpoints(); * Retrieve endpoints information from IS for DataCatalogue URL
* @return list of endpoints for ckan data catalogue
* @throws Exception
*/
private ServiceEndpoint getOldServiceEndpoint() {
List<ServiceEndpoint> serviceEndpoints = getServiceEndpoints(OLD_CATEGORY, OLD_NAME);
if (serviceEndpoints.size() == 0) { if (serviceEndpoints.size() == 0) {
logger.error("There is no {} having Category {} and Name {} in this context.", logger.error("There is no {} having Category {} and Name {} in this context.",
ServiceEndpoint.class.getSimpleName(), CATEGORY, NAME); ServiceEndpoint.class.getSimpleName(), OLD_CATEGORY, OLD_NAME);
return null; return null;
} }
@ -259,7 +248,7 @@ public class GCoreISConfigurationProxy {
if (serviceEndpoints.size() > 1) { if (serviceEndpoints.size() > 1) {
logger.info("Too many {} having Category {} and Name {} in this context. Looking for the one that has the property {}", logger.info("Too many {} having Category {} and Name {} in this context. Looking for the one that has the property {}",
ServiceEndpoint.class.getSimpleName(), CATEGORY, NAME, IS_ROOT_MASTER_PROPERTY_KEY); ServiceEndpoint.class.getSimpleName(), OLD_CATEGORY, OLD_NAME, IS_ROOT_MASTER_PROPERTY_KEY);
for (ServiceEndpoint se : serviceEndpoints) { for (ServiceEndpoint se : serviceEndpoints) {
Iterator<AccessPoint> accessPointIterator = se.profile().accessPoints().iterator(); Iterator<AccessPoint> accessPointIterator = se.profile().accessPoints().iterator();
@ -297,29 +286,9 @@ public class GCoreISConfigurationProxy {
private final static String CKAN_DB_SERVICE_ENDPOINT_CATEGORY= "Database"; private final static String CKAN_DB_SERVICE_ENDPOINT_CATEGORY= "Database";
private final static String CKAN_DB_SERVICE_ENDPOINT_NAME = "CKanDatabase"; private final static String CKAN_DB_SERVICE_ENDPOINT_NAME = "CKanDatabase";
/**
* Retrieve endpoints information from IS for DataCatalogue URL
* @return list of endpoints for ckan data catalogue
* @throws Exception
*/
protected List<ServiceEndpoint> getCKANDBServiceEndpoints() {
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Category/text() eq '" + CKAN_DB_SERVICE_ENDPOINT_CATEGORY + "'");
query.addCondition("$resource/Profile/Name/text() eq '" + CKAN_DB_SERVICE_ENDPOINT_NAME + "'");
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> serviceEndpoints = client.submit(query);
if(serviceEndpoints.size() == 0) {
logger.error("There is no {} having category {} and name {} in this context.",
ServiceEndpoint.class.getSimpleName(), CKAN_DB_SERVICE_ENDPOINT_CATEGORY, CKAN_DB_SERVICE_ENDPOINT_NAME);
throw new InternalServerErrorException("No CKAN configuration on IS");
}
return serviceEndpoints;
}
protected ServiceCKANDB getCKANDBFromIS() { protected ServiceCKANDB getCKANDBFromIS() {
try { try {
List<ServiceEndpoint> serviceEndpoints = getCKANDBServiceEndpoints(); List<ServiceEndpoint> serviceEndpoints = getServiceEndpoints(CKAN_DB_SERVICE_ENDPOINT_CATEGORY, CKAN_DB_SERVICE_ENDPOINT_NAME);
ServiceEndpoint serviceEndpoint = null; ServiceEndpoint serviceEndpoint = null;
if(serviceEndpoints.size() > 1) { if(serviceEndpoints.size() > 1) {
@ -467,7 +436,7 @@ public class GCoreISConfigurationProxy {
public void delete() { public void delete() {
RegistryPublisher registryPublisher = RegistryPublisherFactory.create(); RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
ServiceEndpoint serviceEndpoint = getServiceEndpoint(); ServiceEndpoint serviceEndpoint = getOldServiceEndpoint();
if(serviceEndpoint!=null) { if(serviceEndpoint!=null) {
registryPublisher.remove(serviceEndpoint); registryPublisher.remove(serviceEndpoint);
} }
@ -579,8 +548,8 @@ public class GCoreISConfigurationProxy {
* <Name>CKanDataCatalogue</Name> * <Name>CKanDataCatalogue</Name>
* <Description>gCat Configuration created/updated by the service via REST</Description> * <Description>gCat Configuration created/updated by the service via REST</Description>
*/ */
profile.category(CATEGORY); profile.category(OLD_CATEGORY);
profile.name(NAME); profile.name(OLD_NAME);
profile.description(String.format("gCat configuration %s by the service via REST", update ? "updated" : "created")); profile.description(String.format("gCat configuration %s by the service via REST", update ? "updated" : "created"));
return profile; return profile;
} }
@ -669,7 +638,7 @@ public class GCoreISConfigurationProxy {
} }
public ServiceCatalogueConfiguration createOrUpdateOnIS() throws Exception { public ServiceCatalogueConfiguration createOrUpdateOnIS() throws Exception {
ServiceEndpoint serviceEndpoint = getServiceEndpoint(); ServiceEndpoint serviceEndpoint = getOldServiceEndpoint();
if(serviceEndpoint!=null) { if(serviceEndpoint!=null) {
// It's an update // It's an update
updateOnIS(serviceEndpoint); updateOnIS(serviceEndpoint);

View File

@ -27,7 +27,7 @@ public class GCoreISConfigurationProxyTest extends ContextTest {
ContextTest.setContextByName("/gcube/devNext"); ContextTest.setContextByName("/gcube/devNext");
String context = SecretManager.instance.get().getContext(); String context = SecretManager.instance.get().getContext();
GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context); GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context);
ServiceCatalogueConfiguration catalogueConfiguration = gCoreISConfigurationProxy.getCatalogueConfigurationFromIS(); ServiceCatalogueConfiguration catalogueConfiguration = gCoreISConfigurationProxy.getOLDCatalogueConfigurationFromIS();
String json = catalogueConfiguration.toJsonString(); String json = catalogueConfiguration.toJsonString();
logger.info("Configuration in context {} is {}", context, json); logger.info("Configuration in context {} is {}", context, json);
ServiceCatalogueConfiguration secondCatalogueConfiguration = ServiceCatalogueConfiguration.getServiceCatalogueConfiguration(json); ServiceCatalogueConfiguration secondCatalogueConfiguration = ServiceCatalogueConfiguration.getServiceCatalogueConfiguration(json);
@ -41,6 +41,20 @@ public class GCoreISConfigurationProxyTest extends ContextTest {
logger.info("All as JsonArray [{},{},{},{}]", json, secondJson, decryptedJson, thirdJson); logger.info("All as JsonArray [{},{},{},{}]", json, secondJson, decryptedJson, thirdJson);
} }
@Test
public void updateConfigurationToNewVersion() throws Exception {
ContextTest.setContextByName("/gcube/devsec/devVRE");
String context = SecretManager.instance.get().getContext();
GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context);
ServiceCatalogueConfiguration catalogueConfiguration = gCoreISConfigurationProxy.getOLDCatalogueConfigurationFromIS();
String json = catalogueConfiguration.toJsonString();
logger.debug("Read configuration {}", json);
catalogueConfiguration = gCoreISConfigurationProxy.createOrUpdateOnIS();
json = catalogueConfiguration.toJsonString();
logger.debug("Updated configuration {}", json);
}
// protected GenericResource instantiateGenericResource(String secondaryType, String name, String xml) throws Exception { // protected GenericResource instantiateGenericResource(String secondaryType, String name, String xml) throws Exception {
// GenericResource genericResource = new GenericResource(); // GenericResource genericResource = new GenericResource();