Skip to content

Commit 6789df7

Browse files
authored
{POSTGRESQL} Reformat command files for clarity (#32915)
1 parent f3c4036 commit 6789df7

34 files changed

+2949
-2692
lines changed

src/azure-cli/azure/cli/command_modules/postgresql/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from azure.cli.core import ModExtensionSuppress
88
from azure.cli.core.commands import CliCommandType
99
from azure.cli.core.profiles import ResourceType
10-
from azure.cli.command_modules.postgresql._util import PostgreSQLArgumentContext
10+
from azure.cli.command_modules.postgresql.utils._util import PostgreSQLArgumentContext
1111
from azure.cli.command_modules.postgresql.flexible_server_commands import load_flexibleserver_command_table
1212
from azure.cli.command_modules.postgresql._params import load_arguments
1313
import azure.cli.command_modules.postgresql._help # pylint: disable=unused-import
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
7+
# pylint: disable=too-many-instance-attributes, too-few-public-methods
8+
class DbContext:
9+
"""Database context class for PostgreSQL flexible server operations.
10+
11+
This class serves as a context container for various database-related configurations,
12+
factory functions, and clients used throughout PostgreSQL command operations.
13+
"""
14+
15+
def __init__(self, cmd=None, azure_sdk=None, logging_name=None, cf_firewall=None, cf_db=None,
16+
cf_availability=None, cf_private_dns_zone_suffix=None,
17+
command_group=None, server_client=None, location=None):
18+
"""
19+
Initialize DbContext with configuration and factory parameters.
20+
21+
Args:
22+
cmd: Azure CLI command context
23+
azure_sdk: Azure SDK module (e.g., postgresqlflexibleservers)
24+
logging_name: Name to use for logging (e.g., 'PostgreSQL')
25+
cf_firewall: Client factory for firewall rules
26+
cf_db: Client factory for databases
27+
cf_availability: Client factory for resource availability checks
28+
cf_private_dns_zone_suffix: Client factory for private DNS zone suffixes
29+
command_group: The command group name (e.g., 'postgres')
30+
server_client: The server management client
31+
location: Azure region location
32+
"""
33+
self.cmd = cmd
34+
self.azure_sdk = azure_sdk
35+
self.cf_firewall = cf_firewall
36+
self.cf_availability = cf_availability
37+
self.cf_private_dns_zone_suffix = cf_private_dns_zone_suffix
38+
self.logging_name = logging_name
39+
self.cf_db = cf_db
40+
self.command_group = command_group
41+
self.server_client = server_client
42+
self.location = location

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,20 @@
77
# pylint: disable=too-many-statements
88

99
from knack.arguments import CLIArgumentType
10-
10+
from argcomplete.completers import FilesCompleter
1111
from azure.cli.core.commands.parameters import (
1212
tags_type, get_location_type,
1313
get_enum_type, file_type,
1414
resource_group_name_type,
1515
get_three_state_flag)
16-
from azure.cli.command_modules.postgresql.validators import public_access_validator, maintenance_window_validator, ip_address_validator, \
16+
from azure.cli.command_modules.postgresql.utils._flexible_server_util import get_current_time
17+
from azure.cli.command_modules.postgresql.utils._util import get_autonomous_tuning_settings_map
18+
from azure.cli.command_modules.postgresql.utils.validators import public_access_validator, maintenance_window_validator, ip_address_validator, \
1719
retention_validator, validate_identity, validate_byok_identity, validate_identities, \
1820
virtual_endpoint_name_validator, node_count_validator, postgres_firewall_rule_name_validator, \
1921
db_renaming_cluster_validator
2022
from azure.cli.core.local_context import LocalContextAttribute, LocalContextAction
21-
2223
from .randomname.generate import generate_username
23-
from ._flexible_server_util import get_current_time
24-
from argcomplete.completers import FilesCompleter
25-
from ._util import get_autonomous_tuning_settings_map
2624

2725

2826
def load_arguments(self, _): # pylint: disable=too-many-statements, too-many-locals
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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+
# --------------------------------------------------------------------------------------------
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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+
)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.util import user_confirmation
8+
from knack.log import get_logger
9+
from ..utils.validators import validate_resource_group, validate_backup_name
10+
11+
logger = get_logger(__name__)
12+
13+
14+
def backup_create_func(client, resource_group_name, server_name, backup_name):
15+
validate_resource_group(resource_group_name)
16+
validate_backup_name(backup_name)
17+
18+
return client.begin_create(
19+
resource_group_name,
20+
server_name,
21+
backup_name)
22+
23+
24+
def ltr_precheck_func(client, resource_group_name, server_name, backup_name):
25+
validate_resource_group(resource_group_name)
26+
27+
return client.check_prerequisites(
28+
resource_group_name=resource_group_name,
29+
server_name=server_name,
30+
parameters={"backupSettings": {"backupName": backup_name}}
31+
)
32+
33+
34+
def ltr_start_func(client, resource_group_name, server_name, backup_name, sas_url):
35+
validate_resource_group(resource_group_name)
36+
37+
parameters = {
38+
"backupSettings": {
39+
"backupName": backup_name
40+
},
41+
"targetDetails": {
42+
"sasUriList": [sas_url]
43+
}
44+
}
45+
46+
return client.begin_start(
47+
resource_group_name=resource_group_name,
48+
server_name=server_name,
49+
parameters=parameters
50+
)
51+
52+
53+
def backup_delete_func(client, resource_group_name, server_name, backup_name, yes=False):
54+
validate_resource_group(resource_group_name)
55+
56+
if not yes:
57+
user_confirmation(
58+
"Are you sure you want to delete the backup '{0}' in server '{1}'".format(backup_name, server_name), yes=yes)
59+
60+
return client.begin_delete(
61+
resource_group_name,
62+
server_name,
63+
backup_name)

0 commit comments

Comments
 (0)