-
Notifications
You must be signed in to change notification settings - Fork 107
Dashboard API Key Management #3423
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
Merged
webbnh
merged 5 commits into
distributed-system-analysis:main
from
MVarshini:PBENCH-1131
May 18, 2023
Merged
Changes from all commits
Commits
Show all changes
5 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,97 @@ | ||
| import * as TYPES from "actions/types"; | ||
|
|
||
| import { DANGER, ERROR_MSG, SUCCESS } from "assets/constants/toastConstants"; | ||
|
|
||
| import API from "../utils/axiosInstance"; | ||
| import { showToast } from "./toastActions"; | ||
| import { uriTemplate } from "utils/helper"; | ||
|
|
||
| export const getAPIkeysList = async (dispatch, getState) => { | ||
| try { | ||
| dispatch({ type: TYPES.LOADING }); | ||
|
|
||
| const endpoints = getState().apiEndpoint.endpoints; | ||
| const response = await API.get(uriTemplate(endpoints, "key", { key: "" })); | ||
|
|
||
| if (response.status === 200) { | ||
| dispatch({ | ||
| type: TYPES.SET_API_KEY_LIST, | ||
| payload: response.data, | ||
| }); | ||
| } else { | ||
| dispatch(showToast(DANGER, ERROR_MSG)); | ||
| } | ||
| } catch (error) { | ||
| dispatch(showToast(DANGER, error)); | ||
| } | ||
| dispatch({ type: TYPES.COMPLETED }); | ||
| }; | ||
|
|
||
| export const deleteAPIKey = (id) => async (dispatch, getState) => { | ||
| try { | ||
| dispatch({ type: TYPES.LOADING }); | ||
| const endpoints = getState().apiEndpoint.endpoints; | ||
| const response = await API.delete( | ||
| uriTemplate(endpoints, "key", { key: id }) | ||
| ); | ||
|
|
||
| if (response.status === 200) { | ||
| dispatch({ | ||
| type: TYPES.SET_API_KEY_LIST, | ||
| payload: getState().keyManagement.keyList.filter( | ||
| (item) => item.id !== id | ||
| ), | ||
| }); | ||
|
|
||
| const message = response.data ?? "Deleted"; | ||
| const toastMsg = message?.charAt(0).toUpperCase() + message?.slice(1); | ||
|
|
||
| dispatch(showToast(SUCCESS, toastMsg)); | ||
| } else { | ||
| dispatch(showToast(DANGER, ERROR_MSG)); | ||
| } | ||
| } catch (error) { | ||
| dispatch(showToast(DANGER, error)); | ||
| } | ||
| dispatch({ type: TYPES.COMPLETED }); | ||
| }; | ||
|
|
||
| export const sendNewKeyRequest = (label) => async (dispatch, getState) => { | ||
| try { | ||
| dispatch({ type: TYPES.LOADING }); | ||
| const endpoints = getState().apiEndpoint.endpoints; | ||
| const keyList = [...getState().keyManagement.keyList]; | ||
|
|
||
| const response = await API.post( | ||
| uriTemplate(endpoints, "key", { key: "" }), | ||
| null, | ||
| { params: { label } } | ||
| ); | ||
| if (response.status === 201) { | ||
| keyList.push(response.data); | ||
| dispatch({ | ||
| type: TYPES.SET_API_KEY_LIST, | ||
| payload: keyList, | ||
| }); | ||
| dispatch(showToast(SUCCESS, "API key created successfully")); | ||
|
|
||
| dispatch(toggleNewAPIKeyModal(false)); | ||
| dispatch(setNewKeyLabel("")); | ||
| } else { | ||
| dispatch(showToast(DANGER, response.data.message)); | ||
| } | ||
| } catch { | ||
| dispatch(showToast(DANGER, ERROR_MSG)); | ||
| } | ||
| dispatch({ type: TYPES.COMPLETED }); | ||
| }; | ||
|
|
||
| export const toggleNewAPIKeyModal = (isOpen) => ({ | ||
| type: TYPES.TOGGLE_NEW_KEY_MODAL, | ||
| payload: isOpen, | ||
| }); | ||
|
|
||
| export const setNewKeyLabel = (label) => ({ | ||
| type: TYPES.SET_NEW_KEY_LABEL, | ||
| payload: label, | ||
| }); | ||
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
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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export const DANGER = "danger"; | ||
| export const ERROR_MSG = "Something went wrong!"; | ||
| export const SUCCESS = "success"; |
69 changes: 69 additions & 0 deletions
69
dashboard/src/modules/components/ProfileComponent/KeyListTable.jsx
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,69 @@ | ||
| import { Button, ClipboardCopy } from "@patternfly/react-core"; | ||
| import { | ||
| TableComposable, | ||
| Tbody, | ||
| Td, | ||
| Th, | ||
| Thead, | ||
| Tr, | ||
| } from "@patternfly/react-table"; | ||
| import { useDispatch, useSelector } from "react-redux"; | ||
|
|
||
| import React from "react"; | ||
| import { TrashIcon } from "@patternfly/react-icons"; | ||
| import { deleteAPIKey } from "actions/keyManagementActions"; | ||
| import { formatDateTime } from "utils/dateFunctions"; | ||
|
|
||
| const KeyListTable = () => { | ||
| const dispatch = useDispatch(); | ||
| const keyList = useSelector((state) => state.keyManagement.keyList); | ||
| const columnNames = { | ||
| label: "Label", | ||
| created: "Created Date & Time", | ||
| key: "API key", | ||
| }; | ||
|
|
||
| return ( | ||
| <TableComposable aria-label="key list table" isStriped> | ||
| <Thead> | ||
| <Tr> | ||
| <Th width={10}>{columnNames.label}</Th> | ||
| <Th width={20}>{columnNames.created}</Th> | ||
| <Th width={20}>{columnNames.key}</Th> | ||
| <Th width={5}></Th> | ||
| </Tr> | ||
| </Thead> | ||
| <Tbody className="keylist-table-body"> | ||
| {keyList.map((item) => ( | ||
| <Tr key={item.key}> | ||
| <Td dataLabel={columnNames.label}>{item.label}</Td> | ||
| <Td dataLabel={columnNames.created}> | ||
| {formatDateTime(item.created)} | ||
| </Td> | ||
| <Td dataLabel={columnNames.key} className="key-cell"> | ||
| <ClipboardCopy | ||
| hoverTip="Copy API key" | ||
| clickTip="Copied" | ||
| variant="plain" | ||
| > | ||
| {item.key} | ||
| </ClipboardCopy> | ||
| </Td> | ||
|
|
||
| <Td className="delete-icon-cell"> | ||
| <Button | ||
| variant="plain" | ||
| aria-label="Delete Action" | ||
| onClick={() => dispatch(deleteAPIKey(item.id))} | ||
| > | ||
| <TrashIcon /> | ||
| </Button> | ||
| </Td> | ||
| </Tr> | ||
| ))} | ||
| </Tbody> | ||
| </TableComposable> | ||
| ); | ||
| }; | ||
|
|
||
| export default KeyListTable; |
46 changes: 46 additions & 0 deletions
46
dashboard/src/modules/components/ProfileComponent/KeyManagement.jsx
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,46 @@ | ||
| import { Button, Card, CardBody } from "@patternfly/react-core"; | ||
| import React, { useEffect } from "react"; | ||
| import { | ||
| getAPIkeysList, | ||
| setNewKeyLabel, | ||
| toggleNewAPIKeyModal, | ||
| } from "actions/keyManagementActions"; | ||
| import { useDispatch, useSelector } from "react-redux"; | ||
|
|
||
| import KeyListTable from "./KeyListTable"; | ||
| import NewKeyModal from "./NewKeyModal"; | ||
|
|
||
| const KeyManagementComponent = () => { | ||
| const dispatch = useDispatch(); | ||
| const isModalOpen = useSelector((state) => state.keyManagement.isModalOpen); | ||
| const { idToken } = useSelector((state) => state.apiEndpoint?.keycloak); | ||
| useEffect(() => { | ||
| if (idToken) { | ||
| dispatch(getAPIkeysList); | ||
| } | ||
| }, [dispatch, idToken]); | ||
| const handleModalToggle = () => { | ||
| dispatch(setNewKeyLabel("")); | ||
| dispatch(toggleNewAPIKeyModal(!isModalOpen)); | ||
| }; | ||
| return ( | ||
| <Card className="key-management-container"> | ||
| <CardBody> | ||
| <div className="heading-wrapper"> | ||
| <p className="heading-title">API Keys</p> | ||
| <Button variant="tertiary" onClick={handleModalToggle}> | ||
| New API key | ||
| </Button> | ||
| </div> | ||
| <p className="key-desc"> | ||
| This is a list of API keys associated with your account. Remove any | ||
| keys that you do not recognize. | ||
| </p> | ||
| <KeyListTable /> | ||
| </CardBody> | ||
| <NewKeyModal handleModalToggle={handleModalToggle} /> | ||
| </Card> | ||
| ); | ||
| }; | ||
|
|
||
| export default KeyManagementComponent; |
60 changes: 60 additions & 0 deletions
60
dashboard/src/modules/components/ProfileComponent/NewKeyModal.jsx
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,60 @@ | ||
| import "./index.less"; | ||
|
|
||
| import { | ||
| Button, | ||
| Form, | ||
| FormGroup, | ||
| Modal, | ||
| ModalVariant, | ||
| TextInput, | ||
| } from "@patternfly/react-core"; | ||
| import { | ||
| sendNewKeyRequest, | ||
| setNewKeyLabel, | ||
| } from "actions/keyManagementActions"; | ||
| import { useDispatch, useSelector } from "react-redux"; | ||
|
|
||
| import React from "react"; | ||
|
|
||
| const NewKeyModal = (props) => { | ||
| const dispatch = useDispatch(); | ||
| const { isModalOpen, newKeyLabel } = useSelector( | ||
| (state) => state.keyManagement | ||
| ); | ||
|
|
||
| return ( | ||
| <Modal | ||
| variant={ModalVariant.small} | ||
| title="New API Key" | ||
| isOpen={isModalOpen} | ||
| showClose={false} | ||
| actions={[ | ||
| <Button | ||
| key="create" | ||
| variant="primary" | ||
| form="modal-with-form-form" | ||
| onClick={() => dispatch(sendNewKeyRequest(newKeyLabel))} | ||
| > | ||
| Create | ||
| </Button>, | ||
| <Button key="cancel" variant="link" onClick={props.handleModalToggle}> | ||
| Cancel | ||
| </Button>, | ||
| ]} | ||
| > | ||
| <Form id="new-api-key-form"> | ||
| <FormGroup label="Enter the label" fieldId="new-api-key-form"> | ||
| <TextInput | ||
| type="text" | ||
| id="new-api-key-form" | ||
| name="new-api-key-form" | ||
| value={newKeyLabel} | ||
| onChange={(value) => dispatch(setNewKeyLabel(value))} | ||
| /> | ||
| </FormGroup> | ||
| </Form> | ||
| </Modal> | ||
| ); | ||
| }; | ||
|
|
||
| export default NewKeyModal; |
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.