使用 Azure CLI 为 Azure Cosmos Gremlin API 数据库和图形创建资源锁Create a resource lock for Azure Cosmos Gremlin API database and graph using Azure CLI

备注

在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

选择在本地安装并使用 CLI 时,本主题要求运行 Azure CLI 2.6.0 或更高版本。When you choose to install and use the CLI locally, this topic requires that you are running the Azure CLI version 2.6.0 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

重要

对于使用任何 Gremlin SDK 或 Azure 门户进行连接的用户所做的更改,除非先在启用了 disableKeyBasedMetadataWriteAccess 属性的情况下锁定 Cosmos DB 帐户,否则资源锁无效。Resource locks do not work for changes made by users connecting using any Gremlin SDK or the Azure Portal unless the Cosmos DB account is first locked with the disableKeyBasedMetadataWriteAccess property enabled. 若要详细了解如何启用此属性,请参阅防止 SDK 更改To learn more about how to enable this property see, Preventing changes from SDKs.

示例脚本Sample script

#!/bin/bash
resourceGroupName='myResourceGroup'
accountName='my-cosmos-account'
databaseName='database1'
graphName='graph1'

lockType='CanNotDelete' # CanNotDelete or ReadOnly
databaseParent="databaseAccounts/$accountName"
graphParent="databaseAccounts/$accountName/gremlinDatabases/$databaseName"
databaseLockName="$databaseName-Lock"
graphLockName="$containerName-Lock"

# Create a delete lock on database
az lock create --name $databaseLockName \
    --resource-group $resourceGroupName \
    --resource-type Microsoft.DocumentDB/gremlinDatabases \
    --lock-type $lockType \
    --parent $databaseParent \
    --resource $databaseName

# Create a delete lock on graph
az lock create --name $graphLockName \
    --resource-group $resourceGroupName \
    --resource-type Microsoft.DocumentDB/graphs \
    --lock-type $lockType \
    --parent $graphParent \
    --resource $graphName

# List all locks on a Cosmos account
az lock list --resource-group $resourceGroupName \
    --resource-name $accountName \
    --namespace Microsoft.DocumentDB \
    --resource-type databaseAccounts

# Delete lock on database
lockid=$(az lock show --name $databaseLockName \
        --resource-group $resourceGroupName \
        --resource-type Microsoft.DocumentDB/gremlinDatabases \
        --resource $databaseName --parent $databaseParent \
        --output tsv --query id)
az lock delete --ids $lockid

# Delete lock on graph
lockid=$(az lock show --name $graphLockName \
        --resource-group $resourceGroupName \
        --resource-type Microsoft.DocumentDB/graphs \
        --resource-name $graphName --parent $graphParent \
        --output tsv --query id)
az lock delete --ids $lockid

脚本说明Script explanation

此脚本使用以下命令。This script uses the following commands. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

命令Command 注释Notes
az lock createaz lock create 创建锁。Creates a lock.
az lock listaz lock list 列出锁信息。List lock information.
az lock showaz lock show 显示锁的属性。Show properties of a lock.
az lock deleteaz lock delete 删除锁。Deletes a lock.

后续步骤Next steps

-锁定资源以防止意外更改-Lock resources to prevent unexpected changes

-Azure Cosmos DB CLI 文档-Azure Cosmos DB CLI documentation.

-Azure Cosmos DB CLI GitHub 存储库-Azure Cosmos DB CLI GitHub Repository.