/*** 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 constants = require("./constants");
var $ = utils.getLocaleString;
var resourceUtils = require("../resource/resourceUtils");
var tagUtils = require("../tag/tagUtils");

function VirtualNetwork(cli, networkManagementClient) {
  this.networkManagementClient = networkManagementClient;
  this.output = cli.output;
  this.interaction = cli.interaction;};


__.extend(VirtualNetwork.prototype, {



  create: function create__1(resourceGroupName, vnetName, options, _) { var self, parameters, vnet, progress, __this = this; var __frame = { name: "create__1", line: 35 }; return __func(_, this, arguments, create__1, 3, __frame, function __$create__1() { self = __this;


      parameters = {
        location: options.location,
        addressSpace: {
          addressPrefixes: [] },

        dhcpOptions: {
          dnsServers: [] } };



      parameters = self._parseVNet(parameters, options, true);

      return self.get(resourceGroupName, vnetName, __cb(_, __frame, 15, 20, function ___(__0, __1) { vnet = __1;
        if (vnet) {
          return _(new Error(util.format($("Virtual network \"%s\" already exists in resource group \"%s\""), vnetName, resourceGroupName))); } ;


        progress = self.interaction.progress(util.format($("Creating virtual network \"%s\""), vnetName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$create__1() {

              return self.networkManagementClient.virtualNetworks.createOrUpdate(resourceGroupName, vnetName, parameters, __cb(_, __frame, 22, 58, function ___(__0, __2) { vnet = __2; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$create__1() {

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

            self._showVNet(vnet, resourceGroupName, vnetName); _(); }); }); }, true)); }); },


  set: function set__2(resourceGroupName, vnetName, options, _) { var self, vnet, progress, __this = this; var __frame = { name: "set__2", line: 64 }; return __func(_, this, arguments, set__2, 3, __frame, function __$set__2() { self = __this;


      return self.get(resourceGroupName, vnetName, __cb(_, __frame, 3, 20, function ___(__0, __1) { vnet = __1;
        if (!vnet) {
          return _(new Error(util.format($("Virtual network \"%s\" not found in resource group \"%s\""), vnetName, resourceGroupName))); } ;


        vnet = self._parseVNet(vnet, options, false);

        progress = self.interaction.progress(util.format($("Updating virtual network \"%s\""), vnetName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$set__2() {

              return self.networkManagementClient.virtualNetworks.createOrUpdate(resourceGroupName, vnetName, vnet, __cb(_, __frame, 12, 58, function ___(__0, __2) { vnet = __2; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$set__2() {

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

            self._showVNet(vnet, resourceGroupName, vnetName); _(); }); }); }, true)); }); },


  list: function list__3(options, _) { var self, progress, vnets, __this = this; var __frame = { name: "list__3", line: 83 }; return __func(_, this, arguments, list__3, 1, __frame, function __$list__3() { self = __this;

      progress = self.interaction.progress("Looking up virtual networks");

      vnets = null; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$list__3() { return (function __$list__3(__then) {

              if (options.resourceGroup) {
                return self.networkManagementClient.virtualNetworks.list(options.resourceGroup, __cb(_, __frame, 7, 61, function ___(__0, __1) { vnets = __1; __then(); }, true)); } else {

                return self.networkManagementClient.virtualNetworks.listAll(__cb(_, __frame, 9, 61, function ___(__0, __2) { vnets = __2; __then(); }, true)); } ; })(function __$list__3() { _(null, null, true); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$list__3() {


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


          self.interaction.formatOutput(vnets, function(vnets) {
            if ((vnets.length === 0)) {
              self.output.warn($("No virtual networks found")); }
             else {
              self.output.table(vnets, function(row, vnet) {
                row.cell($("Name"), vnet.name);
                row.cell($("Location"), vnet.location);
                var resInfo = resourceUtils.getResourceInformation(vnet.id);
                row.cell($("Resource group"), resInfo.resourceGroup);
                row.cell($("Provisioning state"), vnet.provisioningState);
                row.cell($("Address prefixes"), vnet.addressSpace.addressPrefixes);
                var dnsServers = "";
                if (vnet.dhcpOptions) {
                  dnsServers = vnet.dhcpOptions.dnsServers; } ;

                row.cell($("DNS servers"), dnsServers);
                row.cell($("Subnets number"), (vnet.subnets.length || "")); }); } ; }); _(); }); }); }); },





  show: function show__4(resourceGroupName, vnetName, options, _) { var self, vnet, __this = this; var __frame = { name: "show__4", line: 120 }; return __func(_, this, arguments, show__4, 3, __frame, function __$show__4() { self = __this;

      return self.get(resourceGroupName, vnetName, __cb(_, __frame, 2, 20, function ___(__0, __1) { vnet = __1;

        self._showVNet(vnet, resourceGroupName, vnetName); _(); }, true)); }); },


  delete: function delete__5(resourceGroupName, vnetName, options, _) { var self, vnet, progress, __this = this; var __frame = { name: "delete__5", line: 127 }; return __func(_, this, arguments, delete__5, 3, __frame, function __$delete__5() { self = __this;


      return self.get(resourceGroupName, vnetName, __cb(_, __frame, 3, 20, function ___(__0, __2) { vnet = __2;
        if (!vnet) {
          return _(new Error(util.format($("Virtual network \"%s\" not found in resource group \"%s\""), vnetName, resourceGroupName))); } ; return (function __$delete__5(_) {


          var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return self.interaction.confirm(util.format($("Delete virtual network %s? [y/n] "), vnetName), __cb(_, __frame, 8, 44, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -126, 17, function ___(__0, __3) { return (function __$delete__5(__then) { if (__3) { return _(null); } else { __then(); } ; })(function __$delete__5() {



            progress = self.interaction.progress(util.format($("Deleting virtual network \"%s\""), vnetName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$delete__5() {

                  return self.networkManagementClient.virtualNetworks.deleteMethod(resourceGroupName, vnetName, __cb(_, __frame, 14, 51, function __$delete__5() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$delete__5() {

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



  get: function get__6(resourceGroupName, vnetName, _) { var self, progress, vnet, __this = this; var __frame = { name: "get__6", line: 147 }; return __func(_, this, arguments, get__6, 2, __frame, function __$get__6() { self = __this;

      progress = self.interaction.progress(util.format($("Looking up the virtual network \"%s\""), vnetName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$get__6() { return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$get__6() {

                  return self.networkManagementClient.virtualNetworks.get(resourceGroupName, vnetName, null, __cb(_, __frame, 4, 62, function ___(__0, __1) { vnet = __1;
                    return _(null, vnet); }, true)); }); })(function ___(e, __result) { __catch(function __$get__6() { if (e) {

                    if ((e.statusCode === 404)) {
                      return _(null, null); } ;

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

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






  _parseVNet: function(vnet, options, useDefaults) {
    var self = this;

    if (options.addressPrefixes) {
      var addressPrefixes = options.addressPrefixes.split(",");
      addressPrefixes.forEach(function(address) {
        if ((vnet.addressSpace.addressPrefixes.indexOf(address) === -1)) {
          validation.isCIDR(address, "--address-prefixes");
          vnet.addressSpace.addressPrefixes.push(address); } ; }); }


     else if (useDefaults) {
      var defAddressPrefix = constants.vnet.defAddressPrefix;
      self.output.warn(util.format($("Using default address prefix: %s"), defAddressPrefix));
      vnet.addressSpace.addressPrefixes.push(defAddressPrefix); }  ;


    if (options.dnsServers) {
      if (utils.argHasValue(options.dnsServers)) {
        var dnsServers = options.dnsServers.split(",");
        dnsServers.forEach(function(dns) {
          if ((vnet.dhcpOptions.dnsServers.indexOf(dns) === -1)) {
            validation.isIP(dns, "--dns-servers");
            vnet.dhcpOptions.dnsServers.push(dns); } ; }); }


       else {
        vnet.dhcpOptions.dnsServers = []; } ; } ;



    if (options.tags) {
      if (utils.argHasValue(options.tags)) {
        tagUtils.appendTags(vnet, options); }
       else {
        vnet.tags = { }; } ; } ;



    return vnet; },


  _showVNet: function(vnet, resourceGroupName, vnetName) {
    var self = this;

    self.interaction.formatOutput(vnet, function(vnet) {
      if ((vnet === null)) {
        self.output.warn(util.format($("Virtual network \"%s\" not found in resource group \"%s\" "), vnetName, resourceGroupName));
        return; } ;


      self.output.nameValue($("Id"), vnet.id);
      self.output.nameValue($("Name"), vnet.name);
      self.output.nameValue($("Type"), vnet.type);
      self.output.nameValue($("Location"), vnet.location);
      self.output.nameValue($("Provisioning state"), vnet.provisioningState);
      self.output.nameValue($("Tags"), tagUtils.getTagsInfo(vnet.tags));
      if ((vnet.addressSpace.addressPrefixes.length > 0)) {
        self.output.header($("Address prefixes"));
        self.output.list(vnet.addressSpace.addressPrefixes, 2); } ;

      if ((vnet.dhcpOptions.dnsServers.length > 0)) {
        self.output.header($("DNS servers"));
        self.output.list(vnet.dhcpOptions.dnsServers, 2); } ;

      if ((vnet.subnets.length > 0)) {
        self.output.header($("Subnets"));
        vnet.subnets.forEach(function(subnet) {
          self.output.nameValue($("Id"), subnet.id, 2);
          self.output.nameValue($("Name"), subnet.name, 2);
          self.output.nameValue($("Provisioning state"), subnet.provisioningState, 2);
          self.output.nameValue($("Address prefix"), subnet.addressPrefix, 2);
          if (subnet.networkSecurityGroup) {
            self.output.nameValue($("Network Security Group id"), subnet.networkSecurityGroup.id, 2); } ;

          if (subnet.routeTable) {
            self.output.nameValue($("Route Table id"), subnet.routeTable.id, 2); } ;

          self.output.data($(""), ""); }); } ; }); }});






module.exports = VirtualNetwork;
