Skip to content

Commit

Permalink
fix issue in async samples
Browse files Browse the repository at this point in the history
  • Loading branch information
leti367 committed May 14, 2024
1 parent 1614983 commit 4fde303
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@


async def environment_create_and_delete_async():
# [START environment_create_and_delete]
# [START environment_create_and_delete_async]
# Set the values of the dev center endpoint, client ID, and client secret of the AAD application as environment variables:
# DEVCENTER_ENDPOINT, AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET
try:
Expand All @@ -59,86 +59,87 @@ async def environment_create_and_delete_async():
# Build a client through AAD
client = DevCenterClient(endpoint, credential=DefaultAzureCredential())

# List available Projects
projects = []
async for project in client.list_projects():
projects.append(project)
if projects:
print("\nList of projects: ")
for project in projects:
print(f"{project.name}")

# Select first project in the list
target_project_name = projects[0].name
else:
raise ValueError("Missing Project - please create one before running the example")

# List available Catalogs
catalogs = []
async for catalog in client.list_catalogs(target_project_name):
catalogs.append(catalog)
if catalogs:
print("\nList of catalogs: ")
for catalog in catalogs:
print(f"{catalog.name}")

# Select first catalog in the list
target_catalog_name = catalogs[0].name
else:
raise ValueError("Missing Catalog - please create one before running the example")

# List available Environment Definitions
environment_definitions = []
async for environment_definition in client.list_environment_definitions_by_catalog(target_project_name, target_catalog_name):
environment_definitions.append(environment_definition)
if environment_definitions:
print("\nList of environment definitions: ")
for environment_definition in environment_definitions:
print(f"{environment_definition.name}")

# Select first environment definition in the list
target_environment_definition_name = environment_definitions[0].name
else:
raise ValueError("Missing Environment Definition - please create one before running the example")

# List available Environment Types
environment_types = []
async for environment_type in client.list_environment_types(target_project_name):
environment_types.append(environment_type)
if environment_types:
print("\nList of environment types: ")
for environment_type in environment_types:
print(f"{environment_type.name}")

# Select first environment type in the list
target_environment_type_name = environment_types[0].name
else:
raise ValueError("Missing Environment Type - please create one before running the example")

print(
f"\nStarting to create environment in project {target_project_name} with catalog {target_catalog_name}, environment definition {target_environment_definition_name}, and environment type {target_environment_type_name}."
)

# Stand up a new environment
environment_name = "MyDevEnv"
environment = {
"environmentType": target_environment_type_name,
"catalogName": target_catalog_name,
"environmentDefinitionName": target_environment_definition_name,
}

environment_poller = await client.begin_create_or_update_environment(
target_project_name, "me", environment_name, environment
)
environment_result = await environment_poller.result()
print(f"Provisioned environment with status {environment_result.provisioning_state}.")

# Tear down the environment when finished
print(f"Starting to delete environment.")
delete_poller = await client.begin_delete_environment(target_project_name, "me", environment_name)
delete_result = await delete_poller.result()
print(f"Completed deletion for the environment with status {delete_result.status}")
# [END environment_create_and_delete]
async with client:
# List available Projects
projects = []
async for project in client.list_projects():
projects.append(project)
if projects:
print("\nList of projects: ")
for project in projects:
print(f"{project.name}")

# Select first project in the list
target_project_name = projects[0].name
else:
raise ValueError("Missing Project - please create one before running the example")

# List available Catalogs
catalogs = []
async for catalog in client.list_catalogs(target_project_name):
catalogs.append(catalog)
if catalogs:
print("\nList of catalogs: ")
for catalog in catalogs:
print(f"{catalog.name}")

# Select first catalog in the list
target_catalog_name = catalogs[0].name
else:
raise ValueError("Missing Catalog - please create one before running the example")

# List available Environment Definitions
environment_definitions = []
async for environment_definition in client.list_environment_definitions_by_catalog(target_project_name, target_catalog_name):
environment_definitions.append(environment_definition)
if environment_definitions:
print("\nList of environment definitions: ")
for environment_definition in environment_definitions:
print(f"{environment_definition.name}")

