Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23160

How can I copy API connections from my original resource group to another backup resource group Azure?

$
0
0

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 ClientSecretCredentialfrom azure.mgmt.resource import ResourceManagementClient# Define your subscription ID and resource group namessubscription_id = [REDACTED]tenant_id = [REDACTED]client_id = [REDACTED]client_secret = [REDACTED]# Authenticate using Azure Identitycredential = ClientSecretCredential(tenant_id, client_id, client_secret)resource_client = ResourceManagementClient(credential, subscription_id)# Resource groups# API Connections must be retained in source_apiresource_group_name_source = "azdnaeurgrgp1dev_source_api" resource_group_name_target = "azmgmteurgrgp_backup_api"# Get all resources in the source resource groupresources = list(resource_client.resources.list_by_resource_group(    resource_group_name_source,     filter="resourceType eq 'Microsoft.Web/connections'"))# Debug: Print the resources retrievedfor resource in resources:    print(resource.id)# Prepare the target resource group IDtarget_resource_group_id = f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name_target}"# Move resources in batches of 20for 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


Viewing all articles
Browse latest Browse all 23160

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>