Microsoft Azure Developer Devcenter Python客户端库
项目描述
Azure DevCenter服务Python客户端库
Azure DevCenter包提供了访问管理Microsoft Dev Box和Azure部署环境资源的功能。此SDK可以用于在Azure中管理开发机器和环境。
使用Azure DevCenter包进行以下操作:
创建、访问、管理和删除Dev Box资源 创建、部署、管理和删除环境资源
入门
安装包
python -m pip install azure-developer-devcenter
先决条件
- 使用此包需要Python 3.7或更高版本。
- 您需要Azure订阅才能使用此包。
- 对于Dev Box操作,您必须已经配置了DevCenter、项目、网络连接、Dev Box定义和池。
- 对于部署环境操作,您必须已配置 DevCenter、项目、目录、环境定义和环境类型。
使用 Azure Active Directory 凭据创建客户端
为了与 Dev Center 服务交互,您需要创建一个客户端实例。实例化客户端对象需要 端点 和 凭证。
对于端点,使用 Dev Center URI。其格式应为 https://{tenantId}-{devCenterName}.{devCenterRegion}.devcenter.azure.com
。
对于凭证,使用 Azure Active Directory (AAD) 令牌凭证,提供从 azure-identity 库获得的所需凭证类型的实例。
要使用 AAD 进行身份验证,您必须首先使用 azure-identity 通过 pip 安装
pip install azure-identity
设置完成后,您可以从 azure.identity 中选择要使用的 凭证 类型。
例如,可以使用 DefaultAzureCredential 来对客户端进行身份验证:将 AAD 应用程序的客户端 ID、租户 ID 和客户端密钥的值设置为环境变量:AZURE_CLIENT_ID
、AZURE_TENANT_ID
、AZURE_CLIENT_SECRET
使用返回的令牌凭证对客户端进行身份验证。
import os
from azure.developer.devcenter import DevCenterClient
from azure.identity import DefaultAzureCredential
# 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:
endpoint = os.environ["DEVCENTER_ENDPOINT"]
except KeyError:
raise ValueError("Missing environment variable 'DEVCENTER_ENDPOINT' - please set it before running the example")
# Build a client through AAD
client = DevCenterClient(endpoint, credential=DefaultAzureCredential())
使用 DevCenterClient
可以在 Dev Center、Dev Box 和环境 REST 操作组 中执行操作。
示例
创建、连接和删除 Dev Box
import os
from azure.developer.devcenter import DevCenterClient
from azure.identity import DefaultAzureCredential
# 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:
endpoint = os.environ["DEVCENTER_ENDPOINT"]
except KeyError:
raise ValueError("Missing environment variable 'DEVCENTER_ENDPOINT' - please set it before running the example")
# Build a client through AAD
client = DevCenterClient(endpoint, credential=DefaultAzureCredential())
# List available Projects
projects = client.list_projects()
if projects:
print("\nList of projects: ")
for project in projects:
print(f"{project.name}")
# Select first project in the list
target_project_name = list(projects)[0].name
else:
raise ValueError("Missing Project - please create one before running the example")
# List available Pools
pools = client.list_pools(target_pool_name)
if pools:
print("\nList of pools: ")
for pool in pools:
print(f"{pool.name}")
# Select first pool in the list
target_pool_name = list(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 = client.begin_create_dev_box(
target_project_name, "me", "Test_DevBox", {"poolName": target_pool_name}
)
dev_box = dev_box_poller.result()
print(f"Provisioned dev box with status {dev_box.provisioning_state}.")
# Connect to the provisioned Dev Box
remote_connection = 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 = client.begin_delete_dev_box(target_project_name, "me", "Test_DevBox")
delete_result = delete_poller.result()
print(f"Completed deletion for the dev box with status {delete_result.status}")
部署和删除环境
import os
from azure.developer.devcenter import DevCenterClient
from azure.identity import DefaultAzureCredential
# 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:
endpoint = os.environ["DEVCENTER_ENDPOINT"]
except KeyError:
raise ValueError("Missing environment variable 'DEVCENTER_ENDPOINT' - please set it before running the example")
# Build a client through AAD
client = DevCenterClient(endpoint, credential=DefaultAzureCredential())
# List available Projects
projects = client.list_projects()
if projects:
print("\nList of projects: ")
for project in projects:
print(f"{project.name}")
# Select first project in the list
target_project_name = list(projects)[0].name
else:
raise ValueError("Missing Project - please create one before running the example")
# List available Catalogs
catalogs = client.list_catalogs(target_project_name)
if catalogs:
print("\nList of catalogs: ")
for catalog in catalogs:
print(f"{catalog.name}")
# Select first catalog in the list
target_catalog_name = list(catalogs)[0].name
else:
raise ValueError("Missing Catalog - please create one before running the example")
# List available Environment Definitions
environment_definitions = client.list_environment_definitions_by_catalog(target_project_name, target_catalog_name)
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 = list(environment_definitions)[0].name
else:
raise ValueError("Missing Environment Definition - please create one before running the example")
# List available Environment Types
environment_types = client.list_environment_types(target_project_name)
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 = list(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 = client.begin_create_or_update_environment(
target_project_name, "me", environment_name, environment
)
environment_result = 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 = client.begin_delete_environment(target_project_name, "me", environment_name)
delete_result = delete_poller.result()
print(f"Completed deletion for the environment with status {delete_result.status}")
关键概念
Dev Boxes 指的是在 Azure 中运行的托管开发人员机器。Dev Boxes 在池中配置,池定义了 Dev Box 的网络和映像。
环境是指模板化的开发环境,它结合了模板(目录项)和参数。
故障排除
在初始请求和长时间运行的操作期间可能会发生错误,并将提供有关如何解决错误的信息。请确保相关资源(如池和目录)已正确设置,并且处于良好状态。如果您的依赖资源处于失败状态,则无法使用包创建资源。
下一步
通过探索我们的 示例 并开始使用该包来入门!
贡献
此项目欢迎贡献和建议。大多数贡献都需要您同意贡献者许可协议 (CLA),声明您有权并且实际上确实授予我们使用您的贡献的权利。有关详细信息,请访问 https://cla.microsoft.com。
当您提交拉取请求时,CLA-bot 将自动确定您是否需要提供 CLA,并适当地装饰 PR(例如,标签、注释)。只需遵循机器人提供的说明即可。您只需在整个使用我们的 CLA 的所有存储库中执行此操作一次。
此项目采用了 Microsoft 开源行为准则。有关更多信息,请参阅行为准则 FAQ 或联系 opencode@microsoft.com 以提出任何额外的问题或评论。
项目详细信息
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。
源分发
构建分发
散列值 for azure_developer_devcenter-1.0.0-py3-none-any.whl
算法 | 散列摘要 | |
---|---|---|
SHA256 | 77f2913662e358e6e6aaaf194bbda5921f0e5a9015a0f719b251696c4bb3b9b2 |
|
MD5 | 4c4637b7e49fb18795985ae581f923f1 |
|
BLAKE2b-256 | 8b89976ff1d6da35d6be1a042aa801cdb2596c5d1a7251658656c69d2f2524f4 |