From ea484bac34db816ce943863bd3fad0a038cab1c7 Mon Sep 17 00:00:00 2001 From: lucio Date: Tue, 7 Apr 2020 15:25:38 +0200 Subject: [PATCH] published used to remove resource changed to ScopedPublisher --- .classpath | 9 ++---- .settings/org.eclipse.jdt.core.prefs | 9 ++++-- pom.xml | 6 ++-- .../utils/sweeper/ContainerSweeperClient.java | 17 +++++++++-- .../smartgears/utils/sweeper/ContextBean.java | 26 +++++++++++++++++ .../smartgears/utils/sweeper/Sweeper.java | 28 ++++++++++++------ .../utils/sweeper/test/SweeperTest.java | 29 ++++++++++++++++++- 7 files changed, 99 insertions(+), 25 deletions(-) diff --git a/.classpath b/.classpath index 184c35d..5aafc5f 100644 --- a/.classpath +++ b/.classpath @@ -6,23 +6,20 @@ - - - - - + + - + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index ec4300d..2f5cc74 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,5 +1,8 @@ eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 -org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.source=1.7 +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/pom.xml b/pom.xml index 34a5837..7d4baea 100644 --- a/pom.xml +++ b/pom.xml @@ -4,11 +4,11 @@ org.gcube.tools maven-parent - 1.0.0 + 1.1.0 org.gcube.core common-smartgears-utils - 1.0.3-SNAPSHOT + 1.0.4-SNAPSHOT org.gcube.common @@ -23,7 +23,7 @@ org.gcube.resources registry-publisher - [1.2.2-SNAPSHOT, 1.3.0-SNAPSHOT) + [1.2.2-SNAPSHOT, 1.4.0-SNAPSHOT) org.slf4j diff --git a/src/main/java/org/gcube/smartgears/utils/sweeper/ContainerSweeperClient.java b/src/main/java/org/gcube/smartgears/utils/sweeper/ContainerSweeperClient.java index 3540851..e07264b 100644 --- a/src/main/java/org/gcube/smartgears/utils/sweeper/ContainerSweeperClient.java +++ b/src/main/java/org/gcube/smartgears/utils/sweeper/ContainerSweeperClient.java @@ -12,12 +12,24 @@ public class ContainerSweeperClient { public static void main (String args[]) { + String savedTokenFileName= null; + String ghnHome = null; + + if (args.length>0) + for (String arg: args) { + if (arg.startsWith("-G")) + ghnHome = arg.replaceAll("-G(.*)$", "$1"); + else savedTokenFileName = arg; + } + + if (ghnHome==null) + ghnHome = System.getenv("GHN_HOME");; Logger logger = LoggerFactory.getLogger(ContainerSweeperClient.class); Sweeper sw = null; try { - sw = new Sweeper(); + sw = new Sweeper(ghnHome); } catch (Exception e) { System.out.println("Error initializing the Sweeper"); e.printStackTrace(); @@ -26,8 +38,7 @@ public class ContainerSweeperClient { } try { - if (args.length>0){ - String savedTokenFileName = args[0]; + if (savedTokenFileName!=null){ sw.saveTokens(savedTokenFileName); logger.info("file saved on Smartgears directory with name {} ",savedTokenFileName); } diff --git a/src/main/java/org/gcube/smartgears/utils/sweeper/ContextBean.java b/src/main/java/org/gcube/smartgears/utils/sweeper/ContextBean.java index dcd0801..aa9f764 100644 --- a/src/main/java/org/gcube/smartgears/utils/sweeper/ContextBean.java +++ b/src/main/java/org/gcube/smartgears/utils/sweeper/ContextBean.java @@ -18,5 +18,31 @@ public class ContextBean { public String getContext() { return context; } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((token == null) ? 0 : token.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ContextBean other = (ContextBean) obj; + if (token == null) { + if (other.token != null) + return false; + } else if (!token.equals(other.token)) + return false; + return true; + } + } diff --git a/src/main/java/org/gcube/smartgears/utils/sweeper/Sweeper.java b/src/main/java/org/gcube/smartgears/utils/sweeper/Sweeper.java index 3698ff7..f12f399 100644 --- a/src/main/java/org/gcube/smartgears/utils/sweeper/Sweeper.java +++ b/src/main/java/org/gcube/smartgears/utils/sweeper/Sweeper.java @@ -7,8 +7,11 @@ import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.ObjectInputStream; -import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; import org.gcube.common.authorization.library.AuthorizationEntry; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; @@ -16,6 +19,7 @@ import org.gcube.common.resources.gcore.HostingNode; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.informationsystem.publisher.RegistryPublisher; import org.gcube.informationsystem.publisher.RegistryPublisherFactory; +import org.gcube.informationsystem.publisher.ScopedPublisher; import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.gcube.resources.discovery.icclient.ICFactory; @@ -38,13 +42,13 @@ public class Sweeper { String ghn_path; String id ; - List contextBeans = new ArrayList(); + Set contextBeans = new HashSet(); - public Sweeper () throws Exception { + public Sweeper (String ghnPath) throws Exception { logger = LoggerFactory.getLogger(Sweeper.class); - ghn_path = System.getenv("GHN_HOME"); + ghn_path = ghnPath; if (ghn_path == null ) { logger.error("GHN_HOME not defined"); @@ -72,21 +76,27 @@ public class Sweeper { public void forceDeleteHostingNode(){ - RegistryPublisher rp=RegistryPublisherFactory.create(); + ScopedPublisher rp=RegistryPublisherFactory.scopedPublisher(); try{ DiscoveryClient client = ICFactory.clientFor(HostingNode.class); SimpleQuery query = ICFactory.queryFor(HostingNode.class); query.addCondition("$resource/ID/text() = '"+id+"'"); + List scopes = contextBeans.stream().map(b -> b.getContext()).collect(Collectors.toList()); + ScopeProvider.instance.set(scopes.get(0)); + HostingNode node =null; for (ContextBean contextBean : contextBeans){ - SecurityTokenProvider.instance.set(contextBean.getToken()); ScopeProvider.instance.set(contextBean.getContext()); List nodes = client.submit(query); if (nodes.isEmpty()) continue; - rp.remove(nodes.get(0)); + else { + node = nodes.get(0); + break; + } + } - - + rp.remove(node, scopes); + ScopeProvider.instance.reset(); }catch(Exception e){ throw new RuntimeException("error removing hosting node resource",e); } diff --git a/src/test/java/org/gcube/smartgears/utils/sweeper/test/SweeperTest.java b/src/test/java/org/gcube/smartgears/utils/sweeper/test/SweeperTest.java index bade898..de382c8 100644 --- a/src/test/java/org/gcube/smartgears/utils/sweeper/test/SweeperTest.java +++ b/src/test/java/org/gcube/smartgears/utils/sweeper/test/SweeperTest.java @@ -1,7 +1,19 @@ package org.gcube.smartgears.utils.sweeper.test; + + +import java.util.List; + +import org.gcube.common.resources.gcore.HostingNode; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.informationsystem.publisher.RegistryPublisher; +import org.gcube.informationsystem.publisher.RegistryPublisherFactory; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.gcube.resources.discovery.icclient.ICFactory; import org.gcube.smartgears.utils.sweeper.Sweeper; import org.junit.Before; +import org.junit.Test; public class SweeperTest { @@ -10,12 +22,27 @@ public class SweeperTest { @Before public void setUp(){ try { - sw = new Sweeper(); + sw = new Sweeper("/home/lucio/Smartgears"); } catch (Exception e) { e.printStackTrace(); } } + @Test + public void deleteHNs() { + sw.forceDeleteHostingNode(); + } + @Test + public void deleteFromScope() { + + RegistryPublisher rp=RegistryPublisherFactory.create(); + ScopeProvider.instance.set("/gcube/devNext/NextNext"); + DiscoveryClient client = ICFactory.clientFor(HostingNode.class); + SimpleQuery query = ICFactory.queryFor(HostingNode.class); + query.addCondition("$resource/ID/text() = 'c8201474-a8cd-440f-9f62-a1e1b107ceb6'"); + List nodes = client.submit(query); + rp.remove(nodes.get(0)); + } }