Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SQL] az sql server outbound-firewall-rule create: Azure CLI Commands for Outbound Firewall Rules #18671

Merged
merged 9 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/azure-cli/azure/cli/command_modules/sql/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,31 @@ def _firewall_rule_table_format(result):
return _apply_format(result, _firewall_rule_table_format)


########################################################
# sql server outbound-firewall-rule #
########################################################


def outbound_firewall_rule_table_format(result):
'''
Formats a single or list of server outbound firewall rules as summary results for display with "-o table".
'''

def _outbound_firewall_rule_table_format(result):
'''
Formats a server outbound firewall rule as summary results for display with "-o table".
'''
from collections import OrderedDict

return OrderedDict([
('resourceGroupName', result['resourceGroupName']),
('serverName', result['serverName']),
('outboundRuleFqdn', result['outboundRuleFqdn'])
])

return _apply_format(result, _outbound_firewall_rule_table_format)


###############################################
# sql mi #
###############################################
Expand Down
43 changes: 43 additions & 0 deletions src/azure-cli/azure/cli/command_modules/sql/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,49 @@
text: az sql server firewall-rule update -g mygroup -s myserver -n myrule --start-ip-address 5.4.3.2 --end-ip-address 9.8.7.6
"""

helps['sql server outbound-firewall-rule'] = """
type: group
short-summary: Manage a server's outbound firewall rules.
"""

helps['sql server outbound-firewall-rule create'] = """
type: command
short-summary: Create a new outbound firewall rule.
examples:
- name: Create a new outbound firewall rule
text: az sql server outbound-firewall-rule create -g mygroup -s myserver -n allowedFQDN
- name: Create a new outbound firewall rule
text: az sql server outbound-firewall-rule create -g mygroup -s myserver --outbound-rule-fqdn allowedFQDN
"""

helps['sql server outbound-firewall-rule list'] = """
type: command
short-summary: List a server's outbound firewall rules.
examples:
- name: List a server's outbound firewall rules
text: az sql server outbound-firewall-rule list -g mygroup -s myserver
"""

helps['sql server outbound-firewall-rule show'] = """
type: command
short-summary: Show the details for an outbound firewall rule.
examples:
- name: Show the outbound firewall rule
text: az sql server outbound-firewall-rule show -g mygroup -s myserver -n myrule
- name: Show the outbound firewall rule
text: az sql server outbound-firewall-rule show -g mygroup -s myserver --outbound-rule-fqdn allowedFQDN
"""

helps['sql server outbound-firewall-rule delete'] = """
type: command
short-summary: Delete the outbound firewall rule.
examples:
- name: Delete the outbound firewall rule
text: az sql server outbound-firewall-rule delete -g mygroup -s myserver -n myrule
- name: Delete the outbound firewall rule
text: az sql server outbound-firewall-rule delete -g mygroup -s myserver --outbound-rule-fqdn allowedFQDN
"""

helps['sql server key'] = """
type: group
short-summary: Manage a server's keys.
Expand Down
20 changes: 20 additions & 0 deletions src/azure-cli/azure/cli/command_modules/sql/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,13 @@ def _configure_security_policy_storage_params(arg_ctx):
'only connections made through Private Links can reach this server.',
is_preview=True)

c.argument('restrict_outbound_network_access',
options_list=['--restrict-outbound-network-access', '-r'],
arg_type=get_three_state_flag(),
help='Set whether outbound network access to server is restricted or not. When true,'
'the outbound connections from the server will be restricted.',
is_preview=True)

c.argument('primary_user_assigned_identity_id',
options_list=['--primary-user-assigned-identity-id', '--pid'],
help='The ID of the primary user managed identity.')
Expand Down Expand Up @@ -1652,6 +1659,19 @@ def _configure_security_policy_storage_params(arg_ctx):
help='The end IP address of the firewall rule. Must be IPv4 format. Use value'
' \'0.0.0.0\' to represent all Azure-internal IP addresses.')

#####
# sql server outbound firewall-rule
#####
with self.argument_context('sql server outbound-firewall-rule') as c:
# Help text needs to be specified because 'sql server outbound-firewall-rule update' is a custom
# command.
c.argument('server_name',
arg_type=server_param_type)

c.argument('outbound_rule_fqdn',
options_list=['--outbound-rule-fqdn', '-n'],
help='The allowed FQDN for the outbound firewall rule.')

#####
# sql server key
#####
Expand Down
4 changes: 4 additions & 0 deletions src/azure-cli/azure/cli/command_modules/sql/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ def get_sql_firewall_rules_operations(cli_ctx, _):
return get_sql_management_client(cli_ctx).firewall_rules


