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

Adding CRUD azure cli commands for Managed instance and Managed database resources #6428

Merged
merged 11 commits into from
May 31, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -426,83 +426,83 @@
- name: Create a vnet rule by providing the vnet and subnet name. The subnet id is created by taking the resource group name and subscription id of the SQL server.
text: az sql server vnet-rule create --subnet subnetName --vnet-name vnetName
"""
helps['sql managed-instance'] = """
helps['sql mi'] = """
type: group
short-summary: Manage SQL managed instances.
"""
helps['sql managed-instance create'] = """
helps['sql mi create'] = """
type: command
short-summary: Create a managed instance.
examples:
- name: Create a managed instance with specified parameters and with identity
text: az sql managed-instance create -g mygroup -n myinstance -l mylocation -i -u myusername -p mypassword --license-type mylicensetype --subnet mysubnetid --capacity vcorecapacity --storage storagesize --edition editionname --family familyname
text: az sql mi create -g mygroup -n myinstance -l mylocation -i -u myusername -p mypassword --license-type mylicensetype --subnet mysubnetid --capacity vcorecapacity --storage storagesize --edition editionname --family familyname
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's have more real-looking values where possible. i.e.:

az sql mi create -g mygroup -n myinstance -l mylocation -i -u myusername -p mypassword --license-type LicenseIncluded --subnet /subscriptions/{SubID}/resourceGroups/{ResourceGroup}/providers/Microsoft.Network/virtualNetworks/{VNETName}/subnets/{SubnetName} --capacity 2 --storage 32GB --edition BusinessCritical --family Gen4

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for adding example btw :) Can you also add example with minimal parameters?

