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>
<application mode='online'>
<name>${project.artifactId}</name>
<group>${serviceClass}</group>
<group>${project.groupId}</group>
<version>${project.version}</version>
<description>${project.description}</description>
</application>

View File

@ -21,7 +21,6 @@
<properties>
<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>
<serviceClass>data-catalogue</serviceClass>
</properties>
<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.queryFor;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
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 SUPPORTED_ORGANIZATION_PROPERTY_KEY = "SUPPORTED_ORGANIZATION";
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 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 MODERATION_ENABLED_KEY_PROPERTY_KEY = "MODERATION_ENABLED";
public final static String CKAN_DB_URL_PROPERTY_KEY = "CKAN_DB_URL";
public final static String CKAN_DB_USERNAME_PROPERTY_KEY = "CKAN_DB_USERNAME";
public final static String CKAN_DB_PASSWORD_PROPERTY_KEY = "CKAN_DB_PASSWORD";
public static final Map<String, String> gCoreToConfigurationMapping;
public static final Map<String, String> configurationToGCoreMapping;
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
private final static String CATEGORY = "Application";
private final static String NAME = "CKanDataCatalogue";
private final static String OLD_CATEGORY = "Application";
private final static String OLD_NAME = "CKanDataCatalogue";
protected final String context;
protected ServiceCatalogueConfiguration catalogueConfiguration;
@ -81,7 +99,7 @@ public class GCoreISConfigurationProxy {
public ServiceCatalogueConfiguration getCatalogueConfiguration() {
if (catalogueConfiguration == null) {
catalogueConfiguration = getCatalogueConfigurationFromIS();
catalogueConfiguration = getOLDCatalogueConfigurationFromIS();
}
return catalogueConfiguration;
}
@ -102,11 +120,15 @@ public class GCoreISConfigurationProxy {
}
protected ServiceCatalogueConfiguration getCatalogueConfigurationFromIS() {
return getOLDCatalogueConfigurationFromIS();
}
protected ServiceCatalogueConfiguration getOLDCatalogueConfigurationFromIS() {
ServiceCatalogueConfiguration catalogueConfiguration = new ServiceCatalogueConfiguration(context);
try {
boolean mustBeUpdated = false;
// boolean mustBeUpdated = false;
ServiceEndpoint serviceEndpoint = getServiceEndpoint();
ServiceEndpoint serviceEndpoint = getOldServiceEndpoint();
if (serviceEndpoint == null) {
throw new InternalServerErrorException("No CKAN configuration on IS");
}
@ -127,17 +149,7 @@ public class GCoreISConfigurationProxy {
catalogueConfiguration.setEncryptedSysAdminToken(encryptedSysAdminToken);
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 defaultOrganization = CatalogueConfiguration.getOrganizationName(context);
String solrURL = null;
@ -154,8 +166,6 @@ public class GCoreISConfigurationProxy {
if (propertyMap.get(SOCIAL_POST_PROPERTY_KEY).value().trim().equalsIgnoreCase("false")) {
socialPostEnabled = false;
}
}else {
mustBeUpdated = true;
}
catalogueConfiguration.setSocialPostEnabled(socialPostEnabled);
@ -166,8 +176,6 @@ public class GCoreISConfigurationProxy {
.equalsIgnoreCase("true")) {
notificationToUsersEnabled = true;
}
}else {
mustBeUpdated = true;
}
catalogueConfiguration.setNotificationToUsersEnabled(notificationToUsersEnabled);
@ -176,21 +184,10 @@ public class GCoreISConfigurationProxy {
if (propertyMap.get(MODERATION_ENABLED_KEY_PROPERTY_KEY).value().trim().equalsIgnoreCase("true")) {
moderationEnabled = true;
}
}else {
mustBeUpdated = true;
}
catalogueConfiguration.setModerationEnabled(moderationEnabled);
Set<String> supportedOrganizations = null;
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;
}
Set<String> supportedOrganizations = getSupportedOrganizationsFromGenericResource();
if (supportedOrganizations != null) {
catalogueConfiguration.setSupportedOrganizations(supportedOrganizations);
if(defaultOrganization==null) {
@ -199,28 +196,9 @@ public class GCoreISConfigurationProxy {
}
}
ServiceCKANDB ckanDB = null;
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();
}
ServiceCKANDB ckanDB = getCKANDBFromIS();
catalogueConfiguration.setCkanDB(ckanDB);
if(mustBeUpdated) {
logger.warn("The ServiceEndpoint with ID {} in context {} should be updated", serviceEndpoint.id(), context);
}
} catch (WebApplicationException e) {
throw e;
} catch (Exception e) {
@ -236,22 +214,33 @@ public class GCoreISConfigurationProxy {
* @return list of endpoints for ckan data catalogue
* @throws Exception
*/
private List<ServiceEndpoint> getServiceEndpoints() {
private List<ServiceEndpoint> getServiceEndpoints(String category, String name) {
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Category/text() eq '" + CATEGORY + "'");
query.addCondition("$resource/Profile/Name/text() eq '" + NAME + "'");
query.addCondition("$resource/Profile/Category/text() eq '" + category + "'");
query.addCondition("$resource/Profile/Name/text() eq '" + name + "'");
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
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;
}
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) {
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;
}
@ -259,7 +248,7 @@ public class GCoreISConfigurationProxy {
if (serviceEndpoints.size() > 1) {
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) {
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_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() {
try {
List<ServiceEndpoint> serviceEndpoints = getCKANDBServiceEndpoints();
List<ServiceEndpoint> serviceEndpoints = getServiceEndpoints(CKAN_DB_SERVICE_ENDPOINT_CATEGORY, CKAN_DB_SERVICE_ENDPOINT_NAME);
ServiceEndpoint serviceEndpoint = null;
if(serviceEndpoints.size() > 1) {
@ -467,7 +436,7 @@ public class GCoreISConfigurationProxy {
public void delete() {
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
ServiceEndpoint serviceEndpoint = getServiceEndpoint();
ServiceEndpoint serviceEndpoint = getOldServiceEndpoint();
if(serviceEndpoint!=null) {
registryPublisher.remove(serviceEndpoint);
}
@ -579,8 +548,8 @@ public class GCoreISConfigurationProxy {
* <Name>CKanDataCatalogue</Name>
* <Description>gCat Configuration created/updated by the service via REST</Description>
*/
profile.category(CATEGORY);
profile.name(NAME);
profile.category(OLD_CATEGORY);
profile.name(OLD_NAME);
profile.description(String.format("gCat configuration %s by the service via REST", update ? "updated" : "created"));
return profile;
}
@ -669,7 +638,7 @@ public class GCoreISConfigurationProxy {
}
public ServiceCatalogueConfiguration createOrUpdateOnIS() throws Exception {
ServiceEndpoint serviceEndpoint = getServiceEndpoint();
ServiceEndpoint serviceEndpoint = getOldServiceEndpoint();
if(serviceEndpoint!=null) {
// It's an update
updateOnIS(serviceEndpoint);

View File

@ -27,7 +27,7 @@ public class GCoreISConfigurationProxyTest extends ContextTest {
ContextTest.setContextByName("/gcube/devNext");
String context = SecretManager.instance.get().getContext();
GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context);
ServiceCatalogueConfiguration catalogueConfiguration = gCoreISConfigurationProxy.getCatalogueConfigurationFromIS();
ServiceCatalogueConfiguration catalogueConfiguration = gCoreISConfigurationProxy.getOLDCatalogueConfigurationFromIS();
String json = catalogueConfiguration.toJsonString();
logger.info("Configuration in context {} is {}", context, json);
ServiceCatalogueConfiguration secondCatalogueConfiguration = ServiceCatalogueConfiguration.getServiceCatalogueConfiguration(json);
@ -41,6 +41,20 @@ public class GCoreISConfigurationProxyTest extends ContextTest {
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 {
// GenericResource genericResource = new GenericResource();