-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
164 additions
and
5 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
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
152 changes: 152 additions & 0 deletions
152
frontend/packages/kubevirt-plugin/src/components/vms/vm-users.tsx
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,152 @@ | ||
import * as React from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { watchURL, stopWatchURL } from '@console/internal/actions/dashboards'; | ||
import { RESULTS_TYPE } from '@console/internal/reducers/dashboards'; | ||
import { RootState } from '@console/internal/redux'; | ||
import * as classNames from 'classnames'; | ||
import { sortable } from '@patternfly/react-table'; | ||
import { fromNow } from '@console/internal/components/utils/datetime'; | ||
import { Table, TableRow, TableData } from '@console/internal/components/factory'; | ||
import { Timestamp } from '@console/internal/components/utils'; | ||
import { StatusItem } from '@console/shared/src/components/dashboard/status-card/AlertItem'; | ||
import { BlueInfoCircleIcon } from '@console/shared/src/components/status'; | ||
import { getName, getNamespace } from '@console/shared/src/selectors/common'; | ||
import { | ||
VIRTUAL_MACHINE_IS_NOT_RUNNING, | ||
NO_GUEST_AGENT_MESSAGE, | ||
} from '../../constants/vm/constants'; | ||
import { VMStatus } from '../../constants/vm/vm-status'; | ||
import { isGuestAgentInstalled } from '../dashboards-page/vm-dashboard/vm-alerts'; | ||
import { VMStatusBundle } from '../../statuses/vm/types'; | ||
import { VMIKind } from '../../types'; | ||
|
||
const guestAgentURL = (name: string, namespace: string) => | ||
`apis/subresources.kubevirt.io/v1alpha3/namespaces/${namespace}/virtualmachineinstances/${name}/guestosinfo`; | ||
|
||
const tableColumnClasses = [ | ||
classNames('col-lg-3', 'col-md-3', 'col-sm-4', 'col-sm-4'), | ||
classNames('col-lg-3', 'col-md-3', 'col-sm-4', 'col-sm-4'), | ||
classNames('col-lg-3', 'col-md-3', 'col-sm-4', 'col-sm-4'), | ||
classNames('col-lg-3', 'col-md-3', 'hidden-sm', 'hidden-xs'), | ||
]; | ||
|
||
const UsersTableHeader = () => { | ||
return [ | ||
{ | ||
title: 'User Name', | ||
sortField: 'metadata.userName', | ||
transforms: [sortable], | ||
props: { className: tableColumnClasses[0] }, | ||
}, | ||
{ | ||
title: 'Domain', | ||
sortField: 'metadata.domain', | ||
transforms: [sortable], | ||
props: { className: tableColumnClasses[1] }, | ||
}, | ||
{ | ||
title: 'Login time', | ||
sortField: 'metadata.loginTime', | ||
transforms: [sortable], | ||
props: { className: tableColumnClasses[2] }, | ||
}, | ||
{ | ||
title: 'Elapsed logged in time', | ||
props: { className: tableColumnClasses[3] }, | ||
}, | ||
]; | ||
}; | ||
UsersTableHeader.displayName = 'UsersTableHeader'; | ||
|
||
const UsersTableRow = ({ obj: user, index, key, style }) => { | ||
return ( | ||
<TableRow id={user?.metadata?.uid} index={index} trKey={key} style={style}> | ||
<TableData className={tableColumnClasses[0]}>{user?.metadata?.userName}</TableData> | ||
<TableData className={classNames(tableColumnClasses[1], 'co-break-word')}> | ||
{user?.metadata?.domain} | ||
</TableData> | ||
<TableData className={tableColumnClasses[2]}> | ||
<Timestamp timestamp={new Date(user?.metadata?.loginTime).toUTCString()} /> | ||
</TableData> | ||
<TableData className={tableColumnClasses[3]}>{fromNow(user?.metadata?.loginTime)}</TableData> | ||
</TableRow> | ||
); | ||
}; | ||
|
||
export const VMUsersListWrapper: React.FC<VMUsersListProps> = ({ | ||
watchURLAction, | ||
stopWatchURLAction, | ||
urlResults, | ||
vmi, | ||
vmStatusBundle, | ||
}) => { | ||
if (vmStatusBundle.status !== VMStatus.RUNNING) { | ||
return <div className="text-center">{VIRTUAL_MACHINE_IS_NOT_RUNNING}</div>; | ||
} | ||
|
||
if (!isGuestAgentInstalled(vmi)) { | ||
return <StatusItem Icon={BlueInfoCircleIcon} message={NO_GUEST_AGENT_MESSAGE} />; | ||
} | ||
|
||
const name = getName(vmi); | ||
const namespace = getNamespace(vmi); | ||
const url = guestAgentURL(name, namespace); | ||
|
||
const usersResult = urlResults.getIn([url, 'data']); | ||
const usersResultError = urlResults.getIn([url, 'loadError']); | ||
|
||
const data = | ||
usersResult && | ||
usersResult?.userList && | ||
usersResult?.userList.map((user, uid) => ({ | ||
metadata: { | ||
uid, | ||
userName: user?.userName, | ||
domain: user?.domain, | ||
loginTime: user?.loginTime && user.loginTime * 1000, | ||
}, | ||
})); | ||
|
||
// eslint-disable-next-line react-hooks/rules-of-hooks | ||
React.useEffect(() => { | ||
watchURLAction(url); | ||
return () => { | ||
stopWatchURLAction(url); | ||
}; | ||
}, [watchURLAction, stopWatchURLAction, url]); | ||
|
||
return ( | ||
<Table | ||
aria-label="Users" | ||
Header={UsersTableHeader} | ||
Row={UsersTableRow} | ||
data={data} | ||
loadError={usersResultError?.message} | ||
loaded={!!usersResult && !usersResultError} | ||
EmptyMsg={() => <div className="text-center">No Logged In Users</div>} | ||
virtualize | ||
/> | ||
); | ||
}; | ||
|
||
type WatchURL = (url: string) => void; | ||
type StopWatchURL = (url: string) => void; | ||
|
||
type VMUsersListProps = { | ||
vmi?: VMIKind; | ||
vmStatusBundle?: VMStatusBundle; | ||
urlResults: any; | ||
watchURLAction: WatchURL; | ||
stopWatchURLAction: StopWatchURL; | ||
}; | ||
|
||
const stateToProps = (state: RootState) => ({ | ||
urlResults: state.dashboards.get(RESULTS_TYPE.URL), | ||
}); | ||
|
||
const dispatchToProps = (dispatch: any) => ({ | ||
watchURLAction: (url) => dispatch(watchURL(url)), | ||
stopWatchURLAction: (url) => dispatch(stopWatchURL(url)), | ||
}); | ||
|
||
export const VMUsersList = connect(stateToProps, dispatchToProps)(VMUsersListWrapper); |
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