Skip to content

Commit c38da1d

Browse files
author
Saif Al-Din Ali
committed
Fix correct location and target resource group creation
1 parent d562112 commit c38da1d

File tree

2 files changed

+99
-13
lines changed

2 files changed

+99
-13
lines changed

src/migrate/azext_migrate/custom.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ def new_local_server_replication(cmd,
274274
)
275275
from azext_migrate.helpers.replication.new._execute_new import (
276276
get_ARC_resource_bridge_info,
277+
ensure_target_resource_group_exists,
277278
construct_disk_and_nic_mapping,
278279
create_protected_item
279280
)
@@ -414,14 +415,23 @@ def new_local_server_replication(cmd,
414415
# 3. Get ARC Resource Bridge info
415416
custom_location_id, custom_location_region, \
416417
target_cluster_id = get_ARC_resource_bridge_info(
418+
cmd,
417419
target_fabric,
418420
migrate_project
419421
)
420422

421-
# 4. Validate target VM name
423+
# 4. Ensure target resource group exists
424+
ensure_target_resource_group_exists(
425+
cmd,
426+
target_resource_group_id,
427+
custom_location_region,
428+
project_name
429+
)
430+
431+
# 5. Validate target VM name
422432
validate_target_VM_name(target_vm_name)
423433

424-
# 5. Construct disk and NIC mappings
434+
# 6. Construct disk and NIC mappings
425435
disks, nics = construct_disk_and_nic_mapping(
426436
is_power_user_mode,
427437
disk_to_include,
@@ -432,7 +442,7 @@ def new_local_server_replication(cmd,
432442
target_virtual_switch_id,
433443
target_test_virtual_switch_id)
434444

435-
# 6. Create the protected item
445+
# 7. Create the protected item
436446
create_protected_item(
437447
cmd,
438448
subscription_id,

src/migrate/azext_migrate/helpers/replication/new/_execute_new.py

Lines changed: 86 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
logger = get_logger(__name__)
2121

2222

23-
def get_ARC_resource_bridge_info(target_fabric, migrate_project):
23+
def get_ARC_resource_bridge_info(cmd, target_fabric, migrate_project):
2424
target_fabric_custom_props = (
2525
target_fabric.get('properties', {}).get('customProperties', {}))
2626
target_cluster_id = (
@@ -47,25 +47,101 @@ def get_ARC_resource_bridge_info(target_fabric, migrate_project):
4747
if target_cluster_id:
4848
cluster_parts = target_cluster_id.split('/')
4949
if len(cluster_parts) >= 5:
50-
custom_location_region = (
51-
migrate_project.get('location', 'eastus'))
5250
custom_location_id = (
5351
f"/subscriptions/{cluster_parts[2]}/"
5452
f"resourceGroups/{cluster_parts[4]}/providers/"
5553
f"Microsoft.ExtendedLocation/customLocations/"
5654
f"{cluster_parts[-1]}-customLocation"
5755
)
58-
else:
59-
custom_location_region = (
60-
migrate_project.get('location', 'eastus'))
61-
else:
62-
custom_location_region = (
63-
migrate_project.get('location', 'eastus'))
64-
else:
56+
57+
# Get the actual region from the custom location resource
58+
custom_location_region = None
59+
if custom_location_id:
60+
try:
61+
custom_location = get_resource_by_id(
62+
cmd, custom_location_id, "2021-08-15")
63+
custom_location_region = custom_location.get('location')
64+
logger.info(f"Retrieved custom location region: {custom_location_region}")
65+
except Exception as e:
66+
logger.warning(
67+
f"Could not retrieve custom location: {str(e)}. "
68+
f"Falling back to migrate project location.")
69+
70+
# Fall back to migrate project location if we couldn't get custom location region
71+
if not custom_location_region:
6572
custom_location_region = migrate_project.get('location', 'eastus')
73+
logger.warning(
74+
f"Using migrate project location as fallback: {custom_location_region}")
75+
6676
return custom_location_id, custom_location_region, target_cluster_id
6777

6878

79+
def ensure_target_resource_group_exists(cmd, target_resource_group_id,
80+
custom_location_region,
81+
project_name):
82+
"""
83+
Ensure the target resource group exists in the target subscription.
84+
Creates it if it doesn't exist.
85+
86+
Args:
87+
cmd: Command context
88+
target_resource_group_id: Full ARM ID of target resource group
89+
custom_location_region: Region for the resource group
90+
project_name: Migrate project name for tagging
91+
"""
92+
# Parse the resource group ID to get subscription and RG name
93+
rg_parts = target_resource_group_id.split('/')
94+
if len(rg_parts) < 5:
95+
raise CLIError(
96+
f"Invalid target resource group ID: {target_resource_group_id}")
97+
98+
target_subscription_id = rg_parts[2]
99+
target_rg_name = rg_parts[4]
100+
101+
# Check if resource group exists
102+
rg_check_uri = (
103+
f"/subscriptions/{target_subscription_id}/"
104+
f"resourceGroups/{target_rg_name}"
105+
)
106+
107+
try:
108+
existing_rg = get_resource_by_id(
109+
cmd, rg_check_uri, "2021-04-01")
110+
if existing_rg:
111+
logger.info(
112+
f"Target resource group '{target_rg_name}' already exists "
113+
f"in subscription '{target_subscription_id}'")
114+
return existing_rg
115+
except CLIError as e:
116+
error_str = str(e)
117+
if "ResourceGroupNotFound" in error_str or "404" in error_str:
118+
# Resource group doesn't exist, create it
119+
logger.info(
120+
f"Target resource group '{target_rg_name}' not found. "
121+
f"Creating in subscription '{target_subscription_id}'...")
122+
123+
rg_body = {
124+
"location": custom_location_region,
125+
"tags": {
126+
"Migrate Project": project_name
127+
}
128+
}
129+
130+
print(
131+
f"Creating target resource group '{target_rg_name}' "
132+
f"in region '{custom_location_region}'...")
133+
134+
created_rg = create_or_update_resource(
135+
cmd, rg_check_uri, "2021-04-01", rg_body)
136+
137+
print(
138+
f"✓ Target resource group '{target_rg_name}' created successfully")
139+
return created_rg
140+
else:
141+
# Some other error, re-raise
142+
raise
143+
144+
69145
def construct_disk_and_nic_mapping(is_power_user_mode,
70146
disk_to_include,
71147
nic_to_include,

0 commit comments

Comments
 (0)