ChameleonCloud / python-chi

Python client for the Chameleon testbed
Apache License 2.0
4 stars 4 forks source link

common commands are not idempotent #23

Closed msherman64 closed 2 years ago

msherman64 commented 3 years ago

common commands, like create lease, create container, are not idempotent. It is common for users to run these multiple times, and then be confused when errors occur.

We should make "ensure_" commands available, since this more often matches user intent.

Example:

from chi import lease

def ensure_lease(lease_name, reservations=[]):
    """
    Ensure that a lease with the name or ID exists, and is active.
    Returns the lease object.
    """

    bad_lease_status = ["Terminated", "Error"]

    try:
        lease_obj = lease.get_lease(lease_name)
        assert(lease_obj["status"] not in bad_lease_status )
    except Exception as ex:
        print(ex)
        print("trying to create new lease")
        try:
            lease_obj = lease.create_lease(lease_name, reservations=reservations)
        except Exception as ex:
            print(ex)
    finally:
        return lease_obj