I’m trying to move all my API connections in Azure from Resource Group A to Resource Group B. RG B is newly created and currently empty.
I used this code:
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)
But it’s not working the error I’m getting is:
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'
I’ve also tried begin_create_or_update, and create_or_update_by_id still no luck on that. ChatGPT is just cycling on this 3 solustions.