From d78059a2773411093c1e5dc3cc831362031a6a22 Mon Sep 17 00:00:00 2001 From: Manuel Salvi Date: Mon, 13 Jan 2025 16:34:30 +0100 Subject: [PATCH] Added page to list the peripherals --- .../src/com/suse/manager/hub/HubManager.java | 22 ++- .../suse/manager/model/hub/HubFactory.java | 13 ++ .../admin/AdminViewsController.java | 11 +- .../admin/beans/PeripheralResponse.java | 148 +++++------------- .../admin/handlers/HubApiController.java | 13 +- .../src/components/hub/PeripheralsList.tsx | 83 +++++----- web/html/src/components/hub/types.tsx | 6 +- .../admin/hub/peripherals.renderer.tsx | 6 +- .../src/manager/admin/hub/peripherals.tsx | 15 +- 9 files changed, 133 insertions(+), 184 deletions(-) diff --git a/java/code/src/com/suse/manager/hub/HubManager.java b/java/code/src/com/suse/manager/hub/HubManager.java index 3c0264d2065..1b1e0970b63 100644 --- a/java/code/src/com/suse/manager/hub/HubManager.java +++ b/java/code/src/com/suse/manager/hub/HubManager.java @@ -914,6 +914,27 @@ public List collectAllChannels(IssAccessToken accessToken) { return ChannelFactory.listAllChannels(); } + /** + * Count the registered peripherals on "this" hub + * @param user the SatAdmin + * @return the count of registered peripherals entities + */ + public Long countRegisteredPeripherals(User user) { + ensureSatAdmin(user); + return hubFactory.countPeripherals(); + } + + /** + * List the peripherals with pagination //TODO: and search + * @param user the SatAdmin + * @param pc the PageControl + * @return a List of Peripherals entities + */ + public List listRegisteredPeripherals(User user, PageControl pc) { + ensureSatAdmin(user); + return hubFactory.listPaginatedPeripherals(pc.getStart() - 1, pc.getPageSize()); + } + /** * add vendor channel to peripheral * @@ -975,7 +996,6 @@ public List addVendorChannels(IssAccessToken accessToken, List .toList(); } - /** * add custom channels to peripheral * diff --git a/java/code/src/com/suse/manager/model/hub/HubFactory.java b/java/code/src/com/suse/manager/model/hub/HubFactory.java index 52a60bc5ef0..1d0d44f6e02 100644 --- a/java/code/src/com/suse/manager/model/hub/HubFactory.java +++ b/java/code/src/com/suse/manager/model/hub/HubFactory.java @@ -146,6 +146,19 @@ public long countPeripherals() { return getSession().createQuery("SELECT count(*) FROM IssPeripheral", Long.class).uniqueResult(); } + /** + * get the list of all the peripheral servers for a hub + * @param offset the first item to retrieve + * @param pageSize the maximum number of items to retrieve + * @return a list of paginated peripherals + */ + public List listPaginatedPeripherals(int offset, int pageSize) { + return getSession().createQuery("FROM IssPeripheral", IssPeripheral.class) + .setFirstResult(offset) + .setMaxResults(pageSize) + .list(); + } + /** * Lookup {@link IssPeripheral} object by its FQDN * @param fqdnIn the fqdn diff --git a/java/code/src/com/suse/manager/webui/controllers/admin/AdminViewsController.java b/java/code/src/com/suse/manager/webui/controllers/admin/AdminViewsController.java index 9dfbd62c950..d9e3b9e06bf 100644 --- a/java/code/src/com/suse/manager/webui/controllers/admin/AdminViewsController.java +++ b/java/code/src/com/suse/manager/webui/controllers/admin/AdminViewsController.java @@ -31,15 +31,12 @@ import com.suse.manager.reactor.utils.OptionalTypeAdapterFactory; import com.suse.manager.webui.controllers.ECMAScriptDateAdapter; import com.suse.manager.webui.controllers.admin.beans.HubDetailsData; -import com.suse.manager.webui.controllers.admin.beans.PeripheralResponse; import com.suse.manager.webui.controllers.admin.mappers.PaygResponseMappers; import com.suse.manager.webui.utils.FlashScopeHelper; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.google.gson.reflect.TypeToken; -import java.lang.reflect.Type; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -146,13 +143,7 @@ public static ModelAndView showISSv3Hub(Request request, Response response, User * @return the view to show */ public static ModelAndView showISSv3Peripherals(Request request, Response response, User user) { - Map data = new HashMap<>(); - Type listType = new TypeToken>() { }.getType(); - List src = HUB_FACTORY.listPeripherals().stream() - .map(ph -> new PeripheralResponse(ph.getId(), ph.getFqdn(), 0L, 0L, 0L)) - .toList(); - data.put("peripherals", GSON.toJson(src, listType)); - return new ModelAndView(data, "controllers/admin/templates/list_peripherals.jade"); + return new ModelAndView(new HashMap<>(), "controllers/admin/templates/list_peripherals.jade"); } /** diff --git a/java/code/src/com/suse/manager/webui/controllers/admin/beans/PeripheralResponse.java b/java/code/src/com/suse/manager/webui/controllers/admin/beans/PeripheralResponse.java index 1fd07596ca8..b706788796d 100644 --- a/java/code/src/com/suse/manager/webui/controllers/admin/beans/PeripheralResponse.java +++ b/java/code/src/com/suse/manager/webui/controllers/admin/beans/PeripheralResponse.java @@ -8,138 +8,72 @@ * along with this software; if not, see * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. */ - package com.suse.manager.webui.controllers.admin.beans; -import org.apache.commons.lang3.builder.EqualsBuilder; -import org.apache.commons.lang3.builder.HashCodeBuilder; +import com.suse.manager.model.hub.IssPeripheral; +import com.suse.manager.model.hub.IssPeripheralChannels; public class PeripheralResponse { - private long id; - private String fqdn; - private long nChannelsSync; - private long nAllChannels; - private long nOrgs; + private final long id; + private final String fqdn; + private final long nChannelsSync; + private final long nSyncOrgs; + private final String rootCA; /** - * Default constructor - * @param idIn the id - * @param fqdnIn the fully qualified domain name - * @param nChannelsSyncIn the number of channels currently synced - * @param nAllChannelsIn the total number of channels - * @param nOrgsIn the number of organizations + * Response for peripherals list + * @param idIn + * @param fqdnIn + * @param nChannelsSyncIn + * @param nSyncOrgsIn + * @param rootCAIn */ - public PeripheralResponse(long idIn, String fqdnIn, long nChannelsSyncIn, long nAllChannelsIn, long nOrgsIn) { - this.id = idIn; - this.fqdn = fqdnIn; - this.nChannelsSync = nChannelsSyncIn; - this.nAllChannels = nAllChannelsIn; - this.nOrgs = nOrgsIn; + public PeripheralResponse( + long idIn, + String fqdnIn, + long nChannelsSyncIn, + long nSyncOrgsIn, + String rootCAIn + ) { + id = idIn; + fqdn = fqdnIn; + nChannelsSync = nChannelsSyncIn; + nSyncOrgs = nSyncOrgsIn; + rootCA = rootCAIn; } public long getId() { return id; } - public void setId(long idIn) { - this.id = idIn; - } - public String getFqdn() { return fqdn; } - public void setFqdn(String fqdnIn) { - this.fqdn = fqdnIn; - } - - /** - * gets the number of synced channels - * @return the number of synced channel - */ - public long getnChannelsSync() { + public long getNChannelsSync() { return nChannelsSync; } - /** - * sets the number of synced channels - * @param nChannelsSyncIn the number of synced channels - */ - public void setnChannelsSync(long nChannelsSyncIn) { - this.nChannelsSync = nChannelsSyncIn; + public long getNSyncOrgs() { + return nSyncOrgs; } - /** - * gets the number of all channels - * @return the number of all channels - */ - public long getnAllChannels() { - return nAllChannels; + public String getRootCA() { + return rootCA; } /** - * sets the number of all channels - * @param nAllChannelsIn the number of all channels + * Helper converter method from db entity + * @param peripheralEntity + * @return */ - public void setnAllChannels(long nAllChannelsIn) { - this.nAllChannels = nAllChannelsIn; - } - - /** - * gets the number of organizations - * @return the number of organizations - */ - public long getnOrgs() { - return nOrgs; - } - - /** - * sets the number of organizations - * @param nOrgsIn the number of organizations - */ - public void setnOrgs(long nOrgsIn) { - this.nOrgs = nOrgsIn; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - - if (!(o instanceof PeripheralResponse that)) { - return false; - } - - return new EqualsBuilder() - .append(getId(), that.getId()) - .append(getnChannelsSync(), that.getnChannelsSync()) - .append(getnAllChannels(), that.getnAllChannels()) - .append(getnOrgs(), that.getnOrgs()) - .append(getFqdn(), that.getFqdn()) - .isEquals(); - } - - @Override - public int hashCode() { - return new HashCodeBuilder(17, 37) - .append(getId()) - .append(getFqdn()) - .append(getnChannelsSync()) - .append(getnAllChannels()) - .append(getnOrgs()) - .toHashCode(); - } - - @Override - public String toString() { - final StringBuilder sb = new StringBuilder("PeripheralResponse{"); - sb.append("id=").append(id); - sb.append(", fqdn='").append(fqdn).append('\''); - sb.append(", nChannelsSync=").append(nChannelsSync); - sb.append(", nAllChannels=").append(nAllChannels); - sb.append(", nOrgs=").append(nOrgs); - sb.append('}'); - return sb.toString(); + public static PeripheralResponse fromIssEntity(IssPeripheral peripheralEntity) { + return new PeripheralResponse( + peripheralEntity.getId(), + peripheralEntity.getFqdn(), + peripheralEntity.getPeripheralChannels().size(), + peripheralEntity.getPeripheralChannels().stream() + .map(IssPeripheralChannels::getPeripheralOrgId).distinct().count(), + peripheralEntity.getRootCa()); } } diff --git a/java/code/src/com/suse/manager/webui/controllers/admin/handlers/HubApiController.java b/java/code/src/com/suse/manager/webui/controllers/admin/handlers/HubApiController.java index fb1840292bd..e9cefb606fa 100644 --- a/java/code/src/com/suse/manager/webui/controllers/admin/handlers/HubApiController.java +++ b/java/code/src/com/suse/manager/webui/controllers/admin/handlers/HubApiController.java @@ -37,6 +37,7 @@ import com.suse.manager.webui.controllers.ECMAScriptDateAdapter; import com.suse.manager.webui.controllers.admin.beans.CreateTokenRequest; import com.suse.manager.webui.controllers.admin.beans.HubRegisterRequest; +import com.suse.manager.webui.controllers.admin.beans.PeripheralResponse; import com.suse.manager.webui.controllers.admin.beans.UpdateRootCARequest; import com.suse.manager.webui.controllers.admin.beans.ValidityRequest; import com.suse.manager.webui.utils.FlashScopeHelper; @@ -105,7 +106,7 @@ public void initRoutes() { post("/manager/api/admin/hub/access-tokens", withProductAdmin(this::createToken)); post("/manager/api/admin/hub/access-tokens/:id/validity", withProductAdmin(this::setAccessTokenValidity)); delete("/manager/api/admin/hub/access-tokens/:id", withProductAdmin(this::deleteAccessToken)); - get("/manager/api/admin/hub/peripherals", withProductAdmin(this::pass)); + get("/manager/api/admin/hub/peripherals", withProductAdmin(this::listPaginatedPeripherals)); post("/manager/api/admin/hub/peripherals", withProductAdmin(this::registerPeripheral)); get("/manager/api/admin/hub/peripherals/:id", withProductAdmin(this::pass)); patch("/manager/api/admin/hub/peripherals/:id", withProductAdmin(this::pass)); @@ -212,6 +213,16 @@ private String registerPeripheral(Request request, Response response, User satAd } } + private String listPaginatedPeripherals(Request request, Response response, User satAdmin) { + PageControlHelper pageHelper = new PageControlHelper(request); + PageControl pc = pageHelper.getPageControl(); + long totalSize = hubManager.countRegisteredPeripherals(satAdmin); + List peripherals = hubManager.listRegisteredPeripherals(satAdmin, pc).stream() + .map(PeripheralResponse::fromIssEntity).toList(); + TypeToken> type = new TypeToken<>() { }; + return json(GSON, response, new PagedDataResultJson<>(peripherals, totalSize, Collections.emptySet()), type); + } + private String listTokens(Request request, Response response, User user) { PageControlHelper pageHelper = new PageControlHelper(request); PageControl pc = pageHelper.getPageControl(); diff --git a/web/html/src/components/hub/PeripheralsList.tsx b/web/html/src/components/hub/PeripheralsList.tsx index d1136abd7a6..faca9abd76f 100644 --- a/web/html/src/components/hub/PeripheralsList.tsx +++ b/web/html/src/components/hub/PeripheralsList.tsx @@ -1,81 +1,70 @@ -import { hot } from "react-hot-loader/root"; - import * as React from "react"; -import { useState } from "react"; -import withPageWrapper from "components/general/with-page-wrapper"; -import { PeripheralListData } from "components/hub"; +import { DeregisterServer, IssRole, PeripheralListData } from "components/hub"; +import { LargeTextAttachment } from "components/large-text-attachment"; import { Column } from "components/table/Column"; import { SearchField } from "components/table/SearchField"; import { Table } from "components/table/Table"; import { Utils } from "utils/functions"; -type Props = { - peripherals: PeripheralListData[]; -}; - -export const PeripheralsList = hot( - withPageWrapper((peripheralsList: Props) => { - const [peripherals] = useState(peripheralsList.peripherals); - - const searchData = (row, criteria) => { - const keysToSearch = ["fqdn"]; - if (criteria) { - const needle = criteria.toLocaleLowerCase(); - return keysToSearch.map((key) => row[key]).some((item) => item.toLocaleLowerCase().includes(needle)); - } - return true; - }; - +export class PeripheralsList extends React.Component<{}> { + public render(): React.ReactNode { let componentContent = ( row.fqdn} - selectable={false} + data="/rhn/manager/api/admin/hub/peripherals" + identifier={(row: PeripheralListData) => row.id} initialSortColumnKey="fqdn" - searchField={} + searchField={} > ( - - {row.fqdn} - - )} + cell={(row) => row.fqdn} /> {row.nChannelsSync}} - /> - {row.nAllChannels}} + cell={(row: PeripheralListData) => row.nChannelsSync} /> {row.nOrgs}} + header={t("N. of Sync Orgs")} + cell={(row: PeripheralListData) => row.nSyncOrgs} /> ( - - DL by {row.id} - - )} + cell={(row: PeripheralListData) => this.renderDownloadRootCA(row)} + /> + this.renderDeregister(row)} />
); return componentContent; - }) -); + } + + private renderDownloadRootCA(row: PeripheralListData): React.ReactNode { + return ( + + ); + } + + private renderDeregister(row: PeripheralListData): React.ReactNode { + return ; + } + + private searchData(row: PeripheralListData, criteria: string | undefined): boolean { + if (criteria !== undefined) { + return row.fqdn.toLocaleLowerCase().includes(criteria.toLocaleLowerCase()); + } + return true; + } +} diff --git a/web/html/src/components/hub/types.tsx b/web/html/src/components/hub/types.tsx index f6959c13237..fc74cee1d07 100644 --- a/web/html/src/components/hub/types.tsx +++ b/web/html/src/components/hub/types.tsx @@ -40,11 +40,11 @@ export type CreateTokenRequest = { }; export type PeripheralListData = { - id: string; + id: number; fqdn: string; + rootCA: string; nChannelsSync: number; - nAllChannels: number; - nOrgs: number; + nSyncOrgs: number; }; export type HubDetailData = { diff --git a/web/html/src/manager/admin/hub/peripherals.renderer.tsx b/web/html/src/manager/admin/hub/peripherals.renderer.tsx index de9806e23a6..c6ba70c8bda 100644 --- a/web/html/src/manager/admin/hub/peripherals.renderer.tsx +++ b/web/html/src/manager/admin/hub/peripherals.renderer.tsx @@ -1,9 +1,7 @@ import SpaRenderer from "core/spa/spa-renderer"; -import { PeripheralListData } from "components/hub"; - import IssPeripheral from "./peripherals"; -export const renderer = (id: string, peripherals: PeripheralListData[]) => { - SpaRenderer.renderNavigationReact(, document.getElementById(id)); +export const renderer = (id: string) => { + SpaRenderer.renderNavigationReact(, document.getElementById(id)); }; diff --git a/web/html/src/manager/admin/hub/peripherals.tsx b/web/html/src/manager/admin/hub/peripherals.tsx index b683c2baabe..b26c4c53a64 100644 --- a/web/html/src/manager/admin/hub/peripherals.tsx +++ b/web/html/src/manager/admin/hub/peripherals.tsx @@ -1,27 +1,20 @@ import { hot } from "react-hot-loader/root"; import * as React from "react"; -import { useState } from "react"; import { Button } from "components/buttons"; import withPageWrapper from "components/general/with-page-wrapper"; -import { PeripheralListData, PeripheralsList } from "components/hub"; +import { PeripheralsList } from "components/hub"; import { SectionToolbar } from "components/section-toolbar/section-toolbar"; import { HelpLink } from "components/utils"; -type Props = { - peripherals: PeripheralListData[]; -}; - -const IssPeripheral = (peripheralsList: Props) => { - const [peripherals] = useState(peripheralsList.peripherals); - +const IssPeripheral = () => { const title = (

  - {t("ISS - Hub Configuration")} + {t("Peripherals Configuration")}  

@@ -32,7 +25,7 @@ const IssPeripheral = (peripheralsList: Props) => { window.pageRenderers?.spaengine?.navigate?.(`/rhn/manager/admin/hub/peripherals/register`); }; - let pageContent = ; + let pageContent = ; return (