/*** 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 tagUtils = require("../tag/tagUtils");

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

var $ = utils.getLocaleString;

function AvailabilitySet(cli, serviceClients, resourceGroupName, params) {
  this.cli = cli;
  this.serviceClients = serviceClients;
  this.computeManagementClient = serviceClients.computeManagementClient;
  this.resourceGroupName = resourceGroupName;
  this.params = params;};


__.extend(AvailabilitySet.prototype, {
  _parseAvailSetCreateParams: function(params) {
    if (!utils.allParamsAreSet([params.availsetName,params.location,])) {
      throw new Error($("To create new availability set the parameters availsetName and location are required")); } ;


    var tags = { };
    if (params.availsetTags) {
      tags = tagUtils.buildTagsParameter(tags, { tags: params.availsetTags }); } ;


    var createRequestProfile = {
      name: params.availsetName,
      location: params.location,
      tags: tags,
      platformUpdateDomainCount: (params.platformUpdateDomainCount ? parseInt(params.platformUpdateDomainCount) : null),
      platformFaultDomainCount: (params.platformFaultDomainCount ? parseInt(params.platformFaultDomainCount) : null) };


    return createRequestProfile; },


  createAvailSetIfRequired: function createAvailSetIfRequired__1(_) { var availsetInfo, availSet, connectedVMRefs, expectedVMId, i, __this = this; var __frame = { name: "createAvailSetIfRequired__1", line: 39 }; return __func(_, this, arguments, createAvailSetIfRequired__1, 0, __frame, function __$createAvailSetIfRequired__1() {
      if (utils.stringIsNullOrEmpty(__this.params.availsetName)) {
        return _(new Error($("The parameters availsetName is required"))); } ;


      if (utils.stringIsNullOrEmpty(__this.params.location)) {
        return _(new Error($("The parameter location is required"))); } ;


      availsetInfo = {
        availsetName: __this.params.availsetName,
        createdNew: false,
        createRequestProfile: { },
        profile: { } };


      return __this.getAvailSet(__this.resourceGroupName, __this.params.availsetName, __cb(_, __frame, 16, 26, function ___(__0, __1) { availSet = __1;
        if (availSet) {
          if (!utils.ignoreCaseAndSpaceEquals(availSet.location, __this.params.location)) {
            return _(new Error(util.format($("An Availability set with name \"%s\" already exists in another region \"%s\""), __this.params.availsetName, availSet.location))); } ;


          __this.cli.output.info(util.format($("Found an Availability set \"%s\""), __this.params.availsetName));
          connectedVMRefs = availSet.virtualMachinesReferences;
          if ((connectedVMRefs instanceof Array)) {
            expectedVMId = ((("/resourceGroups/" + __this.resourceGroupName) + "/providers/Microsoft.Compute/virtualMachines/") + __this.params.vmName);
            for (i = 0; (i < connectedVMRefs.length); i++) {
              if (utils.stringEndsWith(connectedVMRefs[i].id, expectedVMId, true)) {
                return _(new Error(util.format($("A VM with name \"%s\" (reference \"%s\") is already in the availability set \"%s\""), __this.params.vmName, connectedVMRefs[i].id, __this.params.availsetName))); } ; }; } ;




          availsetInfo.profile = availSet;
          return _(null, availsetInfo); } ;


        __this.cli.output.info(util.format($("Availability set with given name not found \"%s\", creating a new one"), __this.params.availsetName));
        return __this.createNewAvailSet(__this.resourceGroupName, __this.params, __cb(_, __frame, 38, 47, function ___(__0, __2) { availsetInfo.createRequestProfile = __2;
          availsetInfo.createdNew = true;
          return __this.getAvailSet(__this.resourceGroupName, __this.params.availsetName, __cb(_, __frame, 40, 22, function ___(__0, __3) { availSet = __3;
            availsetInfo.profile = availSet;
            return _(null, availsetInfo); }, true)); }, true)); }, true)); }); },


  getAvailSetByIdExpanded: function getAvailSetByIdExpanded__2(referenceUri, depth, memoize, dependencies, _) { var resourceInfo, expandedAvailSet, __this = this; var __frame = { name: "getAvailSetByIdExpanded__2", line: 84 }; return __func(_, this, arguments, getAvailSetByIdExpanded__2, 4, __frame, function __$getAvailSetByIdExpanded__2() {
      referenceUri = referenceUri.toLowerCase();
      if (memoize[referenceUri]) {
        return _(null, memoize[referenceUri]); } ;


      resourceInfo = utils.parseResourceReferenceUri(referenceUri);
      return __this.getAvailSetByNameExpanded(resourceInfo.resourceGroupName, resourceInfo.resourceName, depth, memoize, dependencies, __cb(_, __frame, 7, 34, function ___(__0, __1) { expandedAvailSet = __1;
        return _(null, expandedAvailSet); }, true)); }); },


  getAvailSetByNameExpanded: function getAvailSetByNameExpanded__3(resourceGroupName, availsetName, depth, memoize, dependencies, _) { var availSet, expandedAvailSet, __this = this; var __frame = { name: "getAvailSetByNameExpanded__3", line: 95 }; return __func(_, this, arguments, getAvailSetByNameExpanded__3, 5, __frame, function __$getAvailSetByNameExpanded__3() {
      return __this.getAvailSet(resourceGroupName, availsetName, __cb(_, __frame, 1, 26, function ___(__0, __1) { availSet = __1;
        return __this._expandAvailSet(availSet, depth, memoize, dependencies, __cb(_, __frame, 2, 34, function ___(__0, __2) { expandedAvailSet = __2;
          return _(null, expandedAvailSet); }, true)); }, true)); }); },


  getAvailSet: function getAvailSet__4(resourceGroupName, availsetName, _) { var progress, availSet, __this = this; var __frame = { name: "getAvailSet__4", line: 101 }; return __func(_, this, arguments, getAvailSet__4, 2, __frame, function __$getAvailSet__4() {
      progress = __this.cli.interaction.progress(util.format($("Looking up the availability set \"%s\""), availsetName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$getAvailSet__4() { return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$getAvailSet__4() {

                  return __this.computeManagementClient.availabilitySets.get(resourceGroupName, availsetName, __cb(_, __frame, 3, 69, function ___(__0, __1) { availSet = __1;
                    return _(null, availSet); }, true)); }); })(function ___(e, __result) { __catch(function __$getAvailSet__4() { if (e) {

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

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

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



  getAvailList: function getAvailList__5(resourceGroupName, _) { var availSets, progress, __this = this; var __frame = { name: "getAvailList__5", line: 116 }; return __func(_, this, arguments, getAvailList__5, 1, __frame, function __$getAvailList__5() {

      progress = __this.cli.interaction.progress($("Getting availiability sets")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$getAvailList__5() {

            return __this.computeManagementClient.availabilitySets.list(resourceGroupName, __cb(_, __frame, 4, 66, function ___(__0, __1) { availSets = __1; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$getAvailList__5() {

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


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


  deleteAvailSet: function deleteAvailSet__6(resourceGroupName, availsetName, _) { var progress, __this = this; var __frame = { name: "deleteAvailSet__6", line: 128 }; return __func(_, this, arguments, deleteAvailSet__6, 2, __frame, function __$deleteAvailSet__6() {
      progress = __this.cli.interaction.progress(util.format($("Deleting the availability set \"%s\""), availsetName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$deleteAvailSet__6() {

            return __this.computeManagementClient.availabilitySets.deleteMethod(resourceGroupName, availsetName, __cb(_, __frame, 3, 54, function __$deleteAvailSet__6() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$deleteAvailSet__6() {

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



  hasAnyAvailSetParameters: function(params) {
    var allAvailSetParams = [params.availsetName,];
    return utils.atLeastOneParameIsSet(allAvailSetParams); },


  createNewAvailSet: function createNewAvailSet__7(resourceGroupName, params, _) { var createRequestProfile, progress, __this = this; var __frame = { name: "createNewAvailSet__7", line: 142 }; return __func(_, this, arguments, createNewAvailSet__7, 2, __frame, function __$createNewAvailSet__7() {
      createRequestProfile = __this._parseAvailSetCreateParams(params);
      progress = __this.cli.interaction.progress(util.format($("Creating availability set \"%s\""), params.availsetName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$createNewAvailSet__7() {

            return __this.computeManagementClient.availabilitySets.createOrUpdate(resourceGroupName, createRequestProfile.name, createRequestProfile, __cb(_, __frame, 4, 54, function __$createNewAvailSet__7() {
              return _(null, createRequestProfile); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$createNewAvailSet__7() {

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



  _expandAvailSet: function _expandAvailSet__8(availSet, depth, memoize, dependencies, _) { var availabilitySet, referenceUri, i, vmReference, vmReferenceId; var __frame = { name: "_expandAvailSet__8", line: 153 }; return __func(_, this, arguments, _expandAvailSet__8, 4, __frame, function __$_expandAvailSet__8() {
      if (((depth === 0) || (availSet === null))) {
        return _(null, availSet); } ;


      if ((depth !== -1)) {
        depth--; } ;


      availabilitySet = availSet;
      referenceUri = availabilitySet.id.toLowerCase();
      memoize[referenceUri] = availSet; return (function __$_expandAvailSet__8(__then) {

        if ((availabilitySet.virtualMachinesReferences instanceof Array)) {
          i = 0; var __3 = false; return (function ___(__break) { var __more; var __loop = __cb(_, __frame, 0, 0, function __$_expandAvailSet__8() { __more = false; if (__3) { i++; } else { __3 = true; } ; var __2 = (i < availabilitySet.virtualMachinesReferences.length); if (__2) {
                vmReference = availabilitySet.virtualMachinesReferences[i];
                vmReferenceId = vmReference.id.toLowerCase(); return (function __$_expandAvailSet__8(__then) {
                  if (!memoize[vmReferenceId]) {

                    return dependencies.virtualMachine.getVMByIdExpanded(vmReferenceId, depth, memoize, dependencies, __cb(_, __frame, 19, 63, function ___(__0, __1) { vmReference.expanded = __1; __then(); }, true)); } else { __then(); } ; })(function __$_expandAvailSet__8() { while (__more) { __loop(); }; __more = true; }); } else { __break(); } ; }); do { __loop(); } while (__more); __more = true; })(__then); } else { __then(); } ; })(function __$_expandAvailSet__8() {




        return _(null, memoize[referenceUri]); }); }); }});



module.exports = AvailabilitySet;
