Python client for CloudShell Sandbox REST api - consume sandboxes via CI
Project description
Cloudshell Sandbox Rest Api Client
A python client wrapper around the cloudshell sandbox api . No additional library object wrapping implemented. All methods return the json.loads python object in the shape of the documented json response. See the documentation for details.
Installation
pip install cloudshell-sandbox-rest
Basic Usage
from cloudshell.sandbox_rest.sandbox_api import SandboxRestApiSession
# pull in api user credentials
CS_SERVER = "localhost"
CS_USER = "admin"
CS_PASSWORD = "admin"
CS_DOMAIN = "Global"
TARGET_BLUEPRINT = "<MY_BLUEPRINT_NAME>"
DEPLOYED_SANDBOX_NAME = "My Rest Api Blueprint"
api = SandboxRestApiSession(host=CS_SERVER, username=CS_USER, password=CS_PASSWORD, domain=CS_DOMAIN)
start_response = api.start_sandbox(blueprint_id=TARGET_BLUEPRINT, sandbox_name=DEPLOYED_SANDBOX_NAME)
sandbox_id = start_response["id"]
components_response = api.get_sandbox_components(sandbox_id)
print(f"total components in sandbox: {len(components_response)}")
Context Manager Usage
Using the api session with a context manager "with" statement will log out and invalidate the token for you.
from cloudshell.sandbox_rest.sandbox_api import SandboxRestApiSession
CS_SERVER = "localhost"
CS_USER = "admin"
CS_PASSWORD = "admin"
CS_DOMAIN = "Global"
TARGET_BLUEPRINT = "<MY_BLUEPRINT_NAME>"
DEPLOYED_SANDBOX_NAME = "My Rest Api Blueprint"
api = SandboxRestApiSession(host=CS_SERVER, username=CS_USER, password=CS_PASSWORD, domain=CS_DOMAIN)
with api:
start_response = api.start_sandbox(blueprint_id=TARGET_BLUEPRINT, sandbox_name=DEPLOYED_SANDBOX_NAME)
sandbox_id = start_response["id"]
components_response = api.get_sandbox_components(sandbox_id)
print(f"total components in sandbox: {len(components_response)}")
print(f"session token is: {api.token}")
print(f"session token outside context manager: {api.token}")
- NOTE: api login happens during init, not on entering context
- context exit invalidates token
Instantiate Session with Token
Common use case is for admin to pull user token and start a session on their behalf. This can be done as seen in example below.
from cloudshell.sandbox_rest.sandbox_api import SandboxRestApiSession
# admin credentials
CS_SERVER = "localhost"
CS_USER = "admin"
CS_PASSWORD = "admin"
ADMIN_DOMAIN = "Global"
# end user details
TARGET_END_USER = "end user"
TARGET_USER_DOMAIN = "<END_USERS_DOMAIN>"
TARGET_BLUEPRINT = "<MY_BLUEPRINT_NAME>"
DEPLOYED_SANDBOX_NAME = "My Rest Api Blueprint"
admin_api = SandboxRestApiSession(host=CS_SERVER, username=CS_USER, password=CS_PASSWORD, domain=ADMIN_DOMAIN)
with admin_api:
user_token = admin_api.get_token_for_target_user(TARGET_END_USER)
user_api = SandboxRestApiSession(host=CS_SERVER, token=user_token, domain=TARGET_USER_DOMAIN)
with user_api:
start_response = user_api.start_sandbox(blueprint_id=TARGET_BLUEPRINT, sandbox_name=DEPLOYED_SANDBOX_NAME)
sandbox_id = start_response["id"]
components_response = user_api.get_sandbox_components(sandbox_id)
print(f"total components in sandbox: {len(components_response)}")
- Note the use of nested context managers to manage the different session tokens
License
Free Software: MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for cloudshell-sandbox-rest-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24bf8b514d3256db6a0cf834024166d859815f7210337c2d7d7e969947d7b662 |
|
MD5 | a918fc491332dc10ab4b536a7d083c4e |
|
BLAKE2b-256 | 7cf6fe42ff98e95dd8165254c3194bd1d125b91f790e41bf831e0620b7fc9ead |
Close
Hashes for cloudshell_sandbox_rest-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cad55e42897266dd926cdca2fff0d42f5f5bfc28f610a6e4ff266808b214e84b |
|
MD5 | 2b4c89d180bfe814d838205cd4edfe63 |
|
BLAKE2b-256 | 53a135a336d0ff63d4067a0b2d7d2b89008aaac451e850182c03b1918753d3c5 |