forked from openshift/console-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathTopology.tsx
130 lines (117 loc) · 4.11 KB
/
Topology.tsx
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
import React, { FC, Ref, useState } from 'react';
import { useFormContext, useWatch } from 'react-hook-form';
import {
DropdownItem,
Flex,
FlexItem,
FormGroup,
MenuToggle,
MenuToggleElement,
Radio,
Select,
TextInput,
} from '@patternfly/react-core';
import FormGroupHelperText from '@utils/components/FormGroupHelperText/FormGroupHelperText';
import { useNetworkingTranslation } from '@utils/hooks/useNetworkingTranslation';
import { LOCALNET_TOPOLOGY } from '@utils/resources/udns/constants';
import { UserDefinedNetworkRole, UserDefinedNetworkSpec } from '@utils/resources/udns/types';
import { UDNForm } from './constants';
import { createNetworkSpecFromRole, createNetworkSpecFromTopology, getTopology } from './utils';
type TopologyProps = {
isClusterUDN: boolean;
};
const Topology: FC<TopologyProps> = ({ isClusterUDN }) => {
const { t } = useNetworkingTranslation();
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
const { register, setValue } = useFormContext<UDNForm>();
const networkSpecPath = isClusterUDN ? 'spec.network' : 'spec';
const networkSpec: UserDefinedNetworkSpec = useWatch<UDNForm>({
name: networkSpecPath,
});
const topology = getTopology(networkSpec);
const isPrimary =
networkSpec?.layer2?.role === UserDefinedNetworkRole.Primary ||
networkSpec?.layer3?.role === UserDefinedNetworkRole.Primary;
const onChangeRole = (role: UserDefinedNetworkRole) => {
setValue(networkSpecPath, createNetworkSpecFromRole(networkSpec, role));
};
return (
<>
<FormGroup>
<Flex>
<FlexItem>
<Radio
id="primary-topology"
isChecked={isPrimary}
label="Primary"
name="radio-topology"
onChange={() => onChangeRole(UserDefinedNetworkRole.Primary)}
></Radio>
</FlexItem>
<FlexItem>
<Radio
id="secondary-topology"
isChecked={!isPrimary}
label="Secondary"
name="radio-topology"
onChange={() => onChangeRole(UserDefinedNetworkRole.Secondary)}
></Radio>
</FlexItem>
</Flex>
</FormGroup>
<FormGroup fieldId="select-topology" isRequired label={t('Topology')}>
<Select
id="select-topology"
isOpen={isDropdownOpen}
onOpenChange={setIsDropdownOpen}
onSelect={(_, selection) => {
setValue(
isClusterUDN ? 'spec.network' : 'spec',
createNetworkSpecFromTopology(selection as string, networkSpec),
);
setIsDropdownOpen(false);
}}
selected={topology}
toggle={(toggleRef: Ref<MenuToggleElement>) => (
<MenuToggle
id="toggle-udns-operator"
isDisabled={!isPrimary}
isExpanded={isDropdownOpen}
isFullWidth
onClick={() => setIsDropdownOpen(!isDropdownOpen)}
ref={toggleRef}
>
{topology}
</MenuToggle>
)}
>
<>
{isPrimary ? (
<>
<DropdownItem value="Layer2">{t('Layer 2')}</DropdownItem>
<DropdownItem value="Layer3">{t('Layer 3')}</DropdownItem>
</>
) : (
<DropdownItem value="Locanet">{t('Localnet')}</DropdownItem>
)}
</>
</Select>
</FormGroup>
{topology === LOCALNET_TOPOLOGY && (
<FormGroup fieldId="input-name" isRequired label={t('Physical network name')}>
<TextInput
autoFocus
{...register('spec.localnet.physicalNetworkName', { required: true })}
isRequired
/>
<FormGroupHelperText>
{t(
'The name of the physical network. This attribute must match the value of the spec.desiredState.ovn.bridge-mappings.localnet field of the NodeNetworkConfigurationPolicy object that defines the OVS bridge mapping. ',
)}
</FormGroupHelperText>
</FormGroup>
)}
</>
);
};
export default Topology;