Skip to content
This repository has been archived by the owner on Jul 14, 2023. It is now read-only.

Commit

Permalink
Cli argument type (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
qiaozha authored Jun 8, 2020
1 parent ffd69a5 commit f3a2154
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 61 deletions.
4 changes: 2 additions & 2 deletions src/plugins/azgenerator/TemplateAzureCliCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export function GenerateAzureCliCommands(model: CodeModelAz): string[] {
let header: HeaderGenerator = new HeaderGenerator();

// this can't be currently reproduced
// header.disableTooManyStatements = true;
// header.disableTooManyLocals = true;
header.disableTooManyStatements = true;
header.disableTooManyLocals = true;
header.addFromImport("azure.cli.core.commands", ["CliCommandType"]);

let output: string[] = []
Expand Down
10 changes: 7 additions & 3 deletions src/plugins/azgenerator/TemplateAzureCliCustom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export function GenerateAzureCliCustom(model: CodeModelAz): string[] {
header.addFromImport("azure.cli.core.util", ["sdk_no_wait"]);
}

if(required['disableUnusedArgument']) {
header.disableUnusedArgument = true;
}

let output = [];
output = output.concat(body);
output.push("");
Expand Down Expand Up @@ -68,6 +72,9 @@ function GenerateBody(model: CodeModelAz, required: any): string[] {
if (needUpdate) {
output = output.concat(GetCommandBody(model, required, needUpdate, originalOperation, needGeneric, genericParameter));
}
if (needGeneric) {
required['disableUnusedArgument'] = true;
}
}
while (model.SelectNextCommand());
}
Expand Down Expand Up @@ -335,9 +342,6 @@ function GetSingleCommandBody(model: CodeModelAz, required, originalOperation: O
}
continue;
}
required['json'] = true;
output_body.push(" if isinstance(" + model.MethodParameter_MapsTo + ", str):");
output_body.push(" " + model.MethodParameter_MapsTo + " = json.loads(" + model.MethodParameter_MapsTo + ")");
}
else if (model.MethodParameter_DefaultValue !== undefined && model.MethodParameter_Type != SchemaType.Constant) {
// model is simple type with default value
Expand Down
27 changes: 13 additions & 14 deletions src/plugins/azgenerator/TemplateAzureCliParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ export function GenerateAzureCliParams(model: CodeModelAz): string[] {
header.disableTooManyLines = true;
header.disableTooManyStatements = true;

if (hasJson) {
header.addFromImport("knack.arguments", ["CLIArgumentType"]);
}

let parameterImports: string[] = [];
if (hasTags) parameterImports.push("tags_type");
if (hasBoolean) parameterImports.push("get_three_state_flag");
Expand All @@ -82,8 +78,15 @@ export function GenerateAzureCliParams(model: CodeModelAz): string[] {
if (hasLocation) parameterImports.push("get_location_type");

header.addFromImport("azure.cli.core.commands.parameters", parameterImports);
let validatorImports: string[] = [];
if (hasLocationValidator) {
header.addFromImport('azure.cli.core.commands.validators', ['get_default_location_from_resource_group']);
validatorImports.push("get_default_location_from_resource_group");
}
if (hasJson) {
validatorImports.push("validate_file_or_dict");
}
if (validatorImports.length > 0) {
header.addFromImport('azure.cli.core.commands.validators', validatorImports);
}

if (hasActions) {
Expand Down Expand Up @@ -153,6 +156,7 @@ function getCommandBody(model: CodeModelAz, needUpdate: boolean = false, needGen
}
}
let baseParam = null;
let hasResourceGroupInOperation = false;
if (model.SelectFirstMethodParameter()) {
do {
if (model.MethodParameter_IsFlattened) {
Expand Down Expand Up @@ -241,19 +245,19 @@ function getCommandBody(model: CodeModelAz, needUpdate: boolean = false, needGen
argument += "])";
}

let hasJsonLastTime = false;
let needSkip = false;
if (parameterName == "resource_group_name") {
argument += ", resource_group_name_type";
hasResourceGroup = true;
hasResourceGroupInOperation = true;
needSkip = true;
} else if (parameterName == "tags") {
argument += ", tags_type";
hasTags = true;
needSkip = true;
} else if (parameterName == "location") {
argument += ", arg_type=get_location_type(self.cli_ctx)";
if (hasResourceGroup) {
if (hasResourceGroupInOperation) {
argument += ", validator=get_default_location_from_resource_group";
hasLocationValidator = true;
}
Expand All @@ -267,8 +271,7 @@ function getCommandBody(model: CodeModelAz, needUpdate: boolean = false, needGen
continue;
}
hasJson = true;
hasJsonLastTime = true;
argument += ", arg_type=CLIArgumentType(options_list=['--" + parameterName.replace(/_/g, '-') + "']";
argument += ", type=validate_file_or_dict";
} else if (model.MethodParameter_IsList && model.MethodParameter_IsListOfSimple) {
let actionName: string = model.Schema_ActionName(model.MethodParameter.schema);
argument += ", action=" + actionName;
Expand Down Expand Up @@ -312,11 +315,7 @@ function getCommandBody(model: CodeModelAz, needUpdate: boolean = false, needGen
}
}
argument += "'";

if (hasJsonLastTime) {
argument += ")";
hasJsonLastTime = false;
}

if (!isNullOrUndefined(baseParam) && model.MethodParameter['polyBaseParam'] == baseParam) {
argument += ", arg_group='" + Capitalize(ToCamelCase(model.Parameter_MapsTo(baseParam))) + "'";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# Changes may cause incorrect behavior and will be lost if the code is
# regenerated.
# --------------------------------------------------------------------------
# pylint: disable=too-many-statements
# pylint: disable=too-many-locals

from azure.cli.core.commands import CliCommandType

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
# pylint: disable=too-many-lines
# pylint: disable=too-many-statements

from knack.arguments import CLIArgumentType
from azure.cli.core.commands.parameters import (
tags_type,
get_enum_type,
resource_group_name_type,
get_location_type
)
from azure.cli.core.commands.validators import get_default_location_from_resource_group
from azure.cli.core.commands.validators import (
get_default_location_from_resource_group,
validate_file_or_dict
)
from azext_datafactory.action import (
AddFactoryVstsConfiguration,
AddFactoryGitHubConfiguration,
Expand Down Expand Up @@ -105,17 +107,17 @@ def load_arguments(self, _):
c.argument('trigger_name', options_list=['--name', '-n'], help='The trigger name.')
c.argument('if_match', help='ETag of the trigger entity. Should only be specified for update, for which it sho'
'uld match existing entity or can be * for unconditional update.')
c.argument('properties', arg_type=CLIArgumentType(options_list=['--properties'], help='Properties of the trigge'
'r. Expected value: json-string/@json-file.'))
c.argument('properties', type=validate_file_or_dict, help='Properties of the trigger. Expected value: json-stri'
'ng/@json-file.')

with self.argument_context('datafactory trigger update') as c:
c.argument('resource_group_name', resource_group_name_type)
c.argument('factory_name', help='The factory name.', id_part='name')
c.argument('trigger_name', options_list=['--name', '-n'], help='The trigger name.', id_part='child_name_1')
c.argument('if_match', help='ETag of the trigger entity. Should only be specified for update, for which it sho'
'uld match existing entity or can be * for unconditional update.')
c.argument('properties', arg_type=CLIArgumentType(options_list=['--properties'], help='Properties of the trigge'
'r. Expected value: json-string/@json-file.'))
c.argument('properties', type=validate_file_or_dict, help='Properties of the trigger. Expected value: json-stri'
'ng/@json-file.')

with self.argument_context('datafactory trigger delete') as c:
c.argument('resource_group_name', resource_group_name_type)
Expand Down Expand Up @@ -199,12 +201,10 @@ def load_arguments(self, _):
'itHub repo information.', arg_group='RepoConfiguration')
c.argument('fake_identity', action=AddFakeIdentity, nargs='+', help='This is only for az test.')
c.argument('zones', nargs='+', help='This is only for az test.')
c.argument('type_properties_compute_properties', arg_type=CLIArgumentType(options_list=['--type-properties-comp'
'ute-properties'], help='The compute resource for managed integration runtime. Expected value: json-'
'string/@json-file.'))
c.argument('type_properties_ssis_properties', arg_type=CLIArgumentType(options_list=['--type-properties-ssis-pr'
'operties'], help='SSIS properties for managed integration runtime. Expected value: json-string/@jso'
'n-file.'))
c.argument('type_properties_compute_properties', type=validate_file_or_dict, help='The compute resource for man'
'aged integration runtime. Expected value: json-string/@json-file.')
c.argument('type_properties_ssis_properties', type=validate_file_or_dict, help='SSIS properties for managed int'
'egration runtime. Expected value: json-string/@json-file.')

with self.argument_context('datafactory integration-runtime self-hosted create') as c:
c.argument('resource_group_name', resource_group_name_type)
Expand All @@ -213,9 +213,8 @@ def load_arguments(self, _):
c.argument('if_match', help='ETag of the integration runtime entity. Should only be specified for update, for w'
'hich it should match existing entity or can be * for unconditional update.')
c.argument('description', help='Integration runtime description.')
c.argument('type_properties_linked_info', arg_type=CLIArgumentType(options_list=['--type-properties-linked-info'
''], help='The base definition of a linked integration runtime. Expected value: json-string/@json-fi'
'le.'))
c.argument('type_properties_linked_info', type=validate_file_or_dict, help='The base definition of a linked int'
'egration runtime. Expected value: json-string/@json-file.')

with self.argument_context('datafactory integration-runtime update') as c:
c.argument('resource_group_name', resource_group_name_type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# Changes may cause incorrect behavior and will be lost if the code is
# regenerated.
# --------------------------------------------------------------------------
# pylint: disable=too-many-statements
# pylint: disable=too-many-locals

from azure.cli.core.commands import CliCommandType

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# regenerated.
# --------------------------------------------------------------------------
# pylint: disable=too-many-lines
# pylint: disable=unused-argument

import json
from knack.util import CLIError
Expand Down Expand Up @@ -150,8 +151,6 @@ def datafactory_trigger_create(client,
trigger_name,
properties,
if_match=None):
if isinstance(properties, str):
properties = json.loads(properties)
return client.create_or_update(resource_group_name=resource_group_name,
factory_name=factory_name,
trigger_name=trigger_name,
Expand Down Expand Up @@ -300,10 +299,6 @@ def datafactory_integration_runtime_managed_create(client,
raise CLIError('at most one of factory_vsts_configuration, factory_git_hub_configuration is needed for repo_co'
'nfiguration!')
repo_configuration = all_repo_configuration[0] if len(all_repo_configuration) == 1 else None
if isinstance(type_properties_compute_properties, str):
type_properties_compute_properties = json.loads(type_properties_compute_properties)
if isinstance(type_properties_ssis_properties, str):
type_properties_ssis_properties = json.loads(type_properties_ssis_properties)
properties = {}
properties['type'] = 'Managed'
properties['description'] = description
Expand All @@ -326,8 +321,6 @@ def datafactory_integration_runtime_self_hosted_create(client,
if_match=None,
description=None,
type_properties_linked_info=None):
if isinstance(type_properties_linked_info, str):
type_properties_linked_info = json.loads(type_properties_linked_info)
properties = {}
properties['type'] = 'SelfHosted'
properties['description'] = description
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
# pylint: disable=too-many-lines
# pylint: disable=too-many-statements

from knack.arguments import CLIArgumentType
from azure.cli.core.commands.parameters import (
tags_type,
resource_group_name_type,
get_location_type
)
from azure.cli.core.commands.validators import get_default_location_from_resource_group
from azure.cli.core.commands.validators import (
get_default_location_from_resource_group,
validate_file_or_dict
)
from azext_managed_network.action import (
AddSubscriptions,
AddVirtualNetworks,
Expand All @@ -42,8 +44,8 @@ def load_arguments(self, _):
c.argument('location', arg_type=get_location_type(self.cli_ctx),
validator=get_default_location_from_resource_group)
c.argument('tags', tags_type)
c.argument('properties', arg_type=CLIArgumentType(options_list=['--properties'], help='The MNC properties Expec'
'ted value: json-string/@json-file.'))
c.argument('properties', type=validate_file_or_dict, help='The MNC properties Expected value: json-string/@json'
'-file.')

with self.argument_context('managed-network mn update') as c:
c.argument('resource_group_name', resource_group_name_type)
Expand Down Expand Up @@ -77,8 +79,7 @@ def load_arguments(self, _):
'ype}/{resource-name}\' for a resource.')
c.argument('scope_assignment_name', options_list=['--name', '-n'], help='The name of the scope assignment to cr'
'eate.')
c.argument('location', arg_type=get_location_type(self.cli_ctx),
validator=get_default_location_from_resource_group)
c.argument('location', arg_type=get_location_type(self.cli_ctx))
c.argument('assigned_managed_network', help='The managed network ID with scope will be assigned to.')

with self.argument_context('managed-network mn scope-assignment update') as c:
Expand All @@ -89,8 +90,7 @@ def load_arguments(self, _):
'ype}/{resource-name}\' for a resource.')
c.argument('scope_assignment_name', options_list=['--name', '-n'], help='The name of the scope assignment to cr'
'eate.')
c.argument('location', arg_type=get_location_type(self.cli_ctx),
validator=get_default_location_from_resource_group)
c.argument('location', arg_type=get_location_type(self.cli_ctx))
c.argument('assigned_managed_network', help='The managed network ID with scope will be assigned to.')

with self.argument_context('managed-network mn scope-assignment delete') as c:
Expand All @@ -117,8 +117,8 @@ def load_arguments(self, _):
c.argument('group_name', help='The name of the Managed Network Group.')
c.argument('location', arg_type=get_location_type(self.cli_ctx),
validator=get_default_location_from_resource_group)
c.argument('management_groups', arg_type=CLIArgumentType(options_list=['--management-groups'], help='The collec'
'tion of management groups covered by the Managed Network Expected value: json-string/@json-file.'))
c.argument('management_groups', type=validate_file_or_dict, help='The collection of management groups covered b'
'y the Managed Network Expected value: json-string/@json-file.')
c.argument('subscriptions', action=AddSubscriptions, nargs='+', help='The collection of subscriptions covered b'
'y the Managed Network')
c.argument('virtual_networks', action=AddVirtualNetworks, nargs='+', help='The collection of virtual nets cover'
Expand All @@ -132,8 +132,8 @@ def load_arguments(self, _):
c.argument('group_name', help='The name of the Managed Network Group.', id_part='child_name_1')
c.argument('location', arg_type=get_location_type(self.cli_ctx),
validator=get_default_location_from_resource_group)
c.argument('management_groups', arg_type=CLIArgumentType(options_list=['--management-groups'], help='The collec'
'tion of management groups covered by the Managed Network Expected value: json-string/@json-file.'))
c.argument('management_groups', type=validate_file_or_dict, help='The collection of management groups covered b'
'y the Managed Network Expected value: json-string/@json-file.')
c.argument('subscriptions', action=AddSubscriptions, nargs='+', help='The collection of subscriptions covered b'
'y the Managed Network')
c.argument('virtual_networks', action=AddVirtualNetworks, nargs='+', help='The collection of virtual nets cover'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# Changes may cause incorrect behavior and will be lost if the code is
# regenerated.
# --------------------------------------------------------------------------
# pylint: disable=too-many-statements
# pylint: disable=too-many-locals

from azure.cli.core.commands import CliCommandType

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
# regenerated.
# --------------------------------------------------------------------------
# pylint: disable=too-many-lines
# pylint: disable=unused-argument

import json
from azure.cli.core.util import sdk_no_wait


Expand All @@ -31,8 +31,6 @@ def managed_network_mn_create(client,
location,
tags=None,
properties=None):
if isinstance(properties, str):
properties = json.loads(properties)
return client.create_or_update(resource_group_name=resource_group_name,
managed_network_name=managed_network_name,
location=location,
Expand Down Expand Up @@ -134,8 +132,6 @@ def managed_network_mn_group_create(client,
virtual_networks=None,
subnets=None,
no_wait=False):
if isinstance(management_groups, str):
management_groups = json.loads(management_groups)
return sdk_no_wait(no_wait,
client.begin_create_or_update,
resource_group_name=resource_group_name,
Expand All @@ -158,8 +154,6 @@ def managed_network_mn_group_update(client,
virtual_networks=None,
subnets=None,
no_wait=False):
if isinstance(management_groups, str):
management_groups = json.loads(management_groups)
return sdk_no_wait(no_wait,
client.begin_create_or_update,
resource_group_name=resource_group_name,
Expand Down

0 comments on commit f3a2154

Please sign in to comment.