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

extension mode configurable #360

Merged
merged 16 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from 14 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
1 change: 1 addition & 0 deletions readme.az.common.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# configuration for az common

``` yaml $(az)
extension-mode: experimental

cli:
naming:
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/azgenerator/CodeModelAz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface CodeModelAz
Extension_TestScenario: any;
Extension_ClientSubscriptionBound: boolean;
Extension_ClientBaseUrlBound: boolean;
Extension_Mode: string;

SelectFirstCommandGroup(): boolean;
SelectNextCommandGroup(): boolean;
Expand All @@ -73,6 +74,7 @@ export interface CodeModelAz
CommandGroup_Name: string;
CommandGroup_Help: string;
CommandGroup_DefaultName: string;
CommandGroup_HasShowCommand: boolean;

SelectFirstCommand(): boolean;
SelectNextCommand(): boolean;
Expand Down
38 changes: 34 additions & 4 deletions src/plugins/azgenerator/CodeModelAzImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,12 @@ export class CodeModelCliImpl implements CodeModelAz {
if (this.SelectFirstMethod()) {
let id_groups = new Map<string, string>();
id_groups = parseResourceId(this.Request.protocol.http.path);

let hasName = false;
if (this.SelectFirstMethodParameter()) {
do {
let parameters = this.MethodParameter;
let defaultName = parameters.language['cli']['cliKey'];
let defaultToMatch = '{' + defaultName + '}';

if(!isNullOrUndefined(id_groups)) {
for(let k of id_groups.entries()) {
if(k[1] == defaultToMatch && defaultName != 'resourceGroupName') {
Expand All @@ -178,20 +177,43 @@ export class CodeModelCliImpl implements CodeModelAz {
} else {
this.MethodParameter['RequiredByMethod'] = paramRequired.get(this.MethodParameter_Name) == paramTime ? true : false;
}
if (this.MethodParameter_MapsTo == 'name') {
hasName = true;
}
} while (this.SelectNextMethodParameter());
if (hasName) {
this.Method['hasName'] = true;
}
}
while (this.SelectNextMethod()) {
let id_groups = new Map<string, string>();
id_groups = parseResourceId(this.Request.protocol.http.path);
let hasName = false;
if (this.SelectFirstMethodParameter()) {
do {
let parameters = this.MethodParameter;
let defaultName = parameters.language['cli']['cliKey'];
let defaultToMatch = '{' + defaultName + '}';
if(!isNullOrUndefined(id_groups)) {
for(let k of id_groups.entries()) {
if(k[1] == defaultToMatch && defaultName != 'resourceGroupName') {
this.MethodParameter.language['az']['id_part'] = k[0];
}
}
}
if (parameters.language['cli'].required) {
this.MethodParameter['RequiredByMethod'] = true;
} else {
this.MethodParameter['RequiredByMethod'] = paramRequired.get(this.MethodParameter_Name) == paramTime ? true : false;
}
if (this.MethodParameter_MapsTo == 'name') {
hasName = true;
}
} while (this.SelectNextMethodParameter());
if (hasName) {
this.Method['hasName'] = true;
}
}

}
}
} while (this.SelectNextCommand());
Expand Down Expand Up @@ -403,7 +425,11 @@ export class CodeModelCliImpl implements CodeModelAz {
public get Extension_Name() {
return this.extensionName;
}


public get Extension_Mode() {
return this.codeModel.info['extensionMode'];
}

public get Extension_NameUnderscored() {
return this.extensionName.replace(/-/g, '_');
}
Expand Down Expand Up @@ -488,6 +514,10 @@ export class CodeModelCliImpl implements CodeModelAz {
return this.CommandGroup.$key || this.CommandGroup_Name;
}

public get CommandGroup_HasShowCommand(): boolean {
return this.CommandGroup.language['az']['hasShowCommand'];
}

public get CommandGroup_DefaultName(): string {
let eps = new EnglishPluralizationService();
return eps.singularize(this.CommandGroup.language['cli'].cliKey);
Expand Down
7 changes: 6 additions & 1 deletion src/plugins/azgenerator/TemplateAzureCliAzextMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ export function GenerateAzureCliAzextMetadata(model: CodeModelAz) : string[] {
var output: string[] = [];

output.push('{');
output.push(' "azext.isExperimental": true,');
if(model.Extension_Mode == 'experimental') {
output.push(' "azext.isExperimental": true,');
} else if(model.Extension_Mode == 'preview') {
output.push(' "azext.isPreview": true,');
}

output.push(' "azext.minCliCoreVersion": "2.3.1"');
output.push('}');

Expand Down
12 changes: 8 additions & 4 deletions src/plugins/azgenerator/TemplateAzureCliCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ToMultiLine } from "../../utils/helper"
import { isNullOrUndefined } from "util";
import { SchemaType } from "@azure-tools/codemodel";

let showCommandFunctionName = undefined;
export function GenerateAzureCliCommands(model: CodeModelAz): string[] {
let header: HeaderGenerator = new HeaderGenerator();

Expand Down Expand Up @@ -37,13 +38,15 @@ export function GenerateAzureCliCommands(model: CodeModelAz): string[] {
output.push(" client_factory=" + cf_name + ")");
let groupinfos = model.CommandGroup_Name.split(' ');
let extraInfo = "";
if(groupinfos.length == 2) {
if(groupinfos.length == 2 && model.Extension_Mode == 'experimental') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add similar logic when Extension_Mode is preview.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought preview don't need to add this is_preview=True at the end of the command?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not added, it means GA.

extraInfo = ", is_experimental=True";
} else if(groupinfos.length == 2 && model.Extension_Mode == 'preview') {
extraInfo = ", is_preview=True";
}
ToMultiLine(" with self.command_group('" + model.CommandGroup_Name + "', " + model.Extension_NameUnderscored + "_" + model.GetModuleOperationName() + ", client_factory=" + cf_name + extraInfo + ") as g:", output);
let needWait = false;
do {
if (model.Command_IsLongRun) {
if (model.Command_IsLongRun && model.CommandGroup_HasShowCommand) {
needWait = true;
}
output = output.concat(getCommandBody(model));
Expand All @@ -53,7 +56,7 @@ export function GenerateAzureCliCommands(model: CodeModelAz): string[] {
}
while (model.SelectNextCommand());
if (needWait) {
output.push(" g.wait_command('wait')");
output.push(" g.custom_wait_command('wait', '" + showCommandFunctionName + "')");
}
}
} while (model.SelectNextCommandGroup());
Expand All @@ -76,7 +79,7 @@ function getCommandBody(model: CodeModelAz, needUpdate: boolean = false) {
let functionName = model.Command_FunctionName;
let methodName = model.Command_MethodName;
let endStr = ")";
if (model.Command_IsLongRun) {
if (model.Command_IsLongRun && model.CommandGroup_HasShowCommand) {
endStr = ", supports_no_wait=True" + endStr;
}
if (methodName != "show") {
Expand Down Expand Up @@ -113,6 +116,7 @@ function getCommandBody(model: CodeModelAz, needUpdate: boolean = false) {
}
}
else {
showCommandFunctionName = functionName;
ToMultiLine(" g.custom_show_command('" + methodName + "', '" + functionName + "'" + endStr, output);
}
return output;
Expand Down
62 changes: 48 additions & 14 deletions src/plugins/azgenerator/TemplateAzureCliCustom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export function GenerateAzureCliCustom(model: CodeModelAz): string[] {
header.addFromImport("knack.util", ["CLIError"]);
}

if(required['nowait']) {
header.addFromImport("azure.cli.core.util", ["sdk_no_wait"]);
}

let output = [];
output = output.concat(body);
output.push("");
Expand Down Expand Up @@ -60,9 +64,9 @@ function GenerateBody(model: CodeModelAz, required: any): string[] {
needGeneric = true;
}
let needUpdate = model.Command_CanSplit;
output = output.concat(GetCommandBody(model, required, false, originalOperation, false));
output = output.concat(GetCommandBody(model, required, false, originalOperation, false, genericParameter));
if (needUpdate) {
output = output.concat(GetCommandBody(model, required, needUpdate, originalOperation, needGeneric));
output = output.concat(GetCommandBody(model, required, needUpdate, originalOperation, needGeneric, genericParameter));
}
}
while (model.SelectNextCommand());
Expand Down Expand Up @@ -136,7 +140,7 @@ function ConstructMethodBodyParameter(model: CodeModelAz, needGeneric: boolean =
return output_body;
}

function GetSingleCommandDef(model: CodeModelAz, originalOperation: Operation, needUpdate: boolean = false, needGeneric: boolean = false) {
function GetSingleCommandDef(model: CodeModelAz, required: any, originalOperation: Operation, needUpdate: boolean = false, needGeneric: boolean = false, genericParameter: Parameter = null) {

let output: string[] = [];
let updatedMethodName: string = model.Command_FunctionName;
Expand All @@ -153,9 +157,13 @@ function GetSingleCommandDef(model: CodeModelAz, originalOperation: Operation, n
output.push(call);

let allParam: Map<string, boolean> = new Map<string, boolean>();
let hasLongRun = false;
if (model.SelectFirstMethod()) {
do {

if(model.Method_IsLongRun && model.CommandGroup_HasShowCommand) {
required['nowait'] = true,
hasLongRun = true;
}
if (model.SelectFirstMethodParameter()) {
do {
if (model.MethodParameter_IsFlattened) {
Expand All @@ -165,6 +173,9 @@ function GetSingleCommandDef(model: CodeModelAz, originalOperation: Operation, n
continue;
}

if(needUpdate && !isNullOrUndefined(genericParameter) && model.MethodParameter_MapsTo == model.Parameter_MapsTo(genericParameter)) {
continue;
}
if (model.MethodParameter_IsList && !model.MethodParameter_IsListOfSimple) {
if (model.Parameter_IsPolyOfSimple(model.MethodParameter)) {
continue;
Expand Down Expand Up @@ -199,6 +210,10 @@ function GetSingleCommandDef(model: CodeModelAz, originalOperation: Operation, n
continue;
}

if(needUpdate && !isNullOrUndefined(genericParameter) && model.MethodParameter_MapsTo == model.Parameter_MapsTo(genericParameter)) {
continue;
}

if (model.MethodParameter_IsList && !model.MethodParameter_IsListOfSimple) {
if (model.Parameter_IsPolyOfSimple(model.MethodParameter)) {
continue;
Expand All @@ -222,23 +237,26 @@ function GetSingleCommandDef(model: CodeModelAz, originalOperation: Operation, n
} while (model.SelectNextMethod());
}

if(hasLongRun) {
output[output.length - 1] += ",";
output.push(indent + "no_wait=False");
}
output[output.length - 1] += "):";
return output;
}

function GetSingleCommandBody(model: CodeModelAz, required, originalOperation: Operation = null, needGeneric: boolean = false) {
function GetSingleCommandBody(model: CodeModelAz, required, originalOperation: Operation = null, needGeneric: boolean = false, genericParameter: Parameter = null, needUpdate: boolean = false) {
let originalParameters = null;
if(!isNullOrUndefined(originalOperation)) {
originalParameters = originalOperation.parameters;
if(!isNullOrUndefined(originalOperation.requests[0].parameters)) {
originalParameters = originalParameters.concat(originalOperation.requests[0].parameters);
}
}

let output: string[] = [];
let output_body: string[] = []
let output_method_call: string[] = [];

if (model.SelectFirstMethod()) {
// create body transformation for methods that support it
let methodName: string = model.Command_MethodName;
Expand All @@ -248,10 +266,14 @@ function GetSingleCommandBody(model: CodeModelAz, required, originalOperation: O
do {
if (model.SelectFirstMethodParameter()) {
do {
if(needUpdate && !isNullOrUndefined(genericParameter) && model.MethodParameter_MapsTo == model.Parameter_MapsTo(genericParameter)) {
continue;
}
if (model.MethodParameter_IsList && !model.MethodParameter_IsListOfSimple && !model.MethodParameter_IsSimpleArray) {
if (model.Parameter_IsPolyOfSimple(model.MethodParameter)) {
let baseParam = model.MethodParameter;
let baseName = model.MethodParameter_MapsTo;

if(allPolyBaseParam.has(baseName)) {
continue;
}
Expand Down Expand Up @@ -357,25 +379,31 @@ function GetSingleCommandBody(model: CodeModelAz, required, originalOperation: O
return output;
}

function GetCommandBody(model: CodeModelAz, required: boolean, needUpdate: boolean = false, originalOperation: Operation = null, needGeneric: boolean = false) {
function GetCommandBody(model: CodeModelAz, required: any, needUpdate: boolean = false, originalOperation: Operation = null, needGeneric: boolean = false, genericParameter: Parameter = null) {
// create, delete, list, show, update
let output: string[] = [];
output.push("");
output.push("");

output = output.concat(GetSingleCommandDef(model, originalOperation, needUpdate, needGeneric));
output = output.concat(GetSingleCommandBody(model, required, originalOperation, needGeneric))
output = output.concat(GetSingleCommandDef(model, required, originalOperation, needUpdate, needGeneric, genericParameter));
output = output.concat(GetSingleCommandBody(model, required, originalOperation, needGeneric, genericParameter, needUpdate))
return output;
}

function GetPolyMethodCall(model: CodeModelAz, prefix: any, originalOperation: Operation, originalParameters: Parameter[]): string[] {
let methodCall: string = prefix + "return ";
//methodCall += "client." + mode.GetModuleOperationName() +"." + ctx.Methods[methodIdx].Name + "(";
let methodName = originalOperation.language['python'].name;
if (model.Method_IsLongRun) {
if (model.Method_IsLongRun && model.CommandGroup_HasShowCommand) {
methodName = "begin_" + methodName;
methodCall += "sdk_no_wait(no_wait, " + "client." + methodName;
} else {
if(model.Method_IsLongRun) {
methodName = "begin_" + methodName;
}
methodCall += "client." + methodName + "(";
}
methodCall += "client." + methodName + "(";


let indent = " ".repeat(methodCall.length);
let cnt = 0;
Expand Down Expand Up @@ -422,10 +450,16 @@ function GetMethodCall(model: CodeModelAz, prefix: any): string[] {
let methodCall: string = prefix + "return ";
//methodCall += "client." + mode.GetModuleOperationName() +"." + ctx.Methods[methodIdx].Name + "(";
let methodName = model.Method_Name;
if (model.Method_IsLongRun) {
if (model.Method_IsLongRun && model.CommandGroup_HasShowCommand) {
methodName = "begin_" + methodName;
methodCall += "sdk_no_wait(no_wait, " + "client." + methodName;
} else {
if(model.Method_IsLongRun) {
methodName = "begin_" + methodName;
}
methodCall += "client." + methodName + "(";
}
methodCall += "client." + methodName + "(";


let indent = " ".repeat(methodCall.length);
if (model.SelectFirstMethodParameter()) {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/azgenerator/TemplateAzureCliHelp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function GenerateAzureCliHelp(model: CodeModelAz): string[] {
allSubGroup.set(subCommandGroupName, true);
output = output.concat(generateCommandGroupHelp(model, subCommandGroupName));
}
if(model.Command_IsLongRun) {
if(model.Command_IsLongRun && model.CommandGroup_HasShowCommand) {
hasWait = true;
let waitParam = "";
if (allSupportWaited.indexOf(model.Command_MethodName) < 0) {
Expand Down
Loading