OpenStack Image Management (Glance)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ft_utils.py 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright 2018 Verizon Wireless
  2. # All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. import time
  16. from oslo_serialization import jsonutils
  17. import requests
  18. from six.moves import http_client as http
  19. def wait_for_status(request_path, request_headers, status='active',
  20. max_sec=10, delay_sec=0.2, start_delay_sec=None):
  21. """
  22. Performs a time-bounded wait for the entity at the request_path to
  23. reach the requested status.
  24. :param request_path: path to use to make the request
  25. :param request_headers: headers to use when making the request
  26. :param status: the status to wait for (default: 'active')
  27. :param max_sec: the maximum number of seconds to wait (default: 10)
  28. :param delay_sec: seconds to sleep before the next request is
  29. made (default: 0.2)
  30. :param start_delay_sec: seconds to wait before making the first
  31. request (default: None)
  32. :raises Exception: if the entity fails to reach the status within
  33. the requested time or if the server returns something
  34. other than a 200 response
  35. """
  36. start_time = time.time()
  37. done_time = start_time + max_sec
  38. if start_delay_sec:
  39. time.sleep(start_delay_sec)
  40. while time.time() <= done_time:
  41. resp = requests.get(request_path, headers=request_headers)
  42. if resp.status_code != http.OK:
  43. raise Exception("Received {} response from server".format(
  44. resp.status_code))
  45. entity = jsonutils.loads(resp.text)
  46. if entity['status'] == status:
  47. return
  48. time.sleep(delay_sec)
  49. entity_id = request_path.rsplit('/', 1)[1]
  50. msg = "Entity {0} failed to reach status '{1}' within {2} sec"
  51. raise Exception(msg.format(entity_id, status, max_sec))