Skip to content

Commit 095bafd

Browse files
committed
Add commands for tuning options
1 parent 89c41db commit 095bafd

File tree

6 files changed

+120
-3
lines changed

6 files changed

+120
-3
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,10 @@ def cf_postgres_flexible_private_link_resources(cli_ctx, _):
414414
return get_postgresql_flexible_management_client(cli_ctx).private_link_resources
415415

416416

417+
def cf_postgres_flexible_tuning_options(cli_ctx, _):
418+
return get_postgresql_flexible_management_client(cli_ctx).tuning_options
419+
420+
417421
def resource_client_factory(cli_ctx, subscription_id=None):
418422
return get_mgmt_service_client(cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES, subscription_id=subscription_id)
419423

src/azure-cli/azure/cli/command_modules/rdbms/_flexible_server_location_capabilities_util.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def _postgres_parse_list_capability(result):
5151
restricted = offer_restricted[0].status if offer_restricted else None
5252
zone_redundant = [feature for feature in supported_features if feature.name == "ZoneRedundantHa"]
5353
geo_backup = [feature for feature in supported_features if feature.name == "GeoBackup"]
54+
index_tuning = [feature for feature in supported_features if feature.name == "IndexTuning"]
5455

5556
if restricted == "Enabled":
5657
raise InvalidArgumentValueError("The location is restricted for provisioning of flexible servers. Please try using another region.")
@@ -60,6 +61,7 @@ def _postgres_parse_list_capability(result):
6061

6162
single_az = zone_redundant[0].status != "Enabled" if zone_redundant else True
6263
geo_backup_supported = geo_backup[0].status == "Enabled" if geo_backup else False
64+
index_tuning_supported = index_tuning[0].status == "Enabled" if index_tuning else False
6365

6466
tiers = result[0].supported_server_editions
6567
tiers_dict = {}
@@ -102,7 +104,8 @@ def _postgres_parse_list_capability(result):
102104
'single_az': single_az,
103105
'geo_backup_supported': geo_backup_supported,
104106
'zones': zones,
105-
'server_versions': versions
107+
'server_versions': versions,
108+
'index_tuning_supported': index_tuning_supported
106109
}
107110

108111

