"""hello_sdk.py - from Python: make a sandbox, run one command in it, remove it.

    python hello_sdk.py

Use the Python that has the openshell library. The library finds the gateway
the same way the openshell command does, so the gateway must be running.
"""
from openshell import SandboxClient

WORKSPACE = "default"
NAME = "from-python"


def main():
    # "with" closes the connection to the gateway when the block ends.
    with SandboxClient.from_active_cluster() as client:
        print("gateway version:", client.health().version)
        client.create(workspace=WORKSPACE, name=NAME)
        client.wait_ready(NAME, workspace=WORKSPACE, timeout_seconds=120)
        result = client.exec(NAME, ["id"], workspace=WORKSPACE)
        print("the sandbox says:", result.stdout.strip())
        print("exit code:", result.exit_code)
        deletion = client.delete(NAME, workspace=WORKSPACE)
        client.wait_deleted(NAME, workspace=WORKSPACE,
                            expected_sandbox_id=deletion.sandbox_id)
        print("sandbox removed")


if __name__ == "__main__":
    main()
