diff --git a/src/fireedge/src/client/components/Forms/MarketplaceApp/ExportForm/Steps/BasicConfiguration/schema.js b/src/fireedge/src/client/components/Forms/MarketplaceApp/ExportForm/Steps/BasicConfiguration/schema.js index cc73e873f8..6bc966a1fe 100644 --- a/src/fireedge/src/client/components/Forms/MarketplaceApp/ExportForm/Steps/BasicConfiguration/schema.js +++ b/src/fireedge/src/client/components/Forms/MarketplaceApp/ExportForm/Steps/BasicConfiguration/schema.js @@ -24,12 +24,12 @@ const NAME_FIELD = { label: T.Name, tooltip: T.ExportAppNameConcept, type: INPUT_TYPES.TEXT, - validation: lazy((_, { context }) => { - return string() + validation: lazy((_, { context }) => + string() .trim() .required() .default(() => context.app.NAME) - }), + ), } /** @type {Field} Template name field */ @@ -38,20 +38,20 @@ const TEMPLATE_NAME_FIELD = { label: T.VMTemplate, tooltip: T.ExportTemplateNameConcept, type: INPUT_TYPES.TEXT, - validation: lazy((_, { context }) => { - return string() + validation: lazy((_, { context }) => + string() .trim() .required() .default(() => context.app.NAME) - }), + ), } /** @type {Field} Associate field */ const ASSOCIATED_FIELD = { name: 'associated', - label: T.DontAssociateApp, + label: T.ExportAssociateApp, type: INPUT_TYPES.SWITCH, - validation: boolean().yesOrNo(), + validation: boolean().default(() => true), grid: { md: 12 }, } diff --git a/src/fireedge/src/client/components/Forms/MarketplaceApp/ExportForm/Steps/DockerHubTagsTable/index.js b/src/fireedge/src/client/components/Forms/MarketplaceApp/ExportForm/Steps/DockerHubTagsTable/index.js new file mode 100644 index 0000000000..2587ccfb39 --- /dev/null +++ b/src/fireedge/src/client/components/Forms/MarketplaceApp/ExportForm/Steps/DockerHubTagsTable/index.js @@ -0,0 +1,67 @@ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); you may * + * not use this file except in compliance with the License. You may obtain * + * a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + * ------------------------------------------------------------------------- */ +import PropTypes from 'prop-types' +import { useFormContext } from 'react-hook-form' + +import { DockerHubTagsTable } from 'client/components/Tables' +import { SCHEMA } from 'client/components/Forms/MarketplaceApp/ExportForm/Steps/DockerHubTagsTable/schema' +import { Step } from 'client/utils' +import { T } from 'client/constants' + +export const STEP_ID = 'tag' + +const Content = ({ data, app }) => { + const { name } = data?.[0] ?? {} + const { setValue } = useFormContext() + + const handleSelectedRows = (rows) => { + const { original = {} } = rows?.[0] ?? {} + + setValue(STEP_ID, original.id !== undefined ? [original] : []) + } + + return ( + + ) +} + +/** + * Step to select the DockerHub tag. + * + * @param {object} app - Marketplace App resource + * @returns {Step} DockerHub tag step + */ +const DockerHubTagStep = (app) => ({ + id: STEP_ID, + label: T.SelectDockerHubTag, + resolver: SCHEMA, + content: (props) => Content({ ...props, app }), +}) + +Content.propTypes = { + data: PropTypes.any, + setFormData: PropTypes.func, + app: PropTypes.object, +} + +export default DockerHubTagStep diff --git a/src/fireedge/src/client/components/Forms/MarketplaceApp/ExportForm/Steps/DockerHubTagsTable/schema.js b/src/fireedge/src/client/components/Forms/MarketplaceApp/ExportForm/Steps/DockerHubTagsTable/schema.js new file mode 100644 index 0000000000..321b9e2c9e --- /dev/null +++ b/src/fireedge/src/client/components/Forms/MarketplaceApp/ExportForm/Steps/DockerHubTagsTable/schema.js @@ -0,0 +1,19 @@ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); you may * + * not use this file except in compliance with the License. You may obtain * + * a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + * ------------------------------------------------------------------------- */ +import { array, object, ArraySchema } from 'yup' + +/** @type {ArraySchema} Datastore table schema */ +export const SCHEMA = array(object()).max(1).ensure() diff --git a/src/fireedge/src/client/components/Forms/MarketplaceApp/ExportForm/Steps/index.js b/src/fireedge/src/client/components/Forms/MarketplaceApp/ExportForm/Steps/index.js index a6c477d985..7db4302f17 100644 --- a/src/fireedge/src/client/components/Forms/MarketplaceApp/ExportForm/Steps/index.js +++ b/src/fireedge/src/client/components/Forms/MarketplaceApp/ExportForm/Steps/index.js @@ -19,19 +19,38 @@ import BasicConfiguration, { import DatastoresTable, { STEP_ID as DATASTORE_ID, } from 'client/components/Forms/MarketplaceApp/ExportForm/Steps/DatastoresTable' +import DockerHubTagsTable, { + STEP_ID as TAG_ID, +} from 'client/components/Forms/MarketplaceApp/ExportForm/Steps/DockerHubTagsTable' import { createSteps } from 'client/utils' -const Steps = createSteps([BasicConfiguration, DatastoresTable], { - transformInitialValue: (app, schema) => schema.cast({}, { context: { app } }), - transformBeforeSubmit: (formData) => { - const { [BASIC_ID]: configuration, [DATASTORE_ID]: [datastore] = [] } = - formData +const Steps = createSteps( + (app) => { + const isDockerImage = String(app?.MARKETPLACE).toLowerCase() === 'dockerhub' - return { - datastore: datastore?.ID, - ...configuration, - } + return [ + BasicConfiguration, + DatastoresTable, + isDockerImage && DockerHubTagsTable, + ].filter(Boolean) }, -}) + { + transformInitialValue: (app, schema) => + schema.cast({}, { context: { app } }), + transformBeforeSubmit: (formData) => { + const { + [BASIC_ID]: configuration, + [DATASTORE_ID]: [datastore] = [], + [TAG_ID]: [tag] = [], + } = formData + + return { + datastore: datastore?.ID, + tag: tag?.name, + ...configuration, + } + }, + } +) export default Steps diff --git a/src/fireedge/src/client/components/Tables/DockerHubTags/index.js b/src/fireedge/src/client/components/Tables/DockerHubTags/index.js new file mode 100644 index 0000000000..c55c08c4f9 --- /dev/null +++ b/src/fireedge/src/client/components/Tables/DockerHubTags/index.js @@ -0,0 +1,92 @@ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); you may * + * not use this file except in compliance with the License. You may obtain * + * a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + * ------------------------------------------------------------------------- */ +/* eslint-disable jsdoc/require-jsdoc */ +import { useMemo, useEffect, useState, useCallback } from 'react' +import PropTypes from 'prop-types' + +import { useFetch } from 'client/hooks' +import { useMarketplaceAppApi } from 'client/features/One' + +import EnhancedTable from 'client/components/Tables/Enhanced' +import DockerHubTagsRow from 'client/components/Tables/DockerHubTags/row' + +const getNextPageFromUrl = (url) => { + try { + const { searchParams } = new URL(url) + + return searchParams.get('page') + } catch { + return undefined + } +} + +const DockerHubTagsTable = ({ app, ...props } = {}) => { + const { getDockerHubTags } = useMarketplaceAppApi() + const [tags, setTags] = useState(() => []) + + const { + data: { next, results = [] } = {}, + fetchRequest, + loading, + status, + STATUS: { INIT, FETCHED }, + } = useFetch(getDockerHubTags) + + useEffect(() => { + const appId = app?.ID + const requests = app?.ID && { + [INIT]: () => fetchRequest({ id: appId }), + [FETCHED]: () => { + if (!next) return + + const page = getNextPageFromUrl(next) + fetchRequest({ id: appId, page }) + }, + } + + requests[status]?.() + }, [app?.ID, status]) + + useEffect(() => { + status === FETCHED && setTags((prev) => prev.concat(results)) + }, [status]) + + const memoData = useMemo(() => tags, [tags?.length]) + + const memoColumns = useMemo(() => [{ accessor: 'name' }], []) + + return ( + String(row.name), [])} + RowComponent={DockerHubTagsRow} + {...props} + /> + ) +} + +DockerHubTagsTable.displayName = 'DockerHubTagsTable' + +DockerHubTagsTable.propTypes = { + ...EnhancedTable.propTypes, + app: PropTypes.shape({ + ID: PropTypes.string, + }), +} + +export default DockerHubTagsTable diff --git a/src/fireedge/src/client/components/Tables/DockerHubTags/row.js b/src/fireedge/src/client/components/Tables/DockerHubTags/row.js new file mode 100644 index 0000000000..d9e96c9881 --- /dev/null +++ b/src/fireedge/src/client/components/Tables/DockerHubTags/row.js @@ -0,0 +1,148 @@ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2021, OpenNebula Project, OpenNebula Systems * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); you may * + * not use this file except in compliance with the License. You may obtain * + * a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + * ------------------------------------------------------------------------- */ +/* eslint-disable jsdoc/require-jsdoc */ +import { memo, useState } from 'react' +import PropTypes from 'prop-types' +import { Stack, Typography, Box } from '@mui/material' + +import { rowStyles } from 'client/components/Tables/styles' + +import { + isoDateToMilliseconds, + timeFromMilliseconds, +} from 'client/models/Helper' +import { prettyBytes } from 'client/utils' + +const LIMIT_IMAGES_TO_SHOW = 3 +const CHARS_DIGEST = 12 + +const TagImage = memo(({ image }) => { + const { digest = 'Unknown', os, architecture, size } = image + + const fullOs = [os, architecture].filter(Boolean).join('/') + const prettySize = prettyBytes(size, 'B', 2) + + const trimmedDigest = String(digest) + .replace('sha256:', '') + .substring(0, CHARS_DIGEST) + + return ( + + + {trimmedDigest} + + + {fullOs} + + + {prettySize} + + + ) +}) + +const Row = ({ original, value: _, ...props }) => { + const [showMore, setShowMore] = useState(() => false) + const classes = rowStyles() + const { name, tag_last_pushed: lastPushed, images } = original + + const time = timeFromMilliseconds(isoDateToMilliseconds(lastPushed)) + const timeAgo = lastPushed ? `Last pushed ${time.toRelative()}` : '' + + const handleShowMore = (evt) => { + evt.stopPropagation() + setShowMore(true) + } + + return ( +
+
+
+ {name} +
+
+ + {timeAgo} + +
+ {images?.length > 0 && ( + + {showMore ? ( + images?.map((image) => ( + + )) + ) : ( + <> + {images.slice(0, LIMIT_IMAGES_TO_SHOW)?.map((image) => ( + + ))} + {images.length - LIMIT_IMAGES_TO_SHOW > 0 && ( + + {`+${images.length - LIMIT_IMAGES_TO_SHOW} more...`} + + )} + + )} + + )} +
+
+ ) +} + +TagImage.displayName = 'TagImage' + +TagImage.propTypes = { + image: PropTypes.shape({ + digest: PropTypes.string, + os: PropTypes.string, + architecture: PropTypes.string, + size: PropTypes.number, + }), +} + +Row.propTypes = { + original: PropTypes.object, + value: PropTypes.object, + isSelected: PropTypes.bool, + handleClick: PropTypes.func, +} + +export default Row diff --git a/src/fireedge/src/client/components/Tables/Enhanced/Utils/GlobalFilter.js b/src/fireedge/src/client/components/Tables/Enhanced/Utils/GlobalFilter.js index 37017405dd..470f0c3c5d 100644 --- a/src/fireedge/src/client/components/Tables/Enhanced/Utils/GlobalFilter.js +++ b/src/fireedge/src/client/components/Tables/Enhanced/Utils/GlobalFilter.js @@ -90,6 +90,7 @@ const GlobalFilter = ({ useTableProps, className }) => { { setValue(event.target.value) handleChange(event.target.value) diff --git a/src/fireedge/src/client/components/Tables/Enhanced/index.js b/src/fireedge/src/client/components/Tables/Enhanced/index.js index c449679396..11c38bfea0 100644 --- a/src/fireedge/src/client/components/Tables/Enhanced/index.js +++ b/src/fireedge/src/client/components/Tables/Enhanced/index.js @@ -45,6 +45,7 @@ const EnhancedTable = ({ canFetchMore, columns, globalActions, + globalFilter, data, fetchMore, getRowId, @@ -86,6 +87,7 @@ const EnhancedTable = ({ columns, data, defaultColumn, + globalFilter, sortTypes, getRowId, // When table has update, disable all of the auto resetting @@ -158,14 +160,12 @@ const EnhancedTable = ({ {/* PAGINATION */}
- {page?.length > 0 && ( - - )} +
@@ -234,6 +234,7 @@ export const EnhancedTableProps = { globalActions: PropTypes.arrayOf(ActionPropTypes), columns: PropTypes.array, data: PropTypes.array, + globalFilter: PropTypes.func, fetchMore: PropTypes.func, getRowId: PropTypes.func, initialState: PropTypes.object, diff --git a/src/fireedge/src/client/components/Tables/Vms/index.js b/src/fireedge/src/client/components/Tables/Vms/index.js index 3dcd4c2942..6f503b6110 100644 --- a/src/fireedge/src/client/components/Tables/Vms/index.js +++ b/src/fireedge/src/client/components/Tables/Vms/index.js @@ -48,17 +48,17 @@ const VmsTable = (props) => { const { status, data, fetchRequest, loading, reloading, error, STATUS } = useFetch(getVms) - const { INIT, PENDING } = STATUS + const { INIT, PENDING, FETCHED } = STATUS useEffect(() => { const requests = { - INIT: () => + [INIT]: () => fetchRequest({ start: INITIAL_ELEMENT, end: -INTERVAL_ON_FIRST_RENDER, state: -1, // Any state, except DONE }), - FETCHED: () => { + [FETCHED]: () => { const canFetchMore = !error && data?.vms?.length === INTERVAL_ON_FIRST_RENDER diff --git a/src/fireedge/src/client/components/Tables/index.js b/src/fireedge/src/client/components/Tables/index.js index e262b0c936..9f8f637faf 100644 --- a/src/fireedge/src/client/components/Tables/index.js +++ b/src/fireedge/src/client/components/Tables/index.js @@ -15,6 +15,7 @@ * ------------------------------------------------------------------------- */ import ClustersTable from 'client/components/Tables/Clusters' import DatastoresTable from 'client/components/Tables/Datastores' +import DockerHubTagsTable from 'client/components/Tables/DockerHubTags' import EnhancedTable, { EnhancedTableProps, } from 'client/components/Tables/Enhanced' @@ -40,6 +41,7 @@ export { VirtualizedTable, ClustersTable, DatastoresTable, + DockerHubTagsTable, GroupsTable, HostsTable, ImagesTable, diff --git a/src/fireedge/src/client/constants/translates.js b/src/fireedge/src/client/constants/translates.js index 9c3f647806..435bb752b8 100644 --- a/src/fireedge/src/client/constants/translates.js +++ b/src/fireedge/src/client/constants/translates.js @@ -100,13 +100,14 @@ module.exports = { SaveAsTemplate: 'Save as Template', Search: 'Search', Select: 'Select', + SelectDatastore: 'Select a Datastore to store the resource', + SelectDockerHubTag: 'Select DockerHub image tag (default latest)', SelectGroup: 'Select a group', SelectHost: 'Select a host', + SelectMarketplace: 'Select Marketplace', SelectNetwork: 'Select a network', SelectRequest: 'Select request', SelectVmTemplate: 'Select a VM Template', - SelectDatastore: 'Select a Datastore to store the resource', - SelectMarketplace: 'Select Marketplace', Share: 'Share', Show: 'Show', ShowAll: 'Show all', @@ -598,7 +599,7 @@ module.exports = { 'Name that the resource will get for description purposes', ExportTemplateNameConcept: 'The following template will be created in OpenNebula and the previous images will be referenced in the disks', - DontAssociateApp: 'Do not import/export associated VM templates/images', + ExportAssociateApp: 'Export associated VM templates/images', ImportAssociateApp: 'Import associated VM templates/images', /* User inputs */ diff --git a/src/fireedge/src/client/features/One/marketplaceApp/actions.js b/src/fireedge/src/client/features/One/marketplaceApp/actions.js index 4ce7be9ff6..e8ca5adc7e 100644 --- a/src/fireedge/src/client/features/One/marketplaceApp/actions.js +++ b/src/fireedge/src/client/features/One/marketplaceApp/actions.js @@ -31,6 +31,11 @@ export const getMarketplaceApps = createAction( (response) => ({ [RESOURCES.app]: response }) ) +export const getDockerHubTags = createAction( + `${APP}/dockerhub-tags`, + marketplaceAppService.getDockerHubTags +) + export const exportApp = createAction( `${APP}/export`, marketplaceAppService.export diff --git a/src/fireedge/src/client/features/One/marketplaceApp/hooks.js b/src/fireedge/src/client/features/One/marketplaceApp/hooks.js index a73f57eac8..f915a43742 100644 --- a/src/fireedge/src/client/features/One/marketplaceApp/hooks.js +++ b/src/fireedge/src/client/features/One/marketplaceApp/hooks.js @@ -36,6 +36,8 @@ export const useMarketplaceAppApi = () => { getMarketplaceApp: (id) => unwrapDispatch(actions.getMarketplaceApp({ id })), getMarketplaceApps: () => unwrapDispatch(actions.getMarketplaceApps()), + getDockerHubTags: (options) => + unwrapDispatch(actions.getDockerHubTags(options)), exportApp: (id, data) => unwrapDispatch(actions.exportApp({ id, ...data })), create: (id, template) => unwrapDispatch(actions.create({ id, template })), importVm: (id, data) => diff --git a/src/fireedge/src/client/features/One/marketplaceApp/services.js b/src/fireedge/src/client/features/One/marketplaceApp/services.js index c99519f479..e5eacf788e 100644 --- a/src/fireedge/src/client/features/One/marketplaceApp/services.js +++ b/src/fireedge/src/client/features/One/marketplaceApp/services.js @@ -61,6 +61,26 @@ export const marketplaceAppService = { return [res?.data?.MARKETPLACEAPP_POOL?.MARKETPLACEAPP ?? []].flat() }, + /** + * Retrieves DockerHub tags information for marketplace app. + * + * @param {object} params - Request parameters + * @param {string|number} params.id - App id + * @param {string|number} [params.page] - Number of page + * @returns {object[]} List of DockerHub tags + * @throws Fails when response isn't code 200 + */ + getDockerHubTags: async ({ id, page }) => { + const res = await RestClient.request({ + url: `/api/marketapp/dockertags/${id}`, + params: { page }, + }) + + if (!res?.id || res?.id !== httpCodes.ok.id) throw res?.data + + return res?.data + }, + /** * Exports the marketplace app to the OpenNebula cloud. * @@ -71,7 +91,7 @@ export const marketplaceAppService = { * @param {string|number} params.file - File datastore id or name * @param {string} params.tag - DockerHub image tag (default latest) * @param {string|number} params.template - Associate with VM template - * @param {boolean} params.associated - If `true`, don't export associated VM templates/images + * @param {boolean} params.associated - If `false`, Do not export associated VM templates/images * @param {string} params.vmname - The name for the new VM Template, if the App contains one * @returns {number} Template and image ids * @throws Fails when response isn't code 200