published used to remove resource changed to ScopedPublisher

This commit is contained in:
lucio 2020-04-07 15:25:38 +02:00
parent cb0d970d3c
commit ea484bac34
7 changed files with 99 additions and 25 deletions

View File

@ -6,23 +6,20 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>

View File

@ -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

View File

@ -4,11 +4,11 @@
<parent>
<groupId>org.gcube.tools</groupId>
<artifactId>maven-parent</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
</parent>
<groupId>org.gcube.core</groupId>
<artifactId>common-smartgears-utils</artifactId>
<version>1.0.3-SNAPSHOT</version>
<version>1.0.4-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.gcube.common</groupId>
@ -23,7 +23,7 @@
<dependency>
<groupId>org.gcube.resources</groupId>
<artifactId>registry-publisher</artifactId>
<version>[1.2.2-SNAPSHOT, 1.3.0-SNAPSHOT)</version>
<version>[1.2.2-SNAPSHOT, 1.4.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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<ContextBean> contextBeans = new ArrayList<ContextBean>();
Set<ContextBean> contextBeans = new HashSet<ContextBean>();
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<HostingNode> client = ICFactory.clientFor(HostingNode.class);
SimpleQuery query = ICFactory.queryFor(HostingNode.class);
query.addCondition("$resource/ID/text() = '"+id+"'");
List<String> 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<HostingNode> 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);
}

View File

@ -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<HostingNode> client = ICFactory.clientFor(HostingNode.class);
SimpleQuery query = ICFactory.queryFor(HostingNode.class);
query.addCondition("$resource/ID/text() = 'c8201474-a8cd-440f-9f62-a1e1b107ceb6'");
List<HostingNode> nodes = client.submit(query);
rp.remove(nodes.get(0));
}
}