如何使用 Python 将 API 连接从 Azure 中的资源组 A 移动到资源组 B?

问题描述 投票:0回答:1

我正在尝试将 Azure 中的所有 API 连接从资源组 A 移动到资源组 B。RG B 是新创建的,当前为空。

我使用了这个代码:

from azure.identity import ClientSecretCredential
from azure.mgmt.resource import ResourceManagementClient

# Define your subscription ID and resource group names
subscription_id = "[REDACTED]"
tenant_id = "[REDACTED]"
client_id = "[REDACTED]"
client_secret = "[REDACTED]"

# Authenticate using Azure Identity
credential = ClientSecretCredential(tenant_id, client_id, client_secret)
resource_client = ResourceManagementClient(credential, subscription_id)

# Resource groups
resource_group_name_source = "azdnaeurgrgp1dev" #RG A
resource_group_name_target = "azmgmteurgrgpdev" #RG B

# Get all resources in the source resource group
resources = list(resource_client.resources.list_by_resource_group(resource_group_name_source, filter="resourceType eq 'Microsoft.Web/connections'"))

# Move each resource to the target resource group
for resource in resources:
    resource_id = resource.id
    name = resource.name  
    types = resource.type  
    managed_by = resource.managed_by 
    tags = resource.tags

    print(
    "Resource ID:", resource_id,
    "\nName:", name,
    "\nTypes:", types,
    "\nManaged By:", managed_by,
    "\nTags:", tags)

    # Check if properties is None and initialize it if necessary
    if resource.properties is None:
        resource.properties = {}
    
    # Update the resource group property to the target resource group
    resource.properties['resourceGroup'] = resource_group_name_target
    
    # Update the resource in the target resource group
    resource_client.resources.update(
        resource_group_name_target, 
        resource_id, 
        resource)

print("Resources moved successfully to", resource_group_name_target)

但是它不起作用,我收到的错误是:

AttributeError: 'ResourcesOperations' object has no attribute 'update'
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<command-2269390313065795> in <module>
     42 
     43     # Update the resource in the target resource group
---> 44     resource_client.resources.update(
     45         resource_group_name_target,
     46         resource_id,

AttributeError: 'ResourcesOperations' object has no attribute 'update'

我也尝试过begin_create_or_update,并且create_or_update_by_id仍然没有运气。 ChatGPT 只是循环使用这 3 个解决方案。

python azure
1个回答
0
投票

使用 Python 将 API 连接从 Azure 中的资源组 A 移动到资源组 B。

发生错误的原因是 Azure SDK for Python 没有允许以这种方式直接修改资源属性的

update
方法。相反,为了在资源组之间传输资源,Azure 通常采用
move resources
操作,而不是更新单个资源的属性。

更新了Python代码

from azure.identity import ClientSecretCredential
from azure.mgmt.resource import ResourceManagementClient

# Define your subscription ID and resource group names
subscription_id = ""
client_id       = ""
tenant_id       = ""
client_secret   = ""

# Authenticate using Azure Identity
credential = ClientSecretCredential(tenant_id, client_id, client_secret)
resource_client = ResourceManagementClient(credential, subscription_id)

# Resource groups
resource_group_name_source = "vinay-rg"
resource_group_name_target = "vksb-tar"

# Get all resources in the source resource group
resources = list(resource_client.resources.list_by_resource_group(
    resource_group_name_source, 
    filter="resourceType eq 'Microsoft.Web/connections'"))

# Debug: Print the resources retrieved
for resource in resources:
    print(resource.id)

# Prepare the list of resource IDs to be moved
resource_ids = [resource.id for resource in resources]

# Debug: Check if resource_ids list is empty
if not resource_ids:
    print("No resources found to move.")
else:
    print(f"Moving {len(resource_ids)} resources.")

# Prepare the target resource group ID
target_resource_group_id = f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name_target}"

# Move resources to the target resource group
if resource_ids:
    move_resources_poller = resource_client.resources.begin_move_resources(
        resource_group_name_source,
        {
            "targetResourceGroup": target_resource_group_id,
            "resources": resource_ids
        }
    )

    # Wait for the move operation to complete
    move_resources_poller.result()

    print("Resources moved successfully to", resource_group_name_target)
else:
    print("Move operation not initiated due to lack of resources.")

部署成功:

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.