def get_sql_outbound_firewall_rules_operations(cli_ctx, _):
return get_sql_management_client(cli_ctx).outbound_firewall_rules


def get_sql_instance_pools_operations(cli_ctx, _):
return get_sql_management_client(cli_ctx).instance_pools

Expand Down
17 changes: 17 additions & 0 deletions src/azure-cli/azure/cli/command_modules/sql/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
elastic_pool_edition_table_format,
firewall_rule_table_format,
instance_pool_table_format,
outbound_firewall_rule_table_format,
server_table_format,
usage_table_format,
LongRunningOperationResultTransform,
Expand Down Expand Up @@ -44,6 +45,7 @@
get_sql_encryption_protectors_operations,
get_sql_failover_groups_operations,
get_sql_firewall_rules_operations,
get_sql_outbound_firewall_rules_operations,
get_sql_instance_pools_operations,
get_sql_managed_databases_operations,
get_sql_managed_database_restore_details_operations,
Expand Down Expand Up @@ -532,6 +534,21 @@ def load_command_table(self, _):
g.command('list', 'list_by_server',
table_transformer=firewall_rule_table_format)

outbound_firewall_rules_operations = CliCommandType(
operations_tmpl='azure.mgmt.sql.operations#OutboundFirewallRulesOperations.{}',
client_factory=get_sql_outbound_firewall_rules_operations)

with self.command_group('sql server outbound-firewall-rule',
outbound_firewall_rules_operations,
client_factory=get_sql_outbound_firewall_rules_operations) as g:
g.custom_command('create', 'outbound_firewall_rule_create',
table_transformer=outbound_firewall_rule_table_format)
g.command('delete', 'delete')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about add confirmation for delete command?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not need the confirmation since it is one property itself. I am just following the procedure that we followed for Powershell APIs.

g.show_command('show', 'get',
table_transformer=outbound_firewall_rule_table_format)
g.command('list', 'list_by_server',
table_transformer=outbound_firewall_rule_table_format)

aadadmin_operations = CliCommandType(
operations_tmpl='azure.mgmt.sql.operations#ServerAzureADAdministratorsOperations.{}',
client_factory=get_sql_server_azure_ad_administrators_operations)
Expand Down
31 changes: 31 additions & 0 deletions src/azure-cli/azure/cli/command_modules/sql/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -3451,6 +3451,7 @@ def server_create(
assign_identity=False,
no_wait=False,
enable_public_network=None,
restrict_outbound_network_access=None,
key_id=None,
user_assigned_identity_id=None,
primary_user_assigned_identity_id=None,
Expand All @@ -3474,6 +3475,11 @@ def server_create(
ServerNetworkAccessFlag.enabled if enable_public_network
else ServerNetworkAccessFlag.disabled)

if restrict_outbound_network_access is not None:
kwargs['restrict_outbound_network_access'] = (
ServerNetworkAccessFlag.enabled if restrict_outbound_network_access
else ServerNetworkAccessFlag.disabled)

kwargs['key_id'] = key_id

kwargs['primary_user_assigned_identity_id'] = primary_user_assigned_identity_id
Expand Down Expand Up @@ -3543,6 +3549,7 @@ def server_update(
assign_identity=False,
minimal_tls_version=None,
enable_public_network=None,
restrict_outbound_network_access=None,
primary_user_assigned_identity_id=None,
key_id=None,
identity_type=None,
Expand Down Expand Up @@ -3572,6 +3579,11 @@ def server_update(
ServerNetworkAccessFlag.enabled if enable_public_network
else ServerNetworkAccessFlag.disabled)

if restrict_outbound_network_access is not None:
instance.public_network_access = (
ServerNetworkAccessFlag.enabled if restrict_outbound_network_access
else ServerNetworkAccessFlag.disabled)

instance.primary_user_assigned_identity_id = (
primary_user_assigned_identity_id or instance.primary_user_assigned_identity_id)

Expand Down Expand Up @@ -3690,6 +3702,25 @@ def firewall_rule_create(
end_ip_address=end_ip_address))


#########################################################
# sql server outbound-firewall-rule #
#########################################################


def outbound_firewall_rule_create(
client,
server_name,
resource_group_name,
outbound_rule_fqdn):
'''
Creates a new outbound firewall rule.
'''
return client.create_or_update(
server_name=server_name,
resource_group_name=resource_group_name,
outbound_rule_fqdn=outbound_rule_fqdn)


#####
# sql server key
#####
Expand Down
Loading