-
Notifications
You must be signed in to change notification settings - Fork 415
Expand file tree
/
Copy pathprincipal-group-editor.tsx
More file actions
148 lines (137 loc) · 5.1 KB
/
principal-group-editor.tsx
File metadata and controls
148 lines (137 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* Copyright 2022 Redpanda Data, Inc.
*
* Use of this software is governed by the Business Source License
* included in the file https://github.com/redpanda-data/redpanda/blob/dev/licenses/bsl.md
*
* As of the Change Date specified in that file, in accordance with
* the Business Source License, use of this software will be governed
* by the Apache License, Version 2.0
*/
import { Box, Button, Flex, FormField, Grid, Icon, Input, InputGroup, InputLeftAddon, Text } from '@redpanda-data/ui';
import { TrashIcon } from 'components/icons';
import type { ResourceACLs } from './models';
import { Operation } from './operation';
import { AclOperation, type AclStrResourceType } from '../../../state/rest-interfaces';
import { Label } from '../../../utils/tsx-utils';
import { SingleSelect } from '../../misc/select';
export const ResourceACLsEditor = (p: {
resource: ResourceACLs;
resourceType: AclStrResourceType;
setIsFormValid: (isValid: boolean) => void;
onDelete?: () => void;
onChange: (resource: ResourceACLs) => void;
}) => {
const res = p.resource;
if (!res) {
// Happens for clusterAcls?
return null;
}
const isCluster = !('selector' in res);
const isAllSet = res.all === 'Allow' || res.all === 'Deny';
let resourceName = 'Cluster';
if (p.resourceType === 'Topic') {
resourceName = 'Topic';
}
if (p.resourceType === 'Group') {
resourceName = 'Consumer Group';
}
if (p.resourceType === 'TransactionalID') {
resourceName = 'Transactional ID';
}
const isInvalid =
(!isCluster && res.patternType === 'Literal' && res.selector === '') ||
(!isCluster && res.patternType === 'Prefixed' && res.selector === '');
const errorText = 'Selector cannot be empty';
p.setIsFormValid(!isInvalid);
return (
<Flex
border="1px solid"
borderColor="gray.300"
borderRadius="md"
boxShadow="0px 1px 2px 0px #0000000F"
flexDirection="row"
gap={5}
>
<Flex flexDirection="column" flexGrow={1} gap={10} p={6}>
{isCluster ? (
<Text fontWeight={600} whiteSpace="nowrap">
Applies to whole cluster
</Text>
) : (
<FormField errorText={errorText} isInvalid={isInvalid} label={`Selector (${resourceName} Name)`}>
<InputGroup data-testid={`${resourceName}-input-group`} zIndex={1}>
<InputLeftAddon padding="0px" width="124px">
<SingleSelect<'Any' | 'Literal' | 'Prefixed'>
chakraStyles={{
container: () => ({
flexGrow: 1,
marginLeft: '-1px',
marginRight: '-1px',
cursor: 'pointer',
}),
}}
isSearchable={false}
onChange={(e) => {
p.onChange({ ...res, patternType: e, selector: e === 'Any' ? '*' : '' } as ResourceACLs);
}}
options={[
{ value: 'Any', label: 'Any' },
{ value: 'Literal', label: 'Literal' },
{ value: 'Prefixed', label: 'Prefixed' },
]}
value={res.patternType as 'Any' | 'Literal' | 'Prefixed'}
/>
</InputLeftAddon>
<Input
data-testid={`${resourceName}-selector`}
isDisabled={res.patternType === 'Any'}
onChange={(e) => {
p.onChange({ ...res, selector: e.target.value } as ResourceACLs);
}}
spellCheck={false}
value={res.selector}
/>
</InputGroup>
</FormField>
)}
<Label style={{ width: '100%' }} text="Operations">
<Grid gap={6} templateColumns="repeat(auto-fill, minmax(125px, 1fr))" width="full">
<Operation
onChange={(perm) => {
p.onChange({ ...res, all: perm });
}}
operation={AclOperation.All}
value={res.all}
/>
{Object.entries(res.permissions)
.sort(([op1], [op2]) => op1.localeCompare(op2))
.map(([operation, permission]) => (
<Operation
data-testid={`${resourceName}-${operation}`}
disabled={isAllSet}
key={operation}
onChange={(perm) => {
p.onChange({
...res,
permissions: { ...(res.permissions as Record<string, unknown>), [operation]: perm },
} as ResourceACLs);
}}
operation={operation}
value={isAllSet ? res.all : permission}
/>
))}
</Grid>
</Label>
</Flex>
{Boolean(p.onDelete) && (
<Flex>
<Box alignSelf="center" bg="gray.300" height="80%" width="1px" />
<Button alignSelf="center" mx={2} onClick={p.onDelete} variant="destructive-ghost">
<Icon as={TrashIcon} fontSize="22px" />
</Button>
</Flex>
)}
</Flex>
);
};