helpers.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. """
  2. Helper functions
  3. """
  4. import itertools
  5. from cloudbridge.cloud.base.resources import ServerPagedResultList
  6. def os_result_limit(provider, requested_limit):
  7. """
  8. Calculates the limit for OpenStack.
  9. """
  10. limit = requested_limit or provider.config.default_result_limit
  11. # fetch one more than the limit to help with paging.
  12. # i.e. if length(objects) is one more than the limit,
  13. # we know that the object has another page of results,
  14. # so we always request one extra record.
  15. return limit + 1
  16. def to_server_paged_list(provider, objects, limit):
  17. """
  18. A convenience function for wrapping a list of OpenStack native objects in
  19. a ServerPagedResultList. OpenStack
  20. initial list of objects. Thereafter, the return list is wrapped in a
  21. BaseResultList, enabling extra properties like
  22. `is_truncated` and `marker` to be accessed.
  23. """
  24. limit = limit or provider.config.default_result_limit
  25. is_truncated = len(objects) > limit
  26. next_token = objects[limit].id if is_truncated else None
  27. results = ServerPagedResultList(is_truncated,
  28. next_token,
  29. False)
  30. for obj in itertools.islice(objects, limit):
  31. results.append(obj)
  32. return results