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

Table revisions prt 2 (clean up) #771

Merged
merged 1 commit into from
Aug 26, 2016
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
6 changes: 3 additions & 3 deletions doc/authoring_command_modules/authoring_commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ from azure.cli.commands import cli_command

The signature of this method is
```Python
def cli_command(name, operation, client_factory=None, transform=None, simple_output_query=None):
def cli_command(name, operation, client_factory=None, transform=None, table_transformer=None):
```
You will generally only specify `name`, `operation` and possibly `simple_output_query`.
You will generally only specify `name`, `operation` and possibly `table_transformer`.
- `name` - String uniquely naming your command and placing it within the command hierachy. It will be the string that you would type at the command line, omitting `az` (ex: access your command at `az mypackage mycommand` using a name of `mypackage mycommand`).
- `operation` - Your function's name.
- `simple_output_query` (optional) - Supply a JMESPath projection string to enable the table output type for your command. This is most applicable to list-type commands. See www.jmespath.org for guidance on creating JMESPath queries. Alternatively, you can supply a callable that takes, transforms and returns a result. This is appropriate when the structure of the server response makes JMESPath querying difficult.
- `table_transformer` (optional) - Supply a callable that takes, transforms and returns a result for table output.

At this point, you should be able to access your command using `az [name]` and access the built-in help with `az [name] -h/--help`. Your command will automatically be 'wired up' with the global parameters.

Expand Down
9 changes: 4 additions & 5 deletions src/azure/cli/_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ def format_text(obj):
def format_table(obj):
result = obj.result
try:
if obj.simple_output_query and not obj.is_query_active:
if callable(obj.simple_output_query):
result = obj.simple_output_query(result)
if obj.table_transformer and not obj.is_query_active:
result = obj.table_transformer(result)
result_list = result if isinstance(result, list) else [result]
return TableOutput.dump(result_list)
except:
Expand All @@ -81,9 +80,9 @@ def format_tsv(obj):

class CommandResultItem(object): #pylint: disable=too-few-public-methods

def __init__(self, result, simple_output_query=None, is_query_active=False):
def __init__(self, result, table_transformer=None, is_query_active=False):
self.result = result
self.simple_output_query = simple_output_query
self.table_transformer = table_transformer
self.is_query_active = is_query_active

class OutputProducer(object): #pylint: disable=too-few-public-methods
Expand Down
4 changes: 2 additions & 2 deletions src/azure/cli/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ def execute(self, argv):
self.raise_event(self.TRANSFORM_RESULT, event_data=event_data)
self.raise_event(self.FILTER_RESULT, event_data=event_data)
return CommandResultItem(event_data['result'],
simple_output_query=
command_table[args.command].simple_output_query,
table_transformer=
command_table[args.command].table_transformer,
is_query_active=self.session['query_active'])

def raise_event(self, name, **kwargs):
Expand Down
12 changes: 6 additions & 6 deletions src/azure/cli/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ def wrapped(func):

class CliCommand(object):

def __init__(self, name, handler, description=None, simple_output_query=None):
def __init__(self, name, handler, description=None, table_transformer=None):
self.name = name
self.handler = handler
self.description = description
self.help = None
self.arguments = {}
self.simple_output_query = simple_output_query
self.table_transformer = table_transformer

