@@ -621,3 +621,40 @@ def find_security_group(compute_client, name_or_id):
621621 raise exceptions .NotFound (f'{ name_or_id } not found' )
622622
623623 return found
624+
625+
626+ def find_network (compute_client , name_or_id ):
627+ """Find the ID for a given network name or ID
628+
629+ https://docs.openstack.org/api-ref/compute/#show-network-details
630+
631+ :param compute_client: A compute client
632+ :param name_or_id: The name or ID of the network to look up
633+ :returns: A network object
634+ :raises exception.NotFound: If a matching network could not be found or
635+ more than one match was found
636+ """
637+ response = compute_client .get (
638+ f'/os-networks/{ name_or_id } ' , microversion = '2.1'
639+ )
640+ if response .status_code != http .HTTPStatus .NOT_FOUND :
641+ # there might be other, non-404 errors
642+ sdk_exceptions .raise_from_response (response )
643+ return response .json ()['network' ]
644+
645+ response = compute_client .get ('/os-networks' , microversion = '2.1' )
646+ sdk_exceptions .raise_from_response (response )
647+ found = None
648+ networks = response .json ()['networks' ]
649+ for network in networks :
650+ if network ['label' ] == name_or_id :
651+ if found :
652+ raise exceptions .NotFound (
653+ f'multiple matches found for { name_or_id } '
654+ )
655+ found = network
656+
657+ if not found :
658+ raise exceptions .NotFound (f'{ name_or_id } not found' )
659+
660+ return found
0 commit comments