Skip to content

Commit

Permalink
fixed refreshing tree upon config change
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Nov 8, 2023
1 parent ca970f6 commit 17c4cc5
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class ToolFactoryTest extends BasePlatformTestCase {

public void testGetOdo() throws ExecutionException, InterruptedException {
Odo odo = ToolFactory.getInstance().getOdo(getProject()).get();
Odo odo = ToolFactory.getInstance().create(getProject()).get();
assertNotNull(odo);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public abstract class HelmCliTest extends BasePlatformTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
Odo odo = ToolFactory.getInstance().getOdo(getProject()).get();
Odo odo = ToolFactory.getInstance().create(getProject()).get();
OdoCluster.INSTANCE.login(odo);
this.helm = ToolFactory.getInstance().getHelm(getProject()).get();
helm.addRepo("openshift", "https://charts.openshift.io/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ public abstract class OdoCliTest extends BasePlatformTestCase {
protected void setUp() throws Exception {
super.setUp();
previousTestDialog = MessagesHelper.setTestDialog(TestDialog.OK);
odo = ToolFactory.getInstance().getOdo(getProject()).get();
odo = ToolFactory.getInstance().create(getProject()).get();
if (odo.listDevfileRegistries().stream().noneMatch(c -> c.getName().equals(REGISTRY_NAME)))
odo.createDevfileRegistry(REGISTRY_NAME, REGISTRY_URL, null);

if (CLUSTER_URL != null && !odo.getMasterUrl().toString().startsWith(CLUSTER_URL)) {
odo.login(CLUSTER_URL, CLUSTER_USER, CLUSTER_PASSWORD.toCharArray(), null);
odo = ToolFactory.getInstance().getOdo(getProject()).get();
odo = ToolFactory.getInstance().create(getProject()).get();
}
}

Expand All @@ -81,8 +81,8 @@ protected void tearDown() throws Exception {
protected void createProject(String project) throws IOException, ExecutionException, InterruptedException {
odo.createProject(project);
// need to refresh kubernetes client with the correct namespace
ToolFactory.getInstance().resetOdo();
odo = ToolFactory.getInstance().getOdo(getProject()).get();
//resetOdo();
odo = ToolFactory.getInstance().create(getProject()).get();
}

protected void createComponent(String project, String component, ComponentFeature feature) throws IOException, ExecutionException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
import com.redhat.devtools.intellij.common.actions.StructureTreeAction;
import org.jboss.tools.intellij.openshift.telemetry.TelemetryService;
import org.jboss.tools.intellij.openshift.tree.application.ApplicationsRootNode;
import org.jboss.tools.intellij.openshift.tree.application.ProcessingNode;

import javax.swing.tree.TreePath;

import static org.jboss.tools.intellij.openshift.actions.ActionUtils.runWithProgress;
import static org.jboss.tools.intellij.openshift.actions.NodeUtils.clearProcessing;
import static org.jboss.tools.intellij.openshift.actions.NodeUtils.setProcessing;
import static org.jboss.tools.intellij.openshift.telemetry.TelemetryService.PREFIX_ACTION;

Expand All @@ -32,13 +30,14 @@ public RefreshAction() {
@Override
public void actionPerformed(AnActionEvent anActionEvent, TreePath path, Object selected) {
runWithProgress((ProgressIndicator progress) -> {
ProcessingNode node = getElement(selected);
setProcessing("Refreshing ", node);
ApplicationsRootNode root = getElement(selected);

setProcessing("Refreshing... ", root);
TelemetryService.instance().getBuilder()
.action(PREFIX_ACTION + "refresh cluster")
.success()
.send();
clearProcessing(node);
// clearProcessing(node);
},
"Refresh...",
getEventProject(anActionEvent));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public ApplicationsRootNode(Project project, ApplicationsTreeStructure structure
this.project = project;
this.structure = structure;
initConfigWatcher();
config = loadConfig();
this.config = loadConfig();
registerProjectListener(project);
}

Expand All @@ -81,10 +81,14 @@ public CompletableFuture<Odo> getOdo() {
return getOdo(true);
}

public void resetOdo() {
this.odoFuture = null;
}

public CompletableFuture<Odo> getOdo(boolean notify) {
if (odoFuture == null) {
this.odoFuture = ToolFactory.getInstance()
.getOdo(project)
.create(project)
.thenApply(odo -> (Odo) new ApplicationRootNodeOdo(odo, this))
.whenComplete((odo, err) -> {
loadProjectModel(odo, project);
Expand All @@ -100,8 +104,11 @@ public CompletableFuture<Helm> getHelm(boolean notify) {
if (helmFuture == null) {
this.helmFuture = ToolFactory.getInstance()
.getHelm(project)
.whenComplete((odo, err) ->
structure.fireModified(this)
.whenComplete((odo, err) -> {
if (notify) {
structure.fireModified(this);
}
}
);
}
return helmFuture;
Expand Down Expand Up @@ -223,9 +230,9 @@ protected void registerProjectListener(Project project) {
@Override
public void onUpdate(ConfigWatcher source, Config config) {
if (hasContextChanged(config, this.config)) {
this.config = config;
refresh();
}
this.config = config;
}

private boolean hasContextChanged(Config newConfig, Config currentConfig) {
Expand Down Expand Up @@ -260,8 +267,9 @@ private boolean hasNewToken(NamedContext newContext, Config newConfig, NamedCont
}

public void refresh() {
ToolFactory.getInstance().resetOdo();
getOdo().thenAccept(odo -> structure.fireModified(this));
resetOdo();
structure.fireModified(this);
//getOdo().thenAccept(odo -> structure.fireModified(this));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ public Object getApplicationsRoot() {
public Object @NotNull [] getChildElements(@NotNull Object element) {
if (element == this) {
return new Object[]{getApplicationsRoot(), registries};
}
if (element instanceof ApplicationsRootNode) {
} else if (element instanceof ApplicationsRootNode) {
return getNamespaces((ApplicationsRootNode) element);
} else if (element instanceof NamespaceNode) {
return createNamespaceChildren((NamespaceNode) element);
Expand All @@ -86,6 +85,31 @@ public Object getApplicationsRoot() {
return new Object[0];
}

@Override
public boolean isAlwaysLeaf(@NotNull Object element) {
if (element == this) {
return false;
} else if (element instanceof ApplicationsRootNode) {
return false;
} else if (element instanceof NamespaceNode) {
return false;
} else if (element instanceof ComponentNode) {
return true;
} else if (element instanceof DevfileRegistriesNode) {
return false;
} else if (element instanceof DevfileRegistryNode) {
return false;
} else if (element instanceof DevfileRegistryComponentTypeNode) {
return true;
} else if (element instanceof ChartReleaseNode) {
return true;
} else if (element instanceof MessageNode<?>) {
return true;
} else {
return false;
}
}

@NotNull
private Object[] createComponentChildren(ComponentNode element) {
List<URLNode> urls = getURLs(element);
Expand Down Expand Up @@ -125,25 +149,26 @@ private Object[] getNamespaces(ApplicationsRootNode element) {
}
element.setLogged(true);
} catch (Exception e) {
if (e instanceof KubernetesClientException) {
KubernetesClientException kce = (KubernetesClientException) e;
if (kce.getCode() == 401) {
namespaces.add(new MessageNode<>(element, element, LOGIN));
} else if (kce.getCause() instanceof NoRouteToHostException) {
namespaces.add(new MessageNode<>(element, element, kce.getCause().getMessage()));
} else if (kce.getCause().getMessage().contains(Constants.DEFAULT_KUBE_URL)) {
namespaces.add(new MessageNode<>(element, element, LOGIN));
} else {
namespaces.add(new MessageNode<>(element, element, "Unable to get namespaces: " + e.getMessage()));
}
} else {
namespaces.add(new MessageNode<>(element, element, "Unable to get namespaces: " + e.getMessage()));
}
namespaces.add(createNode(element, e));
element.setLogged(false);
}
return namespaces.toArray();
}

private static MessageNode<?> createNode(ApplicationsRootNode element, Exception e) {
if (e instanceof KubernetesClientException) {
KubernetesClientException kce = (KubernetesClientException) e;
if (kce.getCode() == 401) {
return new MessageNode<>(element, element, LOGIN);
} else if (kce.getCause() instanceof NoRouteToHostException) {
return new MessageNode<>(element, element, kce.getCause().getMessage());
} else if (kce.getCause().getMessage().contains(Constants.DEFAULT_KUBE_URL)) {
return new MessageNode<>(element, element, LOGIN);
}
}
return new MessageNode<>(element, element, "Unable to get namespaces: " + e.getMessage());
}

private List<ParentableNode<?>> getComponents(NamespaceNode element, Odo odo) {
if (odo == null) {
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ public class DescriptorFactory {
project,
root,
() -> {
String label = "Loading...";
try {
Odo odo = root.getOdo().getNow(null);
if (odo != null) {
label = odo.getMasterUrl().toString();
if (odo == null) {
return "Loading...";
}
return label;
return odo.getMasterUrl().toString();
} catch (Exception e) {
return "Error: " + e.getCause().getMessage();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,12 @@ public static ToolFactory getInstance() {
private ToolFactory() {
}

public CompletableFuture<Odo> getOdo(Project project) {
return odo.get(project);
}

public void resetOdo() {
this.odo.reset();
public CompletableFuture<Odo> create(Project project) {
return odo.create(project);
}

public CompletableFuture<Helm> getHelm(Project project) {
return helm.get(project);
}

public void resetHelm() {
this.helm.reset();
return helm.create(project);
}

private static class Tool<T> {
Expand All @@ -62,18 +54,13 @@ private static class Tool<T> {

private final BiFunction<Project, String, T> toolFactory;

private CompletableFuture<T> factory = null;

private Tool(String name, BiFunction<Project, String, T> toolFactory) {
this.name = name;
this.toolFactory = toolFactory;
}

private CompletableFuture<T> get(Project project) {
if (factory == null) {
this.factory = create(name, toolFactory, project);
}
return factory;
private CompletableFuture<T> create(Project project) {
return create(name, toolFactory, project);
}

private CompletableFuture<T> create(String name, BiFunction<Project, String, T> toolFactory, Project project) {
Expand All @@ -87,9 +74,5 @@ private CompletableFuture<T> create(String name, BiFunction<Project, String, T>
}
});
}

private void reset() {
this.factory = null;
}
}
}

0 comments on commit 17c4cc5

Please sign in to comment.