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

var $ = utils.getLocaleString;

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



__.extend(NetworkVNet.prototype, {
  _parseVNetCreateParams: function _parseVNetCreateParams__1(params, parseSubnetParams, _) { var createRequestProfile, subnetProfile, __this = this; var __frame = { name: "_parseVNetCreateParams__1", line: 33 }; return __func(_, this, arguments, _parseVNetCreateParams__1, 2, __frame, function __$_parseVNetCreateParams__1() { return (function __$_parseVNetCreateParams__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 __$_parseVNetCreateParams__1() { return (function __$_parseVNetCreateParams__1(__then) {


          if (utils.stringIsNullOrEmpty(params.vnetAddressPrefix)) {
            return __this.cli.interaction.prompt($("Enter vnet address prefix: "), __cb(_, __frame, 6, 56, function ___(__0, __2) { params.vnetAddressPrefix = __2; __then(); }, true)); } else { __then(); } ; })(function __$_parseVNetCreateParams__1() { return (function __$_parseVNetCreateParams__1(__then) {


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


            createRequestProfile = {
              addressSpace: {
                addressPrefixes: [params.vnetAddressPrefix,] },

              dhcpOptions: {
                dnsServers: [] },

              ipConfigurations: [],
              subnets: [],
              location: params.location,
              name: params.vnetName };


            return parseSubnetParams(params, __cb(_, __frame, 26, 26, function ___(__0, __4) { subnetProfile = __4;
              createRequestProfile.subnets.push(subnetProfile);
              return _(null, createRequestProfile); }, true)); }); }); }); }); },


  createOrUpdateVNetIfRequired: function createOrUpdateVNetIfRequired__2(_) { var networkVNetSubnet, vnetInfo, createRequestProfile, __this = this; var __frame = { name: "createOrUpdateVNetIfRequired__2", line: 64 }; return __func(_, this, arguments, createOrUpdateVNetIfRequired__2, 0, __frame, function __$createOrUpdateVNetIfRequired__2() { return (function __$createOrUpdateVNetIfRequired__2(__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 __$createOrUpdateVNetIfRequired__2() {


        networkVNetSubnet = new NetworkVNetSubnet(__this.cli, __this.networkResourceProviderClient, __this.resourceGroupName, __this.params);
        return __this.getVNetInfoByName(__this.resourceGroupName, __this.params.vnetName, __cb(_, __frame, 6, 26, function ___(__0, __2) { vnetInfo = __2; return (function __$createOrUpdateVNetIfRequired__2(__then) {
            if (vnetInfo.profile) {
              if (!utils.ignoreCaseAndSpaceEquals(vnetInfo.profile.location, __this.params.location)) {
                return _(new Error(util.format($("Found a virtual network with name \"%s\" but it exists in different region \"%s\""), vnetInfo.vnetName, vnetInfo.profile.location))); } ;


              __this.cli.output.info((util.format($("Found an existing virtual network \"%s\""), vnetInfo.vnetName)));
              __this._printSubnets(vnetInfo.profile.subnets);
              __this.cli.output.info($("Verifying subnet"));
              return networkVNetSubnet.createSubnetIfRequired(__cb(_, __frame, 15, 48, function ___(__0, __3) { vnetInfo.subnetInfo = __3; __then(); }, true)); } else {


              return __this._createNewVNet(vnetInfo.resourceGroupName, __this.params, __cb(_, __frame, 18, 40, function ___(__0, __4) { createRequestProfile = __4;

                return __this.getVNetInfoByName(vnetInfo.resourceGroupName, vnetInfo.vnetName, __cb(_, __frame, 20, 24, function ___(__0, __5) { vnetInfo = __5;
                  vnetInfo.createdNew = true;
                  vnetInfo.createRequestProfile = createRequestProfile;

                  return networkVNetSubnet.getSubnetInfoByName(vnetInfo.resourceGroupName, vnetInfo.vnetName, __this.params.vnetSubnetName, __cb(_, __frame, 24, 48, function ___(__0, __6) { vnetInfo.subnetInfo = __6;
                    vnetInfo.subnetInfo.createdNew = true; __then(); }, true)); }, true)); }, true)); } ; })(function __$createOrUpdateVNetIfRequired__2() {


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


  getVNetInfoById: function getVNetInfoById__3(referenceUri, _) { var resourceInfo, __this = this; var __frame = { name: "getVNetInfoById__3", line: 95 }; return __func(_, this, arguments, getVNetInfoById__3, 1, __frame, function __$getVNetInfoById__3() {
      resourceInfo = utils.parseResourceReferenceUri(referenceUri);
      return __this.getVNetInfoByName(resourceInfo.resourceGroupName, resourceInfo.resourceName, __cb(_, __frame, 2, 18, _, true)); }); },


  getVNetInfoByName: function getVNetInfoByName__4(resourceGroupName, vnetName, _) { var vnetInfo, vnet, __this = this; var __frame = { name: "getVNetInfoByName__4", line: 100 }; return __func(_, this, arguments, getVNetInfoByName__4, 2, __frame, function __$getVNetInfoByName__4() {
      vnetInfo = {
        vnetName: vnetName,
        resourceGroupName: resourceGroupName,
        createdNew: false,
        profile: null,
        createRequestProfile: { },
        subnetInfo: { } };


      return __this._getVNet(resourceGroupName, vnetName, __cb(_, __frame, 10, 22, function ___(__0, __1) { vnet = __1;
        if (vnet) {
          vnetInfo.profile = vnet; } ;


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


  _getVNet: function _getVNet__5(resourceGroupName, vnetName, _) { var progress, vnet, __this = this; var __frame = { name: "_getVNet__5", line: 118 }; return __func(_, this, arguments, _getVNet__5, 2, __frame, function __$_getVNet__5() {
      progress = __this.cli.interaction.progress(util.format($("Looking up the virtual network \"%s\""), vnetName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$_getVNet__5() { return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$_getVNet__5() {

                  return __this.networkResourceProviderClient.virtualNetworks.get(resourceGroupName, vnetName, __cb(_, __frame, 3, 70, function ___(__0, __1) { vnet = __1;
                    return _(null, vnet); }, true)); }); })(function ___(e, __result) { __catch(function __$_getVNet__5() { if (e) {

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

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

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



  _createNewVNet: function _createNewVNet__6(resourceGroupName, params, _) { var networkVNetSubnet, getVNetSubnetProfile, createRequestProfile, progress, __this = this; var __frame = { name: "_createNewVNet__6", line: 133 }; return __func(_, this, arguments, _createNewVNet__6, 2, __frame, function __$_createNewVNet__6() {
      networkVNetSubnet = new NetworkVNetSubnet(__this.cli, __this.networkResourceProviderClient, resourceGroupName, params);
      __this.cli.output.info($("Preparing to create new virtual network and subnet"));
      getVNetSubnetProfile = __.bind(networkVNetSubnet.getVNetSubnetProfile, networkVNetSubnet);
      return __this._parseVNetCreateParams(__this.params, getVNetSubnetProfile, __cb(_, __frame, 4, 38, function ___(__0, __1) { createRequestProfile = __1;
        progress = __this.cli.interaction.progress(util.format($("Creating a new virtual network \"%s\" [address prefix: \"%s\"] with subnet \"%s\" [address prefix: \"%s\"]"), params.vnetName, params.vnetAddressPrefix, params.vnetSubnetName, params.vnetSubnetAddressprefix)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$_createNewVNet__6() {

              return __this.networkResourceProviderClient.virtualNetworks.createOrUpdate(__this.resourceGroupName, params.vnetName, createRequestProfile, __cb(_, __frame, 7, 59, function __$_createNewVNet__6() {
                return _(null, createRequestProfile); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$_createNewVNet__6() {

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



  _printSubnets: function(subnets) {
    var info = this.cli.output.info;
    if ((subnets instanceof Array)) {
      info("Existing Subnets:");
      subnets.forEach(function(subnet) {
        info(((("  " + subnet.name) + ":") + subnet.addressPrefix)); }); } ; },




  hasAnyVNetParameters: function(params) {
    var allVNetParams = [params.vnetName,params.vnetDnsserver,params.vnetAddressPrefix,params.vnetSubnetName,params.vnetSubnetAddressprefix,params.vnetSubnetDnsserver,];








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





module.exports = NetworkVNet;