# Select first environment definition in the list
target_environment_definition_name = environment_definitions[0].name
else:
raise ValueError("Missing Environment Definition - please create one before running the example")

# List available Environment Types
environment_types = []
async for environment_type in client.list_environment_types(target_project_name):
environment_types.append(environment_type)
if environment_types:
print("\nList of environment types: ")
for environment_type in environment_types:
print(f"{environment_type.name}")

# Select first environment type in the list
target_environment_type_name = environment_types[0].name
else:
raise ValueError("Missing Environment Type - please create one before running the example")

print(
f"\nStarting to create environment in project {target_project_name} with catalog {target_catalog_name}, environment definition {target_environment_definition_name}, and environment type {target_environment_type_name}."
)

# Stand up a new environment
environment_name = "MyDevEnv"
environment = {
"environmentType": target_environment_type_name,
"catalogName": target_catalog_name,
"environmentDefinitionName": target_environment_definition_name,
}

environment_poller = await client.begin_create_or_update_environment(
target_project_name, "me", environment_name, environment
)
environment_result = await environment_poller.result()
print(f"Provisioned environment with status {environment_result.provisioning_state}.")

# Tear down the environment when finished
print(f"Starting to delete environment.")
delete_poller = await client.begin_delete_environment(target_project_name, "me", environment_name)
delete_result = await delete_poller.result()
print(f"Completed deletion for the environment with status {delete_result.status}")
# [END environment_create_and_delete_async]

async def main():
await environment_create_and_delete_async()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,37 +59,38 @@ async def dev_box_action_async():
# Build a client through AAD
client = DevCenterClient(endpoint, credential=DefaultAzureCredential())

# List Dev Boxes
dev_boxes = []
async for dev_box in client.list_all_dev_boxes_by_user("me"):
dev_boxes.append(dev_box)
if dev_boxes:
print("List of dev boxes: ")
for dev_box in dev_boxes:
print(f"{dev_box.name}")
async with client:
# List Dev Boxes
dev_boxes = []
async for dev_box in client.list_all_dev_boxes_by_user("me"):
dev_boxes.append(dev_box)
if dev_boxes:
print("List of dev boxes: ")
for dev_box in dev_boxes:
print(f"{dev_box.name}")

# Select first dev box in the list
target_dev_box = dev_boxes[0]
else:
raise ValueError("Missing Dev Box - please create one before running the example.")
# Select first dev box in the list
target_dev_box = dev_boxes[0]
else:
raise ValueError("Missing Dev Box - please create one before running the example.")

# Get the schedule default action. This action should exist for dev boxes created with auto-stop enabled
action = await client.get_dev_box_action(target_dev_box.project_name, "me", target_dev_box.name, "schedule-default")
next_action_time = action.next.scheduled_time
print(f"\nAction {action.Name} is schedule to {action.ActionType} at {next_action_time}.")
# Get the schedule default action. This action should exist for dev boxes created with auto-stop enabled
action = await client.get_dev_box_action(target_dev_box.project_name, "me", target_dev_box.name, "schedule-default")
next_action_time = action.next.scheduled_time
print(f"\nAction {action.Name} is schedule to {action.ActionType} at {next_action_time}.")

# Delay the action in 1hr
delay_until = next_action_time + timedelta(hours=1)
delayed_action = await client.delay_dev_box_action(
target_dev_box.project_name, "me", target_dev_box.name, action.name, delay_until=delay_until
)
print(
f"\nAction {delayed_action.Name} has been delayed and is now schedule to {delayed_action.ActionType} at {delayed_action.NextAction.ScheduledTime}."
)
# Delay the action in 1hr
delay_until = next_action_time + timedelta(hours=1)
delayed_action = await client.delay_dev_box_action(
target_dev_box.project_name, "me", target_dev_box.name, action.name, delay_until=delay_until
)
print(
f"\nAction {delayed_action.Name} has been delayed and is now schedule to {delayed_action.ActionType} at {delayed_action.NextAction.ScheduledTime}."
)

