I’m trying to copy API connections from my source resource group to another resource group to serve a backup RG. The code I’m currently using is moving the API connection to another RG, so the target RG is not serving its purpose as a backup RG. I slightly modified the code to process 20 API connections since I’m managing around 200+ API connection at the moment.
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
# API Connections must be retained in source_api
resource_group_name_source = "azdnaeurgrgp1dev_source_api"
resource_group_name_target = "azmgmteurgrgp_backup_api"
# 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 target resource group ID
target_resource_group_id = f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name_target}"
# Move resources in batches of 20
for i in range(0, len(resources), 20):
batch_resources = resources[i:i+20]
resource_ids = [resource.id for resource in batch_resources]
# Move resources to the target resource group
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(f"Moved {len(resource_ids)} resources.")
print("All resources moved successfully to", resource_group_name_target)
Ideally I was hoping for API connections to be copied instead of being moved. I tried to use chat GPT for code to be tweak but its only suggesting again to use update