@@ -30,34 +30,15 @@ def random_cidr_addresses_by_family(net_seed: int, host_address: int) -> list[st
3030 Returns:
3131 List of CIDR strings (e.g. ["192.168.1.1/24", "fd00::1/64"]).
3232 """
33- return [
34- f"{ ip } /64" if ipaddress .ip_address (ip ).version == 6 else f"{ ip } /24"
35- for ip in random_ip_addresses_by_family (net_seed = net_seed , host_address = host_address )
36- ]
37-
38-
39- def random_ip_addresses_by_family (
40- net_seed : int ,
41- host_address : int ,
42- ) -> list [str ]:
43- """Generate IP addresses for each IP family supported by the cluster network stack.
44-
45- Args:
46- net_seed: Seed index for selecting the random network portion of the address.
47- host_address: Host portion of the address, used to place VMs on the same subnet.
48-
49- Returns:
50- List of IP address strings, one per IP family supported by the cluster.
51- """
52- ips = []
33+ addresses = []
5334 if ipv4_supported_cluster ():
54- ips .append (random_ipv4_address (net_seed = net_seed , host_address = host_address ))
35+ addresses .append (random_ipv4_address (net_seed = net_seed , host_address = host_address ))
5536 if ipv6_supported_cluster ():
56- ips .append (random_ipv6_address (net_seed = net_seed , host_address = host_address ))
57- return ips
37+ addresses .append (random_ipv6_address (net_seed = net_seed , host_address = host_address ))
38+ return addresses
5839
5940
60- def random_ipv4_address (net_seed : int , host_address : int ) -> str :
41+ def random_ipv4_address (net_seed : int , host_address : int , cidr_required : bool = True ) -> str :
6142 """Construct a random IPv4 address using a cached list of random third octets.
6243
6344 Uses a pre-defined network address, a cached random third octet and the given
@@ -66,12 +47,14 @@ def random_ipv4_address(net_seed: int, host_address: int) -> str:
6647 Args:
6748 net_seed (int): The index used to select a random third octet from the cached list.
6849 host_address (int): The last (fourth) octet of the IPv4 address.
50+ cidr_required (bool): When True (default), appends /24 prefix length to the address.
6951
7052 Returns:
71- str: A string representing a randomized IPv4 address.
53+ str: A randomized IPv4 address, optionally in CIDR notation .
7254 """
7355 third_octets = _random_octets (count = _MAX_NUM_OF_RANDOM_OCTETS_PER_SESSION )
74- return f"{ _IPV4_ADDRESS_SUBNET_PREFIX_VMI } .{ third_octets [net_seed ]} .{ host_address } "
56+ subnet_length = "/24" if cidr_required else ""
57+ return f"{ _IPV4_ADDRESS_SUBNET_PREFIX_VMI } .{ third_octets [net_seed ]} .{ host_address } { subnet_length } "
7558
7659
7760@cache
@@ -90,7 +73,7 @@ def _random_octets(count: int) -> list[int]:
9073 return random .sample (range (1 , 254 ), count )
9174
9275
93- def random_ipv6_address (net_seed : int , host_address : int ) -> str :
76+ def random_ipv6_address (net_seed : int , host_address : int , cidr_required : bool = True ) -> str :
9477 """Construct a random IPv6 address using a cached list of random seventh hextets.
9578
9679 Uses a pre-defined network prefix, a cached random seventh hextet and the given
@@ -99,12 +82,14 @@ def random_ipv6_address(net_seed: int, host_address: int) -> str:
9982 Args:
10083 net_seed (int): The index used to select a random seventh hextet from the cached list.
10184 host_address (int): The last (eighth) hextet of the IPv6 address.
85+ cidr_required (bool): When True (default), appends /64 prefix length to the address.
10286
10387 Returns:
104- str: A string representing a randomized IPv6 address.
88+ str: A randomized IPv6 address, optionally in CIDR notation .
10589 """
10690 seventh_hextets = _random_hextets (count = _MAX_NUM_OF_RANDOM_HEXTETS_PER_SESSION )
107- return f"{ _IPV6_ADDRESS_SUBNET_PREFIX_VMI } ::{ seventh_hextets [net_seed ]:x} :{ host_address :x} "
91+ subnet_length = "/64" if cidr_required else ""
92+ return f"{ _IPV6_ADDRESS_SUBNET_PREFIX_VMI } ::{ seventh_hextets [net_seed ]:x} :{ host_address :x} { subnet_length } "
10893
10994
11095@cache
0 commit comments