# Skip the default schedule action
await client.skip_dev_box_action(target_dev_box.project_name, "me", target_dev_box.name, "schedule-default")
print(f"\nThe scheduled auto-stop action in dev box {target_dev_box.name} has been skipped")
# Skip the default schedule action
await client.skip_dev_box_action(target_dev_box.project_name, "me", target_dev_box.name, "schedule-default")
print(f"\nThe scheduled auto-stop action in dev box {target_dev_box.name} has been skipped")

async def main():
await dev_box_action_async()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from azure.identity import DefaultAzureCredential

async def dev_box_create_connect_delete_async():
# [START dev_box_create_connect_delete]
# [START dev_box_create_connect_delete_async]
# Set the values of the dev center endpoint, client ID, and client secret of the AAD application as environment variables:
# DEVCENTER_ENDPOINT, AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET
try:
Expand All @@ -57,54 +57,55 @@ async def dev_box_create_connect_delete_async():
# Build a client through AAD
client = DevCenterClient(endpoint, credential=DefaultAzureCredential())

# List available Projects
projects = []
async for project in client.list_projects():
projects.append(project)
if projects:
print("\nList of projects: ")
for project in projects:
print(f"{project.name}")

# Select first project in the list
target_project_name = projects[0].name
else:
raise ValueError("Missing Project - please create one before running the example")

# List available Pools
pools = []
async for pool in client.list_pools(target_project_name):
pools.append(pool)
if pools:
print("\nList of pools: ")
for pool in pools:
print(f"{pool.name}")

# Select first pool in the list
target_pool_name = pools[0].name
else:
raise ValueError("Missing Pool - please create one before running the example")

# Stand up a new Dev Box
print(f"\nStarting to create dev box in project {target_project_name} and pool {target_pool_name}")

dev_box_poller = await client.begin_create_dev_box(
target_project_name, "me", "Test_DevBox", {"poolName": target_pool_name}
)
dev_box = await dev_box_poller.result()
print(f"Provisioned dev box with status {dev_box.provisioning_state}.")

# Connect to the provisioned Dev Box
remote_connection = await client.get_remote_connection(target_project_name, "me", dev_box.name)
print(f"Connect to the dev box using web URL {remote_connection.web_url}")

# Tear down the Dev Box when finished
print(f"Starting to delete dev box.")

delete_poller = await client.begin_delete_dev_box(target_project_name, "me", "Test_DevBox")
delete_result = await delete_poller.result()
print(f"Completed deletion for the dev box with status {delete_result.status}")
# [END dev_box_create_connect_delete]
async with client:
# List available Projects
projects = []
async for project in client.list_projects():
projects.append(project)
if projects:
print("\nList of projects: ")
for project in projects:
print(f"{project.name}")

# Select first project in the list
target_project_name = projects[0].name
else:
raise ValueError("Missing Project - please create one before running the example")

# List available Pools
pools = []
async for pool in client.list_pools(target_project_name):
pools.append(pool)
if pools:
print("\nList of pools: ")
for pool in pools:
print(f"{pool.name}")

# Select first pool in the list
target_pool_name = pools[0].name
else:
raise ValueError("Missing Pool - please create one before running the example")

# Stand up a new Dev Box
print(f"\nStarting to create dev box in project {target_project_name} and pool {target_pool_name}")

dev_box_poller = await client.begin_create_dev_box(
target_project_name, "me", "Test_DevBox", {"poolName": target_pool_name}
)
dev_box = await dev_box_poller.result()
print(f"Provisioned dev box with status {dev_box.provisioning_state}.")

# Connect to the provisioned Dev Box
remote_connection = await client.get_remote_connection(target_project_name, "me", dev_box.name)
print(f"Connect to the dev box using web URL {remote_connection.web_url}")

# Tear down the Dev Box when finished
print(f"Starting to delete dev box.")

delete_poller = await client.begin_delete_dev_box(target_project_name, "me", "Test_DevBox")
delete_result = await delete_poller.result()
print(f"Completed deletion for the dev box with status {delete_result.status}")
# [END dev_box_create_connect_delete_async]

async def main():
await dev_box_create_connect_delete_async()
Expand Down
Loading

0 comments on commit 4fde303

Please sign in to comment.