Skip to content
Closed
Show file tree
Hide file tree
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 Jan 27, 2017
fa0f2af
Adding azure-mgmt-monitor sdk as it's not yet released
vishrutshah Jan 27, 2017
78b2793
Adding list command for all azure monitor data plane
vishrutshah Jan 27, 2017
4331c70
Removing management sdk code from azure-cli-monitor
vishrutshah Feb 17, 2017
4d8551e
Updating azure-monitor-cli to azure-mgmt-monitor and azure-monitor sdk
vishrutshah Feb 17, 2017
cdb8d00
Moving factories into _client_factory file
vishrutshah Feb 17, 2017
9b32cf6
Implement filter for metrics-definitions list command
vishrutshah Feb 22, 2017
d81047f
Using nargs to input list of metric-names
vishrutshah Feb 23, 2017
72ba403
Merge pull request #1 from vishrutshah/implement-filter-metric-defini…
vishrutshah Feb 23, 2017
53a4aa7
Implement filter for metrics list command
vishrutshah Feb 23, 2017
3fd8cc4
addressing code review comments
vishrutshah Feb 24, 2017
60972c4
Merge pull request #2 from vishrutshah/implement-filter-metrics
vishrutshah Feb 28, 2017
eb75d64
Implement filter and select in activity-logs command
vishrutshah Feb 24, 2017
65b103e
Implement filter and select for tenant-activity-logs command
vishrutshah Feb 28, 2017
59751d9
Resolving merge conflicts
vishrutshah Feb 28, 2017
3e6fa58
CR comments
vishrutshah Mar 1, 2017
1ec0016
Addressing review comments
vishrutshah Mar 1, 2017
1c655cc
Merge pull request #3 from vishrutshah/implement-activity-logs-filter
vishrutshah Mar 2, 2017
875e53f
Adding scaffold command in autoscale-settings (#4)
vishrutshah Mar 3, 2017
72ac3d8
Adding unittests for custom monitor commands (#5)
vishrutshah Mar 7, 2017
45fe44a
Rename expanded params and resolve bugs
vishrutshah Mar 8, 2017
d9b44ca
Fix expand parameters bug in util to support any parameters (#6)
vishrutshah Mar 13, 2017
6ada6e9
Fix pylint error on line too long
vishrutshah Mar 15, 2017
2d48c8e
resolving merge conflicts
vishrutshah Mar 15, 2017
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
4 changes: 4 additions & 0 deletions src/command_modules/azure-cli-monitor/HISTORY.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.. :changelog:

Release History
===============
1 change: 1 addition & 0 deletions src/command_modules/azure-cli-monitor/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include *.rst
3 changes: 3 additions & 0 deletions src/command_modules/azure-cli-monitor/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Microsoft Azure CLI 'Monitor' Command Module
============================================

7 changes: 7 additions & 0 deletions src/command_modules/azure-cli-monitor/azure/__init__.py
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 src/command_modules/azure-cli-monitor/azure/cli/__init__.py
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__)
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__)
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
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
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):
Copy link
Contributor

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-core for reuse.

Copy link
Contributor Author

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?

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))
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')
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)
Loading