2020logger = 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+
69145def construct_disk_and_nic_mapping (is_power_user_mode ,
70146 disk_to_include ,
71147 nic_to_include ,
0 commit comments