-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add protected check and refactor actions (#606)
- Loading branch information
1 parent
7534720
commit bae2f65
Showing
12 changed files
with
255 additions
and
114 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export enum RESOURCE_ACTIONS { | ||
EDIT = 'edit', | ||
DELETE = 'delete', | ||
} | ||
export const RESOURCE_ACTIONS = { | ||
CREATE: 'create', | ||
EDIT: 'edit', | ||
DELETE: 'delete', | ||
} as const; |
This file contains 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 @@ | ||
export const RESOURCE_LABEL_SELECTOR_PROTECTED = 'app.edp.epam.com/deletion-protection'; |
This file was deleted.
Oops, something went wrong.
This file contains 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,85 @@ | ||
import { KubeObjectInterface } from '@kinvolk/headlamp-plugin/lib/lib/k8s/cluster'; | ||
import { RESOURCE_ACTIONS } from '../../../constants/resourceActions'; | ||
import { RESOURCE_LABEL_SELECTOR_PROTECTED } from '../../../k8s/common/labels'; | ||
import { createResourceAction } from './index'; | ||
|
||
describe('createResourceAction', () => { | ||
const mockItem: KubeObjectInterface = { | ||
metadata: { | ||
labels: {}, | ||
}, | ||
} as KubeObjectInterface; | ||
|
||
it('should create an action with the given parameters', () => { | ||
const action = createResourceAction({ | ||
item: mockItem, | ||
type: RESOURCE_ACTIONS.EDIT, | ||
label: 'Edit', | ||
callback: jest.fn(), | ||
icon: 'edit-icon', | ||
isTextButton: true, | ||
}); | ||
|
||
expect(action.name).toBe(RESOURCE_ACTIONS.EDIT); | ||
expect(action.label).toBe('Edit'); | ||
expect(action.icon).toBe('edit-icon'); | ||
expect(action.disabled.status).toBe(false); | ||
expect(action.isTextButton).toBe(true); | ||
}); | ||
|
||
it('should disable the action if the resource is protected and the action is delete', () => { | ||
const protectedItem = { | ||
...mockItem, | ||
metadata: { | ||
labels: { | ||
[RESOURCE_LABEL_SELECTOR_PROTECTED]: 'true', | ||
}, | ||
}, | ||
} as unknown as KubeObjectInterface; | ||
|
||
const action = createResourceAction({ | ||
item: protectedItem, | ||
type: RESOURCE_ACTIONS.DELETE, | ||
label: 'Delete', | ||
callback: jest.fn(), | ||
}); | ||
|
||
expect(action.disabled.status).toBe(true); | ||
expect(action.disabled.reason).toBe('This resource is protected from deletion.'); | ||
}); | ||
|
||
it('should use the provided disabled status and reason if the resource is not protected', () => { | ||
const action = createResourceAction({ | ||
item: mockItem, | ||
type: RESOURCE_ACTIONS.DELETE, | ||
label: 'Delete', | ||
callback: jest.fn(), | ||
disabled: { | ||
status: true, | ||
reason: 'Custom reason', | ||
}, | ||
}); | ||
|
||
expect(action.disabled.status).toBe(true); | ||
expect(action.disabled.reason).toBe('Custom reason'); | ||
}); | ||
|
||
it('should call the callback function when the action is executed', () => { | ||
const mockCallback = jest.fn(); | ||
const action = createResourceAction({ | ||
item: mockItem, | ||
type: RESOURCE_ACTIONS.EDIT, | ||
label: 'Edit', | ||
callback: mockCallback, | ||
}); | ||
|
||
const mockEvent = { | ||
stopPropagation: jest.fn(), | ||
}; | ||
|
||
action.action(mockEvent as any); | ||
|
||
expect(mockEvent.stopPropagation).toHaveBeenCalled(); | ||
expect(mockCallback).toHaveBeenCalledWith(mockItem); | ||
}); | ||
}); |
This file contains 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,53 @@ | ||
import { KubeObjectInterface } from '@kinvolk/headlamp-plugin/lib/lib/k8s/cluster'; | ||
import { RESOURCE_ACTIONS } from '../../../constants/resourceActions'; | ||
import { RESOURCE_LABEL_SELECTOR_PROTECTED } from '../../../k8s/common/labels'; | ||
import { KubeObjectAction } from '../../../types/actions'; | ||
import { ValueOf } from '../../../types/global'; | ||
|
||
export const createResourceAction = <Item extends KubeObjectInterface>({ | ||
item, | ||
type, | ||
label, | ||
callback, | ||
disabled = { | ||
status: false, | ||
reason: 'You cannot perform this action.', | ||
}, | ||
icon, | ||
isTextButton, | ||
}: { | ||
item: Item; | ||
type: ValueOf<typeof RESOURCE_ACTIONS>; | ||
label: string; | ||
callback?: (item: Item) => void; | ||
disabled?: { | ||
status: boolean; | ||
reason?: string; | ||
}; | ||
icon?: string; | ||
isTextButton?: boolean; | ||
}): KubeObjectAction => { | ||
const isProtected = item?.metadata?.labels?.[RESOURCE_LABEL_SELECTOR_PROTECTED] === 'true'; | ||
const isDeleteAction = type === RESOURCE_ACTIONS.DELETE; | ||
|
||
return { | ||
name: type, | ||
label, | ||
icon, | ||
disabled: | ||
isProtected && isDeleteAction | ||
? { | ||
status: true, | ||
reason: 'This resource is protected from deletion.', | ||
} | ||
: { | ||
status: disabled.status, | ||
reason: disabled.status && disabled.reason, | ||
}, | ||
action: (e) => { | ||
e.stopPropagation(); | ||
callback(item); | ||
}, | ||
isTextButton, | ||
}; | ||
}; |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.