|
| 1 | +# -------------------------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | +# -------------------------------------------------------------------------------------------- |
| 5 | + |
| 6 | +# pylint: disable=unused-argument, line-too-long |
| 7 | +from azure.cli.core.commands.client_factory import get_subscription_id |
| 8 | +from azure.cli.core.util import CLIError |
| 9 | +from knack.log import get_logger |
| 10 | +from .._client_factory import ( |
| 11 | + cf_postgres_flexible_tuning_options, |
| 12 | + get_postgresql_flexible_management_client) |
| 13 | +from ..utils._flexible_server_location_capabilities_util import ( |
| 14 | + get_postgres_location_capability_info, |
| 15 | + get_postgres_server_capability_info) |
| 16 | +from ..utils._util import get_autonomous_tuning_settings_map |
| 17 | +from ..utils.validators import validate_resource_group |
| 18 | +from .parameter_commands import _update_parameters, flexible_parameter_update |
| 19 | + |
| 20 | +logger = get_logger(__name__) |
| 21 | + |
| 22 | + |
| 23 | +def index_tuning_update(cmd, client, resource_group_name, server_name, index_tuning_enabled): |
| 24 | + validate_resource_group(resource_group_name) |
| 25 | + source = "user-override" |
| 26 | + |
| 27 | + if index_tuning_enabled == "True": |
| 28 | + list_capability_info = get_postgres_server_capability_info(cmd, resource_group_name, server_name, is_offer_restriction_check_required=True) |
| 29 | + autonomous_tuning_supported = list_capability_info['autonomous_tuning_supported'] |
| 30 | + if not autonomous_tuning_supported: |
| 31 | + raise CLIError("Index tuning is not supported for the server.") |
| 32 | + |
| 33 | + logger.warning("Enabling index tuning for the server.") |
| 34 | + configuration_name = "index_tuning.mode" |
| 35 | + value = "report" |
| 36 | + _update_parameters(cmd, client, server_name, configuration_name, resource_group_name, source, value) |
| 37 | + configuration_name = "pg_qs.query_capture_mode" |
| 38 | + query_capture_mode_configuration = client.get(resource_group_name, server_name, configuration_name) |
| 39 | + |
| 40 | + if query_capture_mode_configuration.value.lower() == "none": |
| 41 | + value = "all" |
| 42 | + _update_parameters(cmd, client, server_name, configuration_name, resource_group_name, source, value) |
| 43 | + logger.warning("Index tuning is enabled for the server.") |
| 44 | + else: |
| 45 | + logger.warning("Disabling index tuning for the server.") |
| 46 | + configuration_name = "index_tuning.mode" |
| 47 | + value = "off" |
| 48 | + _update_parameters(cmd, client, server_name, configuration_name, resource_group_name, source, value) |
| 49 | + logger.warning("Index tuning is disabled for the server.") |
| 50 | + |
| 51 | + |
| 52 | +def index_tuning_show(client, resource_group_name, server_name): |
| 53 | + validate_resource_group(resource_group_name) |
| 54 | + index_tuning_configuration = client.get(resource_group_name, server_name, "index_tuning.mode") |
| 55 | + query_capture_mode_configuration = client.get(resource_group_name, server_name, "pg_qs.query_capture_mode") |
| 56 | + |
| 57 | + if index_tuning_configuration.value.lower() == "report" and query_capture_mode_configuration.value.lower() != "none": |
| 58 | + logger.warning("Index tuning is enabled for the server.") |
| 59 | + else: |
| 60 | + logger.warning("Index tuning is disabled for the server.") |
| 61 | + |
| 62 | + |
| 63 | +def index_tuning_settings_list(cmd, client, resource_group_name, server_name): |
| 64 | + validate_resource_group(resource_group_name) |
| 65 | + index_tuning_configurations_map_values = get_autonomous_tuning_settings_map().values() |
| 66 | + configurations_list = client.list_by_server(resource_group_name, server_name) |
| 67 | + |
| 68 | + # Filter the list based on the values in the dictionary |
| 69 | + index_tuning_settings = [setting for setting in configurations_list if setting.name in index_tuning_configurations_map_values] |
| 70 | + |
| 71 | + return index_tuning_settings |
| 72 | + |
| 73 | + |
| 74 | +def index_tuning_settings_get(cmd, client, resource_group_name, server_name, setting_name): |
| 75 | + validate_resource_group(resource_group_name) |
| 76 | + index_tuning_configurations_map = get_autonomous_tuning_settings_map() |
| 77 | + index_tuning_configuration_name = index_tuning_configurations_map[setting_name] |
| 78 | + |
| 79 | + return client.get( |
| 80 | + resource_group_name=resource_group_name, |
| 81 | + server_name=server_name, |
| 82 | + configuration_name=index_tuning_configuration_name) |
| 83 | + |
| 84 | + |
| 85 | +def index_tuning_settings_set(client, resource_group_name, server_name, setting_name, value=None): |
| 86 | + source = "user-override" if value else None |
| 87 | + tuning_settings = get_autonomous_tuning_settings_map() |
| 88 | + configuration_name = tuning_settings[setting_name] |
| 89 | + return flexible_parameter_update(client, server_name, configuration_name, resource_group_name, source, value) |
| 90 | + |
| 91 | + |
| 92 | +def index_tuning_recommendations_list(cmd, resource_group_name, server_name, recommendation_type=None): |
| 93 | + validate_resource_group(resource_group_name) |
| 94 | + tuning_options_client = cf_postgres_flexible_tuning_options(cmd.cli_ctx, None) |
| 95 | + |
| 96 | + return tuning_options_client.list_recommendations( |
| 97 | + resource_group_name=resource_group_name, |
| 98 | + server_name=server_name, |
| 99 | + tuning_option="index", |
| 100 | + recommendation_type=recommendation_type |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +def autonomous_tuning_update(cmd, client, resource_group_name, server_name, autonomous_tuning_enabled): |
| 105 | + validate_resource_group(resource_group_name) |
| 106 | + source = "user-override" |
| 107 | + |
| 108 | + if autonomous_tuning_enabled == "True": |
| 109 | + subscription = get_subscription_id(cmd.cli_ctx) |
| 110 | + postgres_source_client = get_postgresql_flexible_management_client(cmd.cli_ctx, subscription) |
| 111 | + source_server_object = postgres_source_client.servers.get(resource_group_name, server_name) |
| 112 | + location = ''.join(source_server_object.location.lower().split()) |
| 113 | + list_location_capability_info = get_postgres_location_capability_info(cmd, location, is_offer_restriction_check_required=True) |
| 114 | + autonomous_tuning_supported = list_location_capability_info['autonomous_tuning_supported'] |
| 115 | + if not autonomous_tuning_supported: |
| 116 | + raise CLIError("Autonomous tuning is not supported for the server.") |
| 117 | + |
| 118 | + logger.warning("Enabling autonomous tuning for the server.") |
| 119 | + configuration_name = "index_tuning.mode" |
| 120 | + value = "report" |
| 121 | + _update_parameters(cmd, client, server_name, configuration_name, resource_group_name, source, value) |
| 122 | + configuration_name = "pg_qs.query_capture_mode" |
| 123 | + query_capture_mode_configuration = client.get(resource_group_name, server_name, configuration_name) |
| 124 | + |
| 125 | + if query_capture_mode_configuration.value.lower() == "none": |
| 126 | + value = "all" |
| 127 | + _update_parameters(cmd, client, server_name, configuration_name, resource_group_name, source, value) |
| 128 | + logger.warning("Autonomous tuning is enabled for the server.") |
| 129 | + else: |
| 130 | + logger.warning("Disabling autonomous tuning for the server.") |
| 131 | + configuration_name = "index_tuning.mode" |
| 132 | + value = "off" |
| 133 | + _update_parameters(cmd, client, server_name, configuration_name, resource_group_name, source, value) |
| 134 | + logger.warning("Autonomous tuning is disabled for the server.") |
| 135 | + |
| 136 | + |
| 137 | +def autonomous_tuning_show(client, resource_group_name, server_name): |
| 138 | + validate_resource_group(resource_group_name) |
| 139 | + autonomous_tuning_configuration = client.get(resource_group_name, server_name, "index_tuning.mode") |
| 140 | + query_capture_mode_configuration = client.get(resource_group_name, server_name, "pg_qs.query_capture_mode") |
| 141 | + |
| 142 | + if autonomous_tuning_configuration.value.lower() == "report" and query_capture_mode_configuration.value.lower() != "none": |
| 143 | + logger.warning("Autonomous tuning is enabled for the server.") |
| 144 | + else: |
| 145 | + logger.warning("Autonomous tuning is disabled for the server.") |
| 146 | + |
| 147 | + |
| 148 | +def autonomous_tuning_settings_list(cmd, client, resource_group_name, server_name): |
| 149 | + validate_resource_group(resource_group_name) |
| 150 | + autonomous_tuning_configurations_map_values = get_autonomous_tuning_settings_map().values() |
| 151 | + configurations_list = client.list_by_server(resource_group_name, server_name) |
| 152 | + |
| 153 | + # Filter the list based on the values in the dictionary |
| 154 | + autonomous_tuning_settings = [setting for setting in configurations_list if setting.name in autonomous_tuning_configurations_map_values] |
| 155 | + |
| 156 | + return autonomous_tuning_settings |
| 157 | + |
| 158 | + |
| 159 | +def autonomous_tuning_settings_get(cmd, client, resource_group_name, server_name, setting_name): |
| 160 | + validate_resource_group(resource_group_name) |
| 161 | + autonomous_tuning_configurations_map = get_autonomous_tuning_settings_map() |
| 162 | + autonomous_tuning_configuration_name = autonomous_tuning_configurations_map[setting_name] |
| 163 | + |
| 164 | + return client.get( |
| 165 | + resource_group_name=resource_group_name, |
| 166 | + server_name=server_name, |
| 167 | + configuration_name=autonomous_tuning_configuration_name) |
| 168 | + |
| 169 | + |
| 170 | +def autonomous_tuning_settings_set(client, resource_group_name, server_name, setting_name, value=None): |
| 171 | + source = "user-override" if value else None |
| 172 | + tuning_settings = get_autonomous_tuning_settings_map() |
| 173 | + configuration_name = tuning_settings[setting_name] |
| 174 | + return flexible_parameter_update(client, server_name, configuration_name, resource_group_name, source, value) |
| 175 | + |
| 176 | + |
| 177 | +def autonomous_tuning_index_recommendations_list(cmd, resource_group_name, server_name, recommendation_type=None): |
| 178 | + validate_resource_group(resource_group_name) |
| 179 | + tuning_options_client = cf_postgres_flexible_tuning_options(cmd.cli_ctx, None) |
| 180 | + |
| 181 | + return tuning_options_client.list_recommendations( |
| 182 | + resource_group_name=resource_group_name, |
| 183 | + server_name=server_name, |
| 184 | + tuning_option="index", |
| 185 | + recommendation_type=recommendation_type |
| 186 | + ) |
| 187 | + |
| 188 | + |
| 189 | +def autonomous_tuning_table_recommendations_list(cmd, resource_group_name, server_name, recommendation_type=None): |
| 190 | + validate_resource_group(resource_group_name) |
| 191 | + tuning_options_client = cf_postgres_flexible_tuning_options(cmd.cli_ctx, None) |
| 192 | + |
| 193 | + return tuning_options_client.list_recommendations( |
| 194 | + resource_group_name=resource_group_name, |
| 195 | + server_name=server_name, |
| 196 | + tuning_option="table", |
| 197 | + recommendation_type=recommendation_type |
| 198 | + ) |
0 commit comments