|
|
|
|
@ -1,5 +1,11 @@
|
|
|
|
|
import * as yup from "yup";
|
|
|
|
|
import { IEditNetworkForm, INetworkNodeItem } from "../../../types";
|
|
|
|
|
import {
|
|
|
|
|
IEditNetworkForm,
|
|
|
|
|
IIPAM,
|
|
|
|
|
INetworkNodeItem,
|
|
|
|
|
IPAMConfig
|
|
|
|
|
} from "../../../types";
|
|
|
|
|
import { pruneArray, pruneObject } from "../../../utils/forms";
|
|
|
|
|
|
|
|
|
|
export const validationSchema = yup.object({
|
|
|
|
|
entryName: yup
|
|
|
|
|
@ -12,10 +18,7 @@ export const validationSchema = yup.object({
|
|
|
|
|
.max(256, "Network name should be 256 characters or less")
|
|
|
|
|
.required("Network name is required"),
|
|
|
|
|
|
|
|
|
|
driver: yup
|
|
|
|
|
.string()
|
|
|
|
|
.max(256, "Driver should be 256 characters or less")
|
|
|
|
|
.default("default"),
|
|
|
|
|
driver: yup.string().max(256, "Driver should be 256 characters or less"),
|
|
|
|
|
|
|
|
|
|
configurations: yup.array(
|
|
|
|
|
yup.object({
|
|
|
|
|
@ -70,7 +73,7 @@ export const tabs = [
|
|
|
|
|
export const initialValues: IEditNetworkForm = {
|
|
|
|
|
entryName: "",
|
|
|
|
|
networkName: "",
|
|
|
|
|
driver: "default",
|
|
|
|
|
driver: "",
|
|
|
|
|
configurations: [],
|
|
|
|
|
options: [],
|
|
|
|
|
labels: []
|
|
|
|
|
@ -93,30 +96,29 @@ export const getInitialValues = (node?: INetworkNodeItem): IEditNetworkForm => {
|
|
|
|
|
networkName: name,
|
|
|
|
|
driver: ipam?.driver ?? "",
|
|
|
|
|
configurations:
|
|
|
|
|
ipam?.config.map((item) => ({
|
|
|
|
|
subnet: item.subnet,
|
|
|
|
|
ipRange: item.ip_range,
|
|
|
|
|
gateway: item.gateway,
|
|
|
|
|
auxAddresses: Object.entries(item.aux_addresses).map(
|
|
|
|
|
ipam?.config?.map((item) => ({
|
|
|
|
|
subnet: item.subnet ?? "",
|
|
|
|
|
ipRange: item.ip_range ?? "",
|
|
|
|
|
gateway: item.gateway ?? "",
|
|
|
|
|
auxAddresses: Object.entries(item.aux_addresses ?? []).map(
|
|
|
|
|
([hostName, ipAddress]) => ({
|
|
|
|
|
hostName,
|
|
|
|
|
ipAddress
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
})) ?? [],
|
|
|
|
|
options: Object.keys(ipam?.options || {}).map((key) => {
|
|
|
|
|
if (!ipam) {
|
|
|
|
|
throw new Error("Control should not reach here.");
|
|
|
|
|
}
|
|
|
|
|
options: Object.keys(ipam?.options ?? {}).map((key) => {
|
|
|
|
|
return {
|
|
|
|
|
key,
|
|
|
|
|
value: ipam.options[key].toString()
|
|
|
|
|
value: ipam?.options?.[key].toString() ?? ""
|
|
|
|
|
};
|
|
|
|
|
}),
|
|
|
|
|
labels: Object.entries(labels as any).map(([key, value]: any) => ({
|
|
|
|
|
key,
|
|
|
|
|
value
|
|
|
|
|
}))
|
|
|
|
|
labels: labels
|
|
|
|
|
? Object.entries(labels as any).map(([key, value]: any) => ({
|
|
|
|
|
key,
|
|
|
|
|
value
|
|
|
|
|
}))
|
|
|
|
|
: []
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@ -129,34 +131,63 @@ export const getFinalValues = (
|
|
|
|
|
return {
|
|
|
|
|
key: previous?.key ?? "network",
|
|
|
|
|
type: "NETWORK",
|
|
|
|
|
position: {
|
|
|
|
|
left: 0,
|
|
|
|
|
top: 0
|
|
|
|
|
},
|
|
|
|
|
inputs: previous?.inputs ?? [],
|
|
|
|
|
outputs: previous?.outputs ?? [],
|
|
|
|
|
config: (previous as any)?.config ?? {},
|
|
|
|
|
canvasConfig: {
|
|
|
|
|
node_name: values.entryName
|
|
|
|
|
},
|
|
|
|
|
networkConfig: {
|
|
|
|
|
name: values.networkName,
|
|
|
|
|
ipam: {
|
|
|
|
|
driver,
|
|
|
|
|
config: configurations.map((configuration) => ({
|
|
|
|
|
subnet: configuration.subnet,
|
|
|
|
|
ip_range: configuration.ipRange,
|
|
|
|
|
gateway: configuration.gateway,
|
|
|
|
|
aux_addresses: Object.fromEntries(
|
|
|
|
|
configuration.auxAddresses.map((auxAddress) => [
|
|
|
|
|
auxAddress.hostName,
|
|
|
|
|
auxAddress.ipAddress
|
|
|
|
|
])
|
|
|
|
|
ipam: pruneObject({
|
|
|
|
|
driver: driver ? driver : undefined,
|
|
|
|
|
config: pruneArray(
|
|
|
|
|
configurations.map((configuration) =>
|
|
|
|
|
pruneObject({
|
|
|
|
|
subnet: configuration.subnet ? configuration.subnet : undefined,
|
|
|
|
|
ip_range: configuration.ipRange
|
|
|
|
|
? configuration.ipRange
|
|
|
|
|
: undefined,
|
|
|
|
|
gateway: configuration.gateway
|
|
|
|
|
? configuration.gateway
|
|
|
|
|
: undefined,
|
|
|
|
|
aux_addresses: (() => {
|
|
|
|
|
if (configuration.auxAddresses.length === 0) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* We do not have to worry about empty `hostName` and `ipAddress`
|
|
|
|
|
* values because Yup would report such values as error.
|
|
|
|
|
*/
|
|
|
|
|
return Object.fromEntries(
|
|
|
|
|
configuration.auxAddresses.map((auxAddress) => [
|
|
|
|
|
auxAddress.hostName,
|
|
|
|
|
auxAddress.ipAddress
|
|
|
|
|
])
|
|
|
|
|
);
|
|
|
|
|
})()
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
})),
|
|
|
|
|
options: Object.fromEntries(
|
|
|
|
|
options.map((option) => [option.key, option.value])
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
labels: Object.fromEntries(
|
|
|
|
|
labels.map((label) => [label.key, label.value])
|
|
|
|
|
)
|
|
|
|
|
) as IPAMConfig[],
|
|
|
|
|
options: (() => {
|
|
|
|
|
if (options.length === 0) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* We do not have to worry about empty `key` and `value`
|
|
|
|
|
* values because Yup would report such values as error.
|
|
|
|
|
*/
|
|
|
|
|
return Object.fromEntries(
|
|
|
|
|
options.map((option) => [option.key, option.value])
|
|
|
|
|
);
|
|
|
|
|
})()
|
|
|
|
|
}) as IIPAM,
|
|
|
|
|
labels: pruneObject(
|
|
|
|
|
Object.fromEntries(labels.map((label) => [label.key, label.value]))
|
|
|
|
|
) as Record<string, string>
|
|
|
|
|
}
|
|
|
|
|
} as any;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|