【问题标题】:Azure tags update 'NoneType' object has no attribute 'update'Azure 标记更新“NoneType”对象没有属性“更新”
【发布时间】:2019-03-19 23:33:43
【问题描述】:

您好,我正在使用 python 和 Runbook 更新 azure 中的资源标签。我可以更新大多数资源的标签,但有些资源给了我这个错误

“NoneType”对象没有“更新”属性

我正在使用thread中提到的代码spinet

resource.tags.update(tag_dic)
if not resource.properties: 
    resource.properties = {}

resource_client.resources.create_or_update(                                   
resource_group_name=resource.id.split('/')[4],                       
resource_provider_namespace=resource.id.split('/')[6],
parent_resource_path='',
resource_type="",
resource_name=resource.name,
api_version=2018-M-D, 
parameters=resource
)

【问题讨论】:

  • 例如这个磁盘资源/subscriptions/xxxx/resourceGroups/CPS_PX/providers/Microsoft.Compute/disks/px-xx给我这个错误这可能是vm资源的这个子资源?

标签: python azure azure-runbook


【解决方案1】:

很可能代码告诉您resource.tags 有时是None。您可能想在您提到的代码之前尝试:

if not resource.tags:
    resource.tags = {}

【讨论】:

  • 抱歉,我尝试回复迟了,但它说 Azure 错误:无效参数消息:缺少必需的参数“creationData”(空)。目标:creationData
  • 因此,当您使用 create_or_update 时,您需要确保 resource 之前来自 GET。否则你可能会比你预期的更新更多,因为 RestAPI 会区分你发送的内容和上次发送的内容。如果您缺少某些部分(creationData),它将失败
  • 您也可以使用简单的update 来执行 HTTP PATCH,但并非所有服务都支持它。
  • 我建议你在这里为每个特定的资源类型打开一个问题github.com/Azure/azure-sdk-for-python/issues
  • 很高兴知道在 MS 工作应该是非常棒的经历。
【解决方案2】:

对于磁盘资源,无法从 resource.client 调用访问它们,因为磁盘是 VM 的子资源,而不是资源组的子资源。对于磁盘,我们需要调用 compute.client

compute_client = ComputeManagementClient(
    azure_credential,
    subscription_id,
    base_url=resourceManager_url)
managed_disk = compute_client.disks.get(resource_group, disk_name)
managed_disk.tags = {"test_tag":"yes"}
try:
    compute_client.disks.create_or_update(
        resource_group,
        resource_name_curr,
        managed_disk
    )

【讨论】:

    猜你喜欢
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    相关资源
    最近更新 更多