|
| 1 | + |
| 2 | +# Azure DevCenter Service client library for Python |
| 3 | +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. |
| 4 | + |
| 5 | +Use the package for Azure DevCenter to: |
| 6 | +> Create, access, manage, and delete Dev Box resources |
| 7 | +> Create, deploy, manage, and delete Environment resources |
| 8 | +
|
| 9 | +## Getting started |
| 10 | + |
| 11 | +### Installating the package |
| 12 | + |
| 13 | +```bash |
| 14 | +python -m pip install azure-developer-devcenter |
| 15 | +``` |
| 16 | + |
| 17 | +#### Prequisites |
| 18 | + |
| 19 | +- Python 3.7 or later is required to use this package. |
| 20 | +- You need an [Azure subscription][azure_sub] to use this package. |
| 21 | +- 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 before you can create Dev Boxes |
| 22 | +- You must have [configured](https://learn.microsoft.com/azure/deployment-environments/) a DevCenter, Project, Catalog, and Environment Type before you can create Environments |
| 23 | + |
| 24 | +#### Create with an Azure Active Directory Credential |
| 25 | +To use an [Azure Active Directory (AAD) token credential][authenticate_with_token], |
| 26 | +provide an instance of the desired credential type obtained from the |
| 27 | +[azure-identity][azure_identity_credentials] library. |
| 28 | + |
| 29 | +To authenticate with AAD, you must first [pip][pip] install [`azure-identity`][azure_identity_pip] |
| 30 | + |
| 31 | +After setup, you can choose which type of [credential][azure_identity_credentials] from azure.identity to use. |
| 32 | +As an example, [DefaultAzureCredential][default_azure_credential] can be used to authenticate the client: |
| 33 | + |
| 34 | +Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables: |
| 35 | +`AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET` |
| 36 | + |
| 37 | +Use the returned token credential to authenticate the client: |
| 38 | + |
| 39 | +```python |
| 40 | +>>> import os |
| 41 | +>>> from azure.developer.devcenter import DevCenterClient |
| 42 | +>>> from azure.identity import DefaultAzureCredential |
| 43 | +>>> tenant_id = os.environ['AZURE_TENANT_ID'] |
| 44 | +>>> client = DevCenterClient(tenant_id=tenant_id, dev_center="my_dev_center", credential=DefaultAzureCredential()) |
| 45 | +``` |
| 46 | + |
| 47 | +## Examples |
| 48 | + |
| 49 | +### Dev Box Management |
| 50 | +```python |
| 51 | +>>> import os |
| 52 | +>>> from azure.developer.devcenter import DevCenterClient |
| 53 | +>>> from azure.identity import DefaultAzureCredential |
| 54 | +>>> from azure.core.exceptions import HttpResponseError |
| 55 | +>>> tenant_id = os.environ['AZURE_TENANT_ID'] |
| 56 | +>>> client = DevCenterClient(tenant_id=tenant_id, dev_center="my_dev_center", credential=DefaultAzureCredential()) |
| 57 | +>>> try: |
| 58 | + # Fetch control plane resource dependencies |
| 59 | + projects = list(client.dev_center.list_projects(top=1)) |
| 60 | + target_project_name = projects[0]['name'] |
| 61 | + |
| 62 | + pools = list(client.dev_boxes.list_pools(target_project_name, top=1)) |
| 63 | + target_pool_name = pools[0]['name'] |
| 64 | + |
| 65 | + # Stand up a new dev box |
| 66 | + create_response = client.dev_boxes.begin_create_dev_box(target_project_name, "Test_DevBox", {"poolName": target_pool_name}) |
| 67 | + devbox_result = create_response.result() |
| 68 | + |
| 69 | + LOG.info(f"Provisioned dev box with status {devbox_result['provisioningState']}.") |
| 70 | + |
| 71 | + # Connect to the provisioned dev box |
| 72 | + remote_connection_response = client.dev_boxes.get_remote_connection(target_project_name, "Test_DevBox") |
| 73 | + LOG.info(f"Connect to the dev box using web URL {remote_connection_response['webUrl']}") |
| 74 | + |
| 75 | + # Tear down the dev box when finished |
| 76 | + delete_response = client.dev_boxes.begin_delete_dev_box(target_project_name, "Test_DevBox") |
| 77 | + delete_response.wait() |
| 78 | + LOG.info("Deleted dev box successfully.") |
| 79 | + except HttpResponseError as e: |
| 80 | + print('service responds error: {}'.format(e.response.json())) |
| 81 | + |
| 82 | +``` |
| 83 | + |
| 84 | +### Environment Management |
| 85 | +```python |
| 86 | +>>> import os |
| 87 | +>>> from azure.developer.devcenter import DevCenterClient |
| 88 | +>>> from azure.identity import DefaultAzureCredential |
| 89 | +>>> from azure.core.exceptions import HttpResponseError |
| 90 | +>>> tenant_id = os.environ['AZURE_TENANT_ID'] |
| 91 | +>>> client = DevCenterClient(tenant_id=tenant_id, dev_center="my_dev_center", credential=DefaultAzureCredential()) |
| 92 | +>>> try: |
| 93 | + # Fetch control plane resource dependencies |
| 94 | + target_project_name = list(client.dev_center.list_projects(top=1))[0]['name'] |
| 95 | + target_catalog_item_name = list(client.environments.list_catalog_items(target_project_name, top=1))[0]['name'] |
| 96 | + target_environment_type_name = list(client.environments.list_environment_types(target_project_name, top=1))[0]['name'] |
| 97 | + |
| 98 | + # Stand up a new environment |
| 99 | + create_response = client.environments.begin_create_environment(target_project_name, |
| 100 | + "Dev_Environment", |
| 101 | + {"catalogItemName": target_catalog_item_name, "environmentType": target_environment_type_name}) |
| 102 | + environment_result = create_response.result() |
| 103 | + |
| 104 | + LOG.info(f"Provisioned environment with status {environment_result['provisioningState']}.") |
| 105 | + |
| 106 | + # Fetch deployment artifacts |
| 107 | + artifact_response = client.environments.list_artifacts_by_environment(target_project_name, "Dev_Environment") |
| 108 | + |
| 109 | + for artifact in artifact_response: |
| 110 | + LOG.info(artifact) |
| 111 | + |
| 112 | + # Tear down the environment when finished |
| 113 | + delete_response = client.environments.begin_delete_environment(target_project_name, "Dev_Environment") |
| 114 | + delete_response.wait() |
| 115 | + LOG.info("Completed deletion for the environment.") |
| 116 | + except HttpResponseError as e: |
| 117 | + print('service responds error: {}'.format(e.response.json())) |
| 118 | + |
| 119 | +``` |
| 120 | +## Key concepts |
| 121 | +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. |
| 122 | + |
| 123 | +Environments refer to templated developer environments, which combine a template (Catalog Item) and parameters. |
| 124 | + |
| 125 | +## Troubleshooting |
| 126 | +Errors can occur during initial requests and long-running operations, and will provide information about how to resolve the error. |
| 127 | +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. |
| 128 | + |
| 129 | +## Next steps |
| 130 | +Get started by exploring our samples and starting to use the package! |
| 131 | + |
| 132 | +## Contributing |
| 133 | + |
| 134 | +This project welcomes contributions and suggestions. Most contributions require |
| 135 | +you to agree to a Contributor License Agreement (CLA) declaring that you have |
| 136 | +the right to, and actually do, grant us the rights to use your contribution. |
| 137 | +For details, visit https://cla.microsoft.com. |
| 138 | + |
| 139 | +When you submit a pull request, a CLA-bot will automatically determine whether |
| 140 | +you need to provide a CLA and decorate the PR appropriately (e.g., label, |
| 141 | +comment). Simply follow the instructions provided by the bot. You will only |
| 142 | +need to do this once across all repos using our CLA. |
| 143 | + |
| 144 | +This project has adopted the |
| 145 | +[Microsoft Open Source Code of Conduct][code_of_conduct]. For more information, |
| 146 | +see the Code of Conduct FAQ or contact [email protected] with any |
| 147 | +additional questions or comments. |
| 148 | + |
| 149 | +<!-- LINKS --> |
| 150 | +[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/ |
| 151 | +[authenticate_with_token]: https://docs.microsoft.com/azure/cognitive-services/authentication?tabs=powershell#authenticate-with-an-authentication-token |
| 152 | +[azure_identity_credentials]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#credentials |
| 153 | +[azure_identity_pip]: https://pypi.org/project/azure-identity/ |
| 154 | +[default_azure_credential]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#defaultazurecredential |
| 155 | +[pip]: https://pypi.org/project/pip/ |
| 156 | +[azure_sub]: https://azure.microsoft.com/free/ |
0 commit comments