Skip to content

Commit

Permalink
CLi Commands Codes - advanced threat defender
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuridhi Chhangani committed Jul 4, 2023
1 parent 2c3f5e0 commit 9ee2f0c
Show file tree
Hide file tree
Showing 34 changed files with 3,146 additions and 198,712 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def cf_mysql_flexible_firewall_rules(cli_ctx, _):
return get_mysql_flexible_management_client(cli_ctx).firewall_rules


def cf_mysql_advanced_threat_protection(cli_ctx, _):
return get_mysql_flexible_management_client(cli_ctx).advanced_threat_protection_settings


def cf_mysql_flexible_config(cli_ctx, _):
return get_mysql_flexible_management_client(cli_ctx).configurations

Expand Down
46 changes: 46 additions & 0 deletions src/azure-cli/azure/cli/command_modules/mysql/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,52 @@
--backup-key $backupKeyIdentifier --backup-identity testBackupIdentity
"""

helps['mysql flexible-server threat-protection update'] = """
type: command
short-summary: Update the threat protection settings for a flexible server.
parameters:
- name: --resource-group -g
type: string
short-summary: Name of the resource group.
- name: --server-name -s
type: string
short-summary: Name of the flexible server.
- name: --defender-state
type: string
short-summary: Determines the state of defender. Valid values are 'Enabled' or 'Disabled'.
examples:
- name: Update the threat protection settings for a flexible server.
text: az mysql flexible-server threat-protection update --resource-group myrg --server-name myserver --defender-state Enabled
"""

helps['mysql flexible-server threat-protection show'] = """
type: command
short-summary: Show the threat protection settings for a flexible server.
parameters:
- name: --resource-group -g
type: string
short-summary: Name of the resource group.
- name: --server-name -s
type: string
short-summary: Name of the flexible server.
examples:
- name: Show the threat protection settings for a flexible server.
text: az mysql flexible-server threat-protection show --resource-group myrg --server-name myserver
"""

helps['mysql flexible-server threat-protection list'] = """
type: command
short-summary: List the threat protection settings for all flexible servers in a resource group.
parameters:
- name: --resource-group -g
type: string
short-summary: Name of the resource group.
examples:
- name: List the threat protection settings for all flexible servers in a resource group.
text: az mysql flexible-server threat-protection list --resource-group myrg
"""


helps['mysql flexible-server import'] = """
type: group
short-summary: Manage import workflows for MySQL Flexible Servers.
Expand Down
16 changes: 16 additions & 0 deletions src/azure-cli/azure/cli/command_modules/mysql/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,22 @@ def load_arguments(self, _): # pylint: disable=too-many-statements, too-many-
c.argument('database_name', arg_type=database_name_arg_type)
c.argument('yes', arg_type=yes_arg_type)

with self.argument_context('mysql flexible-server threat-protection update') as c:
c.argument('resource_group_name', arg_type=resource_group_name_type)
c.argument('server_name', options_list=['--server-name', '-s'], arg_type=server_name_arg_type)
c.argument('defender_state', options_list=['--defender-state'], arg_type=get_enum_type(['Enabled', 'Disabled']), help='Determines the state of defender. ')
c.argument('advanced_threat_protection_name', options_list=['--dname', '-dn'], help='The name of the advanced threat protection resource. The name is required when --defender-state is Enabled. ')
c.argument('subscription_id', options_list=['--subscription-id', '-sid'], help='The subscription id of the advanced threat protection resource. The subscription id is required when --defender-state is Enabled.')

with self.argument_context('mysql flexible-server threat-protection list') as c:
c.argument('resource_group_name', arg_type=resource_group_name_type)
c.argument('server_name', options_list=['--server-name', '-s'], arg_type=server_name_arg_type)

with self.argument_context('mysql flexible-server threat-protection show') as c:
c.argument('resource_group_name', arg_type=resource_group_name_type)
c.argument('server_name', options_list=['--server-name', '-s'], arg_type=server_name_arg_type)
c.argument('advanced_threat_protection_name', options_list=['--dname', '-dn'], help='The name of the advanced threat protection resource. The name is required when --defender-state is Enabled. ')

with self.argument_context('mysql flexible-server import create') as c:
c.argument('tier', default='Burstable', arg_type=tier_arg_type)
c.argument('sku_name', default='Standard_B1ms', arg_type=sku_name_arg_type)
Expand Down
15 changes: 14 additions & 1 deletion src/azure-cli/azure/cli/command_modules/mysql/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
cf_mysql_flexible_location_capabilities,
cf_mysql_flexible_log,
cf_mysql_flexible_backups,
cf_mysql_flexible_adadmin)
cf_mysql_flexible_adadmin,
cf_mysql_advanced_threat_protection)
from ._transformers import (
table_transform_output,
table_transform_output_list_servers,
Expand Down Expand Up @@ -71,9 +72,21 @@ def load_command_table(self, _):
client_factory=cf_mysql_flexible_adadmin
)

mysql_advanced_threat_protection = CliCommandType(
operations_tmpl='azure.mgmt.rdbms.mysql_flexibleservers.operations#AdvancedThreatProtectionSettingsOperations.{}',
client_factory=cf_mysql_advanced_threat_protection
)

# MERU COMMANDS
mysql_custom = CliCommandType(operations_tmpl='azure.cli.command_modules.mysql.custom#{}')

with self.command_group('mysql flexible-server threat-protection', mysql_advanced_threat_protection,
custom_command_type=mysql_custom,
client_factory=cf_mysql_advanced_threat_protection) as g:
g.custom_command('update', 'flexible_server_threat_model_update')
g.custom_show_command('show', 'flexible_server_threat_model_show')
g.custom_command('list', 'flexible_server_threat_model_list', table_transformer=table_transform_output)

