1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
# Azure DevCenter Service client library for Python
The Azure DevCenter package provides access to manage resources for Microsoft Dev Box and Azure Deployment Environments. This SDK enables managing developer machines and environments in Azure.
Use the package for Azure DevCenter to:
> Create, access, manage, and delete Dev Box resources
> Create, deploy, manage, and delete Environment resources
## Getting started
### Installing the package
```bash
python -m pip install azure-developer-devcenter
```
### Prerequisites
- Python 3.7 or later is required to use this package.
- You need an [Azure subscription][azure_sub] to use this package.
- For Dev Box operations you must have [configured](https://learn.microsoft.com/azure/dev-box/quickstart-configure-dev-box-service) a DevCenter, Project, Network Connection, Dev Box Definition, and Pool.
- For Deployment Environments operations you must have [configured](https://learn.microsoft.com/azure/deployment-environments/) a DevCenter, Project, Catalog, Environment Definition and Environment Type.
### Create Client with an Azure Active Directory Credential
In order to interact with the Dev Center service, you will need to create an instance of a client. An **endpoint** and **credential** are necessary to instantiate the client object.
For the endpoint use the Dev Center URI. It should have the format `https://{tenantId}-{devCenterName}.{devCenterRegion}.devcenter.azure.com`.
For the credential use an [Azure Active Directory (AAD) token credential][authenticate_with_token], providing an instance of the desired credential type obtained from the [azure-identity][azure_identity_credentials] library.
To authenticate with AAD, you must first install [azure-identity][azure_identity_pip] using [pip][pip]
```bash
pip install azure-identity
```
After setup, you can choose which type of [credential][azure_identity_credentials] from azure.identity to use.
As an example, [DefaultAzureCredential][default_azure_credential] can be used to authenticate the client:
Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables:
`AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`
Use the returned token credential to authenticate the client.
<!-- SNIPPET:create_client_sample.create_dev_center_client -->
```python
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())
```
<!-- END SNIPPET -->
With `DevCenterClient` you can execute operations in [Dev Center, Dev Box and Environments REST operations group](https://learn.microsoft.com/rest/api/devcenter/developer/operation-groups).
## Examples
* [Create, Connect and Delete a Dev Box](#create-connect-and-delete-a-dev-box)
* [Deploy and Delete an Environment](#deploy-and-delete-an-environment)
### Create, Connect and Delete a Dev Box
<!-- SNIPPET:dev_box_create_sample.dev_box_create_connect_delete -->
```python
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}")
```
<!-- END SNIPPET -->
### Deploy and Delete an Environment
<!-- SNIPPET:deployment_environments_sample.environment_create_and_delete -->
```python
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}")
```
<!-- END SNIPPET -->
## Key concepts
Dev Boxes refer to managed developer machines running in Azure. Dev Boxes are provisioned in Pools, which define the network and image used for a Dev Box.
Environments refer to templated developer environments, which combine a template (Catalog Item) and parameters.
## Troubleshooting
Errors can occur during initial requests and long-running operations, and will provide information about how to resolve the error.
Be sure to confirm that dependent resources, such as pools and catalogs, are set up properly and are in a healthy state. You will not be able to create resources with the package when your dependent resources are in a failed state.
## Next steps
Get started by exploring our [samples][samples_folder] and starting to use the package!
## Contributing
This project welcomes contributions and suggestions. Most contributions require
you to agree to a Contributor License Agreement (CLA) declaring that you have
the right to, and actually do, grant us the rights to use your contribution.
For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether
you need to provide a CLA and decorate the PR appropriately (e.g., label,
comment). Simply follow the instructions provided by the bot. You will only
need to do this once across all repos using our CLA.
This project has adopted the
[Microsoft Open Source Code of Conduct][code_of_conduct]. For more information,
see the Code of Conduct FAQ or contact opencode@microsoft.com with any
additional questions or comments.
<!-- LINKS -->
[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/
[authenticate_with_token]: https://docs.microsoft.com/azure/cognitive-services/authentication?tabs=powershell#authenticate-with-an-authentication-token
[azure_identity_credentials]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#credentials
[azure_identity_pip]: https://pypi.org/project/azure-identity/
[default_azure_credential]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#defaultazurecredential
[pip]: https://pypi.org/project/pip/
[azure_sub]: https://azure.microsoft.com/free/
[samples_folder]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/devcenter/azure-developer-devcenter/samples
|