Skip to content

Commit

Permalink
feat(subnets): show error message if IP is already reserved/used MAAS…
Browse files Browse the repository at this point in the history
…ENG-3516 (#5502)

- "Reserve static DHCP lease" form will now check if an IP is:
  - Already reserved
  - In use by another node
---
Fixes [MAASENG-3516](https://warthogs.atlassian.net/browse/MAASENG-3516)
  • Loading branch information
ndv99 authored Jul 18, 2024
1 parent 64baa11 commit 3cc9cbd
Showing 1 changed file with 24 additions and 0 deletions.
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

0 comments on commit 3cc9cbd

Please sign in to comment.