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

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


__.extend(VpnGateway.prototype, {

  create: function create__1(networkName, options, _) { var self, parameters, progress, __this = this; var __frame = { name: "create__1", line: 30 }; return __func(_, this, arguments, create__1, 2, __frame, function __$create__1() { self = __this;

      parameters = self._parseGateway(options);
      progress = self.interaction.progress(util.format($("Creating VPN Gateway for the virtual network \"%s\""), networkName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$create__1() {

            return self.networkManagementClient.gateways.create(networkName, parameters, __cb(_, __frame, 5, 44, function __$create__1() { _(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() {

          return self.show(networkName, options, __cb(_, __frame, 9, 9, function __$create__1() { _(); }, true)); }); }); }); },


  show: function show__2(networkName, options, _) { var self, gateway, __this = this; var __frame = { name: "show__2", line: 42 }; return __func(_, this, arguments, show__2, 2, __frame, function __$show__2() { self = __this;

      return self.get(networkName, __cb(_, __frame, 2, 23, function ___(__0, __1) { gateway = __1;

        if (!gateway) {
          if (self.output.format().json) {
            self.output.json({ }); }
           else {
            self.output.warn(util.format($("VPN Gateway not found for the virtual network \"%s\""), networkName)); } ; return _(null); } ;




        self.interaction.formatOutput(gateway, function(gateway) {
          self.output.nameValue($("VIP Address"), gateway.vipAddress);
          self.output.nameValue($("Gateway type"), gateway.gatewayType);
          self.output.nameValue($("Gateway size"), gateway.gatewaySKU);
          self.output.nameValue($("State"), gateway.state);
          if (gateway.defaultSite) {
            self.output.nameValue($("Local network default site name"), gateway.defaultSite.name); } ;

          if (gateway.lastEvent) {
            self.output.header($("Last event"));
            self.output.nameValue($("Timestamp"), gateway.lastEvent.timestamp, 2);
            self.output.nameValue($("ID"), gateway.lastEvent.id, 2);
            self.output.nameValue($("Message"), gateway.lastEvent.message, 2);
            self.output.nameValue($("Data"), gateway.lastEvent.data, 2); } ; }); _(); }, true)); }); },




  delete: function delete__3(networkName, options, _) { var self, gateway, progress, __this = this; var __frame = { name: "delete__3", line: 73 }; return __func(_, this, arguments, delete__3, 2, __frame, function __$delete__3() { self = __this;

      return self.get(networkName, __cb(_, __frame, 2, 23, function ___(__0, __2) { gateway = __2;
        if (!gateway) {
          return _(new Error(utils.format($("Gateway not found for the virtual network \"%s\""), networkName))); } ; return (function __$delete__3(_) {


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



            progress = self.interaction.progress(util.format($("Deleting VPN Gateway for the virtual network \"%s\""), networkName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$delete__3() {

                  return self.networkManagementClient.gateways.deleteMethod(networkName, __cb(_, __frame, 13, 44, function __$delete__3() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$delete__3() {

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



  resize: function resize__4(networkName, sku, options, _) { var self, gateway, parameters, progress, __this = this; var __frame = { name: "resize__4", line: 92 }; return __func(_, this, arguments, resize__4, 3, __frame, function __$resize__4() { self = __this;

      return self.get(networkName, __cb(_, __frame, 2, 23, function ___(__0, __1) { gateway = __1;
        if (!gateway) {
          return _(new Error(utils.format($("VPN Gateway not found for the virtual network \"%s\""), networkName))); } ;


        options.sku = sku;
        parameters = self._parseGateway(options);
        progress = self.interaction.progress(util.format($("Resizing VPN Gateway for the virtual network \"%s\""), networkName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$resize__4() {

              return self.networkManagementClient.gateways.resize(networkName, parameters, __cb(_, __frame, 11, 44, function __$resize__4() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$resize__4() {

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



  reset: function reset__5(networkName, options, _) { var self, gateway, parameters, progress, __this = this; var __frame = { name: "reset__5", line: 109 }; return __func(_, this, arguments, reset__5, 2, __frame, function __$reset__5() { self = __this;

      return self.get(networkName, __cb(_, __frame, 2, 23, function ___(__0, __1) { gateway = __1;
        if (!gateway) {
          return _(new Error(utils.format($("VPN Gateway not found for the virtual network \"%s\""), networkName))); } ;


        parameters = {
          gatewaySKU: "Default" };


        progress = self.interaction.progress(util.format($("Resetting VPN Gateway for the virtual network \"%s\""), networkName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$reset__5() {

              return self.networkManagementClient.gateways.reset(networkName, parameters, __cb(_, __frame, 13, 44, function __$reset__5() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$reset__5() {

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



  setDefaultSite: function setDefaultSite__6(networkName, siteName, options, _) { var self, gateway, parameters, progress, __this = this; var __frame = { name: "setDefaultSite__6", line: 128 }; return __func(_, this, arguments, setDefaultSite__6, 3, __frame, function __$setDefaultSite__6() { self = __this;

      return self.get(networkName, __cb(_, __frame, 2, 23, function ___(__0, __1) { gateway = __1;
        if (!gateway) {
          return _(new Error(utils.format($("VPN Gateway not found for the virtual network \"%s\""), networkName))); } ;


        parameters = {
          defaultSite: siteName };


        progress = self.interaction.progress(util.format($("Setting local network default site for the virtual network \"%s\""), networkName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$setDefaultSite__6() {

              return self.networkManagementClient.gateways.setDefaultSites(networkName, parameters, __cb(_, __frame, 13, 44, function __$setDefaultSite__6() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$setDefaultSite__6() {

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



  removeDefaultSite: function removeDefaultSite__7(networkName, options, _) { var self, gateway, progress, __this = this; var __frame = { name: "removeDefaultSite__7", line: 147 }; return __func(_, this, arguments, removeDefaultSite__7, 2, __frame, function __$removeDefaultSite__7() { self = __this;

      return self.get(networkName, __cb(_, __frame, 2, 23, function ___(__0, __1) { gateway = __1;
        if (!gateway) {
          return _(new Error(utils.format($("VPN Gateway not found for the virtual network \"%s\""), networkName))); } ;


        progress = self.interaction.progress(util.format($("Removing local network default site configured in a virtual network \"%s\""), networkName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$removeDefaultSite__7() {

              return self.networkManagementClient.gateways.removeDefaultSites(networkName, __cb(_, __frame, 9, 44, function __$removeDefaultSite__7() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$removeDefaultSite__7() {

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



  listConnections: function listConnections__8(networkName, options, _) { var self, progress, connectionList, __this = this; var __frame = { name: "listConnections__8", line: 162 }; return __func(_, this, arguments, listConnections__8, 2, __frame, function __$listConnections__8() { self = __this;

      progress = self.interaction.progress($("Getting the network connections"));
      connectionList = null; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$listConnections__8() {

            return self.networkManagementClient.gateways.listConnections(networkName, __cb(_, __frame, 5, 61, function ___(__0, __1) { connectionList = __1; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$listConnections__8() {

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


          self.interaction.formatOutput(connectionList.connections, function(data) {
            if ((data.length === 0)) {
              self.output.warn($("No VPN Gateway connections found")); }
             else {
              var indent = 0;
              self.output.header("Connections", indent, true);

              indent += 2;
              var counter = 0;
              data.forEach(function(item) {
                self.output.header(util.format($("Connection %s", counter)), indent);
                indent += 2;
                self.output.nameValue("Local network site name", item.localNetworkSiteName, indent);
                self.output.nameValue("State", item.connectivityState, indent);
                self.output.nameValue("Bytes of data transferred in", item.ingressBytesTransferred, indent);
                self.output.nameValue("Bytes of data transferred out", item.egressBytesTransferred, indent);
                self.output.nameValue("Last connection established", item.lastConnectionEstablished, indent);

                if (item.allocatedIpAddresses) {
                  self.output.list("VPN Client IP Addresses", item.allocatedIpAddresses, indent); } ;


                if (item.lastEvent) {
                  self.output.nameValue("Last event ID", item.lastEvent.id, indent);
                  self.output.nameValue("Last event message", item.lastEvent.message, indent);
                  self.output.nameValue("Last event timestamp", item.lastEvent.timestamp, indent); } ;

                indent -= 2;
                counter++; }); } ; }); _(); }); }); }); },





  get: function get__9(networkName, _) { var self, progress, gateway, __this = this; var __frame = { name: "get__9", line: 206 }; return __func(_, this, arguments, get__9, 1, __frame, function __$get__9() { self = __this;

      progress = self.interaction.progress(util.format($("Looking up network VPN Gateway in virtual network \"%s\""), networkName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$get__9() { return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$get__9() {

                  return self.networkManagementClient.gateways.get(networkName, __cb(_, __frame, 4, 58, function ___(__0, __1) { gateway = __1;
                    return _(null, gateway); }, true)); }); })(function ___(e, __result) { __catch(function __$get__9() { if (e) {

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

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

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



  setSharedKey: function setSharedKey__10(vnetName, keyValue, options, _) { var self, progress, __this = this; var __frame = { name: "setSharedKey__10", line: 222 }; return __func(_, this, arguments, setSharedKey__10, 3, __frame, function __$setSharedKey__10() { self = __this;

      progress = self.interaction.progress(util.format($("Setting up shared key for vnet \"%s\""), vnetName));
      return self._validateDefaultSite(vnetName, options, __cb(_, __frame, 3, 9, function __$setSharedKey__10() {

        options.value = keyValue; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$setSharedKey__10() {

              return self.networkManagementClient.gateways.setSharedKey(vnetName, options.siteName, options, __cb(_, __frame, 7, 44, function __$setSharedKey__10() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$setSharedKey__10() {

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



  resetSharedKey: function resetSharedKey__11(vnetName, keyLength, options, _) { var self, progress, __this = this; var __frame = { name: "resetSharedKey__11", line: 235 }; return __func(_, this, arguments, resetSharedKey__11, 3, __frame, function __$resetSharedKey__11() { self = __this;

      options.keyLength = keyLength;

      return self._validateDefaultSite(vnetName, options, __cb(_, __frame, 4, 9, function __$resetSharedKey__11() {

        progress = self.interaction.progress(util.format($("Resetting shared key for vnet \"%s\""), vnetName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$resetSharedKey__11() {

              return self.networkManagementClient.gateways.resetSharedKey(vnetName, options.siteName, options, __cb(_, __frame, 8, 44, function __$resetSharedKey__11() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$resetSharedKey__11() {

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



  listDevices: function listDevices__12(options, _) { var self, progress, devices, devicesArray, __this = this; var __frame = { name: "listDevices__12", line: 249 }; return __func(_, this, arguments, listDevices__12, 1, __frame, function __$listDevices__12() { self = __this;

      progress = self.interaction.progress($("Listing VPN devices"));
      devices = null; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$listDevices__12() {

            return self.networkManagementClient.gateways.listSupportedDevices(__cb(_, __frame, 5, 54, function ___(__0, __1) { devices = __1; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$listDevices__12() {

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



          devicesArray = [];
          devices.vendors.forEach(function(item) {
            item.platforms.forEach(function(platform) {
              platform.oSFamilies.forEach(function(os) {
                devicesArray.push({ name: item.name, platform: platform.name, os: os.name }); }); }); });




          self.interaction.formatOutput(devicesArray, function(data) {
            self.output.table(data, function(row, device) {
              row.cell($("Vendor"), device.name);
              row.cell($("Platform"), device.platform);
              row.cell($("OS Family"), device.os); }); }); _(); }); }); }); },




  getDeviceScript: function getDeviceScript__13(vnetName, options, _) { var self, progress, script, __this = this; var __frame = { name: "getDeviceScript__13", line: 278 }; return __func(_, this, arguments, getDeviceScript__13, 2, __frame, function __$getDeviceScript__13() { self = __this;

      options.oSFamily = options.osFamily;

      progress = self.interaction.progress($("Getting script for VPN device"));
      script = null; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$getDeviceScript__13() {

            return self.networkManagementClient.gateways.getDeviceConfigurationScript(vnetName, options, __cb(_, __frame, 7, 53, function ___(__0, __1) { script = __1; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$getDeviceScript__13() {

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


          self.output.log(script); _(); }); }); }); },


  startDiagnosticsSession: function startDiagnosticsSession__14(vnetName, options, _) { var self, progress, session, __this = this; var __frame = { name: "startDiagnosticsSession__14", line: 293 }; return __func(_, this, arguments, startDiagnosticsSession__14, 2, __frame, function __$startDiagnosticsSession__14() { self = __this;

      options.captureDurationInSeconds = options.duration;
      options.customerStorageKey = options.storageAccountKey;
      options.customerStorageName = options.storageAccountName;

      progress = self.interaction.progress(util.format($("Starting diagnostics session in a virtual network \"%s\""), vnetName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$startDiagnosticsSession__14() {


            return self.networkManagementClient.gateways.startDiagnostics(vnetName, options, __cb(_, __frame, 9, 54, function ___(__0, __1) { session = __1; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$startDiagnosticsSession__14() {

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



  stopDiagnosticsSession: function stopDiagnosticsSession__15(vnetName, options, _) { var self, progress, session, __this = this; var __frame = { name: "stopDiagnosticsSession__15", line: 308 }; return __func(_, this, arguments, stopDiagnosticsSession__15, 2, __frame, function __$stopDiagnosticsSession__15() { self = __this;

      progress = self.interaction.progress(util.format($("Stopping diagnostics session in a virtual network \"%s\""), vnetName));
      options.operation = "StopDiagnostics"; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$stopDiagnosticsSession__15() {


            return self.networkManagementClient.gateways.stopDiagnostics(vnetName, options, __cb(_, __frame, 6, 54, function ___(__0, __1) { session = __1; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$stopDiagnosticsSession__15() {

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



  getDiagnosticsSession: function getDiagnosticsSession__16(vnetName, options, _) { var self, progress, session, __this = this; var __frame = { name: "getDiagnosticsSession__16", line: 320 }; return __func(_, this, arguments, getDiagnosticsSession__16, 2, __frame, function __$getDiagnosticsSession__16() { self = __this;

      progress = self.interaction.progress(util.format($("Getting diagnostics session in a virtual network \"%s\""), vnetName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$getDiagnosticsSession__16() {


            return self.networkManagementClient.gateways.getDiagnostics(vnetName, __cb(_, __frame, 5, 54, function ___(__0, __1) { session = __1; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$getDiagnosticsSession__16() {

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


          if (!session) {
            if (self.output.format().json) {
              self.output.json({ }); }
             else {
              self.output.warn(util.format($("VPN Gateway not found for the virtual network \"%s\""), networkName)); } ; return _(null); } ;




          self.interaction.formatOutput(session, function(session) {
            self.output.nameValue("Diagnostics URL", session.diagnosticsUrl); }); _(); }); }); }); },



  _validateDefaultSite: function _validateDefaultSite__17(vnetName, options, _) { var self, gateway, __this = this; var __frame = { name: "_validateDefaultSite__17", line: 344 }; return __func(_, this, arguments, _validateDefaultSite__17, 2, __frame, function __$_validateDefaultSite__17() { self = __this; return (function __$_validateDefaultSite__17(__then) {

        if (!options.siteName) {
          return self.get(vnetName, __cb(_, __frame, 3, 25, function ___(__0, __1) { gateway = __1;
            if (!gateway) {
              return _(new Error(util.format($("VPN Gateway not found for virtual network \"%s\""), vnetName))); }
             else {
              if (!gateway.defaultSite.name) {
                return _(new Error(util.format($("Default local network site is not set for virtual network \"%s\""), vnetName))); } ;


              options.siteName = gateway.defaultSite.name; } ; __then(); }, true)); } else { __then(); } ; })(_); }); },




  _parseGateway: function(options) {
    var gateway = { };

    if (options.type) {
      gateway.gatewayType = utils.verifyParamExistsInCollection(constants.vpnGateway.type, options.type, "type"); } ;


    if (options.sku) {
      gateway.gatewaySKU = utils.verifyParamExistsInCollection(constants.vpnGateway.sku, options.sku, "sku"); } ;


    return gateway; }});



module.exports = VpnGateway;
