diff --git a/gso/services/_ipam.py b/gso/services/_ipam.py index 64d6c6b8b0d74e5d325ca38f558fb69639269d8f..1347b17c09660789ee7a940d04be29196c2845de 100644 --- a/gso/services/_ipam.py +++ b/gso/services/_ipam.py @@ -203,7 +203,8 @@ def _allocate_network( return V6ServiceNetwork(v6=allocated_network) -def allocate_service_ipv4_network(service_type, extattrs) -> V4ServiceNetwork: +def allocate_service_ipv4_network(service_type, extattrs={} + ) -> V4ServiceNetwork: """ Allocate IPv4 network within the container of the specified service type. """ @@ -218,7 +219,8 @@ def allocate_service_ipv4_network(service_type, extattrs) -> V4ServiceNetwork: extattrs) -def allocate_service_ipv6_network(service_type, extattrs) -> V6ServiceNetwork: +def allocate_service_ipv6_network(service_type, extattrs={} + ) -> V6ServiceNetwork: """ Allocate IPv6 network within the container of the specified service type. """ @@ -558,6 +560,32 @@ def _delete_host_by_ip(addr) -> Union[V4HostAddress, V6HostAddress]: return V6HostAddress(v6=addr) +def _get_network_usage_status(network): + """ + Get status and usage fields of all hosts in the specified ipv4 or ipv6 + network. + """ + oss = settings.load_oss_params() + assert oss.IPAM.INFOBLOX + infoblox_params = oss.IPAM.INFOBLOX + + ip_version = _ip_network_version(network) + endpoint = 'ipv4address' if ip_version == 4 else 'ipv6address' + + r = requests.get( + f'{_wapi(infoblox_params)}/{endpoint}', + params={ + '_return_fields': 'ip_address,status,usage', + 'network': network}, + auth=HTTPBasicAuth(infoblox_params.username, + infoblox_params.password), + verify=False + ) + assert r.status_code >= 200 and r.status_code < 300, \ + f"HTTP error {r.status_code}: {r.reason}\n\n{r.text}" + return r.json() + + if __name__ == '__main__': while True: print("1. Find all containers") @@ -569,7 +597,8 @@ if __name__ == '__main__': print("7. Allocate host by network CIDR") print("8. Allocate host by service type") print("9. Delete host by IP") - print("10. Exit") + print("10. Get network usage status") + print("11. Exit") choice = input("Enter your choice: ") @@ -634,6 +663,12 @@ if __name__ == '__main__': print(json.dumps(str(deleted_host), indent=2)) elif choice == '10': + network = input( + "Enter network to get host usage status (CIDR notation): ") + usage_status_info = _get_network_usage_status(network=network) + print(json.dumps(usage_status_info, indent=2)) + + elif choice == '11': print("Exiting...") break