From c9eb4283da025f7ff899148719695155a6966d59 Mon Sep 17 00:00:00 2001 From: pshenai Date: Fri, 24 Nov 2023 13:08:39 +0200 Subject: [PATCH] add possibility to create a view via the REST API --- .../config/MoskitoControlConfiguration.java | 17 +++ .../control/core/ComponentRepository.java | 22 +-- .../java/org/moskito/control/core/View.java | 1 + .../ui/restapi/config/ConfigResource.java | 17 ++- .../ui/restapi/config/bean/ViewPO.java | 137 ++++++++++++++++++ 5 files changed, 181 insertions(+), 13 deletions(-) create mode 100644 ui/src/main/java/org/moskito/control/ui/restapi/config/bean/ViewPO.java diff --git a/config/src/main/java/org/moskito/control/config/MoskitoControlConfiguration.java b/config/src/main/java/org/moskito/control/config/MoskitoControlConfiguration.java index d6bdac97..62d99772 100644 --- a/config/src/main/java/org/moskito/control/config/MoskitoControlConfiguration.java +++ b/config/src/main/java/org/moskito/control/config/MoskitoControlConfiguration.java @@ -377,6 +377,23 @@ public void removeChart(String name) { charts = newCharts; } + public void addView(ViewConfig newView){ + //first we check if we have a view by this name, if positive - update, if negative - create. + int i = 0; + for (ViewConfig view : views){ + if (view.getName().equals(newView.getName())){ + views[i] = newView; + return; + } + i++; + } + + ViewConfig[] newViews = Arrays.copyOf(views, views.length + 1); + + newViews[newViews.length - 1] = newView; + views = newViews; + } + public void removeView(String name) { boolean hasView = false; for (ViewConfig view : views){ diff --git a/core/src/main/java/org/moskito/control/core/ComponentRepository.java b/core/src/main/java/org/moskito/control/core/ComponentRepository.java index 9f6ae71a..a85aada6 100644 --- a/core/src/main/java/org/moskito/control/core/ComponentRepository.java +++ b/core/src/main/java/org/moskito/control/core/ComponentRepository.java @@ -217,15 +217,7 @@ private void readConfig() { views.put("ALL", defaultView); } for (ViewConfig vc : configuredViews) { - View v = new View(vc.getName()); - v.setComponentCategoryFilter(vc.getComponentCategories()); - v.setComponentFilter(vc.getComponents()); - v.setComponentTagsFilter(vc.getComponentTags()); - v.setChartFilter(vc.getCharts()); - v.setChartTagsFilter(vc.getChartTags()); - v.setWidgetsFilter(vc.getWidgets()); - v.setWidgetTagsFilter(vc.getWidgetTags()); - views.put(v.getName(), v); + addView(vc); } } @@ -266,6 +258,18 @@ private boolean noCustomConfigurationProviders(){ return customConfigurationProviders==null || customConfigurationProviders.size()==0; } + public void addView(ViewConfig vc) { + View v = new View(vc.getName()); + v.setComponentCategoryFilter(vc.getComponentCategories()); + v.setComponentFilter(vc.getComponents()); + v.setComponentTagsFilter(vc.getComponentTags()); + v.setChartFilter(vc.getCharts()); + v.setChartTagsFilter(vc.getChartTags()); + v.setWidgetsFilter(vc.getWidgets()); + v.setWidgetTagsFilter(vc.getWidgetTags()); + views.put(v.getName(), v); + } + public List getViews() { return new LinkedList<>(views.values()); } diff --git a/core/src/main/java/org/moskito/control/core/View.java b/core/src/main/java/org/moskito/control/core/View.java index d3f58ca9..48551deb 100644 --- a/core/src/main/java/org/moskito/control/core/View.java +++ b/core/src/main/java/org/moskito/control/core/View.java @@ -1,6 +1,7 @@ package org.moskito.control.core; import org.moskito.control.common.HealthColor; +import org.moskito.control.config.ViewConfig; import org.moskito.control.core.chart.Chart; import org.moskito.control.core.history.StatusUpdateHistoryItem; import org.moskito.control.core.history.StatusUpdateHistoryRepository; diff --git a/ui/src/main/java/org/moskito/control/ui/restapi/config/ConfigResource.java b/ui/src/main/java/org/moskito/control/ui/restapi/config/ConfigResource.java index 10be1231..36975c61 100644 --- a/ui/src/main/java/org/moskito/control/ui/restapi/config/ConfigResource.java +++ b/ui/src/main/java/org/moskito/control/ui/restapi/config/ConfigResource.java @@ -1,17 +1,15 @@ package org.moskito.control.ui.restapi.config; -import io.swagger.v3.oas.annotations.OpenAPIDefinition; import io.swagger.v3.oas.annotations.Parameter; -import io.swagger.v3.oas.annotations.info.Contact; -import io.swagger.v3.oas.annotations.info.Info; -import io.swagger.v3.oas.annotations.info.License; import io.swagger.v3.oas.annotations.servers.Server; import org.moskito.control.config.ChartConfig; import org.moskito.control.config.ComponentConfig; import org.moskito.control.config.MoskitoControlConfiguration; +import org.moskito.control.config.ViewConfig; import org.moskito.control.core.Component; import org.moskito.control.core.ComponentRepository; import org.moskito.control.ui.restapi.ReplyObject; +import org.moskito.control.ui.restapi.config.bean.ViewPO; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; @@ -20,6 +18,7 @@ @Path("configuration") @Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) @Server(url = "/api/v2") public class ConfigResource { @@ -64,6 +63,16 @@ public ReplyObject getViews(){ return ReplyObject.success("views", MoskitoControlConfiguration.getConfiguration().getViews()); } + @Path("views") + @POST + public ReplyObject addView(ViewPO viewPO){ + ViewConfig toAdd = viewPO.toViewConfig(); + MoskitoControlConfiguration.getConfiguration().addView(toAdd); + ComponentRepository.getInstance().addView(toAdd); + + return ReplyObject.success(); + } + @Path("views/{name}") @DELETE public ReplyObject deleteView( @Parameter(description = "Name of the view to remove from the config", required = true) diff --git a/ui/src/main/java/org/moskito/control/ui/restapi/config/bean/ViewPO.java b/ui/src/main/java/org/moskito/control/ui/restapi/config/bean/ViewPO.java new file mode 100644 index 00000000..7a6f937c --- /dev/null +++ b/ui/src/main/java/org/moskito/control/ui/restapi/config/bean/ViewPO.java @@ -0,0 +1,137 @@ +package org.moskito.control.ui.restapi.config.bean; + +import org.moskito.control.config.ViewConfig; + +import java.util.Arrays; +import java.util.Objects; + +public class ViewPO { + + private String name; + + private String[] componentCategories; + + private String[] components; + + private String[] charts; + + private String[] chartTags; + + private String[] componentTags; + + private String[] widgets; + + private String[] widgetTags; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String[] getComponentCategories() { + return componentCategories; + } + + public void setComponentCategories(String[] componentCategories) { + this.componentCategories = componentCategories; + } + + public String[] getComponents() { + return components; + } + + public void setComponents(String[] components) { + this.components = components; + } + + public String[] getCharts() { + return charts; + } + + public void setCharts(String[] charts) { + this.charts = charts; + } + + public String[] getChartTags() { + return chartTags; + } + + public void setChartTags(String[] chartTags) { + this.chartTags = chartTags; + } + + public String[] getComponentTags() { + return componentTags; + } + + public void setComponentTags(String[] componentTags) { + this.componentTags = componentTags; + } + + public String[] getWidgets() { + return widgets; + } + + public void setWidgets(String[] widgets) { + this.widgets = widgets; + } + + public String[] getWidgetTags() { + return widgetTags; + } + + public void setWidgetTags(String[] widgetTags) { + this.widgetTags = widgetTags; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ViewPO viewPO = (ViewPO) o; + return Objects.equals(name, viewPO.name) && Arrays.equals(componentCategories, viewPO.componentCategories) && Arrays.equals(components, viewPO.components) && Arrays.equals(charts, viewPO.charts) && Arrays.equals(chartTags, viewPO.chartTags) && Arrays.equals(componentTags, viewPO.componentTags) && Arrays.equals(widgets, viewPO.widgets) && Arrays.equals(widgetTags, viewPO.widgetTags); + } + + @Override + public int hashCode() { + int result = Objects.hash(name); + result = 31 * result + Arrays.hashCode(componentCategories); + result = 31 * result + Arrays.hashCode(components); + result = 31 * result + Arrays.hashCode(charts); + result = 31 * result + Arrays.hashCode(chartTags); + result = 31 * result + Arrays.hashCode(componentTags); + result = 31 * result + Arrays.hashCode(widgets); + result = 31 * result + Arrays.hashCode(widgetTags); + return result; + } + + @Override + public String toString() { + return "ViewPO{" + + "name='" + name + '\'' + + ", componentCategories=" + Arrays.toString(componentCategories) + + ", components=" + Arrays.toString(components) + + ", charts=" + Arrays.toString(charts) + + ", chartTags=" + Arrays.toString(chartTags) + + ", componentTags=" + Arrays.toString(componentTags) + + ", widgets=" + Arrays.toString(widgets) + + ", widgetTags=" + Arrays.toString(widgetTags) + + '}'; + } + + public ViewConfig toViewConfig(){ + ViewConfig view = new ViewConfig(); + view.setName(this.getName()); + view.setComponentCategories(this.getComponentCategories()); + view.setComponents(this.getComponents()); + view.setCharts(this.getCharts()); + view.setChartTags(this.getChartTags()); + view.setComponentTags(this.getComponentTags()); + view.setWidgets(this.getWidgets()); + view.setWidgetTags(this.getWidgetTags()); + return view; + } +}