/*** Generated by streamline 0.10.17 (callbacks) - DO NOT EDIT ***/ var __rt=require('streamline/lib/callbacks/runtime').runtime(__filename, false),__func=__rt.__func,__cb=__rt.__cb,__catch=__rt.__catch,__tryCatch=__rt.__tryCatch; var util = require("util");
















var utils = require("../../util/utils");
var batchUtil = require("./batch.util");
var batchShowUtil = require("./batch.showUtil");
var storageUtil = require("../../util/storage.util");

var $ = utils.getLocaleString;

exports.init = function(cli) {
  batchUtil.init(cli);

  var logger = cli.output;

  var batch = cli.category("batch").description($("Commands to manage your Batch objects"));


  var batchAccount = batch.category("account").description($("Commands to manage your Batch accounts"));


  var keys = batchAccount.category("keys").description($("Commands to manage your Batch account keys"));


  function showProgress(message) {
    return cli.interaction.progress(message); };


  function endProgress(progress) {

    if (progress) {
      progress.end(); } ; };



  function validateResourceGroupName(options, _) { var __frame = { name: "validateResourceGroupName", line: 50 }; return __func(_, this, arguments, validateResourceGroupName, 1, __frame, function __$validateResourceGroupName() {
      return cli.interaction.promptIfNotGiven($("Resource group name: "), options.resourceGroup, __cb(_, __frame, 1, 44, function ___(__0, __1) { options.resourceGroup = __1; _(); }, true)); }); };


  function listAccounts(serviceClient, options, _) { var progress, batchAccounts, response; var __frame = { name: "listAccounts", line: 54 }; return __func(_, this, arguments, listAccounts, 2, __frame, function __$listAccounts() {
      progress = showProgress($("Getting Batch accounts")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$listAccounts() { return (function __$listAccounts(__then) {



              if (options.resourceGroup) {
                return serviceClient.account.listByResourceGroup(options.resourceGroup, __cb(_, __frame, 6, 41, function ___(__0, __1) { response = __1;
                  batchAccounts = response; return (function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$listAccounts() { __more = false;

                      var __6 = response.nextLink; if (__6) {
                        return serviceClient.account.listByResourceGroup(options.resourceGroup, __cb(_, __frame, 10, 43, function ___(__0, __2) { response = __2;
                          batchAccounts.concat(response); while (__more) { __loop(); }; __more = true; }, true)); } else { __break(); } ; }); do { __loop(); } while (__more); __more = true; })(__then); }, true)); } else {



                return serviceClient.account.list(__cb(_, __frame, 15, 41, function ___(__0, __3) { response = __3;
                  batchAccounts = response; return (function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$listAccounts() { __more = false;

                      var __9 = response.nextLink; if (__9) {
                        return serviceClient.account.list(__cb(_, __frame, 19, 43, function ___(__0, __4) { response = __4;
                          batchAccounts.concat(response); while (__more) { __loop(); }; __more = true; }, true)); } else { __break(); } ; }); do { __loop(); } while (__more); __more = true; })(__then); }, true)); } ; })(function __$listAccounts() { _(null, null, true); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$listAccounts() {



              endProgress(progress); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$listAccounts() {


          return _(null, batchAccounts); }); }); }); };


  function showAccount(serviceClient, accountName, options, _) { var progress, message, batchAccount; var __frame = { name: "showAccount", line: 84 }; return __func(_, this, arguments, showAccount, 3, __frame, function __$showAccount() {

      message = $("Getting Batch account"); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$showAccount() {


            return validateResourceGroupName(options, __cb(_, __frame, 5, 6, function __$showAccount() {
              progress = showProgress(message);
              return serviceClient.account.get(options.resourceGroup, accountName, __cb(_, __frame, 7, 43, function ___(__0, __1) { batchAccount = __1; _(null, null, true); }, true)); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$showAccount() {

              endProgress(progress); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$showAccount() {


          return _(null, batchAccount); }); }); }); };


  function createAccount(serviceClient, accountName, parameters, options, _) { var progress, message, batchAccount; var __frame = { name: "createAccount", line: 99 }; return __func(_, this, arguments, createAccount, 4, __frame, function __$createAccount() {

      message = $("Creating Batch account"); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$createAccount() {



            return validateResourceGroupName(options, __cb(_, __frame, 6, 6, function __$createAccount() {
              progress = showProgress(message);
              return serviceClient.account.create(options.resourceGroup, accountName, parameters, __cb(_, __frame, 8, 43, function ___(__0, __1) { batchAccount = __1; _(null, null, true); }, true)); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$createAccount() {

              endProgress(progress); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$createAccount() {


          return _(null, batchAccount); }); }); }); };


  function setAccount(serviceClient, accountName, parameters, options, _) { var progress, message, batchAccount; var __frame = { name: "setAccount", line: 115 }; return __func(_, this, arguments, setAccount, 4, __frame, function __$setAccount() {

      message = $("Updating Batch account"); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$setAccount() {



            return validateResourceGroupName(options, __cb(_, __frame, 6, 6, function __$setAccount() {
              progress = showProgress(message);
              return serviceClient.account.update(options.resourceGroup, accountName, parameters, __cb(_, __frame, 8, 43, function ___(__0, __1) { batchAccount = __1; _(null, null, true); }, true)); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$setAccount() {

              endProgress(progress); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$setAccount() {


          return _(null, batchAccount); }); }); }); };


  function deleteAccount(serviceClient, accountName, options, _) { var progress, message; var __frame = { name: "deleteAccount", line: 131 }; return __func(_, this, arguments, deleteAccount, 3, __frame, function __$deleteAccount() {

      message = $("Deleting Batch account"); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$deleteAccount() { return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$deleteAccount() {


                  return validateResourceGroupName(options, __cb(_, __frame, 5, 6, function __$deleteAccount() {
                    progress = showProgress(message);
                    return serviceClient.account.deleteMethod(options.resourceGroup, accountName, __cb(_, __frame, 7, 28, __then, true)); }, true)); }); })(function ___(err, __result) { __catch(function __$deleteAccount() { if (err) {







                    if (((err.message.indexOf("ResourceNotFound") === -1) || (err.message.indexOf("when polling for operation status") === -1))) {
                      return _(err); } ; __then(); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, function __$deleteAccount() { _(null, null, true); }); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$deleteAccount() {


              endProgress(progress); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$deleteAccount() { _(); }); }); }); };



  function syncAutoStorageKeys(serviceClient, accountName, options, _) { var progress, message; var __frame = { name: "syncAutoStorageKeys", line: 154 }; return __func(_, this, arguments, syncAutoStorageKeys, 3, __frame, function __$syncAutoStorageKeys() {

      message = $("Synchronizing auto storage account keys"); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$syncAutoStorageKeys() {


            return validateResourceGroupName(options, __cb(_, __frame, 5, 6, function __$syncAutoStorageKeys() {
              progress = showProgress(message);
              return serviceClient.account.synchronizeAutoStorageKeys(options.resourceGroup, accountName, __cb(_, __frame, 7, 28, function __$syncAutoStorageKeys() { _(null, null, true); }, true)); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$syncAutoStorageKeys() {

              endProgress(progress); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$syncAutoStorageKeys() { _(); }); }); }); };



  function getAccountKeys(serviceClient, accountName, options, _) { var progress, message, keys; var __frame = { name: "getAccountKeys", line: 167 }; return __func(_, this, arguments, getAccountKeys, 3, __frame, function __$getAccountKeys() {

      message = $("Getting Batch account keys"); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$getAccountKeys() {



            return validateResourceGroupName(options, __cb(_, __frame, 6, 6, function __$getAccountKeys() {
              progress = showProgress(message);
              return serviceClient.account.listKeys(options.resourceGroup, accountName, __cb(_, __frame, 8, 35, function ___(__0, __1) { keys = __1; _(null, null, true); }, true)); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$getAccountKeys() {

              endProgress(progress); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$getAccountKeys() {


          return _(null, keys); }); }); }); };


  function regenerateAccountKeys(serviceClient, accountName, options, _) { var progress, message, keys, keyType; var __frame = { name: "regenerateAccountKeys", line: 183 }; return __func(_, this, arguments, regenerateAccountKeys, 3, __frame, function __$regenerateAccountKeys() {

      message = $("Renewing Batch account key"); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$regenerateAccountKeys() {




            return validateResourceGroupName(options, __cb(_, __frame, 7, 6, function __$regenerateAccountKeys() {
              progress = showProgress(message);
              keyType = (options.primary ? "Primary" : "Secondary");
              return serviceClient.account.regenerateKey(options.resourceGroup, accountName, keyType, __cb(_, __frame, 10, 35, function ___(__0, __1) { keys = __1; _(null, null, true); }, true)); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$regenerateAccountKeys() {

              endProgress(progress); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$regenerateAccountKeys() {


          return _(null, keys); }); }); }); };


  batchAccount.listCommand = function batchAccount_listCommand__1(options, _) { var service, batchAccounts; var __frame = { name: "batchAccount_listCommand__1", line: 201 }; return __func(_, this, arguments, batchAccount_listCommand__1, 1, __frame, function __$batchAccount_listCommand__1() {
      service = batchUtil.createBatchManagementClient(options.subscription);

      return listAccounts(service, options, __cb(_, __frame, 3, 24, function ___(__0, __1) { batchAccounts = __1;

        batchAccounts.forEach(function(batchAccount) {
          batchAccount.resourceGroup = batchUtil.parseResourceGroupNameFromId(batchAccount.id); });


        cli.interaction.formatOutput(batchAccounts, function(outputData) {
          if ((outputData.length === 0)) {
            logger.info($("No Batch accounts found")); }
           else {
            logger.table(outputData, function(row, item) {
              row.cell($("Name"), item.name);
              row.cell($("Location"), item.location);
              row.cell($("Resource Group"), item.resourceGroup); }); } ; }); _(); }, true)); }); };





  batchAccount.showCommand = function batchAccount_showCommand__2(name, options, _) { var service, batchAccount; var __frame = { name: "batchAccount_showCommand__2", line: 223 }; return __func(_, this, arguments, batchAccount_showCommand__2, 2, __frame, function __$batchAccount_showCommand__2() {
      service = batchUtil.createBatchManagementClient(options.subscription);

      return showAccount(service, name, options, __cb(_, __frame, 3, 23, function ___(__0, __1) { batchAccount = __1;

        if (batchAccount) {
          batchShowUtil.showBatchAccount(batchAccount, cli.output); }
         else {
          logger.info($("No Batch account found")); } ; _(); }, true)); }); };



  batchAccount.createCommand = function batchAccount_createCommand__3(name, options, _) { var service, batchCreateParameters; var __frame = { name: "batchAccount_createCommand__3", line: 235 }; return __func(_, this, arguments, batchAccount_createCommand__3, 2, __frame, function __$batchAccount_createCommand__3() {
      service = batchUtil.createBatchManagementClient(options.subscription);

      return cli.interaction.promptIfNotGiven($("Location: "), options.location, __cb(_, __frame, 3, 39, function ___(__0, __1) { options.location = __1;
        batchCreateParameters = {
          location: options.location };


        if (options.tags) {
          batchCreateParameters.tags = storageUtil.parseKvParameterInvariant(options.tags); } ;


        if (options.autostorageAccountId) {
          batchCreateParameters.autoStorage = {
            storageAccountId: options.autostorageAccountId }; } ;



        return createAccount(service, name, batchCreateParameters, options, __cb(_, __frame, 18, 4, function __$batchAccount_createCommand__3() { _(); }, true)); }, true)); }); };


  batchAccount.setCommand = function batchAccount_setCommand__4(name, options, _) { var service, batchUpdateParameters; var __frame = { name: "batchAccount_setCommand__4", line: 256 }; return __func(_, this, arguments, batchAccount_setCommand__4, 2, __frame, function __$batchAccount_setCommand__4() {
      service = batchUtil.createBatchManagementClient(options.subscription);

      batchUpdateParameters = { };

      if (options.tags) {
        batchUpdateParameters.tags = storageUtil.parseKvParameterInvariant(options.tags); } ;


      if (options.autostorageAccountId) {
        batchUpdateParameters.autoStorage = {
          storageAccountId: options.autostorageAccountId }; } ;



      return setAccount(service, name, batchUpdateParameters, options, __cb(_, __frame, 15, 4, function __$batchAccount_setCommand__4() { _(); }, true)); }); };


  batchAccount.deleteCommand = function batchAccount_deleteCommand__5(name, options, _) { var service; var __frame = { name: "batchAccount_deleteCommand__5", line: 274 }; return __func(_, this, arguments, batchAccount_deleteCommand__5, 2, __frame, function __$batchAccount_deleteCommand__5() {
      service = batchUtil.createBatchManagementClient(options.subscription); return (function __$batchAccount_deleteCommand__5(_) {

        var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return cli.interaction.confirm(util.format($("Delete Batch account %s? [y/n] "), name), __cb(_, __frame, 3, 43, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -273, 18, function ___(__0, __2) { return (function __$batchAccount_deleteCommand__5(__then) { if (__2) { return _(null); } else { __then(); } ; })(function __$batchAccount_deleteCommand__5() {



          return deleteAccount(service, name, options, __cb(_, __frame, 7, 4, function __$batchAccount_deleteCommand__5() { _(); }, true)); }); }, true)); }); };


  batchAccount.syncAutoStorageKeysCommand = function batchAccount_syncAutoStorageKeysCommand__6(name, options, _) { var service; var __frame = { name: "batchAccount_syncAutoStorageKeysCommand__6", line: 284 }; return __func(_, this, arguments, batchAccount_syncAutoStorageKeysCommand__6, 2, __frame, function __$batchAccount_syncAutoStorageKeysCommand__6() {
      service = batchUtil.createBatchManagementClient(options.subscription);

      return syncAutoStorageKeys(service, name, options, __cb(_, __frame, 3, 4, function __$batchAccount_syncAutoStorageKeysCommand__6() { _(); }, true)); }); };


  keys.listCommand = function keys_listCommand__7(name, options, _) { var service, keys; var __frame = { name: "keys_listCommand__7", line: 290 }; return __func(_, this, arguments, keys_listCommand__7, 2, __frame, function __$keys_listCommand__7() {
      service = batchUtil.createBatchManagementClient(options.subscription);

      return getAccountKeys(service, name, options, __cb(_, __frame, 3, 15, function ___(__0, __1) { keys = __1;

        if (keys) {
          cli.interaction.formatOutput(keys, function(outputData) {
            logger.data($("Primary:"), outputData.primary);
            logger.data($("Secondary:"), outputData.secondary); }); }

         else {
          logger.info($("No Batch account keys found")); } ; _(); }, true)); }); };



  keys.renewCommand = function keys_renewCommand__8(name, options, _) { var service, keys; var __frame = { name: "keys_renewCommand__8", line: 305 }; return __func(_, this, arguments, keys_renewCommand__8, 2, __frame, function __$keys_renewCommand__8() {
      service = batchUtil.createBatchManagementClient(options.subscription);

      if ((!options.primary && !options.secondary)) {
        return _(new Error($("Need to specify either --primary or --secondary"))); } else {
        if ((options.primary && options.secondary)) {
          return _(new Error($("Only one of primary or secondary keys can be renewed at a time"))); } ; } ;


      return regenerateAccountKeys(service, name, options, __cb(_, __frame, 9, 15, function ___(__0, __1) { keys = __1;

        if (keys) {
          cli.interaction.formatOutput(keys, function(outputData) {
            logger.data($("Primary:"), outputData.primary);
            logger.data($("Secondary:"), outputData.secondary); }); }

         else {
          logger.info($("No Batch account keys found")); } ; _(); }, true)); }); };




  batchAccount.command("list").description($("List the Batch accounts associated with the subscription or resource group")).appendSubscriptionAndResourceGroupOption().execute(batchAccount.listCommand);





  batchAccount.command("show <name>").description($("Show detailed information about the specified Batch account")).appendSubscriptionAndResourceGroupOption().execute(batchAccount.showCommand);





  batchAccount.command("create <name>").description($("Creates a new Batch account with the specified parameters")).option("-l, --location <location>", $("the location")).option("--tags <tags>", $("the account tags. Tags are key=value pairs and separated with semicolon(;)")).option("--autostorage-account-id <autostorageAccountId>", $("the resource id of the storage account to be used for auto storage")).appendSubscriptionAndResourceGroupOption().execute(batchAccount.createCommand);







  batchAccount.command("set <name>").description($("Updates the properties of an existing Batch account in the specified resource group")).option("--tags <tags>", $("the account tags. Tags are key=value pairs and separated with semicolon(;)")).option("--autostorage-account-id <autostorageAccountId>", $("the resource id of the storage account to be used for auto storage")).appendSubscriptionAndResourceGroupOption().execute(batchAccount.setCommand);






  batchAccount.command("delete <name>").description($("Deletes the specified Batch account")).appendSubscriptionAndResourceGroupOption().option("-q, --quiet", $("quiet mode, do not ask for delete confirmation")).execute(batchAccount.deleteCommand);






  batchAccount.command("sync-autostorage-keys <name>").description($("Synchronizes access keys for the auto storage account configured for the specified Batch account")).appendSubscriptionAndResourceGroupOption().execute(batchAccount.syncAutoStorageKeysCommand);





  keys.command("list <name>").description($("Gets the account keys for the given Batch account")).appendSubscriptionAndResourceGroupOption().execute(keys.listCommand);





  keys.command("renew <name>").description($("Regenerates the specified account key for the given Batch account")).option("--primary", $("Update the primary key")).option("--secondary", $("Update the secondary key")).appendSubscriptionAndResourceGroupOption().execute(keys.renewCommand);};
