Skip to content

Commit

Permalink
knack conversion for advisor module (Azure#5083)
Browse files Browse the repository at this point in the history
* finished conversion and enabled tests/checks

* recordings

* forgot to commit _validator file

* added profile constraint

* changed command_level validators to param level ones
  • Loading branch information
williexu authored and tjprescott committed Dec 13, 2017
1 parent dc3675f commit 39cd532
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 228 deletions.
1 change: 0 additions & 1 deletion scripts/ci/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ setup(
EOL

for name in $(ls src/command_modules | grep azure-cli-); do
if [ "$name" == "azure-cli-advisor" ]; then continue; fi
if [ "$name" == "azure-cli-appservice" ]; then continue; fi
if [ "$name" == "azure-cli-backup" ]; then continue; fi
if [ "$name" == "azure-cli-batchai" ]; then continue; fi
Expand Down
2 changes: 1 addition & 1 deletion scripts/ci/test_static.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ set +e
run_style azure.cli.core
run_style azure.cli.command_modules.acr
run_style azure.cli.command_modules.acs
#run_style azure.cli.command_modules.advisor
run_style azure.cli.command_modules.advisor
#run_style azure.cli.command_modules.appservice
#run_style azure.cli.command_modules.backup
run_style azure.cli.command_modules.batch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,27 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from azure.cli.core import AzCommandsLoader

import azure.cli.command_modules.advisor._help # pylint: disable=unused-import


def load_params(_):
import azure.cli.command_modules.advisor._params # pylint: disable=redefined-outer-name, unused-variable
class AdvisorCommandsLoader(AzCommandsLoader):

def __init__(self, cli_ctx=None):
from azure.cli.core.commands import CliCommandType
advisor_custom = CliCommandType(operations_tmpl='azure.cli.command_modules.advisor.custom#{}')
super(AdvisorCommandsLoader, self).__init__(cli_ctx=cli_ctx, custom_command_type=advisor_custom,
min_profile='2017-03-10-profile')

def load_command_table(self, args):
from azure.cli.command_modules.advisor.commands import load_command_table
load_command_table(self, args)
return self.command_table

def load_arguments(self, command):
from azure.cli.command_modules.advisor._params import load_arguments
load_arguments(self, command)


def load_commands():
import azure.cli.command_modules.advisor.commands # pylint: disable=redefined-outer-name, unused-variable
COMMAND_LOADER_CLS = AdvisorCommandsLoader
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
# --------------------------------------------------------------------------------------------


def cf_advisor(**_):
def cf_advisor(cli_ctx, **_):
from azure.cli.core.commands.client_factory import get_mgmt_service_client
from azure.mgmt.advisor import AdvisorManagementClient
return get_mgmt_service_client(AdvisorManagementClient)
return get_mgmt_service_client(cli_ctx, AdvisorManagementClient)


def advisor_mgmt_client_factory(kwargs):
return cf_advisor(**kwargs)
def advisor_mgmt_client_factory(cli_ctx, kwargs):
return cf_advisor(cli_ctx, **kwargs)


def recommendations_mgmt_client_factory(kwargs):
return cf_advisor(**kwargs).recommendations
def recommendations_mgmt_client_factory(cli_ctx, kwargs):
return cf_advisor(cli_ctx, **kwargs).recommendations


def suppressions_mgmt_client_factory(kwargs):
return cf_advisor(**kwargs).suppressions
def suppressions_mgmt_client_factory(cli_ctx, kwargs):
return cf_advisor(cli_ctx, **kwargs).suppressions


def configurations_mgmt_client_factory(kwargs):
return cf_advisor(**kwargs).configurations
def configurations_mgmt_client_factory(cli_ctx, kwargs):
return cf_advisor(cli_ctx, **kwargs).configurations
Original file line number Diff line number Diff line change
Expand Up @@ -3,103 +3,33 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from azure.cli.core.commands import register_cli_argument
from azure.cli.core.commands.parameters import \
(resource_group_name_type, enum_choice_list)
from azure.cli.core.util import CLIError


def validate_include_or_exclude(namespace):
if namespace.include and namespace.exclude:
raise CLIError('usage error: --include | --exclude')


def validate_ids_or_resource_group(namespace):
if namespace.ids and namespace.resource_group_name:
raise CLIError('usage error: --ids | --resource-group')


register_cli_argument(
'advisor recommendation list',
'ids',
nargs='+',
options_list=('--ids'),
help='One or more resource IDs (space delimited). If provided, no other "Resource Id" arguments should be specified.' # pylint: disable=line-too-long
)

register_cli_argument(
'advisor recommendation list',
'resource_group_name',
resource_group_name_type,
validator=validate_ids_or_resource_group
)

register_cli_argument(
'advisor recommendation list',
'category',
options_list=('--category', '-c'),
help='Name of recommendation category.',
**enum_choice_list(['Cost', 'HighAvailability', 'Performance', 'Security'])
)

register_cli_argument(
'advisor recommendation disable',
'ids',
nargs='+',
options_list=('--ids'),
help='One or more resource IDs (space delimited). If provided, no other "Resource Id" arguments should be specified.' # pylint: disable=line-too-long
)

register_cli_argument(
'advisor recommendation disable',
'days',
options_list=('--days', '-d'),
type=int,
help='Number of days to disable. If not specified, the recommendation is disabled forever.'
)

register_cli_argument(
'advisor recommendation enable',
'ids',
nargs='+',
options_list=('--ids'),
help='One or more resource IDs (space delimited). If provided, no other "Resource Id" arguments should be specified.' # pylint: disable=line-too-long
)

register_cli_argument(
'advisor configuration get',
'resource_group_name',
resource_group_name_type
)

register_cli_argument(
'advisor configuration set',
'resource_group_name',
resource_group_name_type
)

register_cli_argument(
'advisor configuration set',
'low_cpu_threshold',
options_list=('--low-cpu-threshold', '-l'),
help='Value for low CPU threshold.',
**enum_choice_list(['5', '10', '15', '20'])
)

register_cli_argument(
'advisor configuration set',
'exclude',
options_list=('--exclude', '-e'),
action='store_true',
help='Exclude from recommendation generation.',
validator=validate_include_or_exclude
)

register_cli_argument(
'advisor configuration set',
'include',
options_list=('--include', '-i'),
action='store_true',
help='Include in recommendation generation.',
validator=validate_include_or_exclude
)
from azure.cli.core.commands.parameters import get_enum_type
from knack.arguments import CLIArgumentType
from ._validators import validate_include_or_exclude, validate_ids_or_resource_group


def load_arguments(self, _):
ids_arg_type = CLIArgumentType(nargs='+', options_list=['--ids'],
help='One or more resource IDs (space delimited). If provided, no other '
'"Resource Id" arguments should be specified.')

with self.argument_context('advisor recommendation list') as c:
c.argument('ids', ids_arg_type, validator=validate_ids_or_resource_group)
c.argument('category', options_list=['--category', '-c'], help='Name of recommendation category.',
arg_type=get_enum_type(['Cost', 'HighAvailability', 'Performance', 'Security']))

with self.argument_context('advisor recommendation disable') as c:
c.argument('ids', ids_arg_type)
c.argument('days', options_list=['--days', '-d'], type=int,
help='Number of days to disable. If not specified, the recommendation is disabled forever.')

with self.argument_context('advisor recommendation enable') as c:
c.argument('ids', ids_arg_type)

with self.argument_context('advisor configuration set') as c:
c.argument('low_cpu_threshold', options_list=['--low-cpu-threshold', '-l'],
help='Value for low CPU threshold.', arg_type=get_enum_type(['5', '10', '15', '20']))
c.argument('exclude', options_list=['--exclude', '-e'], action='store_true',
help='Exclude from recommendation generation.')
c.argument('include', options_list=['--include', '-i'], action='store_true',
help='Include in recommendation generation.', validator=validate_include_or_exclude)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from knack.util import CLIError


def validate_include_or_exclude(namespace):
if namespace.include and namespace.exclude:
raise CLIError('usage error: --include | --exclude')


def validate_ids_or_resource_group(namespace):
if namespace.ids and namespace.resource_group_name:
raise CLIError('usage error: --ids | --resource-group')
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,24 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from azure.cli.core.commands import cli_command
from azure.cli.command_modules.advisor._client_factory import \
(advisor_mgmt_client_factory,
recommendations_mgmt_client_factory,
suppressions_mgmt_client_factory,
configurations_mgmt_client_factory)

custom_path = 'azure.cli.command_modules.advisor.custom#'

cli_command(
__name__,
'advisor recommendation generate',
custom_path + 'cli_advisor_generate_recommendations',
recommendations_mgmt_client_factory
)

cli_command(
__name__,
'advisor recommendation list',
custom_path + 'cli_advisor_list_recommendations',
recommendations_mgmt_client_factory
)

cli_command(
__name__,
'advisor recommendation disable',
custom_path + 'cli_advisor_disable_recommendations',
suppressions_mgmt_client_factory
)

cli_command(
__name__,
'advisor recommendation enable',
custom_path + 'cli_advisor_enable_recommendations',
advisor_mgmt_client_factory # using advisor client here because this spans recommendations and suppressions
)

cli_command(
__name__,
'advisor configuration get',
custom_path + 'cli_advisor_get_configurations',
configurations_mgmt_client_factory
)

cli_command(
__name__,
'advisor configuration set',
custom_path + 'cli_advisor_set_configurations',
configurations_mgmt_client_factory
)
def load_command_table(self, _):
with self.command_group('advisor recommendation') as g:
g.custom_command('generate', 'cli_advisor_generate_recommendations',
client_factory=recommendations_mgmt_client_factory)
g.custom_command('list', 'cli_advisor_list_recommendations',
client_factory=recommendations_mgmt_client_factory)
g.custom_command('disable', 'cli_advisor_disable_recommendations',
client_factory=suppressions_mgmt_client_factory)
g.custom_command('enable', 'cli_advisor_enable_recommendations',
client_factory=advisor_mgmt_client_factory)

with self.command_group('advisor configuration', client_factory=configurations_mgmt_client_factory) as g:
g.custom_command('get', 'cli_advisor_get_configurations')
g.custom_command('set', 'cli_advisor_set_configurations')
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ interactions:
Connection: [keep-alive]
Content-Length: ['0']
Content-Type: [application/json; charset=utf-8]
User-Agent: [python/3.6.3 (Windows-10-10.0.16299-SP0) requests/2.18.4 msrest/0.4.18
msrest_azure/0.4.15 azure-mgmt-advisor/0.1.0 Azure-SDK-For-Python AZURECLI/2.0.22]
User-Agent: [python/3.6.2 (Windows-10-10.0.16299-SP0) requests/2.18.4 msrest/0.4.19
msrest_azure/0.4.18 azure-mgmt-advisor/0.1.0 Azure-SDK-For-Python AZURECLI/2.0.22]
accept-language: [en-US]
method: POST
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Advisor/generateRecommendations?api-version=2017-04-19
Expand All @@ -18,14 +18,14 @@ interactions:
headers:
cache-control: [no-cache]
content-length: ['0']
date: ['Tue, 28 Nov 2017 22:33:03 GMT']
date: ['Mon, 11 Dec 2017 22:58:43 GMT']
expires: ['-1']
location: ['https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Advisor/generateRecommendations/cea1a32d-00e8-4c7f-be70-8c0bf790908b?api-version=2017-04-19']
location: ['https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Advisor/generateRecommendations/0dba36fb-1faf-41f6-a711-37b7f57a29bd?api-version=2017-04-19']
pragma: [no-cache]
server: [Microsoft-HTTPAPI/2.0]
strict-transport-security: [max-age=31536000; includeSubDomains]
x-content-type-options: [nosniff]
x-ms-ratelimit-remaining-subscription-writes: ['1199']
x-ms-ratelimit-remaining-subscription-writes: ['1196']
status: {code: 202, message: Accepted}
- request:
body: null
Expand All @@ -35,22 +35,21 @@ interactions:
CommandName: [advisor recommendation generate]
Connection: [keep-alive]
Content-Type: [application/json; charset=utf-8]
User-Agent: [python/3.6.3 (Windows-10-10.0.16299-SP0) requests/2.18.4 msrest/0.4.18
msrest_azure/0.4.15 azure-mgmt-advisor/0.1.0 Azure-SDK-For-Python AZURECLI/2.0.22]
User-Agent: [python/3.6.2 (Windows-10-10.0.16299-SP0) requests/2.18.4 msrest/0.4.19
msrest_azure/0.4.18 azure-mgmt-advisor/0.1.0 Azure-SDK-For-Python AZURECLI/2.0.22]
accept-language: [en-US]
method: GET
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Advisor/generateRecommendations/cea1a32d-00e8-4c7f-be70-8c0bf790908b?api-version=2017-04-19
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Advisor/generateRecommendations/0dba36fb-1faf-41f6-a711-37b7f57a29bd?api-version=2017-04-19
response:
body: {string: ''}
headers:
cache-control: [no-cache]
content-length: ['0']
date: ['Tue, 28 Nov 2017 22:33:03 GMT']
date: ['Mon, 11 Dec 2017 22:58:43 GMT']
expires: ['-1']
location: ['https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Advisor/generateRecommendations/cea1a32d-00e8-4c7f-be70-8c0bf790908b?api-version=2017-04-19']
pragma: [no-cache]
server: [Microsoft-HTTPAPI/2.0]
strict-transport-security: [max-age=31536000; includeSubDomains]
x-content-type-options: [nosniff]
status: {code: 202, message: Accepted}
x-ms-ratelimit-remaining-subscription-resource-requests: ['359']
status: {code: 204, message: No Content}
version: 1
Loading

0 comments on commit 39cd532

Please sign in to comment.