Skip to content
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

feat(subnets): show error message if IP is already reserved/used MAASENG-3516 #5502

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { reservedIpActions } from "@/app/store/reservedip";
import reservedIpSelectors from "@/app/store/reservedip/selectors";
import type { RootState } from "@/app/store/root/types";
import subnetSelectors from "@/app/store/subnet/selectors";
import { isSubnetDetails } from "@/app/store/subnet/utils";
import {
getImmutableAndEditableOctets,
getIpRangeFromCidr,
Expand Down Expand Up @@ -49,6 +50,19 @@ const ReserveDHCPLease = ({
const reservedIp = useSelector((state: RootState) =>
reservedIpSelectors.getById(state, reservedIpId)
);
const subnetReservedIps = useSelector((state: RootState) =>
reservedIpSelectors.getBySubnet(state, subnetId)
);

const subnetReservedIpList = subnetReservedIps
.map((reservedIp) => reservedIp.ip)
.filter((ipAddress) => ipAddress !== reservedIp?.ip);
const subnetUsedIps = isSubnetDetails(subnet)
? subnet.ip_addresses
.map((address) => address.ip)
.filter((ipAddress) => ipAddress !== reservedIp?.ip)
: [];

const subnetLoading = useSelector(subnetSelectors.loading);
const reservedIpLoading = useSelector(reservedIpSelectors.loading);
const errors = useSelector(reservedIpSelectors.errors);
Expand Down Expand Up @@ -123,6 +137,16 @@ const ReserveDHCPLease = ({
}
}
},
})
.test({
name: "ip-already-reserved",
message: "This IP address is already used or reserved.",
test: (ip_address) => {
const ip = formatIpAddress(ip_address, subnet.cidr);
return (
!subnetReservedIpList.includes(ip) && !subnetUsedIps.includes(ip)
);
},
}),
mac_address: Yup.string().matches(MAC_ADDRESS_REGEX, "Invalid MAC address"),
comment: Yup.string(),
Expand Down
Loading