Skip to content

Commit 7979d03

Browse files
honghrCopilot
andauthored
{MySQL} az mysql flexible-server restore: Support different subscription and resource group (#32620)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 343e28e commit 7979d03

3 files changed

Lines changed: 58 additions & 4 deletions

File tree

src/azure-cli/azure/cli/command_modules/mysql/_client_factory.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ def get_mysql_flexible_management_client(cli_ctx, **_):
3737
return get_mgmt_service_client(cli_ctx, MySQLManagementClient)
3838

3939

40+
def get_mysql_flexible_management_client_by_sub(cli_ctx, subscription_id, **_):
41+
from os import getenv
42+
from azure.mgmt.mysqlflexibleservers import MySQLManagementClient
43+
44+
# Allow overriding resource manager URI using environment variable
45+
rm_uri_override = getenv(RM_URI_OVERRIDE)
46+
if rm_uri_override:
47+
client_id = getenv(AZURE_CLIENT_ID)
48+
if client_id:
49+
credentials = get_environment_credential()
50+
else:
51+
from msrest.authentication import Authentication # pylint: disable=import-error
52+
credentials = Authentication()
53+
54+
return MySQLManagementClient(
55+
subscription_id=subscription_id,
56+
base_url=rm_uri_override,
57+
credential=credentials)
58+
# Normal production scenario.
59+
return get_mgmt_service_client(cli_ctx, MySQLManagementClient, subscription_id=subscription_id)
60+
61+
4062
def get_mysql_management_client(cli_ctx, **_):
4163
from os import getenv
4264
from azure.mgmt.rdbms.mysql import MySQLManagementClient

src/azure-cli/azure/cli/command_modules/mysql/_help.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,19 @@
395395
text: >
396396
az mysql flexible-server restore --resource-group testGroup --name testserverNew \\
397397
--source-server testserver --public-access Enabled
398+
- name: >
399+
Restore 'testserver' to current point-in-time as a new server 'testserverNew' in a different resource group.
400+
Here --resource-group is for the target server's resource group, and --source-server must be passed as resource ID.
401+
text: >
402+
az mysql flexible-server restore --resource-group testGroup --name testserverNew \\
403+
--source-server /subscriptions/{sourceSubscriptionId}/resourceGroups/{sourceResourceGroup}/providers/Microsoft.DBforMySQL/flexibleServers/{sourceServerName}
404+
- name: >
405+
Restore 'testserver' to current point-in-time as a new server 'testserverNew' in a different subscription.
406+
Here --resource-group is for the target server's resource group, and --source-server must be passed as resource ID.
407+
This resource ID can be in a subscription different than the subscription used for az account set.
408+
text: >
409+
az mysql flexible-server restore --resource-group testGroup --name testserverNew \\
410+
--source-server /subscriptions/{sourceSubscriptionId}/resourceGroups/{sourceResourceGroup}/providers/Microsoft.DBforMySQL/flexibleServers/{sourceServerName}
398411
"""
399412

400413
helps['mysql flexible-server geo-restore'] = """
@@ -416,6 +429,13 @@
416429
- name: Geo-restore private access server 'testserver' as a new server 'testserverNew' with public access.
417430
text: >
418431
az mysql flexible-server geo-restore --resource-group testGroup --name testserverNew --source-server testserver --public-access Enabled --location newLocation
432+
- name: >
433+
Geo-restore 'testserver' to current point-in-time as a new server 'testserverNew' in a different subscription / resource group.
434+
Here --resource-group is for the target server's resource group, and --source-server must be passed as resource ID.
435+
This resource ID can be in a subscription different than the subscription used for az account set.
436+
text: >
437+
az mysql flexible-server geo-restore --resource-group testGroup --name testserverNew --location newLocation \\
438+
--source-server /subscriptions/{sourceSubscriptionId}/resourceGroups/{sourceResourceGroup}/providers/Microsoft.DBforMySQL/flexibleServers/{sourceServerName}
419439
"""
420440

421441
helps['mysql flexible-server start'] = """
@@ -656,6 +676,13 @@
656676
examples:
657677
- name: Create a read replica 'testReplicaServer' for 'testserver' in the specified zone if available.
658678
text: az mysql flexible-server replica create --replica-name testReplicaServer -g testGroup --source-server testserver --zone 3
679+
- name: >
680+
Create a read replica 'testReplicaServer' for 'testserver' in a different subscription / resource group 'newTestGroup'.
681+
Here --resource-group is for the read replica's resource group, and --source-server must be passed as resource ID.
682+
This resource ID can be in a subscription different than the subscription used for az account set.
683+
text: >
684+
az mysql flexible-server replica create --replica-name testReplicaServer -g newTestGroup \\
685+
--source-server /subscriptions/{sourceSubscriptionId}/resourceGroups/{sourceResourceGroup}/providers/Microsoft.DBforMySQL/flexibleServers/{sourceServerName}
659686
"""
660687

661688
helps['mysql flexible-server replica list'] = """

src/azure-cli/azure/cli/command_modules/mysql/custom.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from ._client_factory import get_mysql_flexible_management_client, cf_mysql_flexible_firewall_rules, cf_mysql_flexible_db, \
2424
cf_mysql_check_resource_availability, cf_mysql_check_resource_availability_without_location, cf_mysql_flexible_config, \
2525
cf_mysql_flexible_servers, cf_mysql_flexible_replica, cf_mysql_flexible_adadmin, cf_mysql_flexible_private_dns_zone_suffix_operations, cf_mysql_servers, \
26-
cf_mysql_firewall_rules
26+
cf_mysql_firewall_rules, get_mysql_flexible_management_client_by_sub
2727
from ._util import resolve_poller, generate_missing_parameters, get_mysql_list_skus_info, generate_password, parse_maintenance_window, \
2828
replace_memory_optimized_tier, build_identity_and_data_encryption, get_identity_and_data_encryption, get_tenant_id, run_subprocess, \
2929
fill_action_template, get_git_root_dir, get_single_to_flex_sku_mapping, get_firewall_rules_from_paged_response, \
@@ -734,7 +734,9 @@ def flexible_server_restore(cmd, client, resource_group_name, server_name, sourc
734734

735735
try:
736736
id_parts = parse_resource_id(source_server_id)
737-
source_server_object = client.get(id_parts['resource_group'], id_parts['name'])
737+
source_client = get_mysql_flexible_management_client_by_sub(cmd.cli_ctx, id_parts['subscription']).servers
738+
739+
source_server_object = source_client.get(id_parts['resource_group'], id_parts['name'])
738740
location = ''.join(source_server_object.location.lower().split())
739741
list_skus_info = get_mysql_list_skus_info(cmd, location)
740742

@@ -880,7 +882,9 @@ def flexible_server_georestore(cmd, client, resource_group_name, server_name, so
880882

881883
try:
882884
id_parts = parse_resource_id(source_server_id)
883-
source_server_object = client.get(id_parts['resource_group'], id_parts['name'])
885+
source_client = get_mysql_flexible_management_client_by_sub(cmd.cli_ctx, id_parts['subscription']).servers
886+
887+
source_server_object = source_client.get(id_parts['resource_group'], id_parts['name'])
884888
list_skus_info = get_mysql_list_skus_info(cmd, location)
885889

886890
if not tier:
@@ -1340,7 +1344,8 @@ def flexible_replica_create(cmd, client, resource_group_name, source_server, rep
13401344

13411345
source_server_id_parts = parse_resource_id(source_server_id)
13421346
try:
1343-
source_server_object = client.get(source_server_id_parts['resource_group'], source_server_id_parts['name'])
1347+
source_client = get_mysql_flexible_management_client_by_sub(cmd.cli_ctx, source_server_id_parts['subscription']).servers
1348+
source_server_object = source_client.get(source_server_id_parts['resource_group'], source_server_id_parts['name'])
13441349
validate_mysql_replica(source_server_object)
13451350
except Exception as e:
13461351
raise ResourceNotFoundError(e)

0 commit comments

Comments
 (0)