src/azure-cli/azure/cli/command_modules/rdbms/_helptext_pg.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,3 +1170,32 @@
11701170
- name: Update allowed mirrored databases.
11711171
text: az postgres flexible-server fabric-mirroring update-databases -g testgroup -s testsvr --database-names testdb2 testdb3
11721172
"""
1173+
1174+
helps['postgres flexible-server index-tuning'] = """
1175+
type: group
1176+
short-summary: Index tuning analyzes read queries captured in Query Store and recommends index changes to optimize these queries.
1177+
"""
1178+
1179+
helps['postgres flexible-server index-tuning update'] = """
1180+
type: command
1181+
short-summary: Update index tuning to be enabled/disabled on PostgreSQL flexible server.
1182+
examples:
1183+
- name: Update index tuning to be enabled/disabled on PostgreSQL flexible server.
1184+
text: az postgres flexible-server index-tuning update -g testgroup -s testsvr --state Enabled
1185+
"""
1186+
1187+
helps['postgres flexible-server index-tuning list-tuning-options'] = """
1188+
type: command
1189+
short-summary: Get available tuning options associated with a PostgreSQL flexible server.
1190+
examples:
1191+
- name: Get tuning options for a PostgreSQL flexible server.
1192+
text: az postgres flexible-server index-tuning list-tuning-options -g testgroup -s testsvr
1193+
"""
1194+
1195+
helps['postgres flexible-server index-tuning list-recommendations'] = """
1196+
type: command
1197+
short-summary: Get available tuning index recommendations associated with a PostgreSQL flexible server.
1198+
examples:
1199+
- name: Get tuning index recommendations for a PostgreSQL flexible server. Filter by selected type.
1200+
text: az postgres flexible-server index-tuning list-recommendations -g testgroup -s testsvr --recommendation-type CreateIndex
1201+
"""

src/azure-cli/azure/cli/command_modules/rdbms/_params.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,26 @@ def _flexible_server_params(command_group):
10341034
c.argument('resource_group_name', arg_type=resource_group_name_type)
10351035
c.argument('server_name', options_list=['--server-name', '-s'], id_part='name', arg_type=server_name_arg_type, required=False)
10361036

1037+
# index tuning
1038+
if command_group == 'postgres':
1039+
for scope in ['update', 'list-tuning-options', 'list-recommendations']:
1040+
argument_context_string = '{} flexible-server index-tuning {}'.format(command_group, scope)
1041+
with self.argument_context(argument_context_string) as c:
1042+
c.argument('server_name', options_list=['--server-name', '-s'], arg_type=server_name_arg_type)
1043+
1044+
with self.argument_context('{} flexible-server index-tuning update'.format(command_group)) as c:
1045+
c.argument('state',
1046+
options_list=['--state'],
1047+
required=True,
1048+
help='State of index tuning setting.',
1049+
arg_type=get_enum_type(['Enabled', 'Disabled']))
1050+
1051+
with self.argument_context('{} flexible-server index-tuning list-recommendations'.format(command_group)) as c:
1052+
c.argument('recommendation_type',
1053+
options_list=['--recommendation-type', '-r'],
1054+
help='Retrieve recommendations based on type.',
1055+
arg_type=get_enum_type(['CreateIndex', 'DropIndex']))
1056+
10371057
# GTID
10381058
if command_group == 'mysql':
10391059
with self.argument_context('{} flexible-server gtid reset'.format(command_group)) as c:

src/azure-cli/azure/cli/command_modules/rdbms/flexible_server_commands.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
cf_postgres_flexible_private_link_resources,
2424
cf_postgres_flexible_virtual_endpoints,
2525
cf_postgres_flexible_server_threat_protection_settings,
26-
cf_postgres_flexible_server_log_files)
26+
cf_postgres_flexible_server_log_files,
27+
cf_postgres_flexible_tuning_options)
2728

2829
from ._transformers import (
2930
table_transform_output,
@@ -125,6 +126,11 @@ def load_flexibleserver_command_table(self, _):
125126
client_factory=cf_postgres_flexible_private_link_resources
126127
)
127128

129+
postgres_flexible_tuning_options_sdk = CliCommandType(
130+
operations_tmpl='azure.mgmt.postgresqlflexibleservers.operations#TuningOptionsOperations.{}',
131+
client_factory=cf_postgres_flexible_tuning_options
132+
)
133+
128134
# MERU COMMANDS
129135
flexible_server_custom_common = CliCommandType(
130136
operations_tmpl='azure.cli.command_modules.rdbms.flexible_server_custom_common#{}')
@@ -296,3 +302,9 @@ def load_flexibleserver_command_table(self, _):
296302
g.custom_command('start', 'flexible_server_fabric_mirroring_start')
297303
g.custom_command('stop', 'flexible_server_fabric_mirroring_stop')
298304
g.custom_command('update-databases', 'flexible_server_fabric_mirroring_update_databases')
305+
306+
with self.command_group('postgres flexible-server index-tuning', postgres_flexible_tuning_options_sdk,
307+
client_factory=cf_postgres_flexible_tuning_options) as g:
308+
g.custom_command('update', 'index_tuning_update', custom_command_type=flexible_servers_custom_postgres)
309+
g.custom_command('list-tuning-options', 'tuning_options_list', custom_command_type=flexible_servers_custom_postgres)
310+
g.custom_command('list-recommendations', 'recommendations_list', custom_command_type=flexible_servers_custom_postgres)

src/azure-cli/azure/cli/command_modules/rdbms/flexible_server_custom_postgres.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
cf_postgres_flexible_db, cf_postgres_check_resource_availability, cf_postgres_flexible_servers, \
2828
cf_postgres_check_resource_availability_with_location, \
2929
cf_postgres_flexible_private_dns_zone_suffix_operations, \
30-
cf_postgres_flexible_private_endpoint_connections
30+
cf_postgres_flexible_private_endpoint_connections, \
31+
cf_postgres_flexible_config
3132
from ._flexible_server_util import generate_missing_parameters, resolve_poller, \
3233
generate_password, parse_maintenance_window, get_current_time, build_identity_and_data_encryption, \
3334
_is_resource_name, get_tenant_id, get_case_insensitive_key_value, get_enum_value_true_false
@@ -1596,6 +1597,54 @@ def _update_parameters(cmd, client, server_name, configuration_name, resource_gr
15961597
client.begin_update(resource_group_name, server_name, configuration_name, parameters), cmd.cli_ctx, 'PostgreSQL Parameter update')
15971598

15981599

1600+
def index_tuning_update(cmd, resource_group_name, server_name, state):
1601+
validate_resource_group(resource_group_name)
1602+
source = "user-override"
1603+
config_client = cf_postgres_flexible_config(cmd.cli_ctx, '_')
1604+
1605+
if state == 'Enabled':
1606+
subscription = get_subscription_id(cmd.cli_ctx)
1607+
postgres_source_client = get_postgresql_flexible_management_client(cmd.cli_ctx, subscription)
1608+
source_server_object = postgres_source_client.servers.get(resource_group_name, server_name)
1609+
location = ''.join(source_server_object.location.lower().split())
1610+
list_location_capability_info = get_postgres_location_capability_info(cmd, location)
1611+
index_tuning_supported = list_location_capability_info['index_tuning_supported']
1612+
if not index_tuning_supported:
1613+
raise CLIError("Index tuning is not supported for the server.")
1614+
1615+
configuration_name = "index_tuning.mode"
1616+
value = "report"
1617+
_update_parameters(cmd, config_client, server_name, configuration_name, resource_group_name, source, value)
1618+
configuration_name = "pg_qs.query_capture_mode"
1619+
value = "all"
1620+
return _update_parameters(cmd, config_client, server_name, configuration_name, resource_group_name, source, value)
1621+
else:
1622+
configuration_name = "index_tuning.mode"
1623+
value = "off"
1624+
return _update_parameters(cmd, config_client, server_name, configuration_name, resource_group_name, source, value)
1625+
1626+
1627+
def tuning_options_list(client, resource_group_name, server_name):
1628+
validate_resource_group(resource_group_name)
1629+
1630+
return client.get(
1631+
resource_group_name=resource_group_name,
1632+
server_name=server_name,
1633+
tuning_option="index",
1634+
)
1635+
1636+
1637+
def recommendations_list(client, resource_group_name, server_name, recommendation_type=None):
1638+
validate_resource_group(resource_group_name)
1639+
1640+
return client.list_recommendations(
1641+
resource_group_name=resource_group_name,
1642+
server_name=server_name,
1643+
tuning_option="index",
1644+
recommendation_type=recommendation_type
1645+
)
1646+
1647+
15991648
def _update_private_endpoint_connection_status(cmd, client, resource_group_name, server_name,
16001649
private_endpoint_connection_name, is_approved=True, description=None): # pylint: disable=unused-argument
16011650
validate_resource_group(resource_group_name)

0 commit comments

Comments
 (0)