def add_argument(self, param_name, *option_strings, **kwargs):
argument = CliCommandArgument(
Expand Down Expand Up @@ -201,12 +201,12 @@ def register_extra_cli_argument(command, dest, **kwargs):
'''
_cli_extra_argument_registry[command][dest] = CliCommandArgument(dest, **kwargs)

def cli_command(name, operation, client_factory=None, transform=None, simple_output_query=None):
def cli_command(name, operation, client_factory=None, transform=None, table_transformer=None):
""" Registers a default Azure CLI command. These commands require no special parameters. """
command_table[name] = create_command(name, operation, transform, simple_output_query,
command_table[name] = create_command(name, operation, transform, table_transformer,
client_factory)

def create_command(name, operation, transform_result, simple_output_query, client_factory):
def create_command(name, operation, transform_result, table_transformer, client_factory):

def _execute_command(kwargs):
client = client_factory(kwargs) if client_factory else None
Expand All @@ -228,7 +228,7 @@ def _execute_command(kwargs):
raise CLIError(message)

name = ' '.join(name.split())
cmd = CliCommand(name, _execute_command, simple_output_query=simple_output_query)
cmd = CliCommand(name, _execute_command, table_transformer=table_transformer)
cmd.description = extract_full_summary_from_signature(operation)
cmd.arguments.update(extract_args_from_signature(operation))
return cmd
Expand Down
4 changes: 2 additions & 2 deletions src/azure/cli/commands/arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def _get_child(parent, collection_name, item_name, collection_key):
return result

def cli_generic_update_command(name, getter, setter, factory=None, setter_arg_name='parameters', # pylint: disable=too-many-arguments
simple_output_query=None, child_collection_prop_name=None,
table_transformer=None, child_collection_prop_name=None,
child_collection_key='name', child_arg_name='item_name',
custom_function=None):

Expand Down Expand Up @@ -261,7 +261,7 @@ def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, 'ordered_arguments', [])
namespace.ordered_arguments.append((option_string, values))

cmd = CliCommand(name, handler, simple_output_query=simple_output_query)
cmd = CliCommand(name, handler, table_transformer=table_transformer)
cmd.arguments.update(set_arguments)
cmd.arguments.update(get_arguments)
if function_arguments:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

# HELPER METHODS

cli_command('component list', list_components,
simple_output_query="[*].{Name:name, Version:version} | sort_by(@, &Name)")
cli_command('component list', list_components)
cli_command('component install', install)
cli_command('component update', update)
cli_command('component update-all', update_all)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,13 @@ def _keyvault_client_factory(**_):

factory = lambda args: _keyvault_client_factory(**args).vaults

keyvault_show_query = "{Name:name, ResourceGroup:resourceGroup, Location:location, "\
"SkuFamily:properties.sku.family, SkuName:properties.sku.name, "\
"VaultUri:properties.vaultUri}"

cli_command('keyvault create', create_keyvault, factory, simple_output_query=keyvault_show_query)
cli_command('keyvault list', list_keyvault, factory,
simple_output_query="[*].{Name:name, ResourceGroup:resourceGroup, Location:location} |"\
" sort_by(@, &Name)")
cli_command('keyvault show', VaultsOperations.get, factory, simple_output_query=keyvault_show_query)
cli_command('keyvault create', create_keyvault, factory)
cli_command('keyvault list', list_keyvault, factory)
cli_command('keyvault show', VaultsOperations.get, factory)
cli_command('keyvault delete', VaultsOperations.delete, factory)

cli_command('keyvault set-policy', set_policy, factory, simple_output_query=keyvault_show_query)
cli_command('keyvault delete-policy', delete_policy, factory,
simple_output_query=keyvault_show_query)
cli_command('keyvault set-policy', set_policy, factory)
cli_command('keyvault delete-policy', delete_policy, factory)

def keyvault_update_setter(client, resource_group_name, vault_name, parameters):
from azure.mgmt.keyvault.models import VaultCreateOrUpdateParameters
Expand All @@ -44,5 +37,4 @@ def keyvault_update_setter(client, resource_group_name, vault_name, parameters):
cli_generic_update_command('keyvault update',
VaultsOperations.get,
keyvault_update_setter,
lambda: _keyvault_client_factory().vaults,
simple_output_query=keyvault_show_query)
lambda: _keyvault_client_factory().vaults)
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
factory = lambda _: _network_client_factory().application_gateways
cli_command('network application-gateway delete', ApplicationGatewaysOperations.delete, factory)
cli_command('network application-gateway show', ApplicationGatewaysOperations.get, factory)
cli_command('network application-gateway list', list_application_gateways, simple_output_query="[*].{Name:name, ResourceGroup:resourceGroup, Location:location, State:provisioningState} | sort_by(@, &Name)")
cli_command('network application-gateway list', list_application_gateways)
cli_command('network application-gateway start', ApplicationGatewaysOperations.start, factory)
cli_command('network application-gateway stop', ApplicationGatewaysOperations.stop, factory)
cli_generic_update_command('network application-gateway update', ApplicationGatewaysOperations.get, ApplicationGatewaysOperations.create_or_update, factory)
Expand All @@ -143,7 +143,7 @@
'url_path_maps': 'url-path-map'
}
for subresource, alias in property_map.items():
cli_command('network application-gateway {} list'.format(alias), list_network_resource_property('application_gateways', subresource), simple_output_query="[*].{Name:name} | sort_by(@, &Name)")
cli_command('network application-gateway {} list'.format(alias), list_network_resource_property('application_gateways', subresource))
cli_command('network application-gateway {} show'.format(alias), get_network_resource_property_entry('application_gateways', subresource))
cli_command('network application-gateway {} delete'.format(alias), delete_network_resource_property_entry('application_gateways', subresource))

Expand Down Expand Up @@ -282,7 +282,7 @@
factory = lambda _: _network_client_factory().public_ip_addresses
cli_command('network public-ip delete', PublicIPAddressesOperations.delete, factory)
cli_command('network public-ip show', PublicIPAddressesOperations.get, factory)
cli_command('network public-ip list', list_public_ips, simple_output_query="[*].{Name:name, ResourceGroup:resourceGroup, Location:location, IpAddress:ipAddress, FQDN:dnsSettings.fqdn} | sort_by(@, &Name)")
cli_command('network public-ip list', list_public_ips)
cli_generic_update_command('network public-ip update', PublicIPAddressesOperations.get, PublicIPAddressesOperations.create_or_update, factory)

factory = lambda _: get_mgmt_service_client(PublicIPClient).public_ip
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
cli_command('resource group delete', ResourceGroupsOperations.delete, factory)
cli_command('resource group show', ResourceGroupsOperations.get, factory)
cli_command('resource group exists', ResourceGroupsOperations.check_existence, factory)
cli_command('resource group list', list_resource_groups, simple_output_query="[*].{Name:name, Location:location, ProvisioningState:properties.provisioningState}")
cli_command('resource group list', list_resource_groups)
cli_command('resource group create', create_resource_group)
cli_command('resource group export', export_group_as_template)

Expand All @@ -44,27 +44,26 @@
cli_command('resource exists', ResourcesOperations.check_existence, factory)
cli_command('resource delete', ResourcesOperations.delete, factory)
cli_command('resource show', ResourcesOperations.get, factory)
cli_command('resource list', list_resources, simple_output_query="[*].{Id:id,Name:name,ResourceGroup:resourceGroup,Type:type,Location:location}")
cli_command('resource list', list_resources)
cli_command('resource tag', tag_resource)
cli_command('resource move', move_resource)

# Resource provider commands
factory = lambda _: _resource_client_factory().providers
cli_command('resource provider list', ProvidersOperations.list, factory, simple_output_query="[*].{Namespace:namespace,Registration:registrationState}")
cli_command('resource provider list', ProvidersOperations.list, factory)
cli_command('resource provider show', ProvidersOperations.get, factory)
cli_command('resource provider register', register_provider)
cli_command('resource provider unregister', unregister_provider)

# Resource feature commands
feature_tab_query = '{Name:name, State:properties.state}'
factory = lambda _: _resource_feature_client_factory().features
cli_command('resource feature list', list_features, factory, simple_output_query='[].'+feature_tab_query)
cli_command('resource feature show', FeaturesOperations.get, factory, simple_output_query=feature_tab_query)
cli_command('resource feature register', FeaturesOperations.register, factory, simple_output_query=feature_tab_query)
cli_command('resource feature list', list_features, factory)
cli_command('resource feature show', FeaturesOperations.get, factory)
cli_command('resource feature register', FeaturesOperations.register, factory)

# Tag commands
factory = lambda _: _resource_client_factory().tags
cli_command('tag list', TagsOperations.list, factory, simple_output_query="[*].{Name:tagName,Count:count.value}")
cli_command('tag list', TagsOperations.list, factory)
cli_command('tag create', TagsOperations.create_or_update, factory)
cli_command('tag delete', TagsOperations.delete, factory)
cli_command('tag add-value', TagsOperations.create_or_update_value, factory)
Expand Down Expand Up @@ -94,15 +93,13 @@
ResourceGroupsOperations.create_or_update,
lambda: _resource_client_factory().resource_groups)

policy_assignment_tab_query = '{Name:name, Scope:scope, Policy:policyDefinitionId}'
cli_command('resource policy assignment create', create_policy_assignment, simple_output_query=policy_assignment_tab_query)
cli_command('resource policy assignment create', create_policy_assignment)
cli_command('resource policy assignment delete', delete_policy_assignment)
cli_command('resource policy assignment list', list_policy_assignment, simple_output_query='[].'+policy_assignment_tab_query)
cli_command('resource policy assignment show', show_policy_assignment, simple_output_query=policy_assignment_tab_query)
cli_command('resource policy assignment list', list_policy_assignment)
cli_command('resource policy assignment show', show_policy_assignment)
factory = lambda _: _resource_policy_client_factory().policy_definitions
policy_definition_tab_query = '{Name:name, Description:description}'
cli_command('resource policy create', create_policy_definition, simple_output_query=policy_definition_tab_query)
cli_command('resource policy create', create_policy_definition)
cli_command('resource policy delete', PolicyDefinitionsOperations.delete, factory)
cli_command('resource policy list', PolicyDefinitionsOperations.list, factory, simple_output_query='[].'+policy_definition_tab_query)
cli_command('resource policy show', PolicyDefinitionsOperations.get, factory, simple_output_query=policy_definition_tab_query)
cli_command('resource policy update', update_policy_definition, simple_output_query=policy_definition_tab_query)
cli_command('resource policy list', PolicyDefinitionsOperations.list, factory)
cli_command('resource policy show', PolicyDefinitionsOperations.get, factory)
cli_command('resource policy update', update_policy_definition)
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,32 @@
_auth_client_factory, _graph_client_factory)

factory = lambda _: _auth_client_factory().role_definitions
simple_output_query = '[*].{Name:properties.roleName, Id:name, Type:properties.type}'
cli_command('role list', list_role_definitions, simple_output_query=simple_output_query)
cli_command('role list', list_role_definitions)
cli_command('role delete', delete_role_definition)
cli_command('role create', create_role_definition, simple_output_query=simple_output_query)
cli_command('role create', create_role_definition)
cli_generic_update_command('role update',
RoleDefinitionsOperations.get,
RoleDefinitionsOperations.create_or_update,
factory)

simple_output_query = '[*].{Name:name, PrincipalName:properties.principalName, Role:properties.roleDefinitionName, Scope:properties.scope}'
factory = lambda _: _auth_client_factory().role_assignments
cli_command('role assignment delete', delete_role_assignments)
cli_command('role assignment list', list_role_assignments,
simple_output_query='[*].{Name:name, PrincipalName:properties.principalName, Role:properties.roleDefinitionName, Scope:properties.scope}')
cli_command('role assignment create', create_role_assignment,
simple_output_query="{Name:name, PrincipalId:properties.principalId, Scope:properties.scope}")
cli_command('role assignment list', list_role_assignments)
cli_command('role assignment create', create_role_assignment)

factory = lambda _: _graph_client_factory().applications
#for table ouput, display the 1st identifier and replyurl, so all can fit in a row
single_app_table_query = '{DisplayName:displayName, IdentifierUri:identifierUris[0], ObjectId:objectId, ReplyUrl:replyUrls[0]}'
cli_command('ad app create', create_application, factory, simple_output_query=single_app_table_query)
cli_command('ad app create', create_application, factory)
cli_command('ad app delete', delete_application, factory)
cli_command('ad app list', list_apps, factory, simple_output_query='[*].'+ single_app_table_query)
cli_command('ad app show', show_application, factory, simple_output_query=single_app_table_query)
cli_command('ad app list', list_apps, factory)
cli_command('ad app show', show_application, factory)
cli_command('ad app update', update_application, factory)

factory = lambda _: _graph_client_factory().service_principals
#for table ouput, display the 1st principal name, so all can fit in a row
single_sp_table_query = '{DisplayName:displayName, ServicePrincipalName:servicePrincipalNames[0], ObjectId:objectId}'
cli_command('ad sp create', create_service_principal, simple_output_query=single_sp_table_query)
cli_command('ad sp create', create_service_principal)
cli_command('ad sp delete', delete_service_principal, factory)
#paging is broken at SDK https://github.com/Azure/azure-cli/issues/540
cli_command('ad sp list', list_sps, factory, simple_output_query='[*].'+single_sp_table_query)
cli_command('ad sp show', show_service_principal, factory, simple_output_query=single_sp_table_query)
cli_command('ad sp list', list_sps, factory)
cli_command('ad sp show', show_service_principal, factory)
#RBAC related
cli_command('ad sp create-for-rbac', create_service_principal_for_rbac)
cli_command('ad sp reset-credentials', reset_service_principal_credential)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
from azure.cli.command_modules.storage._validators import validate_client_parameters

def cli_storage_data_plane_command(name, operation, client_factory,
transform=None, simple_output_query=None):
transform=None, table_transformer=None):
""" Registers an Azure CLI Storage Data Plane command. These commands always include the
four parameters which can be used to obtain a storage client: account-name, account-key,
connection-string, and sas-token. """
command = create_command(name, operation, transform, simple_output_query, client_factory)
command = create_command(name, operation, transform, table_transformer, client_factory)

# add parameters required to create a storage client
group_name = 'Storage Account'
Expand Down
Loading