Skip to content

Commit

Permalink
Implemented Redfish message ID checks using Regex (#170)
Browse files Browse the repository at this point in the history
- Replaced the hardcoded checks for message IDs with Regular Expressions. Implemented the possible message IDs using regex in a separate module(src/utilities/GlobalConstants.js)

- JIRA: https://jsw.ibm.com/browse/PFEBMC-1934

- Defect: https://jazz07.rchland.ibm.com:13443/jazz/web/projects/CSSD#action=com.ibm.team.workitem.viewWorkItem&id=588634

Signed-off-by: Nabil Ananthamangalath <nabilmanjeri@gmail.com>
  • Loading branch information
nabilpandiyalayil authored and rfrandse committed Jan 25, 2024
1 parent e5ad774 commit 9f3ae79
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 43 deletions.
39 changes: 21 additions & 18 deletions src/store/modules/Logs/DumpsStore.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import api, { getResponseCount } from '@/store/api';
import i18n from '@/i18n';
import { REGEX_MAPPINGS } from '@/utilities/GlobalConstants';

const DumpsStore = {
namespaced: true,
Expand Down Expand Up @@ -66,12 +67,11 @@ const DumpsStore = {
const messageId =
error.response.data.error?.['@Message.ExtendedInfo'][0].MessageId;

const message =
messageId === 'Base.1.8.1.ResourceInStandby'
? i18n.t('pageDumps.toast.errorStartDumpAnotherInProgress', {
dump: dumpType,
})
: i18n.t('pageDumps.toast.errorStartBmcDump');
const message = REGEX_MAPPINGS.resourceInStandby.test(messageId)
? i18n.t('pageDumps.toast.errorStartDumpAnotherInProgress', {
dump: dumpType,
})
: i18n.t('pageDumps.toast.errorStartBmcDump');

throw new Error(message);
});
Expand Down Expand Up @@ -99,9 +99,11 @@ const DumpsStore = {
.then(({ data }) => {
const messageId = data.Messages.filter(
(message) =>
message.MessageId === 'Base.1.8.1.ActionParameterUnknown' ||
message.MessageId === 'Base.1.8.1.ResourceAtUriUnauthorized' ||
message.MessageId === 'Base.1.8.1.InsufficientPrivilege'
REGEX_MAPPINGS.actionParameterUnknown.test(message.MessageId) ||
REGEX_MAPPINGS.resourceAtUriUnauthorized.test(
message.MessageId
) ||
REGEX_MAPPINGS.insufficientPrivilege.test(message.MessageId)
)[0]?.MessageId;

if (messageId) {
Expand All @@ -111,21 +113,22 @@ const DumpsStore = {
.catch((error) => {
const errorMsg = error;
if (
error.response?.data?.error?.code ===
'Base.1.13.0.ResourceInStandby'
REGEX_MAPPINGS.resourceInStandby.test(
error.response?.data?.error?.code
)
) {
throw new Error(i18n.t('pageDumps.toast.errorPhypInStandby'));
}
switch (errorMsg) {
case 'Base.1.8.1.ActionParameterUnknown':
switch (true) {
case REGEX_MAPPINGS.actionParameterUnknown.test(errorMsg):
throw new Error(
i18n.t('pageDumps.toast.errorStartResourceDumpInvalidSelector')
);
case 'Base.1.8.1.ResourceAtUriUnauthorized':
case REGEX_MAPPINGS.resourceAtUriUnauthorized.test(errorMsg):
throw new Error(
i18n.t('pageDumps.toast.errorStartResourceDumpInvalidPassword')
);
case 'Base.1.8.1.InsufficientPrivilege':
case REGEX_MAPPINGS.insufficientPrivilege.test(errorMsg):
throw new Error(i18n.t('global.toast.unAuthDescription'));
default:
throw new Error(i18n.t('pageDumps.toast.errorStartResourceDump'));
Expand All @@ -146,14 +149,14 @@ const DumpsStore = {
const errorMsg =
error.response.data.error?.['@Message.ExtendedInfo'][0].MessageId;

switch (errorMsg) {
case 'Base.1.8.1.ResourceInUse':
switch (true) {
case REGEX_MAPPINGS.resourceInUse.test(errorMsg):
throw new Error(
i18n.t('pageDumps.toast.errorStartDumpAnotherInProgress', {
dump: dumpType,
})
);
case 'Base.1.8.1.ResourceInStandby':
case REGEX_MAPPINGS.resourceInStandby.test(errorMsg):
throw new Error(
i18n.t('pageDumps.toast.errorStartDumpResourceInStandby', {
dump: dumpType,
Expand Down
49 changes: 26 additions & 23 deletions src/store/modules/SecurityAndAccess/UserManagementStore.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import api, { getResponseCount } from '@/store/api';
import i18n from '@/i18n';
import { REGEX_MAPPINGS } from '@/utilities/GlobalConstants';

const UserManagementStore = {
namespaced: true,
Expand Down Expand Up @@ -128,8 +129,8 @@ const UserManagementStore = {

const errorMsg = error.response?.data?.error?.code;

switch (errorMsg) {
case 'Base.1.8.1.PropertyValueFormatError':
switch (true) {
case REGEX_MAPPINGS.propertyValueFormatError.test(errorMsg):
throw new Error(
i18n.t(
'pageUserManagement.toast.errorCreateUserPasswordNotAccepted',
Expand All @@ -138,7 +139,7 @@ const UserManagementStore = {
}
)
);
case 'Base.1.8.1.CreateLimitReachedForResource':
case REGEX_MAPPINGS.createLimitReachedForResource.test(errorMsg):
throw new Error(
i18n.t('pageUserManagement.toast.errorCreateUserMaxUsers', {
username,
Expand Down Expand Up @@ -191,17 +192,18 @@ const UserManagementStore = {
)
.catch((error) => {
const messageId = error?.response?.data?.error?.code;
const message =
messageId === 'Base.1.8.1.PropertyValueFormatError'
? i18n.t(
'pageUserManagement.toast.errorUpdateUserPasswordNotAccepted',
{
username: originalUsername,
}
)
: i18n.t('pageUserManagement.toast.errorUpdateUser', {
const message = REGEX_MAPPINGS.propertyValueFormatError.test(
messageId
)
? i18n.t(
'pageUserManagement.toast.errorUpdateUserPasswordNotAccepted',
{
username: originalUsername,
});
}
)
: i18n.t('pageUserManagement.toast.errorUpdateUser', {
username: originalUsername,
});
throw new Error(message);
});
},
Expand Down Expand Up @@ -229,17 +231,18 @@ const UserManagementStore = {
const messageId =
error.response.data['Password@Message.ExtendedInfo'][0].MessageId;

const message =
messageId === 'Base.1.8.1.PropertyValueFormatError'
? i18n.t(
'pageUserManagement.toast.errorUpdateUserPasswordNotAccepted',
{
username: originalUsername,
}
)
: i18n.t('pageUserManagement.toast.errorUpdateUser', {
const message = REGEX_MAPPINGS.propertyValueFormatError.test(
messageId
)
? i18n.t(
'pageUserManagement.toast.errorUpdateUserPasswordNotAccepted',
{
username: originalUsername,
});
}
)
: i18n.t('pageUserManagement.toast.errorUpdateUser', {
username: originalUsername,
});
throw new Error(message);
});
},
Expand Down
5 changes: 3 additions & 2 deletions src/store/modules/Settings/HardwareDeconfigurationStore.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import api from '@/store/api';
import i18n from '@/i18n';
import { REGEX_MAPPINGS } from '@/utilities/GlobalConstants';

const HardwareDeconfigurationStore = {
namespaced: true,
Expand Down Expand Up @@ -204,7 +205,7 @@ const HardwareDeconfigurationStore = {
const messageId =
error.response.data.error['@Message.ExtendedInfo'][0].MessageId;

if (messageId === 'Base.1.8.1.ResourceCannotBeDeleted') {
if (REGEX_MAPPINGS.resourceCannotBeDeleted.test(messageId)) {
throw new Error(
i18n.t('pageDeconfigurationHardware.toast.deleteReqFailed')
);
Expand All @@ -229,7 +230,7 @@ const HardwareDeconfigurationStore = {
const messageId =
error.response.data.error['@Message.ExtendedInfo'][0].MessageId;

if (messageId === 'Base.1.8.1.ResourceCannotBeDeleted') {
if (REGEX_MAPPINGS.resourceCannotBeDeleted.test(messageId)) {
throw new Error(
i18n.t('pageDeconfigurationHardware.toast.deleteReqFailed')
);
Expand Down
10 changes: 10 additions & 0 deletions src/utilities/GlobalConstants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const REGEX_MAPPINGS = Object.freeze({
resourceInStandby: /Base\.(\d+\.){3}ResourceInStandby/,
actionParameterUnknown: /Base\.(\d+\.){3}ActionParameterUnknown/,
resourceAtUriUnauthorized: /Base\.(\d+\.){3}ResourceAtUriUnauthorized/,
insufficientPrivilege: /Base\.(\d+\.){3}InsufficientPrivilege/,
resourceInUse: /Base\.(\d+\.){3}ResourceInUse/,
propertyValueFormatError: /Base\.(\d+\.){3}PropertyValueFormatError/,
createLimitReachedForResource: /Base\.(\d+\.){3}CreateLimitReachedForResource/,
resourceCannotBeDeleted: /Base\.(\d+\.){3}ResourceCannotBeDeleted/,
});

0 comments on commit 9f3ae79

Please sign in to comment.