Skip to content

Commit

Permalink
Merge pull request Azure#13 from DaeunYim/local-context
Browse files Browse the repository at this point in the history
Adding local context and test code for new parameters and changes
  • Loading branch information
arde0708 authored Sep 9, 2020
2 parents 36d81a8 + 550c733 commit d3b92d7
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 134 deletions.
49 changes: 39 additions & 10 deletions src/azure-cli/azure/cli/command_modules/rdbms/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,27 @@ def _flexible_server_params(command_group):
local_context_attribute=LocalContextAttribute(name='server_name',
actions=[LocalContextAction.SET, LocalContextAction.GET], scopes=['{} flexible-server'.format(command_group)]))

administrator_login_setter_arg_type = CLIArgumentType(metavar='NAME',
local_context_attribute=LocalContextAttribute(name='administrator_login',
actions=[LocalContextAction.SET], scopes=['{} flexible-server'.format(command_group)]))

administrator_login_arg_type = CLIArgumentType(metavar='NAME',
local_context_attribute=LocalContextAttribute(name='administrator_login',
actions=[LocalContextAction.GET, LocalContextAction.SET], scopes=['{} flexible-server'.format(command_group)]))

database_name_setter_arg_type = CLIArgumentType(metavar='NAME',
local_context_attribute=LocalContextAttribute(name='database_name',
actions=[LocalContextAction.SET], scopes=['{} flexible-server'.format(command_group)]))

database_name_getter_arg_type = CLIArgumentType(metavar='NAME',
local_context_attribute=LocalContextAttribute(name='database_name',
actions=[LocalContextAction.GET], scopes=['{} flexible-server'.format(command_group)]))

database_name_arg_type = CLIArgumentType(metavar='NAME',
local_context_attribute=LocalContextAttribute(name='database_name',
actions=[LocalContextAction.GET, LocalContextAction.SET], scopes=['{} flexible-server'.format(command_group)]))


overriding_none_arg_type = CLIArgumentType(local_context_attribute=LocalContextAttribute(name='context_name', actions=[LocalContextAction.GET]))

