-
Notifications
You must be signed in to change notification settings - Fork 123
Support listing volumes #1959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jelly
wants to merge
2
commits into
cockpit-project:main
Choose a base branch
from
jelly:list-volumes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Support listing volumes #1959
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* SPDX-License-Identifier: LGPL-2.1-or-later */ | ||
| import React, { useState } from 'react'; | ||
|
|
||
| import { Button } from "@patternfly/react-core/dist/esm/components/Button"; | ||
| import { | ||
| Modal, ModalBody, ModalFooter, ModalHeader | ||
| } from '@patternfly/react-core/dist/esm/components/Modal'; | ||
| import { useDialogs } from "dialogs.jsx"; | ||
|
|
||
| import cockpit from 'cockpit'; | ||
|
|
||
| import * as client from './client.js'; | ||
|
|
||
| const _ = cockpit.gettext; | ||
|
|
||
| export const VolumeDeleteModal = ({ con, volume }) => { | ||
| const [force, setForce] = useState(false); | ||
| const [reason, setReason] = useState(null); | ||
| const Dialogs = useDialogs(); | ||
|
|
||
| const handleRemoveVolume = async () => { | ||
| setReason(null); | ||
| try { | ||
| await client.deleteVolume(con, volume.Name, force); | ||
| Dialogs.close(); | ||
| } catch (exc) { | ||
| setReason(exc.message); | ||
| setForce(true); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <Modal isOpen | ||
| position="top" variant="medium" | ||
| onClose={Dialogs.close} | ||
| > | ||
| <ModalHeader title={cockpit.format(_("Delete $0 volume?"), volume.Name)} | ||
| titleIconVariant="warning" | ||
| /> | ||
| <ModalBody> | ||
| {reason} | ||
| </ModalBody> | ||
| <ModalFooter> | ||
| <Button id="btn-volume-delete" variant="danger" | ||
| onClick={() => handleRemoveVolume()}> | ||
| {force ? _("Force delete volume") : _("Delete volume")} | ||
| </Button> | ||
| <Button variant="link" onClick={Dialogs.close}>{_("Cancel")}</Button> | ||
| </ModalFooter> | ||
| </Modal> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| /* SPDX-License-Identifier: LGPL-2.1-or-later */ | ||
| import React from 'react'; | ||
|
|
||
| import { Card, CardBody, CardHeader, CardTitle } from "@patternfly/react-core/dist/esm/components/Card"; | ||
| import { DropdownItem } from '@patternfly/react-core/dist/esm/components/Dropdown/index.js'; | ||
| import { ExpandableSection } from "@patternfly/react-core/dist/esm/components/ExpandableSection"; | ||
| import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex"; | ||
| import { cellWidth, SortByDirection } from '@patternfly/react-table'; | ||
| import { KebabDropdown } from "cockpit-components-dropdown.jsx"; | ||
| import { ListingTable } from "cockpit-components-table.jsx"; | ||
| import { useDialogs } from 'dialogs.jsx'; | ||
|
|
||
| import cockpit from 'cockpit'; | ||
|
|
||
| import { VolumeDeleteModal } from './VolumeDeleteModal.jsx'; | ||
| import * as utils from './util.js'; | ||
|
|
||
| const _ = cockpit.gettext; | ||
|
|
||
| const Volumes = ({ users, volumes, ownerFilter, textFilter, volumeContainerMap }) => { | ||
| const [isExpanded, setIsExpanded] = React.useState(false); | ||
|
|
||
| const getUsedByText = (volume) => { | ||
| if (volumeContainerMap === null) { | ||
| return { title: _("unused"), count: 0 }; | ||
| } | ||
| const containers = volumeContainerMap[volume.key]; | ||
| if (containers !== undefined) { | ||
| const title = cockpit.format(cockpit.ngettext("$0 container", "$0 containers", containers.length), containers.length); | ||
| return { title, count: containers.length }; | ||
| } else { | ||
| return { title: _("unused"), count: 0 }; | ||
| } | ||
| }; | ||
|
|
||
| const renderRow = volume => { | ||
| const { title: usedByText, count: usedByCount } = getUsedByText(volume); | ||
| const user = users.find(user => user.uid === volume.uid); | ||
| cockpit.assert(user, `User not found for volume uid ${volume.uid}`); | ||
|
|
||
| const columns = [ | ||
| { title: volume.Name, header: true, props: { modifier: "breakWord" } }, | ||
| { | ||
| title: (volume.uid === 0) ? _("system") : <div><span className="ct-grey-text">{_("user:")} </span>{user.name}</div>, | ||
| props: { modifier: "nowrap" }, | ||
| sortKey: volume.key, | ||
| }, | ||
| { title: volume.Mountpoint, header: true, props: { modifier: "breakWord" } }, | ||
| { title: volume.Driver, header: true, props: { modifier: "breakWord" } }, | ||
| { title: <utils.RelativeTime time={volume.CreatedAt} />, props: { className: "ignore-pixels" } }, | ||
| { title: <span className={usedByCount === 0 ? "ct-grey-text" : ""}>{usedByText}</span>, props: { className: "ignore-pixels", modifier: "nowrap" }, sortKey: usedByCount }, | ||
| { | ||
| title: <VolumeActions con={user.con} volume={volume} />, | ||
| props: { className: 'pf-v6-c-table__action content-action' } | ||
| }, | ||
| ]; | ||
|
|
||
| return { | ||
| columns, | ||
| props: { | ||
| key: volume.key, | ||
| "data-row-id": volume.key, | ||
| "data-row-name": `${volume.uid === null ? 'user' : volume.uid}-${volume.Name}` | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| const sortRows = (rows, direction, idx) => { | ||
| // Name / Owner / Mount Point / Driver / Created / Used by | ||
| const isNumeric = idx == 4 || idx == 5; | ||
| const sortedRows = rows.sort((a, b) => { | ||
| const aitem = a.columns[idx].sortKey ?? a.columns[idx].title; | ||
| const bitem = b.columns[idx].sortKey ?? b.columns[idx].title; | ||
| if (isNumeric) { | ||
| return bitem - aitem; | ||
| } else { | ||
| return aitem.localeCompare(bitem); | ||
| } | ||
| }); | ||
| return direction === SortByDirection.asc ? sortedRows : sortedRows.reverse(); | ||
| }; | ||
|
|
||
| const columnTitles = [ | ||
| { title: _("Name"), transforms: [cellWidth(20)], sortable: true }, | ||
| { title: _("Owner"), sortable: true }, | ||
| { title: _("Mount point"), sortable: true }, | ||
| { title: _("Driver"), sortable: true }, | ||
| { title: _("Created"), sortable: true }, | ||
| { title: _("Used by"), sortable: true }, | ||
| ]; | ||
|
|
||
| let emptyCaption = _("No volumes"); | ||
| if (volumes === null) { | ||
| emptyCaption = _("Loading..."); | ||
| } else if (textFilter.length > 0) { | ||
| emptyCaption = _("No volumes that match the current filter"); | ||
| } | ||
|
|
||
| const volumeKeys = Object.keys(volumes || {}); | ||
| const volumesTotal = volumeKeys.length; | ||
|
|
||
| let filtered = [...volumeKeys]; | ||
| if (volumes !== null) { | ||
| if (ownerFilter !== "all") { | ||
| filtered = filtered.filter(id => { | ||
| if (ownerFilter === "user") | ||
| return volumes[id].uid === null; | ||
| return volumes[id].uid === ownerFilter; | ||
| }); | ||
| } | ||
|
|
||
| if (textFilter.length > 0) { | ||
| filtered = filtered.filter(id => { | ||
| const name = volumes[id].Name; | ||
| return name.toLowerCase().includes(textFilter); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| const rows = filtered.map(name => renderRow(volumes[name])); | ||
|
|
||
| const cardBody = ( | ||
| <ListingTable variant='compact' | ||
| aria-label={_("Volumes")} | ||
| emptyCaption={emptyCaption} | ||
| columns={columnTitles} | ||
| rows={rows} | ||
| sortMethod={sortRows} | ||
| /> | ||
| ); | ||
|
|
||
| const volumesTitleStats = ( | ||
| <h5> | ||
| {cockpit.format(cockpit.ngettext("$0 volume total", "$0 volumes total", volumesTotal), volumesTotal)} | ||
| </h5> | ||
| ); | ||
|
|
||
| return ( | ||
| <Card id="containers-volumes" className="containers-volumes"> | ||
| <CardHeader> | ||
| <Flex flexWrap={{ default: 'nowrap' }} className="pf-v6-u-w-100"> | ||
| <FlexItem grow={{ default: 'grow' }}> | ||
| <Flex> | ||
| <CardTitle> | ||
| <h2 className="containers-volumes-title">{_("Volumes")}</h2> | ||
| </CardTitle> | ||
| <Flex className="ignore-pixels" style={{ rowGap: "var(--pf-v6-global--spacer--xs)" }}>{volumesTitleStats}</Flex> | ||
| </Flex> | ||
| </FlexItem> | ||
| </Flex> | ||
| </CardHeader> | ||
| <CardBody> | ||
| {volumes && Object.keys(volumes).length | ||
| ? <ExpandableSection toggleText={isExpanded ? _("Hide volumes") : _("Show volumes")} | ||
| onToggle={() => setIsExpanded(!isExpanded)} | ||
| isExpanded={isExpanded}> | ||
| {cardBody} | ||
| </ExpandableSection> | ||
| : cardBody} | ||
| </CardBody> | ||
| </Card> | ||
| ); | ||
| }; | ||
|
|
||
| const VolumeActions = ({ con, volume }) => { | ||
| const Dialogs = useDialogs(); | ||
|
|
||
| const removeVolume = () => { | ||
| Dialogs.show(<VolumeDeleteModal | ||
| con={con} | ||
| volume={volume} | ||
|
|
||
| />); | ||
| }; | ||
|
|
||
| const dropdownActions = [ | ||
| <DropdownItem key={volume.Name + "delete"} | ||
| component="button" | ||
| className="pf-m-danger btn-delete" | ||
| onClick={removeVolume}> | ||
| {_("Delete")} | ||
| </DropdownItem> | ||
| ]; | ||
|
|
||
| return <KebabDropdown position="right" dropdownItems={dropdownActions} />; | ||
| }; | ||
|
|
||
| export default Volumes; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Force is never used atm, state is never modified