/*** 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 $ = utils.getLocaleString;

function NetworkVNetSubnet(cli, networkResourceProviderClient, resourceGroupName, params) {
  this.cli = cli;
  this.networkResourceProviderClient = networkResourceProviderClient;
  this.resourceGroupName = resourceGroupName;
  this.params = params;};



__.extend(NetworkVNetSubnet.prototype, {
  _parseSubnetCreateParams: function _parseSubnetCreateParams__1(params, _) { var createRequestProfile, __this = this; var __frame = { name: "_parseSubnetCreateParams__1", line: 32 }; return __func(_, this, arguments, _parseSubnetCreateParams__1, 1, __frame, function __$_parseSubnetCreateParams__1() { return (function __$_parseSubnetCreateParams__1(__then) {
        if (utils.stringIsNullOrEmpty(params.vnetName)) {
          return __this.cli.interaction.prompt($("Enter vnet name: "), __cb(_, __frame, 2, 47, function ___(__0, __1) { params.vnetName = __1; __then(); }, true)); } else { __then(); } ; })(function __$_parseSubnetCreateParams__1() { return (function __$_parseSubnetCreateParams__1(__then) {


          if (utils.stringIsNullOrEmpty(params.vnetSubnetName)) {
            return __this.cli.interaction.prompt($("Enter vnet subnet name: "), __cb(_, __frame, 6, 53, function ___(__0, __2) { params.vnetSubnetName = __2; __then(); }, true)); } else { __then(); } ; })(function __$_parseSubnetCreateParams__1() { return (function __$_parseSubnetCreateParams__1(__then) {


            if (utils.stringIsNullOrEmpty(params.vnetSubnetAddressprefix)) {
              return __this.cli.interaction.prompt($("Enter vnet subnet address prefix: "), __cb(_, __frame, 10, 62, function ___(__0, __3) { params.vnetSubnetAddressprefix = __3; __then(); }, true)); } else { __then(); } ; })(function __$_parseSubnetCreateParams__1() {


            createRequestProfile = {
              addressPrefix: params.vnetSubnetAddressprefix,
              dhcpOptions: {
                dnsServers: [] },

              ipConfigurations: [],
              name: params.vnetSubnetName };


            if (!utils.stringIsNullOrEmpty(params.vnetSubnetDnsserver)) {
              createRequestProfile.dhcpOptions.dnsServers.push(params.vnetSubnetDnsserver); } ;


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


  getVNetSubnetProfile: function getVNetSubnetProfile__2(params, _) { var __this = this; var __frame = { name: "getVNetSubnetProfile__2", line: 61 }; return __func(_, this, arguments, getVNetSubnetProfile__2, 1, __frame, function __$getVNetSubnetProfile__2() {
      return __this._parseSubnetCreateParams(params, __cb(_, __frame, 1, 18, _, true)); }); },


  createSubnetIfRequired: function createSubnetIfRequired__3(_) { var subnetInfo, createRequestProfile, __this = this; var __frame = { name: "createSubnetIfRequired__3", line: 65 }; return __func(_, this, arguments, createSubnetIfRequired__3, 0, __frame, function __$createSubnetIfRequired__3() { return (function __$createSubnetIfRequired__3(__then) {
        if (utils.stringIsNullOrEmpty(__this.params.vnetName)) {
          return __this.cli.interaction.prompt($("Enter vnet name: "), __cb(_, __frame, 2, 52, function ___(__0, __1) { __this.params.vnetName = __1; __then(); }, true)); } else { __then(); } ; })(function __$createSubnetIfRequired__3() { return (function __$createSubnetIfRequired__3(__then) {


          if (utils.stringIsNullOrEmpty(__this.params.vnetSubnetName)) {
            return __this.cli.interaction.prompt($("Enter vnet subnet name: "), __cb(_, __frame, 6, 58, function ___(__0, __2) { __this.params.vnetSubnetName = __2; __then(); }, true)); } else { __then(); } ; })(function __$createSubnetIfRequired__3() {


          return __this.getSubnetInfoByName(__this.resourceGroupName, __this.params.vnetName, __this.params.vnetSubnetName, __cb(_, __frame, 9, 28, function ___(__0, __3) { subnetInfo = __3; return (function __$createSubnetIfRequired__3(__then) {
              if (subnetInfo.profile) {
                __this.cli.output.info(util.format($("Subnet with given name \"%s\" exists under the virtual network \"%s\", using this subnet"), subnetInfo.subnetName, subnetInfo.vnetName)); __then(); } else {

                __this.cli.output.info(util.format($("Subnet with given name not found \"%s\" under the virtual network \"%s\", creating a new one"), subnetInfo.subnetName, subnetInfo.vnetName));
                return __this._createNewSubnet(subnetInfo.resourceGroupName, __this.params, __cb(_, __frame, 14, 40, function ___(__0, __4) { createRequestProfile = __4;

                  return __this.getSubnetInfoByName(subnetInfo.resourceGroupName, subnetInfo.vnetName, subnetInfo.subnetName, __cb(_, __frame, 16, 26, function ___(__0, __5) { subnetInfo = __5;
                    subnetInfo.createdNew = true;
                    subnetInfo.createRequestProfile = createRequestProfile; __then(); }, true)); }, true)); } ; })(function __$createSubnetIfRequired__3() {


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


  getSubnetInfoById: function getSubnetInfoById__4(referenceUri, _) { var resourceInfo, parentVnetName, __this = this; var __frame = { name: "getSubnetInfoById__4", line: 89 }; return __func(_, this, arguments, getSubnetInfoById__4, 1, __frame, function __$getSubnetInfoById__4() {
      resourceInfo = utils.parseResourceReferenceUri(referenceUri);
      parentVnetName = resourceInfo.parentResource.split("/")[1];
      return __this.getSubnetInfoByName(resourceInfo.resourceGroupName, parentVnetName, resourceInfo.resourceName, __cb(_, __frame, 3, 18, _, true)); }); },


  getSubnetInfoByName: function getSubnetInfoByName__5(resourceGroupName, vnetName, subnetName, _) { var subnetInfo, subnet, __this = this; var __frame = { name: "getSubnetInfoByName__5", line: 95 }; return __func(_, this, arguments, getSubnetInfoByName__5, 3, __frame, function __$getSubnetInfoByName__5() {
      subnetInfo = {
        vnetName: vnetName,
        subnetName: subnetName,
        resourceGroupName: resourceGroupName,
        createdNew: false,
        createRequestProfile: { },
        profile: null };


      return __this._getSubnet(resourceGroupName, vnetName, subnetName, __cb(_, __frame, 10, 24, function ___(__0, __1) { subnet = __1;
        if (subnet) {
          subnetInfo.profile = subnet; } ;


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


  getSubnetByIdExpanded: function getSubnetByIdExpanded__6(referenceUri, depth, memoize, dependencies, _) { var resourceInfo, parentVnetName, expandedSubnet, __this = this; var __frame = { name: "getSubnetByIdExpanded__6", line: 113 }; return __func(_, this, arguments, getSubnetByIdExpanded__6, 4, __frame, function __$getSubnetByIdExpanded__6() {
      referenceUri = referenceUri.toLowerCase();
      if (memoize[referenceUri]) {
        return _(null, memoize[referenceUri]); } ;


      resourceInfo = utils.parseResourceReferenceUri(referenceUri);
      parentVnetName = resourceInfo.parentResource.split("/")[1];
      return __this.getSubnetByNameExpanded(resourceInfo.resourceGroupName, parentVnetName, resourceInfo.resourceName, depth, memoize, dependencies, __cb(_, __frame, 8, 32, function ___(__0, __1) { expandedSubnet = __1;
        return _(null, expandedSubnet); }, true)); }); },


  getSubnetByNameExpanded: function getSubnetByNameExpanded__7(resourceGroupName, vnetName, subnetName, depth, memoize, dependencies, _) { var subnet, expandedSubnet, __this = this; var __frame = { name: "getSubnetByNameExpanded__7", line: 125 }; return __func(_, this, arguments, getSubnetByNameExpanded__7, 6, __frame, function __$getSubnetByNameExpanded__7() {
      return __this._getSubnet(resourceGroupName, vnetName, subnetName, __cb(_, __frame, 1, 24, function ___(__0, __1) { subnet = __1;
        expandedSubnet = __this._expandSubnet(subnet, depth, memoize);
        return _(null, expandedSubnet); }, true)); }); },


  _expandSubnet: function(subnet, depth, memoize) {
    if (((depth === 0) || (subnet === null))) {
      return subnet; } ;


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


    var snet = subnet;
    var referenceUri = snet.id.toLowerCase();

    memoize[referenceUri] = subnet;

    return memoize[referenceUri]; },


  _getSubnet: function _getSubnet__8(resourceGroupName, vnetName, subnetName, _) { var progress, subnet, __this = this; var __frame = { name: "_getSubnet__8", line: 148 }; return __func(_, this, arguments, _getSubnet__8, 3, __frame, function __$_getSubnet__8() {
      progress = __this.cli.interaction.progress(util.format($("Looking up the subnet \"%s\" under the virtual network \"%s\""), subnetName, vnetName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$_getSubnet__8() { return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$_getSubnet__8() {

                  return __this.networkResourceProviderClient.subnets.get(resourceGroupName, vnetName, subnetName, __cb(_, __frame, 3, 64, function ___(__0, __1) { subnet = __1;
                    return _(null, subnet); }, true)); }); })(function ___(e, __result) { __catch(function __$_getSubnet__8() { if (e) {



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

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

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



  _createNewSubnet: function _createNewSubnet__9(resourceGroupName, params, _) { var createRequestProfile, progress, __this = this; var __frame = { name: "_createNewSubnet__9", line: 165 }; return __func(_, this, arguments, _createNewSubnet__9, 2, __frame, function __$_createNewSubnet__9() {
      return __this._parseSubnetCreateParams(params, __cb(_, __frame, 1, 38, function ___(__0, __1) { createRequestProfile = __1;
        progress = __this.cli.interaction.progress(util.format($("Creating subnet \"%s\" [Address prefix \"%s\"] under the virtual network \"%s\""), params.vnetSubnetName, params.vnetSubnetAddressprefix, params.vnetName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$_createNewSubnet__9() {

              return __this.networkResourceProviderClient.subnets.createOrUpdate(resourceGroupName, params.vnetName, params.vnetSubnetName, createRequestProfile, __cb(_, __frame, 4, 51, function __$_createNewSubnet__9() {
                return _(null, createRequestProfile); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$_createNewSubnet__9() {

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



  hasAnySubnetParameters: function(params) {
    var allSubnetParams = [params.vnetSubnetName,params.vnetSubnetAddressprefix,params.vnetSubnetDnsserver,];





    return utils.atLeastOneParameIsSet(allSubnetParams); }});




module.exports = NetworkVNetSubnet;
