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
|
This document currently details Python commands for building Azure Cosmos SDK.
### Fork and clone the repository
To build and develop locally, fork and clone the [Azure Cosmos DB SDK repository][cosmos_db_sdk_repo]
### Setup SDK with Pycharm
#### Prerequisites
* [Pycharm][pycharm]
* Azure subscription - [Create a free account][azure_sub]
* Azure [Cosmos DB account][cosmos_account] - SQL API
* [Python 3.6+][python]
#### Add Interpreter
Your Python interpreter might not be the right version(**Python 3.6+**). Change the Python versions in Pycharm.
1. Add local interpreter

2. Select the right interpreter version

#### Install dependent Packages
1. On the bottom left tool bar, go to `Python Packages`
2. Goto `Add Package` > `From Disk`

3. Add Path to dependent packages from disk
* `/<PATH_TO_CLONED_REPO>/azure-sdk-for-python/sdk/cosmos/azure-cosmos`
* azure-core
* azure-cosmos
* `/<PATH_TO_CLONED_REPO>/azure-sdk-for-python/sdk/identity/azure-identity`
* `/<PATH_TO_CLONED_REPO>/azure-sdk-for-python/tools/azure-sdk-tools`

4. Add Path to dependent packages from `PyPl`
* pytest
* pyrebase
* aiohttp

#### Run Cosmos DB Emulator
Azure CosmosDB Emulator is required to run unit tests.
However, the emulator is not ready on MAC OSX yet. Please follow the instructions below to run unit tests on MAC OSX
##### On Windows
1. Download [Azure Cosmos DB emulator][cosmos_db_emulator]
2. Run emulator
##### On MAC
**<u>NOTE:</u>** Unfortunately, Azure Cosmos DB Emulator is not supported on Mac OS. Alternatively, you can manually modify some config variables in `azure-cosmos/test/test_config.py` from your personal Cosmos DB accounts to run unit tests.
**<u>WARNING:</u>** Do not commit your updated `test_config.py`. Always revert the changes before pushing your commit.
1. Open `test_config.py`
2. Replace the default values of `ACCOUNT_KEY` and `ACCOUNT_HOST` to the values from Azure Cosmos DB account(Create new Cosmos DB account if you don't have any)

- `ACCOUNT_KEY`: Primary key from the keys from `Settings` in Azure Cosmos DB account home

- `ACCOUNT_HOST`: URI from the overview page of Azure Cosmos DB account

3. Update the usage of `credential` in the `setUpClass` in `test_query.py`
- `cls.credential` -> `cls.config.masterKey`

To run aad tests, follow the steps below to create `RoleAssignment` that uses the `RoleDefinition`
1. Save the following content into a JSON file named `expandedAction.json`
```json
{
"RoleName": "ExpandedRBACActions",
"Type": "CustomRole",
"AssignableScopes": ["/"],
"Permissions": [{
"DataActions": [
"Microsoft.DocumentDB/databaseAccounts/readMetadata",
"Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/*",
"Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/*"
]
}]
}
```
2. Run the following commands:
- Set account details
- `<subscriptionId>`: Subscription ID to `CosmosDB-SDK-Dev`
- `<resourceGroupName>`: Resource name of your Cosmos DB account
- `<accountName>`: Your Cosmos DB account name
```shell
subscriptionId='<subscriptionId>'
resourceGroupName='<resourceGroupName>'
accountName='<accountName>'
```
- Set subscription
```shell
az account set --subscription $subscriptionId
```
- Create the RoleDefinition
```shell
az cosmosdb sql role definition create --account-name $accountName --resource-group $resourceGroupName --body expandedActions.json
```
- Get the principalId associated with you Azure account. Replace with any other principalId if necessary. You can also get this value from Azure Portal, by visiting the EntraId Users page. It is called 'Object Id' there.
```shell
az ad signed-in-user show --query id -o tsv
```
- Set principalId to a variable
- `<principalId>`: The returned id from the command above
```shell
principalId=<principalId>
```
- Create a RoleAssignment for the principalId that uses the RoleDefinition created above
```shell
az cosmosdb sql role assignment create --account-name $accountName --resource-group $resourceGroupName --role-definition-name "ExpandedRBACActions" --scope "/" --principal-id $principalId
```
#### Run unit tests
The unit tests can be run by right-clicking a specific test file or specific test function in test files
- Run all tests on a test file

- Run a single test on a test file

<!-- LINKS -->
[cosmos_db_sdk_repo]: https://github.com/Azure/azure-sdk-for-python
[azure_sub]: https://azure.microsoft.com/free/
[cosmos_account]: https://learn.microsoft.com/azure/cosmos-db/account-overview
[python]: https://www.python.org/downloads/
[pycharm]: https://www.jetbrains.com/pycharm/
[cosmos_db_emulator]: https://learn.microsoft.com/azure/cosmos-db/emulator
|