Find an unallocated VLAN outside the range allocated to the physical network. @note: This does not guarantee that the VLAN is available for use in the deployment's network gear @return: physical_network, shared_vlan_tag
(apiclient, zoneid)
| 1132 | |
| 1133 | |
| 1134 | def get_free_vlan(apiclient, zoneid): |
| 1135 | """ |
| 1136 | Find an unallocated VLAN outside the range allocated to the physical network. |
| 1137 | |
| 1138 | @note: This does not guarantee that the VLAN is available for use in |
| 1139 | the deployment's network gear |
| 1140 | @return: physical_network, shared_vlan_tag |
| 1141 | """ |
| 1142 | list_physical_networks_response = PhysicalNetwork.list( |
| 1143 | apiclient, |
| 1144 | zoneid=zoneid |
| 1145 | ) |
| 1146 | assert isinstance(list_physical_networks_response, list) |
| 1147 | assert len( |
| 1148 | list_physical_networks_response) > 0, "No physical networks found in zone %s" % zoneid |
| 1149 | |
| 1150 | physical_network = list_physical_networks_response[0] |
| 1151 | |
| 1152 | networks = list_networks(apiclient, zoneid=zoneid) |
| 1153 | usedVlanIds = [] |
| 1154 | |
| 1155 | if isinstance(networks, list) and len(networks) > 0: |
| 1156 | usedVlanIds = [int(nw.broadcasturi.split("//")[-1]) |
| 1157 | for nw in networks if (nw.broadcasturi and nw.broadcasturi.lower() != "vlan://untagged")] |
| 1158 | |
| 1159 | ipranges = list_vlan_ipranges(apiclient, zoneid=zoneid) |
| 1160 | if isinstance(ipranges, list) and len(ipranges) > 0: |
| 1161 | usedVlanIds += [int(iprange.vlan.split("/")[-1]) |
| 1162 | for iprange in ipranges if (iprange.vlan and iprange.vlan.split("/")[-1].lower() != "untagged")] |
| 1163 | |
| 1164 | if not hasattr(physical_network, "vlan"): |
| 1165 | while True: |
| 1166 | shared_ntwk_vlan = random.randrange(1, 4095) |
| 1167 | if shared_ntwk_vlan in usedVlanIds: |
| 1168 | continue |
| 1169 | else: |
| 1170 | break |
| 1171 | else: |
| 1172 | vlans = xsplit(physical_network.vlan, ['-', ',']) |
| 1173 | |
| 1174 | assert len(vlans) > 0 |
| 1175 | assert int(vlans[0]) < int( |
| 1176 | vlans[-1]), "VLAN range %s was improperly split" % physical_network.vlan |
| 1177 | |
| 1178 | # Assuming random function will give different integer each time |
| 1179 | retriesCount = 20 |
| 1180 | |
| 1181 | shared_ntwk_vlan = None |
| 1182 | |
| 1183 | while True: |
| 1184 | |
| 1185 | if retriesCount == 0: |
| 1186 | break |
| 1187 | |
| 1188 | free_vlan = int(vlans[-1]) + random.randrange(1, 20) |
| 1189 | |
| 1190 | if free_vlan > 4095: |
| 1191 | free_vlan = int(vlans[0]) - random.randrange(1, 20) |