Helpers

Wait Resource State

ucloud.helpers.wait.wait_for_state(pending: List[str], target: List[str], refresh: Callable, timeout: float, startup_delay: float = 0, min_backoff_interval: float = 0.1, max_backoff_interval: float = 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: str) str[source]

base64 decode

Parameters

s (str) – base64 string

Returns

output string

ucloud.helpers.utils.b64encode(s: str) str[source]

base64 encode

Parameters

s (str) – input string

Returns

base64 string

ucloud.helpers.utils.gen_password(n: int, lower_letters: str = 'abcdefghijklmnopqrstuvwxyz', upper_letters: str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', number_letters: str = '0123456789', special_letters: str = '_', min_lower: int = 1, min_upper: int = 1, min_number: int = 1, min_specials: int = 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