async createCommands()

in powershell/plugins/create-commands-v2.ts [164:257]


  async createCommands() {
    const model = this.state.model;
    this.state.model.commands = <any>{
      operations: new Dictionary<any>(),
      parameters: new Dictionary<any>(),
    };
    const disableGetPut = await this.state.getValue('disable-getput', false);
    const disableTransformIdentityType = await this.state.getValue('disable-transform-identity-type', false);
    const disableTransformIdentityTypeForOperation = await this.state.getValue('disable-transform-identity-type-for-operation', []);
    const optsToExclude = new Array<string>();
    if (disableTransformIdentityTypeForOperation) {
      for (const item of values(disableTransformIdentityTypeForOperation)) {
        optsToExclude.push(item);
      }
    }
    this.state.message({ Channel: Channel.Debug, Text: 'detecting high level commands...' });
    for (const operationGroup of values(model.operationGroups)) {
      let hasPatch = false;
      let commandType!: CommandType;
      const getOperations: Array<Operation> = [];
      let putOperation: Operation | undefined;
      let patchOperation: Operation | undefined;
      for (const operation of values(operationGroup.operations)) {
        if (operation.requests?.[0]?.protocol?.http?.method.toLowerCase() === 'patch') {
          hasPatch = true;
          patchOperation = operation;
          // bez: remove patch operation to avoid conflicts with replacements
          if (this.isAzure && !disableTransformIdentityType && this.IsManagedIdentityOperation(operation)) {
            continue;
          }
        } else if (operation.requests?.[0]?.protocol?.http?.method.toLowerCase() === 'get') {
          getOperations.push(operation);
        } else if (operation.requests?.[0]?.protocol?.http?.method.toLowerCase() === 'put') {
          putOperation = operation;
          if (this.isAzure && !disableTransformIdentityType && this.IsManagedIdentityOperation(operation)) {
            commandType = CommandType.ManagedIdentityNew;
          }
        }
        for (const variant of await this.inferCommandNames(operation, operationGroup.$key, this.state)) {
          await this.addVariants(operation.parameters, operation, variant, '', this.state, undefined, commandType);
        }
      }

      // bez: if we have get+put, try to replace
      if (this.isAzure && getOperations && putOperation && putOperation.requests?.length == 1) {
        const getOperation = getOperations.find(getOperation => getOperation.requests?.[0]?.protocol?.http?.path === putOperation?.requests?.[0]?.protocol?.http?.path);
        const supportsCombineGetPutOperation = getOperation && this.supportsGetPut(getOperation, putOperation);
        if (!disableTransformIdentityType && supportsCombineGetPutOperation &&
          (hasPatch && patchOperation && this.IsManagedIdentityOperation(patchOperation)
            || !hasPatch && putOperation && this.IsManagedIdentityOperation(putOperation))) {
          const variant = (await this.inferCommandNames(getOperation, operationGroup.$key, this.state))[0];
          await this.addVariants(putOperation.parameters, putOperation, variant, '', this.state, [getOperation], CommandType.ManagedIdentityUpdate);
        } else if (!disableTransformIdentityType && !supportsCombineGetPutOperation && hasPatch && patchOperation && this.IsManagedIdentityOperation(patchOperation)) {
          if (!optsToExclude.includes(patchOperation.operationId ?? '')) {
            const transformIdentityTypeErrorMessage = `Parameter IdentityType in operation '${patchOperation.operationId}' can not be transformed as the best practice design. See https://github.com/Azure/azure-powershell/blob/main/documentation/development-docs/design-guidelines/managed-identity-best-practices.md#frequently-asked-question to mitigate this issue.`;
            this.state.message({ Channel: Channel.Error, Text: transformIdentityTypeErrorMessage });
            throw new Error(transformIdentityTypeErrorMessage);
          }
          // bez: add patch operation back and disable transforming identity type
          for (const variant of await this.inferCommandNames(patchOperation, operationGroup.$key, this.state)) {
            await this.addVariants(patchOperation.parameters, patchOperation, variant, '', this.state);
          }
        } else if (!disableGetPut && !hasPatch && supportsCombineGetPutOperation) {
          /* generate variants for Update(Get+Put) for subjects only if: 
           - there is a get operation 
           - there is a put operation
           - get operation path is the same as put operation path
           - there is only one put request schema
           - get operation response schema type is the same as put operation request schema type
           */
          const variant = (await this.inferCommandNames(getOperation, operationGroup.$key, this.state))[0];
          await this.addVariants(putOperation.parameters, putOperation, variant, '', this.state, [getOperation], CommandType.GetPut);
        }
      } else if (this.isAzure && !disableTransformIdentityType && patchOperation && this.IsManagedIdentityOperation(patchOperation)) {
        if (!optsToExclude.includes(patchOperation.operationId ?? '')) {
          const transformIdentityTypeErrorMessage = `Parameter IdentityType in operation '${patchOperation.operationId}' can not be transformed as the best practice design. See https://github.com/Azure/azure-powershell/blob/main/documentation/development-docs/design-guidelines/managed-identity-best-practices.md#frequently-asked-question to mitigate this issue.`;
          this.state.message({ Channel: Channel.Error, Text: transformIdentityTypeErrorMessage });
          throw new Error(transformIdentityTypeErrorMessage);
        }

        // bez: add variants back and disable transforming identity type as no put or get
        for (const variant of await this.inferCommandNames(patchOperation, operationGroup.$key, this.state)) {
          await this.addVariants(patchOperation.parameters, patchOperation, variant, '', this.state);
        }
      }
    }
    // for (const operation of values(model.http.operations)) {
    //   for (const variant of await this.inferCommandNames(operation, this.state)) {
    //     // no common parameters (standard variations)
    //     await this.addVariants(operation.parameters, operation, variant, '', this.state);
    //   }
    // }
    return model;
  }