Helpers

Wait Resource State

ucloud.helpers.wait.wait_for_state(pending, target, refresh, timeout, startup_delay=0, min_backoff_interval=0.1, max_backoff_interval=10)[source]

wait_for_state is a utilities function to wait the state return by refresh function achieve the specific state, the generally usage is wait the cloud resource, such as uhost, udb … is ready after created.

>>> wait_for_state(
...     pending=["pending"],
...     target=["running"],
...     refresh=lambda: "running",
...     timeout=0.5,
... )
Parameters:
  • pending – pending is the list of pending state, the state is returned by refresh function
  • target – target is the list of target state, it is usually the terminate state, eg. running and fail
  • refresh – the customized refresh function, expect no arguments and return a state
  • timeout – timeout is the total time to wait state is achieved
  • startup_delay – the time to wait before first refresh function is called
  • min_backoff_interval – the backoff time for first refresh interval
  • max_backoff_interval – the max backoff time for refresh interval

Utilities

ucloud.helpers.utils.b64decode(s)[source]

base64 decode

Parameters:s (str) – base64 string
Returns:output string
ucloud.helpers.utils.b64encode(s)[source]

base64 encode

Parameters:s (str) – input string
Returns:base64 string
ucloud.helpers.utils.gen_password(n, lower_letters='abcdefghijklmnopqrstuvwxyz', upper_letters='ABCDEFGHIJKLMNOPQRSTUVWXYZ', number_letters='0123456789', special_letters='_', min_lower=1, min_upper=1, min_number=1, min_specials=1)[source]

generate password for any resource

>>> len(gen_password(20))
20
Parameters:
  • n (int) – password total length
  • lower_letters (str) – all lowercase letters
  • upper_letters (str) – all uppercase letters
  • number_letters (str) – all number letters
  • special_letters (str) – all special letters
  • min_lower (int) – minimal number of lowercase letters
  • min_upper (int) – minimal number of uppercase letters
  • min_number (int) – minimal number of number letters
  • min_specials (int) – minimal number of special letters
Returns: