-
Notifications
You must be signed in to change notification settings - Fork 66
/
storagePoolDelete.jsx
252 lines (225 loc) · 9.77 KB
/
storagePoolDelete.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* This file is part of Cockpit.
*
* Copyright (C) 2019 Red Hat, Inc.
*
* Cockpit is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Cockpit is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
*/
import React from 'react';
import PropTypes from 'prop-types';
import { Button } from "@patternfly/react-core/dist/esm/components/Button";
import { Checkbox } from "@patternfly/react-core/dist/esm/components/Checkbox";
import { DropdownItem } from "@patternfly/react-core/dist/esm/components/Dropdown";
import { HelperText, HelperTextItem } from "@patternfly/react-core/dist/esm/components/HelperText";
import { List, ListItem } from "@patternfly/react-core/dist/esm/components/List";
import { Modal } from "@patternfly/react-core/dist/esm/components/Modal";
import { Stack } from "@patternfly/react-core/dist/esm/layouts/Stack";
import { Tooltip } from "@patternfly/react-core/dist/esm/components/Tooltip";
import { ExclamationTriangleIcon, InfoIcon } from '@patternfly/react-icons';
import { useDialogs, DialogsContext } from 'dialogs.jsx';
import { getStorageVolumesUsage, storagePoolId } from '../../helpers.js';
import { ModalError } from 'cockpit-components-inline-notification.jsx';
import { storageVolumeDelete } from '../../libvirtApi/storageVolume.js';
import { storagePoolDeactivate, storagePoolUndefine } from '../../libvirtApi/storagePool.js';
import cockpit from 'cockpit';
import './storagePoolDelete.scss';
const _ = cockpit.gettext;
/*
* Finds out if any volume is used as a disk independently
* with no reference to a pool (e.g. using direct volume path).
* If so, then pool can be deleted but only without its content.
*
* @param {object} pool
* @param {array} vms
* returns {boolean}
*/
function canDeleteOnlyWithoutVolumes(pool, vms) {
if (!canDelete(pool, vms))
return false;
const isVolumeUsed = getStorageVolumesUsage(vms, pool);
for (const property in isVolumeUsed) {
if (isVolumeUsed[property].length > 0)
return true;
}
return false;
}
/*
* Finds out if any disk uses pool name in it's definition.
* If so, then pool cannot be deleted with nor without its content.
*
* @param {object} pool
* @param {array} vms
* returns {boolean}
*/
function canDelete(pool, vms) {
for (let i = 0; i < vms.length; i++) {
const vm = vms[i];
const disks = Object.values(vm.disks);
if (disks.some(disk => disk.source.pool === pool.name))
return false;
}
return true;
}
function getPoolDeleteHelperText(vms, storagePool) {
const usage = getStorageVolumesUsage(vms, storagePool);
let vmsUsage = [];
for (const property in usage)
vmsUsage = vmsUsage.concat(usage[property]);
vmsUsage = [...new Set(vmsUsage)]; // remove duplicates
return (
<>
{_("Pool's volumes are used by VMs ")}
<b> {vmsUsage.join(', ') + "."} </b>
{_("Detach the disks using this pool from any VMs before attempting deletion.")}
</>
);
}
class StoragePoolDelete extends React.Component {
static contextType = DialogsContext;
constructor(props) {
super(props);
this.state = {
dialogError: undefined,
deleteVolumes: false,
};
this.delete = this.delete.bind(this);
this.onValueChanged = this.onValueChanged.bind(this);
this.dialogErrorSet = this.dialogErrorSet.bind(this);
}
onValueChanged(key, value) {
const stateDelta = { [key]: value };
this.setState(stateDelta);
}
dialogErrorSet(text, detail) {
this.setState({ dialogError: text, dialogErrorDetail: detail });
}
delete() {
const Dialogs = this.context;
const storagePool = this.props.storagePool;
const volumes = storagePool.volumes || [];
const storagePoolDeactivateAndUndefine = (storagePool) => {
if (storagePool.active) {
return storagePoolDeactivate({ connectionName: storagePool.connectionName, objPath: storagePool.id })
.then(() => storagePoolUndefine({ connectionName: storagePool.connectionName, objPath: storagePool.id }));
} else {
return storagePoolUndefine({ connectionName: storagePool.connectionName, objPath: storagePool.id });
}
};
if (this.state.deleteVolumes && storagePool.volumes.length > 0) {
Promise.all(volumes.map(volume => storageVolumeDelete({ connectionName: storagePool.connectionName, poolName: storagePool.name, volName: volume.name })))
.then(() => storagePoolDeactivateAndUndefine(storagePool))
.then(() => Dialogs.close(),
exc => this.dialogErrorSet(_("The storage pool could not be deleted"), exc.message));
} else {
storagePoolDeactivateAndUndefine(storagePool)
.then(() => Dialogs.close(),
exc => this.dialogErrorSet(_("The storage pool could not be deleted"), exc.message));
}
}
render() {
const Dialogs = this.context;
const { storagePool, vms } = this.props;
const volumes = storagePool.volumes || [];
const showWarning = () => {
if (canDeleteOnlyWithoutVolumes(storagePool, vms) && this.state.deleteVolumes) {
return (
<HelperText>
<HelperTextItem icon={<InfoIcon />}>
{getPoolDeleteHelperText(vms, storagePool)}
</HelperTextItem>
</HelperText>
);
}
};
const defaultBody = (
<Stack hasGutter>
{this.state.dialogError && <ModalError dialogError={this.state.dialogError} dialogErrorDetail={this.state.dialogErrorDetail} />}
{ storagePool.active
? (volumes.length > 0
? <Checkbox id='storage-pool-delete-volumes'
isChecked={this.state.deleteVolumes}
label={_("Also delete all volumes inside this pool:")}
body={
<List className="pool-volumes-delete-list">
{volumes.sort((a, b) => a.name.localeCompare(b.name))
.map(vol => <ListItem key={storagePool.name + vol.name}>{vol.name}</ListItem>)
}
</List>
}
onChange={(_event, checked) => this.onValueChanged('deleteVolumes', checked)} />
: _("No volumes exist in this storage pool."))
: _("Deleting an inactive storage pool will only undefine the pool. Its content will not be deleted.")
}
</Stack>
);
return (
<Modal id="storage-pool-delete-modal" position="top" variant="small" isOpen onClose={Dialogs.close}
title={<>
<ExclamationTriangleIcon color="orange" className="pf-v5-u-mr-sm" />
{ cockpit.format(_("Delete $0 storage pool?"), storagePool.name) }
</>}
footer={
<>
{showWarning()}
<Button variant='danger'
onClick={this.delete}
isDisabled={canDeleteOnlyWithoutVolumes(storagePool, vms) && this.state.deleteVolumes}>
{_("Delete")}
</Button>
<Button variant='link' onClick={Dialogs.close}>
{_("Cancel")}
</Button>
</>
}>
{defaultBody}
</Modal>
);
}
}
StoragePoolDelete.propTypes = {
storagePool: PropTypes.object.isRequired,
vms: PropTypes.array.isRequired,
};
export const StoragePoolDeleteAction = ({ storagePool, vms }) => {
const Dialogs = useDialogs();
const id = storagePoolId(storagePool.name, storagePool.connectionName);
let tooltipText;
if (!canDelete(storagePool, vms)) {
tooltipText = getPoolDeleteHelperText(vms, storagePool);
} else if (!storagePool.persistent) {
tooltipText = _("Non-persistent storage pool cannot be deleted. It ceases to exists when it's deactivated.");
}
if (!canDelete(storagePool, vms) || !storagePool.persistent) {
return (
<Tooltip id='delete-tooltip'
content={tooltipText}>
<span>
<DropdownItem id={`delete-${id}`}
className='pf-m-danger'
isAriaDisabled>
{_("Delete")}
</DropdownItem>
</span>
</Tooltip>
);
} else {
return (
<DropdownItem id={`delete-${id}`}
className='pf-m-danger'
onClick={() => Dialogs.show(<StoragePoolDelete storagePool={storagePool} vms={vms} />)}>
{_("Delete")}
</DropdownItem>
);
}
};