-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Azure Monitor cli commands for Managemnt & Data plane #1953
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
Closed
Closed
Changes from 9 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
d4b6ea5
Adding basic structure for monitor service
vishrutshah fa0f2af
Adding azure-mgmt-monitor sdk as it's not yet released
vishrutshah 78b2793
Adding list command for all azure monitor data plane
vishrutshah 4331c70
Removing management sdk code from azure-cli-monitor
vishrutshah 4d8551e
Updating azure-monitor-cli to azure-mgmt-monitor and azure-monitor sdk
vishrutshah cdb8d00
Moving factories into _client_factory file
vishrutshah 9b32cf6
Implement filter for metrics-definitions list command
vishrutshah d81047f
Using nargs to input list of metric-names
vishrutshah 72ba403
Merge pull request #1 from vishrutshah/implement-filter-metric-defini…
vishrutshah 53a4aa7
Implement filter for metrics list command
vishrutshah 3fd8cc4
addressing code review comments
vishrutshah 60972c4
Merge pull request #2 from vishrutshah/implement-filter-metrics
vishrutshah eb75d64
Implement filter and select in activity-logs command
vishrutshah 65b103e
Implement filter and select for tenant-activity-logs command
vishrutshah 59751d9
Resolving merge conflicts
vishrutshah 3e6fa58
CR comments
vishrutshah 1ec0016
Addressing review comments
vishrutshah 1c655cc
Merge pull request #3 from vishrutshah/implement-activity-logs-filter
vishrutshah 875e53f
Adding scaffold command in autoscale-settings (#4)
vishrutshah 72ac3d8
Adding unittests for custom monitor commands (#5)
vishrutshah 45fe44a
Rename expanded params and resolve bugs
vishrutshah d9b44ca
Fix expand parameters bug in util to support any parameters (#6)
vishrutshah 6ada6e9
Fix pylint error on line too long
vishrutshah 2d48c8e
resolving merge conflicts
vishrutshah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| .. :changelog: | ||
|
|
||
| Release History | ||
| =============== |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| include *.rst |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Microsoft Azure CLI 'Monitor' Command Module | ||
| ============================================ | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| import pkg_resources | ||
| pkg_resources.declare_namespace(__name__) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| import pkg_resources | ||
| pkg_resources.declare_namespace(__name__) |
7 changes: 7 additions & 0 deletions
7
src/command_modules/azure-cli-monitor/azure/cli/command_modules/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| import pkg_resources | ||
| pkg_resources.declare_namespace(__name__) |
14 changes: 14 additions & 0 deletions
14
src/command_modules/azure-cli-monitor/azure/cli/command_modules/monitor/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| import azure.cli.command_modules.monitor.help # pylint: disable=unused-import | ||
|
|
||
|
|
||
| def load_params(_): | ||
| import azure.cli.command_modules.monitor.params # pylint: disable=redefined-outer-name | ||
|
|
||
|
|
||
| def load_commands(): | ||
| import azure.cli.command_modules.monitor.commands # pylint: disable=redefined-outer-name |
58 changes: 58 additions & 0 deletions
58
src/command_modules/azure-cli-monitor/azure/cli/command_modules/monitor/_client_factory.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| # MANAGEMENT CLIENT FACTORIES | ||
| def get_monitor_management_client(_): | ||
| from azure.mgmt.monitor import MonitorManagementClient | ||
| from azure.cli.core.commands.client_factory import get_mgmt_service_client | ||
| return get_mgmt_service_client(MonitorManagementClient) | ||
|
|
||
|
|
||
| def get_monitor_autoscale_settings_operation(kwargs): | ||
| return get_monitor_management_client(kwargs).autoscale_settings | ||
|
|
||
|
|
||
| def get_monitor_service_diagnostic_settings_operation(kwargs): | ||
| return get_monitor_management_client(kwargs).service_diagnostic_settings | ||
|
|
||
|
|
||
| def get_monitor_alert_rules_operation(kwargs): | ||
| return get_monitor_management_client(kwargs).alert_rules | ||
|
|
||
|
|
||
| def get_monitor_alert_rule_incidents_operation(kwargs): | ||
| return get_monitor_management_client(kwargs).alert_rule_incidents | ||
|
|
||
|
|
||
| def get_monitor_log_profiles_operation(kwargs): | ||
| return get_monitor_management_client(kwargs).log_profiles | ||
|
|
||
|
|
||
| # DATA CLIENT FACTORIES | ||
| def get_monitor_client(_): | ||
| from azure.monitor import MonitorClient | ||
| from azure.cli.core.commands.client_factory import get_mgmt_service_client | ||
| return get_mgmt_service_client(MonitorClient) | ||
|
|
||
|
|
||
| def get_monitor_event_categories_operation(kwargs): | ||
| return get_monitor_client(kwargs).event_categories | ||
|
|
||
|
|
||
| def get_monitor_activity_logs_operation(kwargs): | ||
| return get_monitor_client(kwargs).activity_logs | ||
|
|
||
|
|
||
| def get_monitor_tenant_activity_logs_operation(kwargs): | ||
| return get_monitor_client(kwargs).tenant_activity_logs | ||
|
|
||
|
|
||
| def get_monitor_metric_definitions_operation(kwargs): | ||
| return get_monitor_client(kwargs).metric_definitions | ||
|
|
||
|
|
||
| def get_monitor_metrics_operation(kwargs): | ||
| return get_monitor_client(kwargs).metrics |
139 changes: 139 additions & 0 deletions
139
src/command_modules/azure-cli-monitor/azure/cli/command_modules/monitor/_util.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| from azure.cli.core.commands import register_cli_argument, cli_command | ||
| from azure.cli.core.commands.parameters import ignore_type | ||
| from azure.cli.core.commands.arm import cli_generic_update_command | ||
|
|
||
|
|
||
| # COMMANDS UTILITIES | ||
|
|
||
| def create_service_adapter(service_model, service_class=None): | ||
| def _service_adapter(method_name): | ||
| if service_class is not None: | ||
| return '{}#{}.{}'.format(service_model, service_class, method_name) | ||
| else: | ||
| return '{}#{}'.format(service_model, method_name) | ||
|
|
||
| return _service_adapter | ||
|
|
||
|
|
||
| # pylint: disable=too-few-public-methods | ||
| class ServiceGroup(object): | ||
| def __init__(self, scope, client_factory, service_adapter=None): | ||
| self._scope = scope | ||
| self._factory = client_factory | ||
| self._service_adapter = service_adapter or (lambda name: name) | ||
|
|
||
| def __enter__(self): | ||
| return self | ||
|
|
||
| def __exit__(self, exc_type, exc_val, exc_tb): | ||
| pass | ||
|
|
||
| def group(self, group_name): | ||
| return CommandGroup(self._scope, group_name, self._factory, self._service_adapter) | ||
|
|
||
|
|
||
| class CommandGroup(object): | ||
| def __init__(self, scope, group_name, client_factory, service_adapter=None): | ||
| self._scope = scope | ||
| self._group_name = group_name | ||
| self._client_factory = client_factory | ||
| self._service_adapter = service_adapter or (lambda name: name) | ||
|
|
||
| def __enter__(self): | ||
| return self | ||
|
|
||
| def __exit__(self, exc_type, exc_val, exc_tb): | ||
| pass | ||
|
|
||
| def command(self, name, method_name): | ||
| cli_command(self._scope, | ||
| '{} {}'.format(self._group_name, name), | ||
| self._service_adapter(method_name), | ||
| client_factory=self._client_factory) | ||
|
|
||
| def generic_update_command(self, name, getter_op, setter_op): | ||
| cli_generic_update_command( | ||
| self._scope, | ||
| '{} {}'.format(self._group_name, name), | ||
| self._service_adapter(getter_op), | ||
| self._service_adapter(setter_op), | ||
| factory=self._client_factory) | ||
|
|
||
|
|
||
| # PARAMETERS UTILITIES | ||
|
|
||
| def patch_arg_make_required(argument): | ||
| argument.type.settings['required'] = True | ||
|
|
||
|
|
||
| def patch_arg_update_description(description): | ||
| def _patch_action(argument): | ||
| argument.type.settings['help'] = description | ||
|
|
||
| return _patch_action | ||
|
|
||
|
|
||
| class ParametersContext(object): | ||
| def __init__(self, command): | ||
| self._commmand = command | ||
|
|
||
| def __enter__(self): | ||
| return self | ||
|
|
||
| def __exit__(self, exc_type, exc_val, exc_tb): | ||
| pass | ||
|
|
||
| def ignore(self, argument_name): | ||
| register_cli_argument(self._commmand, argument_name, ignore_type) | ||
|
|
||
| def argument(self, argument_name, arg_type=None, **kwargs): | ||
| register_cli_argument(self._commmand, argument_name, arg_type=arg_type, **kwargs) | ||
|
|
||
| def register_alias(self, argument_name, options_list): | ||
| register_cli_argument(self._commmand, argument_name, options_list=options_list) | ||
|
|
||
| def register(self, argument_name, options_list, **kwargs): | ||
| register_cli_argument(self._commmand, argument_name, options_list=options_list, **kwargs) | ||
|
|
||
| def expand(self, argument_name, model_type, group_name=None, patches=None): | ||
| # TODO: | ||
| # two privates symbols are imported here. they should be made public or this utility class | ||
| # should be moved into azure.cli.core | ||
| from azure.cli.core.commands import _cli_extra_argument_registry | ||
| from azure.cli.core.commands._introspection import \ | ||
| (extract_args_from_signature, _option_descriptions) | ||
|
|
||
| from azure.cli.command_modules.monitor.validators import get_complex_argument_processor | ||
|
|
||
| if not patches: | ||
| patches = dict() | ||
|
|
||
| self.ignore(argument_name) | ||
|
|
||
| # fetch the documentation for model parameters first. for models, which are the classes | ||
| # derive from msrest.serialization.Model and used in the SDK API to carry parameters, the | ||
| # document of their properties are attached to the classes instead of constructors. | ||
| parameter_docs = _option_descriptions(model_type) | ||
|
|
||
| expanded_arguments = [] | ||
| for name, arg in extract_args_from_signature(model_type.__init__): | ||
| if name in parameter_docs: | ||
| arg.type.settings['help'] = parameter_docs[name] | ||
|
|
||
| if group_name: | ||
| arg.type.settings['arg_group'] = group_name | ||
|
|
||
| if name in patches: | ||
| patches[name](arg) | ||
|
|
||
| _cli_extra_argument_registry[self._commmand][name] = arg | ||
| expanded_arguments.append(name) | ||
|
|
||
| self.argument(argument_name, | ||
| arg_type=ignore_type, | ||
| validator=get_complex_argument_processor(expanded_arguments, model_type)) | ||
118 changes: 118 additions & 0 deletions
118
src/command_modules/azure-cli-monitor/azure/cli/command_modules/monitor/commands.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| from ._client_factory import (get_monitor_alert_rules_operation, | ||
| get_monitor_alert_rule_incidents_operation, | ||
| get_monitor_log_profiles_operation, | ||
| get_monitor_autoscale_settings_operation, | ||
| get_monitor_service_diagnostic_settings_operation, | ||
| get_monitor_event_categories_operation, | ||
| get_monitor_activity_logs_operation, | ||
| get_monitor_tenant_activity_logs_operation, | ||
| get_monitor_metric_definitions_operation, | ||
| get_monitor_metrics_operation) | ||
| from ._util import (ServiceGroup, create_service_adapter) | ||
|
|
||
|
|
||
| # MANAGEMENT COMMANDS | ||
| alert_rules_operations = create_service_adapter( | ||
| 'azure.mgmt.monitor.operations.alert_rules_operations', | ||
| 'AlertRulesOperations') | ||
|
|
||
| with ServiceGroup(__name__, get_monitor_alert_rules_operation, alert_rules_operations) as s: | ||
| with s.group('monitor alert-rules') as c: | ||
| c.command('create', 'create_or_update') | ||
| c.command('delete', 'delete') | ||
| c.command('show', 'get') | ||
| c.command('list', 'list_by_resource_group') | ||
| c.generic_update_command('update', 'get', 'create_or_update') | ||
|
|
||
| alert_rule_incidents_operations = create_service_adapter( | ||
| 'azure.mgmt.monitor.operations.alert_rule_incidents_operations', | ||
| 'AlertRuleIncidentsOperations') | ||
|
|
||
| with ServiceGroup(__name__, get_monitor_alert_rule_incidents_operation, | ||
| alert_rule_incidents_operations) as s: | ||
| with s.group('monitor alert-rule-incidents') as c: | ||
| c.command('show', 'get') | ||
| c.command('list', 'list_by_alert_rule') | ||
|
|
||
| log_profiles_operations = create_service_adapter( | ||
| 'azure.mgmt.monitor.operations.log_profiles_operations', | ||
| 'LogProfilesOperations') | ||
|
|
||
| with ServiceGroup(__name__, get_monitor_log_profiles_operation, | ||
| log_profiles_operations) as s: | ||
| with s.group('monitor log-profiles') as c: | ||
| c.command('create', 'create_or_update') | ||
| c.command('delete', 'delete') | ||
| c.command('show', 'get') | ||
| c.command('list', 'list') | ||
| c.generic_update_command('update', 'get', 'create_or_update') | ||
|
|
||
| service_diagnostic_settings_operations = create_service_adapter( | ||
| 'azure.mgmt.monitor.operations.service_diagnostic_settings_operations', | ||
| 'ServiceDiagnosticSettingsOperations') | ||
|
|
||
| with ServiceGroup(__name__, get_monitor_service_diagnostic_settings_operation, | ||
| service_diagnostic_settings_operations) as s: | ||
| with s.group('monitor service-diagnostic-settings') as c: | ||
| c.command('create', 'create_or_update') | ||
| c.command('show', 'get') | ||
| c.generic_update_command('update', 'get', 'create_or_update') | ||
|
|
||
| autoscale_settings_operations = create_service_adapter( | ||
| 'azure.mgmt.monitor.operations.autoscale_settings_operations', | ||
| 'AutoscaleSettingsOperations') | ||
|
|
||
| with ServiceGroup(__name__, get_monitor_autoscale_settings_operation, | ||
| autoscale_settings_operations) as s: | ||
| with s.group('monitor autoscale-settings') as c: | ||
| c.command('create', 'create_or_update') | ||
| c.command('delete', 'delete') | ||
| c.command('show', 'get') | ||
| c.command('list', 'list_by_resource_group') | ||
| c.generic_update_command('update', 'get', 'create_or_update') | ||
|
|
||
|
|
||
| # DATA COMMANDS | ||
| event_categories_operations = create_service_adapter( | ||
| 'azure.monitor.operations.event_categories_operations', 'EventCategoriesOperations') | ||
|
|
||
| with ServiceGroup(__name__, get_monitor_event_categories_operation, | ||
| event_categories_operations) as s: | ||
| with s.group('monitor event-categories') as c: | ||
| c.command('list', 'list') | ||
|
|
||
| activity_logs_operations = create_service_adapter( | ||
| 'azure.monitor.operations.activity_logs_operations', 'ActivityLogsOperations') | ||
|
|
||
| with ServiceGroup(__name__, get_monitor_activity_logs_operation, | ||
| activity_logs_operations) as s: | ||
| with s.group('monitor activity-logs') as c: | ||
| c.command('list', 'list') | ||
|
|
||
| tenant_activity_logs_operations = create_service_adapter( | ||
| 'azure.monitor.operations.tenant_activity_logs_operations', 'TenantActivityLogsOperations') | ||
|
|
||
| with ServiceGroup(__name__, get_monitor_tenant_activity_logs_operation, | ||
| tenant_activity_logs_operations) as s: | ||
| with s.group('monitor tenant-activity-logs') as c: | ||
| c.command('list', 'list') | ||
|
|
||
| metric_definitions_operations = create_service_adapter( | ||
| 'azure.cli.command_modules.monitor.custom') | ||
|
|
||
| with ServiceGroup(__name__, get_monitor_metric_definitions_operation, | ||
| metric_definitions_operations) as s: | ||
| with s.group('monitor metric-definitions') as c: | ||
| c.command('list', 'list_metric_definitions') | ||
|
|
||
| metrics_operations = create_service_adapter( | ||
| 'azure.monitor.operations.metrics_operations', 'MetricsOperations') | ||
|
|
||
| with ServiceGroup(__name__, get_monitor_metrics_operation, metrics_operations) as s: | ||
| with s.group('monitor metrics') as c: | ||
| c.command('list', 'list') |
24 changes: 24 additions & 0 deletions
24
src/command_modules/azure-cli-monitor/azure/cli/command_modules/monitor/custom.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def list_metric_definitions(client, resource_uri, metric_names=None): | ||
| '''Commands to manage metric definitions. | ||
| :param str resource_uri: The identifier of the resource | ||
| :param str metric_names: The list of metric names | ||
| ''' | ||
| odata_filter = _list_metric_definitions_filter_builder(metric_names) | ||
| metric_definitions = client.list(resource_uri, filter=odata_filter) | ||
| return list(metric_definitions) | ||
|
|
||
|
|
||
| def _list_metric_definitions_filter_builder(metric_names=None): | ||
| '''Build up OData filter string from metric_names | ||
| ''' | ||
| filters = [] | ||
| if metric_names: | ||
| for metric_name in metric_names: | ||
| filters.append("name.value eq '{}'".format(metric_name)) | ||
| return ' or '.join(filters) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is duplicate with the sql command module. Consider move it up to the
azure-cli-corefor reuse.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we should. I'd prefer it to do that refactoring once this PR completes so I don't confuse you guys more on actual PR. Does that seem good?