Last active
November 9, 2023 07:33
-
-
Save cosh/473e3cbd47de31bcca65ef05a6074654 to your computer and use it in GitHub Desktop.
VNet injection migration for ADX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"clusters": [ | |
{ | |
"subscription_id": "sid", | |
"resource_group_name": "kusto-workload", | |
"cluster_name": "adx-in-vnet" | |
}, | |
{ | |
"subscription_id": "sid", | |
"resource_group_name": "kusto-workload", | |
"cluster_name": "adx-in-vnet-2" | |
}, | |
{ | |
"subscription_id": "sid", | |
"resource_group_name": "kusto-workload", | |
"cluster_name": "adx-in-vnet-3" | |
} | |
], | |
"vnet_state": "disabled" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from azure.identity import DefaultAzureCredential | |
from azure.mgmt.kusto import KustoManagementClient | |
from azure.mgmt.kusto.models import ClusterUpdate, VnetState, VirtualNetworkConfiguration | |
import json | |
import time | |
# Load the configuration from a JSON file | |
with open('config.json', 'r') as config_file: | |
config = json.load(config_file) | |
CLUSTERS = config['clusters'] | |
if config['vnet_state'] == 'enabled': | |
VNET_STATE = VnetState.ENABLED | |
else: | |
VNET_STATE = VnetState.DISABLED | |
# Authenticate with Azure using interactive authentication | |
credential = DefaultAzureCredential() | |
def MigrateCluster(VNET_STATE, credential, cluster): | |
kusto_client = KustoManagementClient(credential, cluster["subscription_id"]) | |
# Get the cluster config | |
clusterConfig = kusto_client.clusters.get( | |
resource_group_name=cluster["resource_group_name"], | |
cluster_name=cluster["cluster_name"] | |
) | |
clusterConfig.virtual_network_configuration.state=VNET_STATE | |
# Create a ClusterUpdate object with the desired changes | |
cluster_update = ClusterUpdate.from_dict(clusterConfig.as_dict()) | |
print(f"Starting migration of {cluster['cluster_name']}") | |
# Update the Kusto cluster | |
poller = kusto_client.clusters.begin_update( | |
resource_group_name=cluster["resource_group_name"], | |
cluster_name=cluster["cluster_name"], | |
parameters=cluster_update | |
) | |
return poller | |
# Start the migration of all clusters | |
allpoller = {} | |
for cluster in CLUSTERS: | |
poller = MigrateCluster(VNET_STATE, credential, cluster) | |
allpoller[cluster["cluster_name"]] = poller | |
# Wait for the migration to complete | |
workTodo = True | |
while workTodo: | |
workTodo = False | |
for clusterName, poller in allpoller.items(): | |
if(poller.done()): | |
continue | |
workTodo = True | |
print(f"Migrating {clusterName}... Status: {poller.status()}") | |
time.sleep(1) | |
if workTodo: | |
time.sleep(30) | |
# Check the status of the migration | |
for clusterName, poller in allpoller.items(): | |
poller_result = poller.result() | |
if poller_result.provisioning_state == "Failed": | |
print(f"Error while migrating {clusterName}... Status: {poller.status()}") | |
print(f"Error details: {poller_result.error}") | |
else: | |
print(f"Migration of {clusterName} completed. Status: {poller.status()}") | |
print("Done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment