Skip to content

Commit

Permalink
ui/ducks/volumes: Change the payload of createVolume to an array
Browse files Browse the repository at this point in the history
Refs: #2964
  • Loading branch information
ChengYanJin committed Dec 18, 2020
1 parent 214b5d1 commit 03e4f76
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 117 deletions.
132 changes: 66 additions & 66 deletions ui/src/ducks/app/volumes.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ export const updateStorageClassAction = (payload) => {
return { type: UPDATE_STORAGECLASS, payload };
};

export const createVolumeAction = (newVolume) => {
return { type: CREATE_VOLUMES, payload: { newVolume } };
export const createVolumeAction = (newVolumes) => {
return { type: CREATE_VOLUMES, payload: { newVolumes } };
};

export const refreshVolumesAction = () => {
Expand Down Expand Up @@ -205,97 +205,97 @@ export function* fetchStorageClass() {
* This function construct the body that is needed to create a volume.
* Then it calls K8s API to create it.
*
* @param {object} newVolume - fields of the createVolume form
* @param {array} newVolume - The array of fields of the createVolume form
* @param {string} nodeName
*
* More examples in volumes.test.js
* @example
*
* const action = {
* payload: {
* newVolume: {
* newVolume: [{
* name: 'volume1',
* node: 'bootrap',
* storageClass: 'metalk8s-default',
* type: 'sparseLoopDevice',
* size: '1Gi'
* },
* }],
* }
* };
*
* createVolumes(action)
*/
export function* createVolumes({ payload }) {
const { newVolume } = payload;

const body = {
apiVersion: 'storage.metalk8s.scality.com/v1alpha1',
kind: 'Volume',
metadata: {
name: newVolume.name,
labels: newVolume.labels,
},
spec: {
nodeName: newVolume.node,
storageClassName: newVolume.storageClass,
template: {
metadata: {
labels: newVolume.labels,
const { newVolumes } = payload;
for (var i = 0; i < newVolumes?.length; i++) {
const body = {
apiVersion: 'storage.metalk8s.scality.com/v1alpha1',
kind: 'Volume',
metadata: {
name: newVolumes[i].name,
labels: newVolumes[i].labels,
},
spec: {
nodeName: newVolumes[i].node,
storageClassName: newVolumes[i].storageClass,
template: {
metadata: {
labels: newVolumes[i].labels,
},
},
},
},
};

/**
* Size should be set for SPARSE_LOOP_DEVICE
* Path should be set for RAW_BLOCK_DEVICE
*/
let isNewVolumeValid =
newVolume &&
newVolume.name &&
newVolume.storageClass &&
((newVolume.type === SPARSE_LOOP_DEVICE && newVolume.size) ||
(newVolume.type === RAW_BLOCK_DEVICE && newVolume.path));

if (isNewVolumeValid) {
if (newVolume.type === SPARSE_LOOP_DEVICE) {
body.spec.sparseLoopDevice = { size: newVolume.size };
} else {
body.spec.rawBlockDevice = { devicePath: newVolume.path };
}

const result = yield call(VolumesApi.createVolume, body);
if (!result.error) {
yield call(
history.push,
`/volumes/${newVolume.name}/overview?node=${newVolume.node}`,
);
yield put(
addNotificationSuccessAction({
title: intl.translate('volume_creation'),
message: intl.translate('volume_creation_success', {
name: newVolume.name,
};
/**
* Size should be set for SPARSE_LOOP_DEVICE
* Path should be set for RAW_BLOCK_DEVICE
*/
let isNewVolumeValid =
newVolumes[i] &&
newVolumes[i].name &&
newVolumes[i].storageClass &&
((newVolumes[i].type === SPARSE_LOOP_DEVICE && newVolumes[i].size) ||
(newVolumes[i].type === RAW_BLOCK_DEVICE && newVolumes[i].path));

if (isNewVolumeValid) {
if (newVolumes[i].type === SPARSE_LOOP_DEVICE) {
body.spec.sparseLoopDevice = { size: newVolumes[i].size };
} else {
body.spec.rawBlockDevice = { devicePath: newVolumes[i].path };
}

const result = yield call(VolumesApi.createVolume, body);
if (!result.error) {
yield call(
history.push,
`/volumes/${newVolumes[i].name}/overview?node=${newVolumes[i].node}`,
);
yield put(
addNotificationSuccessAction({
title: intl.translate('volume_creation'),
message: intl.translate('volume_creation_success', {
name: newVolumes[i].name,
}),
}),
}),
);
);
} else {
yield put(
addNotificationErrorAction({
title: intl.translate('volume_creation'),
message: intl.translate('volume_creation_failed', {
name: newVolumes[i].name,
}),
}),
);
}
} else {
// We might want to change this behavior later
yield put(
addNotificationErrorAction({
title: intl.translate('volume_creation'),
message: intl.translate('volume_creation_failed', {
name: newVolume.name,
}),
title: 'Volume Form Error',
message: 'Volume not created, some fields are missing.',
}),
);
}
} else {
// We might want to change this behavior later
yield put(
addNotificationErrorAction({
title: 'Volume Form Error',
message: 'Volume not created, some fields are missing.',
}),
);
}
}

Expand Down
109 changes: 58 additions & 51 deletions ui/src/ducks/app/volumes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,37 +281,38 @@ it('does not update PV if there is an error', () => {
it('create volume with the type sparseloopdevice', () => {
const action = {
payload: {
newVolume: {
name: 'volume1',
node: 'bootstrap',
storageClass: 'metalk8s-default',
type: 'sparseLoopDevice',
size: '1Gi',
labels: {
name: 'carlito',
newVolumes: [
{
name: 'volume1',
node: 'bootstrap',
storageClass: 'metalk8s-default',
type: 'sparseLoopDevice',
size: '1Gi',
labels: {
name: 'carlito',
},
},
},
],
},
};

const { newVolume } = action.payload;
const { newVolumes } = action.payload;

const gen = createVolumes(action);

const body = {
apiVersion: 'storage.metalk8s.scality.com/v1alpha1',
kind: 'Volume',
metadata: {
name: newVolume.name,
name: newVolumes[0].name,
labels: {
name: 'carlito',
},
},
spec: {
nodeName: newVolume.node,
storageClassName: newVolume.storageClass,
sparseLoopDevice: { size: newVolume.size },
storageClassName: 'metalk8s-default',
nodeName: newVolumes[0].node,
storageClassName: newVolumes[0].storageClass,
sparseLoopDevice: { size: newVolumes[0].size },
template: {
metadata: {
labels: {
Expand Down Expand Up @@ -349,7 +350,7 @@ it('create volume with the type sparseloopdevice', () => {
expect(gen.next(result).value).toEqual(
call(
history.push,
`/volumes/${newVolume.name}/overview?node=${newVolume.node}`,
`/volumes/${newVolumes[0].name}/overview?node=${newVolumes[0].node}`,
),
);

Expand All @@ -363,34 +364,36 @@ it('create volume with the type sparseloopdevice', () => {
it('create a volume with the type rawBlockdevice', () => {
const action = {
payload: {
newVolume: {
name: 'volume1',
node: 'bootstrap',
storageClass: 'metalk8s-default',
type: 'rawBlockDevice',
path: '/dev/disk1',
labels: {
name: 'carlito',
newVolumes: [
{
name: 'volume1',
node: 'bootstrap',
storageClass: 'metalk8s-default',
type: 'rawBlockDevice',
path: '/dev/disk1',
labels: {
name: 'carlito',
},
},
},
],
},
};
const { newVolume } = action.payload;
const { newVolumes } = action.payload;
const gen = createVolumes(action);

const body = {
apiVersion: 'storage.metalk8s.scality.com/v1alpha1',
kind: 'Volume',
metadata: {
name: newVolume.name,
name: newVolumes[0].name,
labels: {
name: 'carlito',
},
},
spec: {
nodeName: newVolume.node,
storageClassName: newVolume.storageClass,
rawBlockDevice: { devicePath: newVolume.path },
nodeName: newVolumes[0].node,
storageClassName: newVolumes[0].storageClass,
rawBlockDevice: { devicePath: newVolumes[0].path },
template: {
metadata: {
labels: {
Expand Down Expand Up @@ -426,7 +429,7 @@ it('create a volume with the type rawBlockdevice', () => {
expect(gen.next(result).value).toEqual(
call(
history.push,
`/volumes/${newVolume.name}/overview?node=${newVolume.node}`,
`/volumes/${newVolumes[0].name}/overview?node=${newVolumes[0].node}`,
),
);
expect(gen.next().value.payload.action.type).toEqual(
Expand All @@ -439,12 +442,14 @@ it('create a volume with the type rawBlockdevice', () => {
it('display a notification when the params are wrong', () => {
const action = {
payload: {
newVolume: {
name: 'volume1',
storageClass: 'metalk8s-default',
type: 'rawBlockDevice',
path: '',
},
newVolumes: [
{
name: 'volume1',
storageClass: 'metalk8s-default',
type: 'rawBlockDevice',
path: '',
},
],
nodeName: 'bootstrap',
},
};
Expand All @@ -458,33 +463,35 @@ it('display a notification when the params are wrong', () => {
it('does not create a volume when there is an error', () => {
const action = {
payload: {
newVolume: {
name: 'volume1',
node: 'bootstrap',
storageClass: 'metalk8s-default',
type: 'rawBlockDevice',
path: '/dev/disk1',
labels: {
name: 'carlito',
newVolumes: [
{
name: 'volume1',
node: 'bootstrap',
storageClass: 'metalk8s-default',
type: 'rawBlockDevice',
path: '/dev/disk1',
labels: {
name: 'carlito',
},
},
},
],
},
};
const { newVolume } = action.payload;
const { newVolumes } = action.payload;
const gen = createVolumes(action);
const body = {
apiVersion: 'storage.metalk8s.scality.com/v1alpha1',
kind: 'Volume',
metadata: {
name: newVolume.name,
name: newVolumes[0].name,
labels: {
name: 'carlito',
},
},
spec: {
nodeName: newVolume.node,
storageClassName: newVolume.storageClass,
rawBlockDevice: { devicePath: newVolume.path },
nodeName: newVolumes[0].node,
storageClassName: newVolumes[0].storageClass,
rawBlockDevice: { devicePath: newVolumes[0].path },
template: {
metadata: {
labels: {
Expand Down

0 comments on commit 03e4f76

Please sign in to comment.