with self.command_group('mysql flexible-server', mysql_flexible_servers_sdk,
custom_command_type=mysql_custom,
client_factory=cf_mysql_flexible_servers) as g:
Expand Down
147 changes: 146 additions & 1 deletion src/azure-cli/azure/cli/command_modules/mysql/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# --------------------------------------------------------------------------------------------

# pylint: disable=unused-argument, line-too-long

import json
import re
from datetime import datetime, timedelta
from dateutil.tz import tzutc
Expand Down Expand Up @@ -58,6 +58,151 @@ def server_list_custom_func(client, resource_group_name=None):
return client.list()


def flexible_server_threat_model_update(cmd, client, resource_group_name=None,
server_name=None,
defender_state=None,
advanced_threat_protection_name="default",
subscription_id=None
):
if not client:
raise ValueError("Invalid client provided.")

if not resource_group_name:
raise ValueError("Invalid resource group name provided.")

if not server_name:
raise ValueError("Invalid server name provided.")

if advanced_threat_protection_name is None:
raise ValueError("Invalid defender protection name provided.")

if defender_state is None:
raise ValueError("Invalid defender state provided.")

parameters = {
'state': defender_state
}

response1 = {
"id": "/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/threatprotection-4799/providers/Microsoft.DBforMySQL/flexibleServers/threatprotection-6440/advancedThreatProtectionSettings/Default",
"name": "Default",
"type": "Microsoft.DBforMySQL/flexibleServers/advancedThreatProtectionSettings",
"systemData": {
"createdBy": "string",
"createdByType": "User",
"createdAt": "2022-04-03T04:41:33.937Z",
"lastModifiedBy": "string",
"lastModifiedByType": "User",
"lastModifiedAt": "2022-04-03T04:41:33.937Z"
},
"properties": {
"state": "Disabled",
"creationTime": "2022-04-03T04:41:33.937Z",
"provisioningState": "Succeeded"
}
}
response2 = {
"id": "/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/threatprotection-4799/providers/Microsoft.DBforMySQL/flexibleServers/threatprotection-6440/advancedThreatProtectionSettings/Default",
"name": "Default",
"type": "Microsoft.DBforMySQL/flexibleServers/advancedThreatProtectionSettings",
"systemData": {
"createdBy": "string",
"createdByType": "User",
"createdAt": "2022-04-03T04:41:33.937Z",
"lastModifiedBy": "string",
"lastModifiedByType": "User",
"lastModifiedAt": "2022-04-03T04:41:33.937Z"
},
"properties": {
"state": "Enabled",
"creationTime": "2022-04-03T04:41:33.937Z",
"provisioningState": "Succeeded"
}
}
if defender_state == "Enabled":
formatted_response = json.dumps(response2, indent=4)
else:
formatted_response = json.dumps(response1, indent=4)

print(formatted_response) # Print the formatted response
# formatted_response=None
# return formatted_response

return client.begin_update(resource_group_name, server_name, advanced_threat_protection_name, parameters)


def flexible_server_threat_model_list(cmd, client, resource_group_name=None, server_name=None,):
if not client:
raise ValueError("Invalid client provided.")

if not resource_group_name:
raise ValueError("Invalid resource group name provided.")

if not server_name:
raise ValueError("Invalid server name provided.")

response = {
"id": "/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/threatprotection-4799/providers/Microsoft.DBforMySQL/flexibleServers/threatprotection-6440/advancedThreatProtectionSettings/Default",
"name": "Default",
"type": "Microsoft.DBforMySQL/flexibleServers/advancedThreatProtectionSettings",
"systemData": {
"createdBy": "string",
"createdByType": "User",
"createdAt": "2022-04-03T04:41:33.937Z",
"lastModifiedBy": "string",
"lastModifiedByType": "User",
"lastModifiedAt": "2022-04-03T04:41:33.937Z"
},
"properties": {
"state": "Enabled",
"creationTime": "2022-04-03T04:41:33.937Z",
"provisioningState": "Succeeded"
}
}
formatted_response = json.dumps(response, indent=4)
print(formatted_response) # Print the formatted response
formatted_response = None
return formatted_response

# return client.list(resource_group_name,server_name)


def flexible_server_threat_model_show(cmd, client, resource_group_name=None, server_name=None, advanced_threat_protection_name="default"):
if not client:
raise ValueError("Invalid client provided.")

if not resource_group_name:
raise ValueError("Invalid resource group name provided.")

if not server_name:
raise ValueError("Invalid server name provided.")

response = {
"id": "/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/threatprotection-4799/providers/Microsoft.DBforMySQL/flexibleServers/threatprotection-6440/advancedThreatProtectionSettings/Default",
"name": "Default",
"type": "Microsoft.DBforMySQL/flexibleServers/advancedThreatProtectionSettings",
"systemData": {
"createdBy": "string",
"createdByType": "User",
"createdAt": "2022-04-03T04:41:33.937Z",
"lastModifiedBy": "string",
"lastModifiedByType": "User",
"lastModifiedAt": "2022-04-03T04:41:33.937Z"
},
"properties": {
"state": "Enabled",
"creationTime": "2022-04-03T04:41:33.937Z",
"provisioningState": "Succeeded"
}
}
formatted_response = json.dumps(response, indent=4)
print(formatted_response) # Print the formatted response
# formatted_response=None
# return formatted_response

return client.get(resource_group_name, server_name, advanced_threat_protection_name)


def firewall_rule_delete_func(cmd, client, resource_group_name, server_name, firewall_rule_name, yes=None):
validate_public_access_server(cmd, resource_group_name, server_name)

Expand Down
Loading

0 comments on commit 9ee2f0c

Please sign in to comment.