"""
helps['sql managed-instance list'] = """
helps['sql mi list'] = """
type: command
short-summary: List available managed instances.
examples:
- name: List all managed instances in the current subscription.
text: az sql managed-instance list
text: az sql mi list
- name: List all managed instances in a resource group.
text: az sql managed-instance list -g mygroup
text: az sql mi list -g mygroup
"""
helps['sql managed-instance show'] = """
helps['sql mi show'] = """
type: command
short-summary: Get the details for a managed instance.
examples:
- name: Get the details for a managed instance
text: az sql managed-instance show -g mygroup -n myinstance
text: az sql mi show -g mygroup -n myinstance
"""
helps['sql managed-instance update'] = """
helps['sql mi update'] = """
type: command
short-summary: Update a managed instance.
examples:
- name: Updates a managed-instance with specified parameters and with identity
text: az sql managed-instance update -g mygroup -n myinstance -i -p mypassword --license-type mylicensetype --capacity vcorecapacity --storage storagesize
- name: Updates a mi with specified parameters and with identity
text: az sql mi update -g mygroup -n myinstance -i -p mypassword --license-type mylicensetype --capacity vcorecapacity --storage storagesize
"""
helps['sql managed-instance delete'] = """
helps['sql mi delete'] = """
type: command
short-summary: Delete a managed instance.
examples:
- name: Delete a managed instance
text: az sql managed-instance delete -g mygroup -n myinstance --yes
text: az sql mi delete -g mygroup -n myinstance --yes
"""
helps['sql managed-db'] = """
helps['sql midb'] = """
type: group
short-summary: Manage SQL managed databases.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you change this to Manage SQL managed instance databases. ?

"""
helps['sql managed-db create'] = """
helps['sql midb create'] = """
type: command
short-summary: Create a managed database.
examples:
- name: Create a managed database with specified collation
text: az sql managed-db create -g mygroup --mi myinstance -n mymanageddb --collation Latin1_General_100_CS_AS_SC
text: az sql midb create -g mygroup --mi myinstance -n mymanageddb --collation Latin1_General_100_CS_AS_SC
"""
helps['sql managed-db list'] = """
helps['sql midb list'] = """
type: command
short-summary: List maanged databases on a managed instance.
examples:
- name: List managed databases on a managed instance
text: az sql managed-db list -g mygroup --mi myinstance
text: az sql midb list -g mygroup --mi myinstance
"""
helps['sql managed-db show'] = """
helps['sql midb show'] = """
type: command
short-summary: Get the details for a managed database.
examples:
- name: Get the details for a managed database
text: az sql managed-db show -g mygroup --mi myinstance -n mymanageddb
text: az sql midb show -g mygroup --mi myinstance -n mymanageddb
"""
helps['sql managed-db restore'] = """
helps['sql midb restore'] = """
type: command
short-summary: Restore a managed database.
examples:
- name: Restore a managed database using Point in time restore
text: az sql managed-db restore -g mygroup --mi myinstance -n mymanageddb --target-db targetmidb --time "2018-05-20T05:34:22"
text: az sql midb restore -g mygroup --mi myinstance -n mymanageddb --target-db targetmidb --time "2018-05-20T05:34:22"
"""
helps['sql managed-db delete'] = """
helps['sql midb delete'] = """
type: command
short-summary: Delete a managed database.
examples:
- name: Delete a managed database
text: az sql managed-db delete -g mygroup --mi myinstance -n mymanageddb --yes
text: az sql midb delete -g mygroup --mi myinstance -n mymanageddb --yes
"""
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ def __repr__(self):

db_service_objective_examples = 'Basic, S0, P1, GP_Gen4_1, BC_Gen5_2.'
dw_service_objective_examples = 'DW100, DW1000c'
mi_service_objective_examples = 'GP_Gen4, GP_Gen5'


###############################################
Expand Down Expand Up @@ -1027,7 +1026,7 @@ def _configure_security_policy_storage_params(arg_ctx):
###############################################
# sql managed instance #
###############################################
with self.argument_context('sql managed-instance') as c:
with self.argument_context('sql mi') as c:
c.argument('managed_instance_name',
help='The managed instance name',
options_list=['--name', '-n'],
Expand All @@ -1046,7 +1045,7 @@ def _configure_security_policy_storage_params(arg_ctx):
c.argument('storage_size_in_gb',
options_list=['--storage'],
arg_type=storage_param_type,
help='Determines how much storage size to associate with Managed instance. '
help='The storage size of the managed instance. '
'Storage size must be specified in increments of 32 GB')

c.argument('license_type',
Expand All @@ -1055,9 +1054,9 @@ def _configure_security_policy_storage_params(arg_ctx):

c.argument('vcores',
options_list=['--capacity', '-c'],
help='Determines how much VCore to associate with Managed instance.')
help='The capacity of the managed instance in vcores.')

with self.argument_context('sql managed-instance create') as c:
with self.argument_context('sql mi create') as c:
# Create args that will be used to build up the ManagedInstance object
create_args_for_complex_type(
c, 'parameters', ManagedInstance, [
Expand Down Expand Up @@ -1103,7 +1102,7 @@ def _configure_security_policy_storage_params(arg_ctx):
help='Generate and assign an Azure Active Directory Identity for this managed instance '
'for use with key management services like Azure KeyVault.')

with self.argument_context('sql managed-instance update') as c:
with self.argument_context('sql mi update') as c:
# Create args that will be used to build up the ManagedInstance object
create_args_for_complex_type(
c, 'parameters', ManagedInstance, [
Expand All @@ -1121,7 +1120,7 @@ def _configure_security_policy_storage_params(arg_ctx):
###############################################
# sql managed db #
###############################################
with self.argument_context('sql managed-db') as c:
with self.argument_context('sql midb') as c:
c.argument('managed_instance_name',
arg_type=managed_instance_param_type,
# Allow --ids command line argument. id_part=name is 1st name in uri
Expand All @@ -1133,7 +1132,7 @@ def _configure_security_policy_storage_params(arg_ctx):
# Allow --ids command line argument. id_part=child_name_1 is 2nd name in uri
id_part='child_name_1')

with self.argument_context('sql managed-db create') as c:
with self.argument_context('sql midb create') as c:
create_args_for_complex_type(
c, 'parameters', ManagedDatabase, [
'collation',
Expand All @@ -1144,17 +1143,25 @@ def _configure_security_policy_storage_params(arg_ctx):
help='The collation of the Azure SQL Managed Database collation to use, '
'e.g.: SQL_Latin1_General_CP1_CI_AS or Latin1_General_100_CS_AS_SC')

with self.argument_context('sql managed-db restore') as c:
with self.argument_context('sql midb restore') as c:
create_args_for_complex_type(
c, 'parameters', ManagedDatabase, [
'target_managed_database_name',
'target_managed_instance_name',
'restore_point_in_time'
])

c.argument('target_managed_database_name',
options_list=['--target-db'],
Copy link
Contributor

Choose a reason for hiding this comment

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

I just remembered that sql db restore uses --dest-name param names. See https://docs.microsoft.com/en-us/cli/azure/sql/db?view=azure-cli-latest#az-sql-db-restore Can you match that? Also --dest-server. --target-db and --target-mi sound good too, but I'd like to aim for consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed to --dest-name and --dest-mi

required=True,
help='Name of the managed database that will be created as the restore destination.')

c.argument('target_managed_instance_name',
options_list=['--target-mi'],
help='Name of the managed instance to restore managed database to. '
'This can be same managed instance, or another managed instance withing same resource group. '
Copy link
Contributor

Choose a reason for hiding this comment

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

typo withing -> within

Is it not possible to specify another resource group? We have this for sql db copy command, see https://docs.microsoft.com/en-us/cli/azure/sql/db?view=azure-cli-latest#az-sql-db-copy

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! You are right, we can specify different rg. Fixed.

'When not specified it defaults to source managed instance.')

restore_point_arg_group = 'Restore Point'

c.argument('restore_point_in_time',
Expand All @@ -1165,5 +1172,5 @@ def _configure_security_policy_storage_params(arg_ctx):
' new database. Must be greater than or equal to the source database\'s'
' earliestRestoreDate value. Time should be in following format: "YYYY-MM-DDTHH:MM:SS"')
Copy link
Contributor

Choose a reason for hiding this comment

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

Very nice!


with self.argument_context('sql managed-db list') as c:
with self.argument_context('sql midb list') as c:
c.argument('managed_instance_name', id_part=None)
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add comment explaining what this does and why? That will help the next person to read this :)

Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ def load_command_table(self, _):
operations_tmpl='azure.mgmt.sql.operations.managed_instances_operations#ManagedInstancesOperations.{}',
client_factory=get_sql_managed_instances_operations)

with self.command_group('sql managed-instance',
with self.command_group('sql mi',
managed_instances_operations,
client_factory=get_sql_managed_instances_operations) as g:

Expand All @@ -435,12 +435,12 @@ def load_command_table(self, _):
operations_tmpl='azure.mgmt.sql.operations.managed_databases_operations#ManagedDatabasesOperations.{}',
client_factory=get_sql_managed_databases_operations)

with self.command_group('sql managed-db',
with self.command_group('sql midb',
managed_databases_operations,
client_factory=get_sql_managed_databases_operations) as g:

g.custom_command('create', 'managed_db_create', supports_no_wait=True)
g.custom_command('restore', 'managed_db_restore', supports_no_wait=True)
g.command('show', 'get')
g.custom_command('list', 'managed_db_list')
g.command('list', 'list_by_instance')
g.command('delete', 'delete', confirmation=True, supports_no_wait=True)
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ def _find_family_capability(sku, supported_families):
try:
return next(e for e in supported_families if e.name == sku.family)
except StopIteration:
candiate_families = [e.name for e in supported_families]
candidate_families = [e.name for e in supported_families]
raise CLIError('Could not find family ''{}''. Supported families are: {}'.format(
sku.family, candiate_families
sku.family, candidate_families
))
else:
# Find default family capability
Expand Down Expand Up @@ -1930,14 +1930,13 @@ def managed_instance_create(
kwargs['identity'] = ResourceIdentity(type=IdentityType.system_assigned.value)

kwargs['location'] = location

kwargs['sku'] = _find_managed_instance_sku_from_capabilities(cmd.cli_ctx, kwargs['location'], sku)
kwargs['subnet_id'] = virtual_network_subnet_id

# Create
return client.create_or_update(
managed_instance_name=managed_instance_name,
resource_group_name=resource_group_name,
subnet_id = virtual_network_subnet_id,
parameters=kwargs)


Expand Down Expand Up @@ -2010,42 +2009,29 @@ def managed_db_create(
parameters=kwargs)


def managed_db_list(
client,
managed_instance_name,
resource_group_name):
'''
Lists databases in a managed instance.
'''

# List all databases in the server
return client.list_by_instance(resource_group_name=resource_group_name, managed_instance_name=managed_instance_name)


def managed_db_restore(
cmd,
client,
database_name,
managed_instance_name,
resource_group_name,
target_managed_database_name,
restore_point_in_time=None,
target_managed_instance_name=None,
**kwargs):
'''
Restores an existing managed DB (i.e. create with 'PointInTimeRestore' create mode.)

Custom function makes create mode more convenient.
'''

if not restore_point_in_time:
raise CLIError('--restore_point_in_time must be specified.')
if not target_managed_instance_name:
target_managed_instance_name = managed_instance_name

kwargs['location'] = _get_managed_instance_location(
cmd.cli_ctx,
managed_instance_name=managed_instance_name,
resource_group_name=resource_group_name)

kwargs['restore_point_in_time'] = restore_point_in_time
kwargs['create_mode'] = CreateMode.point_in_time_restore.value
kwargs['source_database_id'] = _get_managed_db_resource_id(
cmd.cli_ctx,
Expand All @@ -2055,6 +2041,6 @@ def managed_db_restore(

return client.create_or_update(
database_name=target_managed_database_name,
managed_instance_name=managed_instance_name,
managed_instance_name=target_managed_instance_name,
resource_group_name=resource_group_name,
parameters=kwargs)
Loading