Azure Java SDK-无法添加资源锁

问题描述

我有一个创建ContainerGroup的简单代码:

ContainerGroup instance = azure.containerGroups()
                <truncated>
                .create();

创建实例后,我想创建删除锁:

azure.managementLocks().define("preventDelete").withLockedResource(instance).withLevel(LockLevel.CAN_NOT_DELETE).create();

永不创建锁,失败并出现以下错误:

状态代码403,{“错误”:{“代码”:“ AuthorizationFailed”,“消息”:“ 客户“”(对象ID为“”)没有执行权限 在范围上执行“ Microsoft.Authorization / locks / write”操作 '/subscriptions//resourceGroups//providers/Microsoft.ContainerInstance/containerGroups//providers/Microsoft.Authorization/locks/preventDelete' 或范围无效。如果最近授予访问权限,请 刷新您的凭据。“}}

使用Azure UI完美工作。有什么想法吗?

解决方法

根据错误消息,您没有足够的权限来创建资源锁。实际上,要创建或删除管理锁,您必须有权访问Microsoft.Authorization/*Microsoft.Authorization/locks/*操作。在内置角色中,只有OwnerUser Access Administrator被授予这些操作。有关更多详细信息,请参阅article。请检查您用于在应用程序中进行身份验证的服务主体的权限。

关于如何检查,请参考以下脚本

Connect-AzAccount

$role=Get-AzRoleAssignment -ObjectId <object id of service principal or user>

Get-AzRoleDefinition -Id $role.RoleDefinitionId
,

最终使用Azure CLI而非SDK:

az lock create --lock-type CanNotDelete --name PreventDelete --resource <resourceId>

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...