with self.argument_context('{} flexible-server'.format(command_group)) as c:
Expand All @@ -217,7 +238,7 @@ def _flexible_server_params(command_group):
if command_group == 'postgres':
c.argument('tier', default='GeneralPurpose', options_list=['--tier'],
help='Compute tier of the server. Accepted values: Burstable, GeneralPurpose, Memory Optimized ')
c.argument('sku_name', default='Standard_D4s_v3', options_list=['--sku-name'],
c.argument('sku_name', default='Standard_D2s_v3', options_list=['--sku-name'],
help='The name of the compute SKU. Follows the convention Standard_{VM name}. Examples: Standard_B1ms, Standard_D4s_v3 ')
c.argument('storage_mb', default='128', options_list=['--storage-size'], type=int,
validator=pg_storage_validator,
Expand Down Expand Up @@ -249,7 +270,7 @@ def _flexible_server_params(command_group):
help='Name or ID of the subnet that allows access to an Azure Flexible Server. ')
c.argument('server_name', options_list=['--name', '-n'], arg_type=server_name_setter_arg_type)
c.argument('location', arg_type=get_location_type(self.cli_ctx))#, validator=get_default_location_from_resource_group)
c.argument('administrator_login', default=generate_username(), options_list=['--admin-user, -u'], arg_group='Authentication',
c.argument('administrator_login', default=generate_username(), options_list=['--admin-user, -u'], arg_group='Authentication', arg_type=administrator_login_setter_arg_type,
help='Administrator username for the server. Once set, it cannot be changed. ')
c.argument('administrator_login_password', options_list=['--admin-password', '-p'],
help='The password of the administrator. Minimum 8 characters and maximum 128 characters. Password must contain characters from three of the following categories: English uppercase letters, English lowercase letters, numbers, and non-alphanumeric characters.',
Expand All @@ -261,12 +282,10 @@ def _flexible_server_params(command_group):
c.argument('public_access', options_list=['--public-access'],
help='Determines the public access. Enter single or range of IP addresses to be included in the allowed list of IPs. IP address ranges must be dash-separated and not contain any spaces. Specifying 0.0.0.0 allows public access from any resources deployed within Azure to access your server. Specifying no IP address sets the server in public access mode but does not create a firewall rule. ',
validator=public_access_validator)

c.argument('high_availability', default="Disabled", options_list=['--high-availability'], help='Enable or disable high availability feature. Default value is Disabled.')

c.ignore('database_name')
c.argument('assign_identity', options_list=['--assign-identity'],
help='Generate and assign an Azure Active Directory Identity for this server for use with key management services like Azure KeyVault. No need to enter extra argument.')
c.ignore('database_name')

with self.argument_context('{} flexible-server delete'.format(command_group)) as c:
c.argument('server_name', id_part='name', options_list=['--name', '-n'], arg_type=server_name_getter_arg_type)
Expand All @@ -282,12 +301,11 @@ def _flexible_server_params(command_group):
help='The point in time to restore from (ISO8601 format), e.g., 2017-04-26T02:10:00+08:00')

with self.argument_context('{} flexible-server update'.format(command_group)) as c:
c.ignore('assign_identity')
c.argument('sku_name', options_list=['--sku-name'],
help='The name of the sku. Follows the convention {pricing tier}_{compute generation}_{vCores} in shorthand. Examples: B_Gen5_1, GP_Gen5_4, MO_Gen5_16.')
c.argument('tier', default='GeneralPurpose', options_list=['--tier'],
help='Compute tier of the server. Accepted values: Burstable, GeneralPurpose, Memory Optimized ')
c.argument('assign_identity', options_list=['--assign-identity'],
help='Generate and assign an Azure Active Directory Identity for this server for use with key management services like Azure KeyVault. No need to enter extra argument.')
c.argument('tags', options_list=['--tags'],
help='Space-separated tags: key[=value] [key[=value] ...]. Use \"\" to clear existing tags.')
c.argument('backup_retention', type=int, options_list=['--backup-retention'],
Expand Down Expand Up @@ -326,7 +344,6 @@ def _flexible_server_params(command_group):
for scope in ['list', 'set', 'show']:
argument_context_string = '{} flexible-server parameter {}'.format(command_group, scope)
with self.argument_context(argument_context_string) as c:
# c.argument('resource_group_name', arg_type=resource_group_name_type)
c.argument('server_name', id_part='name', options_list=['--server-name', '-s'], arg_type=server_name_arg_type)
c.argument('json', options_list=['--json'], help='Output in json format. true/false')

Expand Down Expand Up @@ -369,14 +386,26 @@ def _flexible_server_params(command_group):
help='When using \'set\' or \'add\', preserve string literals instead of attempting to convert to JSON.')
c.argument('start_ip_address', options_list=['--start-ip-address'],
help='The start IP address of the firewall rule. Must be IPv4 format. Use value \'0.0.0.0\' to represent all Azure-internal IP addresses. ')

# db
with self.argument_context('{} flexible-server db'.format(command_group)) as c:
c.argument('server_name', options_list=['--server-name', '-s'], help='Name of the server.')
c.argument('database_name', arg_type=database_name_arg_type, options_list=['--database-name', '-d'], help='The name of a database.')

with self.argument_context('{} flexible-server db create'.format(command_group)) as c:
c.argument('database_name', arg_type=database_name_setter_arg_type, options_list=['--database-name', '-d'], help='The name of a database.')

with self.argument_context('{} flexible-server db delete'.format(command_group)) as c:
c.argument('database_name', arg_type=database_name_getter_arg_type, options_list=['--database-name', '-d'], help='The name of a database.')


with self.argument_context('{} flexible-server show-connection-string'.format(command_group)) as c:
c.argument('server_name', options_list=['--server-name', '-s'], help='Name of the server.')
c.argument('administrator_login', options_list=['--admin-user', '-u'],
c.argument('administrator_login', arg_type=administrator_login_arg_type, options_list=['--admin-user', '-u'],
help='The login username of the administrator.')
c.argument('administrator_login_password', options_list=['--admin-password', '-p'],
help='The login password of the administrator.')
c.argument('database_name', options_list=['--database-name', '-d'], help='The name of a database.')
c.argument('database_name', arg_type=database_name_arg_type, options_list=['--database-name', '-d'], help='The name of a database.')


_flexible_server_params('postgres')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def _server_delete_func(cmd, client, resource_group_name=None, server_name=None,


## Parameter update command
def _flexible_parameter_update(client, ids, server_name, configuration_name, resource_group_name, source=None, value=None):
def _flexible_parameter_update(client, server_name, configuration_name, resource_group_name, source=None, value=None):
if source is None and value is None:
# update the command with system default
try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def _flexible_server_create(cmd, client,
logger.warning('Make a note of your password. If you forget, you would have to'\
' reset your password with CLI command for reset password')

_update_local_contexts(cmd, server_name, resource_group_name, location)
_update_local_contexts(cmd, server_name, resource_group_name, location, user)

return _form_response(user, sku, loc, id, host, version,
administrator_login_password if administrator_login_password is not None else '*****',
Expand Down Expand Up @@ -192,6 +192,7 @@ def _server_delete_func(cmd, client, resource_group_name=None, server_name=None,
if cmd.cli_ctx.local_context.is_on:
local_context_file = cmd.cli_ctx.local_context._get_local_context_file()
local_context_file.remove_option('postgres flexible-server', 'server_name')
local_context_file.remove_option('postgres flexible-server', 'administrator_login')
except Exception as ex: # pylint: disable=broad-except
logger.error(ex)
return result
Expand All @@ -203,7 +204,7 @@ def _flexible_server_postgresql_get(cmd, resource_group_name, server_name):
return client.servers.get(resource_group_name, server_name)


def _flexible_parameter_update(client, ids, server_name, configuration_name, resource_group_name, source=None, value=None):
def _flexible_parameter_update(client, server_name, configuration_name, resource_group_name, source=None, value=None):
if source is None and value is None:
# update the command with system default
try:
Expand Down Expand Up @@ -342,15 +343,16 @@ def _form_response(username, sku, location, id, host, version, password, connect
return output


def _update_local_contexts(cmd, server_name, resource_group_name, location):
def _update_local_contexts(cmd, server_name, resource_group_name, location, user):
if cmd.cli_ctx.local_context.is_on:
cmd.cli_ctx.local_context.set(['postgres flexible-server'], 'server_name',
server_name) # Setting the server name in the local context
cmd.cli_ctx.local_context.set(['postgres flexible-server'], 'administrator_login',
user) # Setting the server name in the local context
cmd.cli_ctx.local_context.set([ALL], 'location',
location) # Setting the location in the local context
cmd.cli_ctx.local_context.set([ALL], 'resource_group_name', resource_group_name)


# pylint: disable=too-many-instance-attributes,too-few-public-methods
class DbContext:
def __init__(self, azure_sdk=None, logging_name=None, cf_firewall=None,
Expand Down
Loading

0 comments on commit d3b92d7

Please sign in to comment.