diff --git a/src/azure-cli/azure/cli/command_modules/mysql/_client_factory.py b/src/azure-cli/azure/cli/command_modules/mysql/_client_factory.py index 20b91412d57..8962648b06a 100644 --- a/src/azure-cli/azure/cli/command_modules/mysql/_client_factory.py +++ b/src/azure-cli/azure/cli/command_modules/mysql/_client_factory.py @@ -37,6 +37,28 @@ def get_mysql_flexible_management_client(cli_ctx, **_): return get_mgmt_service_client(cli_ctx, MySQLManagementClient) +def get_mysql_flexible_management_client_by_sub(cli_ctx, subscription_id, **_): + from os import getenv + from azure.mgmt.mysqlflexibleservers import MySQLManagementClient + + # Allow overriding resource manager URI using environment variable + rm_uri_override = getenv(RM_URI_OVERRIDE) + if rm_uri_override: + client_id = getenv(AZURE_CLIENT_ID) + if client_id: + credentials = get_environment_credential() + else: + from msrest.authentication import Authentication # pylint: disable=import-error + credentials = Authentication() + + return MySQLManagementClient( + subscription_id=subscription_id, + base_url=rm_uri_override, + credential=credentials) + # Normal production scenario. + return get_mgmt_service_client(cli_ctx, MySQLManagementClient, subscription_id=subscription_id) + + def get_mysql_management_client(cli_ctx, **_): from os import getenv from azure.mgmt.rdbms.mysql import MySQLManagementClient diff --git a/src/azure-cli/azure/cli/command_modules/mysql/_help.py b/src/azure-cli/azure/cli/command_modules/mysql/_help.py index dcf4e272b3d..3e85466a636 100644 --- a/src/azure-cli/azure/cli/command_modules/mysql/_help.py +++ b/src/azure-cli/azure/cli/command_modules/mysql/_help.py @@ -395,6 +395,19 @@ text: > az mysql flexible-server restore --resource-group testGroup --name testserverNew \\ --source-server testserver --public-access Enabled + - name: > + Restore 'testserver' to current point-in-time as a new server 'testserverNew' in a different resource group. + Here --resource-group is for the target server's resource group, and --source-server must be passed as resource ID. + text: > + az mysql flexible-server restore --resource-group testGroup --name testserverNew \\ + --source-server /subscriptions/{sourceSubscriptionId}/resourceGroups/{sourceResourceGroup}/providers/Microsoft.DBforMySQL/flexibleServers/{sourceServerName} + - name: > + Restore 'testserver' to current point-in-time as a new server 'testserverNew' in a different subscription. + Here --resource-group is for the target server's resource group, and --source-server must be passed as resource ID. + This resource ID can be in a subscription different than the subscription used for az account set. + text: > + az mysql flexible-server restore --resource-group testGroup --name testserverNew \\ + --source-server /subscriptions/{sourceSubscriptionId}/resourceGroups/{sourceResourceGroup}/providers/Microsoft.DBforMySQL/flexibleServers/{sourceServerName} """ helps['mysql flexible-server geo-restore'] = """ @@ -416,6 +429,13 @@ - name: Geo-restore private access server 'testserver' as a new server 'testserverNew' with public access. text: > az mysql flexible-server geo-restore --resource-group testGroup --name testserverNew --source-server testserver --public-access Enabled --location newLocation + - name: > + Geo-restore 'testserver' to current point-in-time as a new server 'testserverNew' in a different subscription / resource group. + Here --resource-group is for the target server's resource group, and --source-server must be passed as resource ID. + This resource ID can be in a subscription different than the subscription used for az account set. + text: > + az mysql flexible-server geo-restore --resource-group testGroup --name testserverNew --location newLocation \\ + --source-server /subscriptions/{sourceSubscriptionId}/resourceGroups/{sourceResourceGroup}/providers/Microsoft.DBforMySQL/flexibleServers/{sourceServerName} """ helps['mysql flexible-server start'] = """ @@ -656,6 +676,13 @@ examples: - name: Create a read replica 'testReplicaServer' for 'testserver' in the specified zone if available. text: az mysql flexible-server replica create --replica-name testReplicaServer -g testGroup --source-server testserver --zone 3 + - name: > + Create a read replica 'testReplicaServer' for 'testserver' in a different subscription / resource group 'newTestGroup'. + Here --resource-group is for the read replica's resource group, and --source-server must be passed as resource ID. + This resource ID can be in a subscription different than the subscription used for az account set. + text: > + az mysql flexible-server replica create --replica-name testReplicaServer -g newTestGroup \\ + --source-server /subscriptions/{sourceSubscriptionId}/resourceGroups/{sourceResourceGroup}/providers/Microsoft.DBforMySQL/flexibleServers/{sourceServerName} """ helps['mysql flexible-server replica list'] = """ diff --git a/src/azure-cli/azure/cli/command_modules/mysql/custom.py b/src/azure-cli/azure/cli/command_modules/mysql/custom.py index cf49d1b8036..a06de8fd9b0 100644 --- a/src/azure-cli/azure/cli/command_modules/mysql/custom.py +++ b/src/azure-cli/azure/cli/command_modules/mysql/custom.py @@ -23,7 +23,7 @@ from ._client_factory import get_mysql_flexible_management_client, cf_mysql_flexible_firewall_rules, cf_mysql_flexible_db, \ cf_mysql_check_resource_availability, cf_mysql_check_resource_availability_without_location, cf_mysql_flexible_config, \ cf_mysql_flexible_servers, cf_mysql_flexible_replica, cf_mysql_flexible_adadmin, cf_mysql_flexible_private_dns_zone_suffix_operations, cf_mysql_servers, \ - cf_mysql_firewall_rules + cf_mysql_firewall_rules, get_mysql_flexible_management_client_by_sub from ._util import resolve_poller, generate_missing_parameters, get_mysql_list_skus_info, generate_password, parse_maintenance_window, \ replace_memory_optimized_tier, build_identity_and_data_encryption, get_identity_and_data_encryption, get_tenant_id, run_subprocess, \ 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 try: id_parts = parse_resource_id(source_server_id) - source_server_object = client.get(id_parts['resource_group'], id_parts['name']) + source_client = get_mysql_flexible_management_client_by_sub(cmd.cli_ctx, id_parts['subscription']).servers + + source_server_object = source_client.get(id_parts['resource_group'], id_parts['name']) location = ''.join(source_server_object.location.lower().split()) list_skus_info = get_mysql_list_skus_info(cmd, location) @@ -880,7 +882,9 @@ def flexible_server_georestore(cmd, client, resource_group_name, server_name, so try: id_parts = parse_resource_id(source_server_id) - source_server_object = client.get(id_parts['resource_group'], id_parts['name']) + source_client = get_mysql_flexible_management_client_by_sub(cmd.cli_ctx, id_parts['subscription']).servers + + source_server_object = source_client.get(id_parts['resource_group'], id_parts['name']) list_skus_info = get_mysql_list_skus_info(cmd, location) if not tier: @@ -1340,7 +1344,8 @@ def flexible_replica_create(cmd, client, resource_group_name, source_server, rep source_server_id_parts = parse_resource_id(source_server_id) try: - source_server_object = client.get(source_server_id_parts['resource_group'], source_server_id_parts['name']) + source_client = get_mysql_flexible_management_client_by_sub(cmd.cli_ctx, source_server_id_parts['subscription']).servers + source_server_object = source_client.get(source_server_id_parts['resource_group'], source_server_id_parts['name']) validate_mysql_replica(source_server_object) except Exception as e: raise ResourceNotFoundError(e)