/*** 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 __ = require("underscore");















var util = require("util");

var utils = require("../../../util/utils");
var validation = require("../../../util/validation");

var $ = utils.getLocaleString;

function VMStorageProfile(cli, resourceGroupName, params, serviceClients) {
  this.cli = cli;
  this.storageManagementClient = serviceClients.storageManagementClient;
  this.resourceGroupName = resourceGroupName;

  this.params = params;
  this.prefix = params.prefix;};


__.extend(VMStorageProfile.prototype, {
  generateStorageProfile: function generateStorageProfile__1(_) { var storageProfile, containerUri, __this = this; var __frame = { name: "generateStorageProfile__1", line: 34 }; return __func(_, this, arguments, generateStorageProfile__1, 0, __frame, function __$generateStorageProfile__1() {
      storageProfile = {
        profile: { },
        osDiskDataDiskImgInfo: null,
        storageAccountInfo: null };


      return __this._parseOSDataDiskImageParams(__this.params, __cb(_, __frame, 7, 54, function ___(__0, __1) { storageProfile.osDiskDataDiskImgInfo = __1;
        containerUri = null; return (function __$generateStorageProfile__1(__then) {
          if (storageProfile.osDiskDataDiskImgInfo.requireStorage) {
            __this.cli.output.info(util.format($("The [OS, Data] Disk or image configuration requires storage account")));

            __this.params.storageAccountContainerName = (__this.params.storageAccountContainerName || "vhds");
            return __this._createStorageAccountIfRequired(__cb(_, __frame, 13, 53, function ___(__0, __2) { storageProfile.storageAccountInfo = __2;
              if (storageProfile.storageAccountInfo.profile.storageAccount) {
                containerUri = ((storageProfile.storageAccountInfo.profile.storageAccount.primaryEndpoints.blob + __this.params.storageAccountContainerName) + "/"); }
               else {
                containerUri = ((storageProfile.storageAccountInfo.profile.primaryEndpoints.blob + __this.params.storageAccountContainerName) + "/"); } ; __then(); }, true)); } else { __then(); } ; })(function __$generateStorageProfile__1() {



          if ((storageProfile.osDiskDataDiskImgInfo.osDiskInfo && !storageProfile.osDiskDataDiskImgInfo.osDiskInfo.isVhdParamAUrl)) {
            storageProfile.osDiskDataDiskImgInfo.osDiskInfo.profile.vhd.uri = (containerUri + storageProfile.osDiskDataDiskImgInfo.osDiskInfo.profile.vhd.uri); } ;


          if (storageProfile.osDiskDataDiskImgInfo.osDiskInfo) {
            storageProfile.profile.osDisk = storageProfile.osDiskDataDiskImgInfo.osDiskInfo.profile; } ;


          if ((storageProfile.osDiskDataDiskImgInfo.dataDiskInfo && !storageProfile.osDiskDataDiskImgInfo.dataDiskInfo.isVhdParamAUrl)) {
            storageProfile.osDiskDataDiskImgInfo.dataDiskInfo.profile.vhd.uri = (containerUri + storageProfile.osDiskDataDiskImgInfo.dataDiskInfo.profile.vhd.uri); } ;


          if (storageProfile.osDiskDataDiskImgInfo.dataDiskInfo) {
            storageProfile.osDiskDataDiskImgInfo.dataDiskInfo.profile.lun = __this._generateDataDiskLun();
            storageProfile.profile.dataDisks = [storageProfile.osDiskDataDiskImgInfo.dataDiskInfo.profile,]; } ;


          if (storageProfile.osDiskDataDiskImgInfo.imageInfo) {

            storageProfile.profile.imageReference = storageProfile.osDiskDataDiskImgInfo.imageInfo.profile;
            storageProfile.profile.destinationVhdsContainer = containerUri; } ;


          if (storageProfile.osDiskDataDiskImgInfo.userImageVhd) {
            if (!storageProfile.profile.osDisk) {
              storageProfile.profile.osDisk = { }; } ;


            storageProfile.profile.osDisk.osType = __this.params.osType;
            storageProfile.profile.osDisk.image = { };
            storageProfile.profile.osDisk.image.uri = storageProfile.osDiskDataDiskImgInfo.userImageVhd;

            __this.cli.output.info(util.format($("Using \"%s\" as the user image."), storageProfile.osDiskDataDiskImgInfo.userImageVhd)); } ;


          return _(null, storageProfile); }); }, true)); }); },


  generateDataDiskProfile: function generateDataDiskProfile__2(_) { var dataDiskInfo, lun, containerUri, __this = this; var __frame = { name: "generateDataDiskProfile__2", line: 93 }; return __func(_, this, arguments, generateDataDiskProfile__2, 0, __frame, function __$generateDataDiskProfile__2() {
      dataDiskInfo = __this._parseDataDiskParams(__this.params);
      if (__this.params.lun) {
        lun = utils.parseInt(__this.params.lun);
        if (isNaN(lun)) {
          return _(new Error($("lun must be an integer"))); } ;

        dataDiskInfo.profile.lun = lun; }
       else {
        dataDiskInfo.profile.lun = __this._generateDataDiskLun(); } ;


      if (dataDiskInfo.isVhdParamAUrl) {
        return _(null, dataDiskInfo.profile); } else {
        if (!__this.params.newDataDisk) {
          return _(new Error($("dataDiskVhd URL param should be a valid URL."))); } ; } ;


      return __this._generateDataDiskContainerURI(__cb(_, __frame, 18, 34, function ___(__0, __1) { containerUri = __1;
        dataDiskInfo.profile.vhd.uri = (containerUri + dataDiskInfo.profile.vhd.uri);

        return _(null, dataDiskInfo.profile); }, true)); }); },


  removeDataDiskByLun: function(virtualMachine, lun) {
    var dataDisks = virtualMachine.storageProfile.dataDisks;
    if ((!dataDisks || (dataDisks.length === 0))) {
      throw new Error(util.format($("There are no data disks attached to virtual machine \"%s\""), virtualMachine.name)); } ;


    var dataDiskIndex = -1;
    for (var index = 0; (index < dataDisks.length); index++) {
      if ((dataDisks[index].lun !== lun)) {
        continue; } ;


      dataDiskIndex = index;
      break; };


    if ((dataDiskIndex === -1)) {
      throw new Error(util.format($("There is no data disk with lun \"%s\" attached to virtual machine \"%s\""), lun, virtualMachine.name)); } ;


    dataDisks.splice(dataDiskIndex, 1); },


  hasAllOSDiskParams: function(params) {
    return utils.allParamsAreSet([params.osDiskType,params.osDiskVhd,]); },


  _generateDataDiskLun: function() {
    var lunArray = [];
    for (var i = 0; (i < this.params.dataDisks.length); i++) {
      var lun = (this.params.dataDisks[i].lun ? parseInt(this.params.dataDisks[i].lun, 10) : 0);
      lunArray[lun] = true; };


    for (var j = 0; (j < lunArray.length); j++) {
      if (!lunArray[j]) {
        return j; } ; };



    return lunArray.length; },


  _generateDataDiskContainerURI: function _generateDataDiskContainerURI__3(_) { var containerUri, storageAccountInfo, __this = this; var __frame = { name: "_generateDataDiskContainerURI__3", line: 160 }; return __func(_, this, arguments, _generateDataDiskContainerURI__3, 0, __frame, function __$_generateDataDiskContainerURI__3() { return (function __$_generateDataDiskContainerURI__3(__then) {

        if (utils.allParamsAreSet([__this.params.storageAccountName,__this.params.storageAccountContainerName,])) {
          return __this._createStorageAccountIfRequired(__cb(_, __frame, 3, 42, function ___(__0, __1) { storageAccountInfo = __1;
            if (storageAccountInfo.profile.storageAccount) {
              containerUri = ((storageAccountInfo.profile.storageAccount.primaryEndpoints.blob + __this.params.storageAccountContainerName) + "/"); }
             else {
              containerUri = ((storageAccountInfo.profile.primaryEndpoints.blob + __this.params.storageAccountContainerName) + "/"); } ; __then(); }, true)); } else {



          if (utils.stringIsNullOrEmpty(__this.params.osDiskUri)) {
            return _(new Error($("params.osDiskUri is required when vhd-name and --storage-account-* parameters are not specified"))); } ;


          containerUri = (__this.params.osDiskUri.slice(0, __this.params.osDiskUri.lastIndexOf("/")) + "/"); __then(); } ; })(function __$_generateDataDiskContainerURI__3() {


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


  _parseOSDataDiskImageParams: function _parseOSDataDiskImageParams__4(params, _) { var osDiskDataDiskImageInfo, imageUrnParts, __this = this; var __frame = { name: "_parseOSDataDiskImageParams__4", line: 181 }; return __func(_, this, arguments, _parseOSDataDiskImageParams__4, 1, __frame, function __$_parseOSDataDiskImageParams__4() {
      osDiskDataDiskImageInfo = {
        osDiskInfo: null,
        dataDiskInfo: null,
        imageInfo: null,
        requireStorage: false }; return (function __$_parseOSDataDiskImageParams__4(__then) {


        if (params.imageUrn) {
          imageUrnParts = params.imageUrn.split(":");

          if ((imageUrnParts.length === 4)) {
            osDiskDataDiskImageInfo.imageInfo = {
              profile: {
                publisher: imageUrnParts[0],
                offer: imageUrnParts[1],
                sku: imageUrnParts[2],
                version: imageUrnParts[3] } }; } else {


            if (validation.isURL(params.imageUrn)) {
              osDiskDataDiskImageInfo.imageInfo = null;
              osDiskDataDiskImageInfo.userImageVhd = params.imageUrn; }
             else {
              return _(new Error($("--image-urn must be in the form \"publisherName:offer:skus:version\", or a blob url to the user image VHD."))); } ; } ;



          return __this._parseOSDiskParams(params, "FromImage", __cb(_, __frame, 28, 56, function ___(__0, __1) { osDiskDataDiskImageInfo.osDiskInfo = __1;
            if ((osDiskDataDiskImageInfo.osDiskInfo && !osDiskDataDiskImageInfo.osDiskInfo.isVhdParamAUrl)) {
              osDiskDataDiskImageInfo.requireStorage = true; } ; __then(); }, true)); } else {



          return __this._parseOSDiskParams(params, "Attach", __cb(_, __frame, 34, 56, function ___(__0, __2) { osDiskDataDiskImageInfo.osDiskInfo = __2;
            if ((osDiskDataDiskImageInfo.osDiskInfo && !osDiskDataDiskImageInfo.osDiskInfo.isVhdParamAUrl)) {
              osDiskDataDiskImageInfo.requireStorage = true; } ; __then(); }, true)); } ; })(function __$_parseOSDataDiskImageParams__4() {



        if ((((osDiskDataDiskImageInfo.imageInfo === null) && (osDiskDataDiskImageInfo.userImageVhd === null)) && (osDiskDataDiskImageInfo.osDiskInfo === null))) {
          return _(new Error($("Either imageUrn or os-disk-vhd parameter is required to create VM"))); } ;


        osDiskDataDiskImageInfo.dataDiskInfo = __this._parseDataDiskParams(params);
        if ((osDiskDataDiskImageInfo.dataDiskInfo && !osDiskDataDiskImageInfo.dataDiskInfo.isVhdParamAUrl)) {
          osDiskDataDiskImageInfo.requireStorage = true; } ;



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


  _parseOSDiskParams: function _parseOSDiskParams__5(params, createOption, _) { var attachMode, osType, useOsDisk, osDiskInfo, supportedDiskCaching, __this = this; var __frame = { name: "_parseOSDiskParams__5", line: 234 }; return __func(_, this, arguments, _parseOSDiskParams__5, 2, __frame, function __$_parseOSDiskParams__5() {
      if (((createOption !== "Attach") && (createOption !== "FromImage"))) {
        return _(new Error(util.format($("invalid createOption \"%s\"\""), createOption))); } ;



      attachMode = (createOption === "Attach");
      osType = null; return (function __$_parseOSDiskParams__5(__then) {
        if (attachMode) {
          useOsDisk = utils.atLeastOneParameIsSet([params.osDiskType,params.osDiskCaching,params.osDiskVhd,]);
          if (!useOsDisk) {
            return _(null, null); } ; return (function __$_parseOSDiskParams__5(__then) {


            if (utils.stringIsNullOrEmpty(params.osDiskType)) {
              return __this.cli.interaction.prompt($("Enter OS disk type: "), __cb(_, __frame, 15, 55, function ___(__0, __1) { params.osDiskType = __1; __then(); }, true)); } else { __then(); } ; })(function __$_parseOSDiskParams__5() {


            osType = utils.verifyParamExistsInCollection(["Windows","Linux",], __this.params.osDiskType, "osDiskType"); return (function __$_parseOSDiskParams__5(__then) {
              if (utils.stringIsNullOrEmpty(params.osDiskVhd)) {
                return __this.cli.interaction.prompt($("Enter OS disk VHD: "), __cb(_, __frame, 20, 54, function ___(__0, __2) { params.osDiskVhd = __2; __then(); }, true)); } else { __then(); } ; })(__then); }); } else { __then(); } ; })(function __$_parseOSDiskParams__5() {



        osDiskInfo = {
          profile: {
            createOption: createOption,
            osType: osType,
            name: null,
            caching: null,
            vhd: {
              uri: null },

            encryptionSettings: null },

          isVhdParamAUrl: false };



        if ((__this.params.diskEncryptionKeyVaultId || __this.params.diskEncryptionKeySecretUrl)) {
          osDiskInfo.profile.encryptionSettings = {
            diskEncryptionKey: {
              sourceVault: {
                id: __this.params.diskEncryptionKeyVaultId },

              secretUrl: __this.params.diskEncryptionKeySecretUrl },

            keyEncryptionKey: null }; } ;



        if ((__this.params.keyEncryptionKeyVaultId || __this.params.keyEncryptionKeyUrl)) {
          if ((osDiskInfo.profile.encryptionSettings === null)) {
            osDiskInfo.profile.encryptionSettings = {
              diskEncryptionKey: null,
              keyEncryptionKey: null }; } ;



          osDiskInfo.profile.encryptionSettings.keyEncryptionKey = {
            sourceVault: {
              id: __this.params.keyEncryptionKeyVaultId },

            keyUrl: __this.params.keyEncryptionKeyUrl }; } ;



        return __this._generateOSDiskName(__cb(_, __frame, 67, 42, function ___(__0, __3) { osDiskInfo.profile.name = __3;
          supportedDiskCaching = ["None","ReadOnly","ReadWrite",];
          if (!utils.stringIsNullOrEmpty(params.osDiskCaching)) {
            osDiskInfo.profile.caching = utils.verifyParamExistsInCollection(supportedDiskCaching, params.osDiskCaching, "osDiskCaching"); }
           else {
            osDiskInfo.profile.caching = supportedDiskCaching[2]; } ;


          if ((!attachMode && utils.stringIsNullOrEmpty(params.osDiskVhd))) {
            params.osDiskVhd = (osDiskInfo.profile.name + ".vhd"); } ;



          if (params.osDiskVhd.match(/^((http|https):\/\/)/)) {
            osDiskInfo.parsedOsDiskVhd = __this._parseBlobUrl(params.osDiskVhd, "osDiskVhd");
            osDiskInfo.isVhdParamAUrl = true; } ;



          osDiskInfo.profile.vhd.uri = params.osDiskVhd;
          return _(null, osDiskInfo); }, true)); }); }); },


  _parseDataDiskParams: function(params) {
    var useDataDisk = utils.atLeastOneParameIsSet([params.dataDiskSize,params.dataDiskCaching,params.dataDiskVhd,]);
    if (!useDataDisk) {
      return null; } ;


    var dataDiskInfo = {
      profile: {
        name: null,
        diskSizeGB: null,
        caching: null,
        createOption: null,
        vhd: {
          uri: null } },


      isVhdParamAUrl: false };


    if (params.newDataDisk) {
      dataDiskInfo.profile.createOption = "empty";
      var sizeAsInt = utils.parseInt(params.dataDiskSize);
      if ((isNaN(sizeAsInt) || (sizeAsInt === 0))) {
        throw new Error($("dataDiskSize is required when any one of the dataDisk configuration parameter is specified and must be an integer")); } ;

      dataDiskInfo.profile.diskSizeGB = sizeAsInt; }
     else {
      dataDiskInfo.profile.createOption = "attach"; } ;


    var supportedDiskCaching = ["None","ReadOnly","ReadWrite",];
    if (!utils.stringIsNullOrEmpty(params.dataDiskCaching)) {
      dataDiskInfo.profile.caching = utils.verifyParamExistsInCollection(supportedDiskCaching, params.dataDiskCaching, "dataDiskCaching"); }
     else {
      dataDiskInfo.profile.caching = supportedDiskCaching[2]; } ;


    if (utils.stringIsNullOrEmpty(params.dataDiskVhd)) {
      dataDiskInfo.profile.name = this._generateDataDiskName(params.vmName);
      dataDiskInfo.profile.vhd.uri = (dataDiskInfo.profile.name + ".vhd"); }
     else {


      if (params.dataDiskVhd.match(/^((http|https):\/\/)/)) {
        var vhdUrl = this._parseBlobUrl(params.dataDiskVhd, "dataDiskVhd");
        dataDiskInfo.profile.name = vhdUrl.blobName.substr(0, (vhdUrl.blobName.length - 4));
        dataDiskInfo.profile.vhd.uri = params.dataDiskVhd;
        dataDiskInfo.isVhdParamAUrl = true; }
       else {
        if (utils.stringEndsWith(params.dataDiskVhd, ".vhd", true)) {
          dataDiskInfo.profile.name = params.dataDiskVhd.substr(0, (params.dataDiskVhd.length - 4)); }
         else {
          dataDiskInfo.profile.name = params.dataDiskVhd;
          params.dataDiskVhd = (params.dataDiskVhd + ".vhd"); } ;

        dataDiskInfo.profile.vhd.uri = params.dataDiskVhd; } ; } ;



    return dataDiskInfo; },


  _parseStorageCreateParams: function _parseStorageCreateParams__6(params, _) { var createRequestProfile, __this = this; var __frame = { name: "_parseStorageCreateParams__6", line: 386 }; return __func(_, this, arguments, _parseStorageCreateParams__6, 1, __frame, function __$_parseStorageCreateParams__6() { return (function __$_parseStorageCreateParams__6(__then) {
        if (!utils.allParamsAreSet([params.storageAccountName,params.location,])) {
          return __this.cli.interaction.prompt($("Enter storage account name: "), __cb(_, __frame, 2, 61, function ___(__0, __1) { params.storageAccountName = __1;
            return __this.cli.interaction.prompt($("Enter location: "), __cb(_, __frame, 3, 51, function ___(__0, __2) { params.location = __2; __then(); }, true)); }, true)); } else { __then(); } ; })(function __$_parseStorageCreateParams__6() {


        createRequestProfile = {
          sku: { name: ((params.vmSize && utils.stringStartsWith(params.vmSize.toLowerCase(), "standard_ds")) ? "Premium_LRS" : "Standard_LRS") },
          kind: "Storage",
          name: params.storageAccountName,
          location: params.location }; return (function __$_parseStorageCreateParams__6(__then) {


          if ((createRequestProfile.accountType === "Premium_LRS")) { return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$_parseStorageCreateParams__6() {

                  return __this.storageManagementClient.storageAccounts.create(__this.resourceGroupName, params.storageAccountName, createRequestProfile, __cb(_, __frame, 15, 59, __then, true)); }); })(function ___(err, __result) { __catch(function __$_parseStorageCreateParams__6() { if (err) {


                    if (((err.code === "AccountTypeNotSupportedInLocation") || (err.code === "StorageAccountAlreadyExists"))) {
                      __this.cli.output.warn(util.format($("%s : %s [%s]"), err.code, err.message, defaultType));
                      createRequestProfile.accountType = "Standard_LRS"; }
                     else {
                      return _(err); } ; __then(); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, __then); }); } else { __then(); } ; })(function __$_parseStorageCreateParams__6() {




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


  _createStorageAccountIfRequired: function _createStorageAccountIfRequired__7(_) { var storageInfo, existingStorageAccount, defaultStorageAccount, __this = this; var __frame = { name: "_createStorageAccountIfRequired__7", line: 416 }; return __func(_, this, arguments, _createStorageAccountIfRequired__7, 0, __frame, function __$_createStorageAccountIfRequired__7() {
      storageInfo = {
        storageAccountName: null,
        createdNew: false,
        profile: null,
        createRequestProfile: null }; return (function __$_createStorageAccountIfRequired__7(__then) {


        if (utils.stringIsNullOrEmpty(__this.params.location)) {
          return __this.cli.interaction.prompt($("Enter location: "), __cb(_, __frame, 9, 56, function ___(__0, __1) { __this.params.location = __1; __then(); }, true)); } else { __then(); } ; })(function __$_createStorageAccountIfRequired__7() { return (function __$_createStorageAccountIfRequired__7(__then) {


          if (!utils.stringIsNullOrEmpty(__this.params.storageAccountName)) {
            storageInfo.storageAccountName = __this.params.storageAccountName;
            return __this._findStorageAccount(storageInfo.storageAccountName, __cb(_, __frame, 14, 46, function ___(__0, __2) { existingStorageAccount = __2; return (function __$_createStorageAccountIfRequired__7(__then) {
                if (existingStorageAccount) {
                  storageInfo.profile = existingStorageAccount;
                  if (!utils.ignoreCaseAndSpaceEquals(storageInfo.profile.location, __this.params.location)) {
                    return _(new Error(util.format($("A storage account with name \"%s\" already exists in another region \"%s\""), __this.params.storageAccountName, storageInfo.profile.location))); } ; __then(); } else {


                  __this.cli.output.info(util.format($("Could not find the storage account \"%s\", trying to create new one"), __this.params.storageAccountName));
                  return __this._createNewStorageAccount(storageInfo.storageAccountName, __this.params, __cb(_, __frame, 22, 55, function ___(__0, __3) { storageInfo.createRequestProfile = __3;
                    return __this._findStorageAccount(storageInfo.storageAccountName, __cb(_, __frame, 23, 41, function ___(__0, __4) { storageInfo.profile = __4;
                      storageInfo.createdNew = true; __then(); }, true)); }, true)); } ; })(__then); }, true)); } else {


            return __this._getFirstStorageAccount(__this.resourceGroupName, __this.params.location, __cb(_, __frame, 27, 45, function ___(__0, __5) { defaultStorageAccount = __5; return (function __$_createStorageAccountIfRequired__7(__then) {
                if (defaultStorageAccount) {
                  storageInfo.profile = defaultStorageAccount.storageAccount;
                  storageInfo.storageAccountName = storageInfo.profile.name;
                  __this.cli.output.info(util.format($("Using the storage account \"%s\" in \"%s\""), storageInfo.storageAccountName, __this.params.location)); __then(); } else {

                  __this.cli.output.info(util.format($("Could not find any storage accounts in the region \"%s\", trying to create new one"), __this.params.location));
                  return __this._generateNewStorageAccountName(__cb(_, __frame, 34, 52, function ___(__0, __6) { storageInfo.storageAccountName = __6;
                    __this.params.storageAccountName = storageInfo.storageAccountName;
                    return __this._createNewStorageAccount(storageInfo.storageAccountName, __this.params, __cb(_, __frame, 36, 55, function ___(__0, __7) { storageInfo.createRequestProfile = __7;

                      return __this._findStorageAccount(storageInfo.storageAccountName, __cb(_, __frame, 38, 41, function ___(__0, __8) { storageInfo.profile = __8;
                        storageInfo.createdNew = true; __then(); }, true)); }, true)); }, true)); } ; })(__then); }, true)); } ; })(function __$_createStorageAccountIfRequired__7() {



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


  _isStorageAccountNameAvailable: function _isStorageAccountNameAvailable__8(params, _) { var progress, result, __this = this; var __frame = { name: "_isStorageAccountNameAvailable__8", line: 462 }; return __func(_, this, arguments, _isStorageAccountNameAvailable__8, 1, __frame, function __$_isStorageAccountNameAvailable__8() {
      progress = __this.cli.interaction.progress(util.format($("Checking storage account name \"%s\" is available"), params.storageAccountName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$_isStorageAccountNameAvailable__8() {

            return __this.storageManagementClient.storageAccounts.checkNameAvailability(params.storageAccountName, __cb(_, __frame, 3, 70, function ___(__0, __1) { result = __1;

              return _(null, result.nameAvailable); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$_isStorageAccountNameAvailable__8() {

              progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$_isStorageAccountNameAvailable__8() { _(); }); }); }); },



  _createNewStorageAccount: function _createNewStorageAccount__9(resourceGroupName, params, _) { var createRequestProfile, progress, __this = this; var __frame = { name: "_createNewStorageAccount__9", line: 473 }; return __func(_, this, arguments, _createNewStorageAccount__9, 2, __frame, function __$_createNewStorageAccount__9() {
      return __this._parseStorageCreateParams(params, __cb(_, __frame, 1, 42, function ___(__0, __1) { createRequestProfile = __1;
        progress = __this.cli.interaction.progress(util.format($("Creating storage account \"%s\" in \"%s\""), params.storageAccountName, params.location)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$_createNewStorageAccount__9() {

              return __this.storageManagementClient.storageAccounts.create(__this.resourceGroupName, params.storageAccountName, createRequestProfile, __cb(_, __frame, 4, 57, function __$_createNewStorageAccount__9() {
                return _(null, createRequestProfile); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$_createNewStorageAccount__9() {

                progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$_createNewStorageAccount__9() { _(); }); }); }, true)); }); },



  _getFirstStorageAccount: function _getFirstStorageAccount__10(resourceGroup, location, _) { var progress, listStorageAccountResult, storageAccounts, i, storageResourceGroup, __this = this; var __frame = { name: "_getFirstStorageAccount__10", line: 484 }; return __func(_, this, arguments, _getFirstStorageAccount__10, 2, __frame, function __$_getFirstStorageAccount__10() {
      progress = __this.cli.interaction.progress($("Retrieving storage accounts")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$_getFirstStorageAccount__10() {

            return __this.storageManagementClient.storageAccounts.list(__cb(_, __frame, 3, 88, function ___(__0, __1) { listStorageAccountResult = __1;
              storageAccounts = (((listStorageAccountResult && (listStorageAccountResult.storageAccounts instanceof Array))) ? listStorageAccountResult.storageAccounts : []);


              for (i = 0; (i < storageAccounts.length); i++) {
                storageResourceGroup = utils.parseResourceReferenceUri(storageAccounts[i].id).resourceGroupName;
                if ((utils.ignoreCaseAndSpaceEquals(storageAccounts[i].location, location) && utils.ignoreCaseAndSpaceEquals(storageResourceGroup, resourceGroup))) {

                  return _(null, {
                    storageAccount: storageAccounts[i] }); } ; };




              return _(null, null); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$_getFirstStorageAccount__10() {

              progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$_getFirstStorageAccount__10() { _(); }); }); }); },



  _findStorageAccount: function _findStorageAccount__11(storageAccountName, _) { var progress, storageAccount, __this = this; var __frame = { name: "_findStorageAccount__11", line: 507 }; return __func(_, this, arguments, _findStorageAccount__11, 1, __frame, function __$_findStorageAccount__11() {
      progress = __this.cli.interaction.progress(util.format($("Looking up the storage account %s"), storageAccountName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$_findStorageAccount__11() { return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$_findStorageAccount__11() {

                  return __this.storageManagementClient.storageAccounts.getProperties(__this.resourceGroupName, storageAccountName, __cb(_, __frame, 3, 80, function ___(__0, __1) { storageAccount = __1;
                    return _(null, storageAccount); }, true)); }); })(function ___(e, __result) { __catch(function __$_findStorageAccount__11() { if (e) {

                    if (((e.code === "ResourceNotFound") || (e.code === "StorageAccountNotFound"))) {
                      return _(null, null); } ;

                    return _(e); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, function __$_findStorageAccount__11() { _(null, null, true); }); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$_findStorageAccount__11() {

              progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$_findStorageAccount__11() { _(); }); }); }); },



  _getPrefix: function _getPrefix__12(_) { var __this = this; var __frame = { name: "_getPrefix__12", line: 522 }; return __func(_, this, arguments, _getPrefix__12, 0, __frame, function __$_getPrefix__12() { return (function __$_getPrefix__12(__then) {
        if (utils.stringIsNullOrEmpty(__this.prefix)) {
          return require("crypto").randomBytes(8, __cb(_, __frame, 2, 53, function ___(__0, __1) { __this.prefix = ("cli" + (__1.toString("hex"))); __then(); }, true)); } else { __then(); } ; })(function __$_getPrefix__12() {


        return _(null, __this.prefix); }); }); },


  _generateNewStorageAccountName: function _generateNewStorageAccountName__13(_) { var __this = this; var __frame = { name: "_generateNewStorageAccountName__13", line: 530 }; return __func(_, this, arguments, _generateNewStorageAccountName__13, 0, __frame, function __$_generateNewStorageAccountName__13() {
      return __this._getPrefix(__cb(_, __frame, 1, 44, function ___(__0, __2) { var __1 = __this._normalizeString((__2 + (new Date()).getTime().toString())); return _(null, __1); }, true)); }); },


  _generateNewStorageAccountNameWithPrefix: function _generateNewStorageAccountNameWithPrefix__14(prefix, _) { var __this = this; var __frame = { name: "_generateNewStorageAccountNameWithPrefix__14", line: 534 }; return __func(_, this, arguments, _generateNewStorageAccountNameWithPrefix__14, 1, __frame, function __$_generateNewStorageAccountNameWithPrefix__14() { return (function __$_generateNewStorageAccountNameWithPrefix__14(__then) {
        if (utils.stringIsNullOrEmpty(prefix)) {
          return require("crypto").randomBytes(8, __cb(_, __frame, 2, 48, function ___(__0, __1) { prefix = ("cli" + (__1.toString("hex"))); __then(); }, true)); } else { __then(); } ; })(function __$_generateNewStorageAccountNameWithPrefix__14() {


        return _(null, __this._normalizeString((prefix + (new Date()).getTime().toString()))); }); }); },


  _generateOSDiskName: function _generateOSDiskName__15(_) { var __this = this; var __frame = { name: "_generateOSDiskName__15", line: 542 }; return __func(_, this, arguments, _generateOSDiskName__15, 0, __frame, function __$_generateOSDiskName__15() {
      return __this._getPrefix(__cb(_, __frame, 1, 44, function ___(__0, __2) { var __1 = ((__this._normalizeString(__2) + "-os-") + (new Date()).getTime().toString()); return _(null, __1); }, true)); }); },




  _generateDataDiskName: function(vmName) {
    var currentDateTime = this._getCurrentDateTime();
    return ((((vmName + "-") + currentDateTime.date) + "-") + currentDateTime.time); },



  _getCurrentDateTime: function() {
    var newDate = new Date();
    var currentDate = ((((((newDate.getFullYear() + "") + ((((((newDate.getMonth() + 1)) < 10)) ? "0" : ""))) + "") + ((newDate.getMonth() + 1))) + ((((newDate.getDate() < 10)) ? "0" : ""))) + newDate.getDate());
    var currentTime = (((((((((((((newDate.getHours() < 10)) ? "0" : "")) + newDate.getHours()) + "") + ((((newDate.getMinutes() < 10)) ? "0" : ""))) + newDate.getMinutes()) + "") + ((((newDate.getSeconds() < 10)) ? "0" : ""))) + newDate.getSeconds()) + "") + newDate.getMilliseconds());
    return {
      date: currentDate,
      time: currentTime }; },



  _normalizeString: function(str) {
    return str.replace(/[^a-zA-Z0-9]+/g, "").slice(0, 24).toLowerCase(); },


  _parseBlobUrl: function(blobUrl, paramName) {
    var result = { };

    if (utils.stringIsNullOrEmpty(blobUrl)) {
      throw new Exception(util.format($("%s cannot be null"), paramName)); } ;


    var protocolSplit = blobUrl.split("://");
    if (((protocolSplit.length != 2) || (((protocolSplit[0] !== "http") && (protocolSplit[0] !== "https"))))) {
      throw new Error(util.format($("Invalid %s, url must contain protocol part and it must be either http or https"), paramName)); } ;


    result.protocol = protocolSplit[0];

    var urlWithoutProtocol = protocolSplit.slice(-1)[0];
    var urlPartsSplit = urlWithoutProtocol.split("/");

    if ((urlPartsSplit.length < 3)) {
      throw new Error(util.format($("Invalid %s, the url must contain container name and blob name"), paramName)); } ;


    var hostSplit = urlPartsSplit[0].split(".");

    if ((/^([a-z0-9]){3,24}$/.test(hostSplit[0]) === false)) {
      throw new Error(util.format($("Invalid %s, the storage account name %s is invalid"), paramName, hostSplit[0])); } ;


    result.storageAccountName = hostSplit[0];


    if ((/^[a-z0-9](?!.*([-])\1)[a-z0-9\-]+[a-z0-9]$/.test(urlPartsSplit[1]) === false)) {
      throw new Error(util.format($("Invalid %s, the container name %s is invalid"), paramName, urlPartsSplit[1])); } ;


    result.containerName = urlPartsSplit[1];

    for (var i = 2; (i < urlPartsSplit.length); i++) {
      var p = urlPartsSplit[i].replace(/\s+/g, "");
      if ((p === "")) {
        throw new Error(util.format($("Invalid %s, the blob name part is invalid"), paramName)); } ; };



    result.blobName = urlPartsSplit.slice(2).join("/");

    return result; }});




module.exports = VMStorageProfile;
