/*** 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 fs = require("fs");
var util = require("util");
var utils = require("../../../util/utils");
var validation = require("../../../util/validation");
var $ = utils.getLocaleString;
var constants = require("./constants");
var tagUtils = require("../tag/tagUtils");
var resourceUtils = require("../resource/resourceUtils");
var PublicIp = require("./publicIp");
var Subnet = require("./subnet");
var LocalNetworkGateway = require("./localNetworkGateway");

function VirtualNetworkGateway(cli, networkManagementClient) {
  this.networkManagementClient = networkManagementClient;
  this.publicIpCrud = new PublicIp(cli, networkManagementClient);
  this.subnetCrud = new Subnet(cli, networkManagementClient);
  this.lnetGatewayCrud = new LocalNetworkGateway(cli, networkManagementClient);
  this.output = cli.output;
  this.interaction = cli.interaction;};


__.extend(VirtualNetworkGateway.prototype, {



  create: function create__1(resourceGroupName, gatewayName, options, _) { var self, defSubnetName, parameters, gateway, progress, __this = this; var __frame = { name: "create__1", line: 42 }; return __func(_, this, arguments, create__1, 3, __frame, function __$create__1() { self = __this; return (function __$create__1(__then) {


        if ((!options.publicIpId && !options.publicIpName)) {
          return self.interaction.prompt($("Public IP name: "), __cb(_, __frame, 4, 46, function ___(__0, __1) { options.publicIpName = __1; __then(); }, true)); } else { __then(); } ; })(function __$create__1() { return (function __$create__1(__then) {


          if ((!options.subnetId && !options.vnetName)) {
            return self.interaction.prompt($("Virtual network name: "), __cb(_, __frame, 8, 42, function ___(__0, __2) { options.vnetName = __2; __then(); }, true)); } else { __then(); } ; })(function __$create__1() {


          if ((options.vnetName && !options.subnetName)) {
            defSubnetName = constants.vpnGateway.subnetName;
            self.output.warn(util.format($("Using default subnet name: %s"), defSubnetName));
            options.subnetName = defSubnetName; } ;


          parameters = {
            gatewayType: constants.vpnGateway.gatewayType[0],
            vpnType: constants.vpnGateway.vpnType[0],
            sku: {
              name: constants.vpnGateway.sku[0],
              tier: constants.vpnGateway.sku[0] },

            enableBgp: false,
            location: options.location,
            vpnClientConfiguration: {
              vpnClientAddressPool: {
                addressPrefixes: [] },

              vpnClientRootCertificates: [],
              vpnClientRevokedCertificates: [] },

            ipConfigurations: [{

              name: "ip-config" },] };




          return self._parseGateway(resourceGroupName, parameters, options, __cb(_, __frame, 40, 22, function ___(__0, __3) { parameters = __3;

            return self.get(resourceGroupName, gatewayName, __cb(_, __frame, 42, 23, function ___(__0, __4) { gateway = __4;
              if (gateway) {
                return _(new Error(util.format($("A virtual network gateway with name \"%s\" already exists in the resource group \"%s\""), gatewayName, resourceGroupName))); } ;


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

                    return self.networkManagementClient.virtualNetworkGateways.createOrUpdate(resourceGroupName, gatewayName, parameters, __cb(_, __frame, 49, 68, function ___(__0, __5) { gateway = __5; _(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._showGateway(gateway, resourceGroupName, gatewayName); _(); }); }); }, true)); }, true)); }); }); }); },


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


      return self.get(resourceGroupName, gatewayName, __cb(_, __frame, 3, 23, function ___(__0, __1) { gateway = __1;
        if (!gateway) {
          return _(new Error(util.format($("A virtual network gateway with name \"%s\" not found in the resource group \"%s\""), gatewayName, resourceGroupName))); } ;


        return self._parseGateway(resourceGroupName, gateway, options, __cb(_, __frame, 8, 19, function ___(__0, __2) { gateway = __2;

          return self.update(resourceGroupName, gatewayName, gateway, __cb(_, __frame, 10, 19, function ___(__0, __3) { gateway = __3;

            self._showGateway(gateway, resourceGroupName, gatewayName); _(); }, true)); }, true)); }, true)); }); },


  list: function list__3(resourceGroupName, options, _) { var self, gateways, progress, __this = this; var __frame = { name: "list__3", line: 114 }; return __func(_, this, arguments, list__3, 2, __frame, function __$list__3() { self = __this;


      gateways = null;
      progress = self.interaction.progress($("Looking up virtual network gateways")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$list__3() {

            return self.networkManagementClient.virtualNetworkGateways.list(resourceGroupName, __cb(_, __frame, 6, 69, function ___(__0, __1) { gateways = __1; _(null, null, true); }, 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(gateways, function(gateways) {
            if ((gateways.length === 0)) {
              self.output.warn($("No virtual network gateways found")); }
             else {
              self.output.table(gateways, function(row, gateway) {
                row.cell($("Name"), gateway.name);
                row.cell($("Location"), gateway.location);
                row.cell($("Provisioning state"), gateway.provisioningState);
                row.cell($("Gateway type"), gateway.gatewayType);
                row.cell($("VPN type"), gateway.vpnType);
                row.cell($("SKU"), gateway.sku.name);
                row.cell($("Enable BGP"), gateway.enableBgp);
                row.cell($("Private IP allocation"), gateway.ipConfigurations[0].privateIPAllocationMethod);
                row.cell($("Private IP address"), (gateway.ipConfigurations[0].privateIPAddress || "")); }); } ; }); _(); }); }); }); },





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


      return self.get(resourceGroupName, gatewayName, __cb(_, __frame, 3, 23, function ___(__0, __1) { gateway = __1;
        self._showGateway(gateway, resourceGroupName, gatewayName); _(); }, true)); }); },


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


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


          var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return self.interaction.confirm(util.format($("Delete virtual network gateway \"%s\"? [y/n] "), gatewayName), __cb(_, __frame, 8, 44, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -150, 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 gateway \"%s\""), gatewayName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$delete__5() {

                  return self.networkManagementClient.virtualNetworkGateways.deleteMethod(resourceGroupName, gatewayName, __cb(_, __frame, 14, 58, 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, gatewayName, _) { var self, progress, gateway, __this = this; var __frame = { name: "get__6", line: 171 }; return __func(_, this, arguments, get__6, 2, __frame, function __$get__6() { self = __this;

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

                  return self.networkManagementClient.virtualNetworkGateways.get(resourceGroupName, gatewayName, __cb(_, __frame, 4, 72, function ___(__0, __1) { gateway = __1;
                    self._createVpnConfigIfNotExist(gateway);
                    return _(null, gateway); }, 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() { _(); }); }); }); },



  update: function update__7(resourceGroupName, gatewayName, gateway, _) { var self, progress, __this = this; var __frame = { name: "update__7", line: 188 }; return __func(_, this, arguments, update__7, 3, __frame, function __$update__7() { self = __this;

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

            return self.networkManagementClient.virtualNetworkGateways.createOrUpdate(resourceGroupName, gatewayName, gateway, __cb(_, __frame, 4, 68, function ___(__0, __1) { gateway = __1; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$update__7() {

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

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





  createRootCert: function createRootCert__8(resourceGroupName, gatewayName, certName, options, _) { var self, gateway, certData, rootCert, __this = this; var __frame = { name: "createRootCert__8", line: 202 }; return __func(_, this, arguments, createRootCert__8, 4, __frame, function __$createRootCert__8() { self = __this;


      return self.get(resourceGroupName, gatewayName, __cb(_, __frame, 3, 23, function ___(__0, __1) { gateway = __1;
        if (!gateway) {
          return _(new Error(util.format($("A virtual network gateway with name \"%s\" not found in the resource group \"%s\""), gatewayName, resourceGroupName))); } ;


        if ((gateway.vpnClientConfiguration.vpnClientAddressPool.addressPrefixes.length === 0)) {
          return _(new Error($("You must add address pool before adding root certificate. Please use --address-prefixes option for vpn-gateway create|set commands."))); } ;


        certData = fs.readFileSync(options.certFile);
        certData = certData.toString("base64");

        rootCert = {
          name: certName,
          publicCertData: certData };


        if (utils.findFirstCaseIgnore(gateway.vpnClientConfiguration.vpnClientRootCertificates, { name: certName })) {
          return _(new Error(util.format($("A Root Certificate with name \"%s\" already exists in Virtual Network Gateway \"%s\""), certName, gatewayName))); } ;


        gateway.vpnClientConfiguration.vpnClientRootCertificates.push(rootCert);
        return self.update(resourceGroupName, gatewayName, gateway, __cb(_, __frame, 25, 19, function ___(__0, __2) { gateway = __2;

          self._showGateway(gateway, resourceGroupName, gatewayName); _(); }, true)); }, true)); }); },


  deleteRootCert: function deleteRootCert__9(resourceGroupName, gatewayName, certName, options, _) { var self, gateway, index, __this = this; var __frame = { name: "deleteRootCert__9", line: 232 }; return __func(_, this, arguments, deleteRootCert__9, 4, __frame, function __$deleteRootCert__9() { self = __this;


      return self.get(resourceGroupName, gatewayName, __cb(_, __frame, 3, 23, function ___(__0, __2) { gateway = __2;
        if (!gateway) {
          return _(new Error(util.format($("A virtual network gateway with name \"%s\" not found in the resource group \"%s\""), gatewayName, resourceGroupName))); } ;


        index = utils.indexOfCaseIgnore(gateway.vpnClientConfiguration.vpnClientRootCertificates, { name: certName });
        if ((index === -1)) {
          return _(new Error(util.format($("A Root Certificate with name \"%s\" not found in Virtual Network Gateway \"%s\""), certName, gatewayName))); } ; return (function __$deleteRootCert__9(_) {


          var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return self.interaction.confirm(util.format($("Delete a root certificate \"%s?\" [y/n] "), certName), __cb(_, __frame, 13, 44, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -231, 17, function ___(__0, __3) { return (function __$deleteRootCert__9(__then) { if (__3) { return _(null); } else { __then(); } ; })(function __$deleteRootCert__9() {



            gateway.vpnClientConfiguration.vpnClientRootCertificates.splice(index, 1);
            return self.update(resourceGroupName, gatewayName, gateway, __cb(_, __frame, 18, 19, function ___(__0, __4) { gateway = __4;

              self._showGateway(gateway, resourceGroupName, gatewayName); _(); }, true)); }); }, true)); }, true)); }); },





  createRevokedCert: function createRevokedCert__10(resourceGroupName, gatewayName, certName, options, _) { var self, gateway, revokedCert, __this = this; var __frame = { name: "createRevokedCert__10", line: 258 }; return __func(_, this, arguments, createRevokedCert__10, 4, __frame, function __$createRevokedCert__10() { self = __this;


      return self.get(resourceGroupName, gatewayName, __cb(_, __frame, 3, 23, function ___(__0, __1) { gateway = __1;
        if (!gateway) {
          return _(new Error(util.format($("A virtual network gateway with name \"%s\" not found in the resource group \"%s\""), gatewayName, resourceGroupName))); } ;


        if ((gateway.vpnClientConfiguration.vpnClientAddressPool.addressPrefixes.length === 0)) {
          return _(new Error($("You must add address pool before adding revoked certificate. Please use --address-prefixes option for vpn-gateway create|set commands."))); } ;


        revokedCert = {
          name: certName,
          thumbprint: options.thumbprint };


        if (utils.findFirstCaseIgnore(gateway.vpnClientConfiguration.vpnClientRevokedCertificates, { name: certName })) {
          return _(new Error(util.format($("A Revoked Certificate with name \"%s\" already exists in Virtual Network Gateway \"%s\""), certName, gatewayName))); } ;


        gateway.vpnClientConfiguration.vpnClientRevokedCertificates.push(revokedCert);
        return self.update(resourceGroupName, gatewayName, gateway, __cb(_, __frame, 22, 19, function ___(__0, __2) { gateway = __2;

          self._showGateway(gateway, resourceGroupName, gatewayName); _(); }, true)); }, true)); }); },


  deleteRevokedCert: function deleteRevokedCert__11(resourceGroupName, gatewayName, certName, options, _) { var self, gateway, index, __this = this; var __frame = { name: "deleteRevokedCert__11", line: 285 }; return __func(_, this, arguments, deleteRevokedCert__11, 4, __frame, function __$deleteRevokedCert__11() { self = __this;


      return self.get(resourceGroupName, gatewayName, __cb(_, __frame, 3, 23, function ___(__0, __2) { gateway = __2;
        if (!gateway) {
          return _(new Error(util.format($("A virtual network gateway with name \"%s\" not found in the resource group \"%s\""), gatewayName, resourceGroupName))); } ;


        index = utils.indexOfCaseIgnore(gateway.vpnClientConfiguration.vpnClientRevokedCertificates, { name: certName });
        if ((index === -1)) {
          return _(new Error(util.format($("A Revoked Certificate with name \"%s\" not found in Virtual Network Gateway \"%s\""), certName, gatewayName))); } ; return (function __$deleteRevokedCert__11(_) {


          var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return self.interaction.confirm(util.format($("Delete a revoked certificate \"%s?\" [y/n] "), certName), __cb(_, __frame, 13, 44, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -284, 17, function ___(__0, __3) { return (function __$deleteRevokedCert__11(__then) { if (__3) { return _(null); } else { __then(); } ; })(function __$deleteRevokedCert__11() {



            gateway.vpnClientConfiguration.vpnClientRevokedCertificates.splice(index, 1);
            return self.update(resourceGroupName, gatewayName, gateway, __cb(_, __frame, 18, 19, function ___(__0, __4) { gateway = __4;

              self._showGateway(gateway, resourceGroupName, gatewayName); _(); }, true)); }); }, true)); }, true)); }); },





  createConnection: function createConnection__12(resourceGroupName, connectionName, options, _) { var self, parameters, connection, progress, __this = this; var __frame = { name: "createConnection__12", line: 311 }; return __func(_, this, arguments, createConnection__12, 3, __frame, function __$createConnection__12() { self = __this;


      parameters = {
        location: options.location,
        virtualNetworkGateway1: { } };


      return self._parseConnection(resourceGroupName, parameters, options, true, __cb(_, __frame, 8, 22, function ___(__0, __1) { parameters = __1;

        return self.getConnection(resourceGroupName, connectionName, __cb(_, __frame, 10, 26, function ___(__0, __2) { connection = __2;
          if (connection) {
            return _(new Error(util.format($("A virtual network gateway connection with name \"%s\" already exists in the resource group \"%s\""), connectionName, resourceGroupName))); } ;


          progress = self.interaction.progress(util.format($("Creating gateway connection \"%s\" between \"%s\" and \"%s\""), connectionName, options.vnetGateway1, (options.vnetGateway2 || options.lnetGateway2))); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$createConnection__12() {

                return self.networkManagementClient.virtualNetworkGatewayConnections.createOrUpdate(resourceGroupName, connectionName, parameters, __cb(_, __frame, 17, 81, function ___(__0, __3) { connection = __3; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$createConnection__12() {

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

              self._showConnection(connection, resourceGroupName, connectionName); _(); }); }); }, true)); }, true)); }); },


  setConnection: function setConnection__13(resourceGroupName, connectionName, options, _) { var self, connection, progress, __this = this; var __frame = { name: "setConnection__13", line: 335 }; return __func(_, this, arguments, setConnection__13, 3, __frame, function __$setConnection__13() { self = __this;


      return self.getConnection(resourceGroupName, connectionName, __cb(_, __frame, 3, 26, function ___(__0, __1) { connection = __1;
        if (!connection) {
          return _(new Error(util.format($("A virtual network gateway connection with name \"%s\" not found in the resource group \"%s\""), connectionName, resourceGroupName))); } ;


        return self._parseConnection(resourceGroupName, connection, options, false, __cb(_, __frame, 8, 22, function ___(__0, __2) { connection = __2;

          progress = self.interaction.progress(util.format($("Updating gateway connection \"%s\""), connectionName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$setConnection__13() {

                return self.networkManagementClient.virtualNetworkGatewayConnections.createOrUpdate(resourceGroupName, connectionName, connection, __cb(_, __frame, 12, 81, function ___(__0, __3) { connection = __3; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$setConnection__13() {

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

              self._showConnection(connection, resourceGroupName, connectionName); _(); }); }); }, true)); }, true)); }); },


  listConnections: function listConnections__14(resourceGroupName, options, _) { var self, progress, connections, __this = this; var __frame = { name: "listConnections__14", line: 354 }; return __func(_, this, arguments, listConnections__14, 2, __frame, function __$listConnections__14() { self = __this;


      progress = self.interaction.progress($("Looking up gateway connections"));
      connections = null; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$listConnections__14() {


            return self.networkManagementClient.virtualNetworkGatewayConnections.list(resourceGroupName, __cb(_, __frame, 7, 82, function ___(__0, __1) { connections = __1; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$listConnections__14() {

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


          self.interaction.formatOutput(connections, function(connections) {
            if ((connections.length === 0)) {
              self.output.warn($("No gateway connections found")); }
             else {
              self.output.table(connections, function(row, connection) {
                row.cell($("Name"), connection.name);
                row.cell($("Location"), connection.location);
                row.cell($("Provosioning state"), connection.provisioningState);
                row.cell($("Type"), connection.connectionType);
                row.cell($("Routing weight"), connection.routingWeight);
                var vnetGatewayInfo = resourceUtils.getResourceInformation(connection.virtualNetworkGateway1.id);
                row.cell($("Virtual network gateway"), vnetGatewayInfo.resourceName);
                var connectedEntityInfo;
                if (connection.virtualNetworkGateway2) {
                  connectedEntityInfo = resourceUtils.getResourceInformation(connection.virtualNetworkGateway2.id); }
                 else {
                  connectedEntityInfo = resourceUtils.getResourceInformation(connection.localNetworkGateway2.id); } ;

                row.cell($("Connected entity"), connectedEntityInfo.resourceName); }); } ; }); _(); }); }); }); },





  showConnection: function showConnection__15(resourceGroupName, connectionName, options, _) { var self, connection, __this = this; var __frame = { name: "showConnection__15", line: 390 }; return __func(_, this, arguments, showConnection__15, 3, __frame, function __$showConnection__15() { self = __this;

      return self.getConnection(resourceGroupName, connectionName, __cb(_, __frame, 2, 26, function ___(__0, __1) { connection = __1;
        self._showConnection(connection, resourceGroupName, connectionName); _(); }, true)); }); },


  deleteConnection: function deleteConnection__16(resourceGroupName, connectionName, options, _) { var self, connection, progress, __this = this; var __frame = { name: "deleteConnection__16", line: 396 }; return __func(_, this, arguments, deleteConnection__16, 3, __frame, function __$deleteConnection__16() { self = __this;

      return self.getConnection(resourceGroupName, connectionName, __cb(_, __frame, 2, 26, function ___(__0, __2) { connection = __2;

        if (!connection) {
          return _(new Error(util.format($("A gateway connection with name \"%s\" not found in the resource group \"%s\""), connectionName, resourceGroupName))); } ; return (function __$deleteConnection__16(_) {


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



            progress = self.interaction.progress(util.format($("Deleting gateway connection \"%s\""), connectionName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$deleteConnection__16() {

                  return self.networkManagementClient.virtualNetworkGatewayConnections.deleteMethod(resourceGroupName, connectionName, __cb(_, __frame, 14, 68, function __$deleteConnection__16() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$deleteConnection__16() {

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



  getConnection: function getConnection__17(resourceGroupName, connectionName, _) { var self, progress, connection, __this = this; var __frame = { name: "getConnection__17", line: 416 }; return __func(_, this, arguments, getConnection__17, 2, __frame, function __$getConnection__17() { self = __this;

      progress = self.interaction.progress(util.format($("Looking up gateway connection \"%s\""), connectionName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$getConnection__17() { return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$getConnection__17() {

                  return self.networkManagementClient.virtualNetworkGatewayConnections.get(resourceGroupName, connectionName, __cb(_, __frame, 4, 85, function ___(__0, __1) { connection = __1;
                    return _(null, connection); }, true)); }); })(function ___(e, __result) { __catch(function __$getConnection__17() { if (e) {

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

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

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






  setConnectionSharedKey: function setConnectionSharedKey__18(resourceGroupName, connectionName, options, _) { var self, connection, progress, __this = this; var __frame = { name: "setConnectionSharedKey__18", line: 435 }; return __func(_, this, arguments, setConnectionSharedKey__18, 3, __frame, function __$setConnectionSharedKey__18() { self = __this;


      return self.getConnection(resourceGroupName, connectionName, __cb(_, __frame, 3, 26, function ___(__0, __1) { connection = __1;
        if (!connection) {
          return _(new Error(util.format($("A virtual network gateway connection with name \"%s\" not found in the resource group \"%s\""), connectionName, resourceGroupName))); } ;


        progress = self.interaction.progress(util.format($("Setting shared key for gateway connection \"%s\""), connectionName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$setConnectionSharedKey__18() {

              return self.networkManagementClient.virtualNetworkGatewayConnections.setSharedKey(resourceGroupName, connectionName, options, __cb(_, __frame, 10, 68, function __$setConnectionSharedKey__18() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$setConnectionSharedKey__18() {

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



  resetConnectionSharedKey: function resetConnectionSharedKey__19(resourceGroupName, connectionName, options, _) { var self, connection, params, progress, __this = this; var __frame = { name: "resetConnectionSharedKey__19", line: 451 }; return __func(_, this, arguments, resetConnectionSharedKey__19, 3, __frame, function __$resetConnectionSharedKey__19() { self = __this;


      if (isNaN(options.keyLength)) {
        return _(new Error($("--key-length parameter must be an integer"))); } ;


      return self.getConnection(resourceGroupName, connectionName, __cb(_, __frame, 7, 26, function ___(__0, __1) { connection = __1;
        if (!connection) {
          return _(new Error(util.format($("A virtual network gateway connection with name \"%s\" not found in the resource group \"%s\""), connectionName, resourceGroupName))); } ;


        params = {
          keyLength: utils.parseInt(options.keyLength) };


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

              return self.networkManagementClient.virtualNetworkGatewayConnections.resetSharedKey(resourceGroupName, connectionName, params, __cb(_, __frame, 18, 68, function __$resetConnectionSharedKey__19() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$resetConnectionSharedKey__19() {

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



  showConnectionSharedKey: function showConnectionSharedKey__20(resourceGroupName, connectionName, options, _) { var self, connection, sharedKey, __this = this; var __frame = { name: "showConnectionSharedKey__20", line: 475 }; return __func(_, this, arguments, showConnectionSharedKey__20, 3, __frame, function __$showConnectionSharedKey__20() { self = __this;


      return self.getConnection(resourceGroupName, connectionName, __cb(_, __frame, 3, 26, function ___(__0, __1) { connection = __1;
        if (!connection) {
          return _(new Error(util.format($("A virtual network gateway connection with name \"%s\" not found in the resource group \"%s\""), connectionName, resourceGroupName))); } ;


        return self.getConnectionSharedKey(resourceGroupName, connectionName, __cb(_, __frame, 8, 25, function ___(__0, __2) { sharedKey = __2;
          self.interaction.formatOutput(sharedKey, function(sharedKey) {
            if ((sharedKey === null)) {
              self.output.warn(util.format($("A shared key for gateway connection \"%s\" not found"), connectionName)); }
             else {
              self.output.nameValue($("Value"), sharedKey.value); } ; }); _(); }, true)); }, true)); }); },




  getConnectionSharedKey: function getConnectionSharedKey__21(resourceGroupName, connectionName, _) { var self, progress, sharedKey, __this = this; var __frame = { name: "getConnectionSharedKey__21", line: 493 }; return __func(_, this, arguments, getConnectionSharedKey__21, 2, __frame, function __$getConnectionSharedKey__21() { self = __this;

      progress = self.interaction.progress(util.format($("Looking up shared key for gateway connection \"%s\""), connectionName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$getConnectionSharedKey__21() { return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$getConnectionSharedKey__21() {

                  return self.networkManagementClient.virtualNetworkGatewayConnections.getSharedKey(resourceGroupName, connectionName, __cb(_, __frame, 4, 84, function ___(__0, __1) { sharedKey = __1;
                    return _(null, sharedKey); }, true)); }); })(function ___(e, __result) { __catch(function __$getConnectionSharedKey__21() { if (e) {

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

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

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






  _parseGateway: function _parseGateway__22(resourceGroupName, gateway, options, _) { var self, publicip, subnet, localGateway, addrPrefixes, prefixes, __this = this; var __frame = { name: "_parseGateway__22", line: 512 }; return __func(_, this, arguments, _parseGateway__22, 3, __frame, function __$_parseGateway__22() { self = __this;


      if (options.gatewayType) {
        gateway.gatewayType = validation.isIn(options.gatewayType, constants.vpnGateway.gatewayType, "--gateway-type"); } ;


      if (options.vpnType) {
        gateway.vpnType = validation.isIn(options.vpnType, constants.vpnGateway.vpnType, "--vpn-type"); } ;


      if (options.skuName) {
        gateway.sku.name = validation.isIn(options.skuName, constants.vpnGateway.sku, "--sku-name");
        gateway.sku.tier = gateway.sku.name; } ;


      if (options.enableBgp) {
        gateway.enableBgp = validation.isBool(options.enableBgp, "--enable-bgp"); } ;


      if (options.privateIpAddress) {
        gateway.ipConfigurations[0].privateIPAddress = validation.isIP(options.privateIpAddress, "--private-ip-address");
        gateway.ipConfigurations[0].privateIPAllocationMethod = "Static"; } ; return (function __$_parseGateway__22(__then) {


        if (options.publicIpId) {
          if (options.publicIpName) {
            self.output.warn($("--public-ip-name parameter will be ignored because --public-ip-id and --public-ip-name are mutually exclusive")); } ;

          gateway.ipConfigurations[0].publicIPAddress = {
            id: options.publicIpId }; __then(); } else { return (function __$_parseGateway__22(__then) {

            if (options.publicIpName) {
              return self.publicIpCrud.get(resourceGroupName, options.publicIpName, __cb(_, __frame, 33, 39, function ___(__0, __1) { publicip = __1;
                if (!publicip) {
                  return _(new Error(util.format($("A public ip address with name \"%s\" not found in the resource group \"%s\""), options.publicIpName, resourceGroupName))); } ;

                gateway.ipConfigurations[0].publicIPAddress = {
                  id: publicip.id }; __then(); }, true)); } else { __then(); } ; })(__then); } ; })(function __$_parseGateway__22() { return (function __$_parseGateway__22(__then) {



          if (options.subnetId) {
            if ((options.vnetName || options.subnetName)) {
              self.output.warn($("--vnet-name, --subnet-name gateway will be ignored because --subnet-id and --vnet-name, --subnet-name are mutually exclusive")); } ;

            gateway.ipConfigurations[0].subnet = {
              id: options.subnetId }; __then(); } else { return (function __$_parseGateway__22(__then) {

              if ((options.vnetName && options.subnetName)) {
                return self.subnetCrud.get(resourceGroupName, options.vnetName, options.subnetName, __cb(_, __frame, 50, 35, function ___(__0, __2) { subnet = __2;
                  if (!subnet) {
                    return _(new Error(util.format($("A subnet with name \"%s\" not found in the resource group \"%s\""), options.subnetName, resourceGroupName))); } ;

                  gateway.ipConfigurations[0].subnet = {
                    id: subnet.id }; __then(); }, true)); } else { __then(); } ; })(__then); } ; })(function __$_parseGateway__22() { return (function __$_parseGateway__22(__then) {



            if (options.defaultSiteId) {
              if (options.defaultSiteName) {
                self.output.warn($("--default-site-name parameter will be ignored because --default-site-id and --default-site-name are mutually exclusive")); } ;

              if (utils.argHasValue(options.defaultSiteId)) {
                gateway.gatewayDefaultSite = {
                  id: options.defaultSiteId }; }

               else {
                delete gateway.gatewayDefaultSite; } ; __then(); } else { return (function __$_parseGateway__22(__then) {

                if (options.defaultSiteName) { return (function __$_parseGateway__22(__then) {
                    if (utils.argHasValue(options.defaultSiteName)) {
                      return self.lnetGatewayCrud.get(resourceGroupName, options.defaultSiteName, __cb(_, __frame, 72, 48, function ___(__0, __3) { localGateway = __3;
                        if (!localGateway) {
                          return _(new Error(util.format($("A Local Network Gateway with name \"%s\" not found in the resource group \"%s\""), options.defaultSiteName, resourceGroupName))); } ;

                        gateway.gatewayDefaultSite = {
                          id: localGateway.id }; __then(); }, true)); } else {


                      delete gateway.gatewayDefaultSite; __then(); } ; })(__then); } else { __then(); } ; })(__then); } ; })(function __$_parseGateway__22() {



            if (options.addressPrefixes) {
              addrPrefixes = gateway.vpnClientConfiguration.vpnClientAddressPool.addressPrefixes;
              prefixes = options.addressPrefixes.split(",");
              prefixes.forEach(function(address) {
                if ((addrPrefixes.indexOf(address) === -1)) {
                  validation.isCIDR(address, "--address-prefixes");
                  addrPrefixes.push(address); } ; }); } ;




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



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


  _createVpnConfigIfNotExist: function(gateway) {
    if ((gateway.vpnClientConfiguration === undefined)) {
      gateway.vpnClientConfiguration = {
        vpnClientAddressPool: {
          addressPrefixes: [] },

        vpnClientRootCertificates: [],
        vpnClientRevokedCertificates: [] }; }

     else if ((gateway.vpnClientConfiguration.vpnClientAddressPool === undefined)) {
      gateway.vpnClientConfiguration.vpnClientAddressPool = {
        addressPrefixes: [] }; }  ; },




  _parseConnection: function _parseConnection__23(resourceGroupName, connection, options, useDefaults, _) { var self, resGroupGateway1, gateway1, resGroupGateway2, gateway2, resGroupLnetGateway2, lnet2, __this = this; var __frame = { name: "_parseConnection__23", line: 634 }; return __func(_, this, arguments, _parseConnection__23, 4, __frame, function __$_parseConnection__23() { self = __this;


      if (options.type) {
        connection.connectionType = validation.isIn(options.type, constants.vpnGateway.connectionType, "--type"); } else {
        if (useDefaults) {
          connection.connectionType = utils.takeDefault(self.output, constants.vpnGateway.connectionType[0], "--type"); } ; } ;


      if (((useDefaults && !options.vnetGateway2) && !options.lnetGateway2)) {
        return _(new Error($("You must specify connected entity with --vnet-gateway2 or --lnet-gateway2 option"))); } ;


      if (options.routingWeight) {
        connection.routingWeight = validation.isInt(options.routingWeight, { }, "--weight"); } else {
        if (useDefaults) {
          connection.routingWeight = utils.takeDefault(self.output, constants.vpnGateway.defWeight, "--weight"); } ; } ;


      if (options.sharedKey) {
        connection.sharedKey = options.sharedKey; } ;


      if (options.tags) {
        if (utils.argHasValue(options.tags)) {
          tagUtils.appendTags(connection, options); }
         else {
          connection.tags = { }; } ; } ; return (function __$_parseConnection__23(__then) {



        if (options.vnetGateway1) {
          resGroupGateway1 = (options.vnetGateway1Group || resourceGroupName);
          if (!options.vnetGateway1Group) {
            self.output.warn(util.format($("Using resource group for Gateway 1: %s"), resGroupGateway1)); } ;

          return self.get(resGroupGateway1, options.vnetGateway1, __cb(_, __frame, 36, 26, function ___(__0, __1) { gateway1 = __1;
            if (!gateway1) { return _(new Error(util.format($("A virtual network gateway with name \"%s\" not found in the resource group \"%s\""), options.vnetGateway1, resGroupGateway1))); } ;
            connection.virtualNetworkGateway1 = gateway1; __then(); }, true)); } else { __then(); } ; })(function __$_parseConnection__23() { return (function __$_parseConnection__23(__then) {


          if (options.vnetGateway2) {
            resGroupGateway2 = (options.vnetGateway2Group || resourceGroupName);
            if (!options.vnetGateway2Group) {
              self.output.warn(util.format($("Using resource group for Gateway 2: %s"), resGroupGateway2)); } ;

            return self.get(resGroupGateway2, options.vnetGateway2, __cb(_, __frame, 46, 26, function ___(__0, __2) { gateway2 = __2;
              if (!gateway2) { return _(new Error(util.format($("A virtual network gateway with name \"%s\" not found in the resource group \"%s\""), options.vnetGateway2, resGroupGateway2))); } ;
              connection.virtualNetworkGateway2 = gateway2; __then(); }, true)); } else { __then(); } ; })(function __$_parseConnection__23() { return (function __$_parseConnection__23(__then) {


            if (options.lnetGateway2) {
              resGroupLnetGateway2 = (options.lnetGateway2Group || resourceGroupName);
              if (!options.lnetGateway2Group) {
                self.output.warn(util.format($("Using resource group for Local Network Gateway 2: %s"), resGroupLnetGateway2)); } ;

              return self.lnetGatewayCrud.get(resGroupLnetGateway2, options.lnetGateway2, __cb(_, __frame, 56, 39, function ___(__0, __3) { lnet2 = __3;
                if (!lnet2) { return _(new Error(util.format($("A local network gateway with name \"%s\" not found in the resource group \"%s\""), options.lnetGateway2, resGroupLnetGateway2))); } ;
                connection.localNetworkGateway2 = lnet2; __then(); }, true)); } else { __then(); } ; })(function __$_parseConnection__23() {


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


  _showGateway: function(gateway, resourceGroupName, gatewayName) {
    var self = this;

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


      self.output.nameValue($("Id"), gateway.id);
      self.output.nameValue($("Name"), gateway.name);
      self.output.nameValue($("Type"), gateway.type);
      self.output.nameValue($("Location"), gateway.location);
      self.output.nameValue($("Tags"), tagUtils.getTagsInfo(gateway.tags));
      self.output.nameValue($("Provisioning state"), gateway.provisioningState);
      self.output.nameValue($("Gateway type"), gateway.gatewayType);
      self.output.nameValue($("VPN type"), gateway.vpnType);
      self.output.nameValue($("Enable BGP"), gateway.enableBgp);

      if (gateway.gatewayDefaultSite) {
        self.output.nameValue($("Default Site id"), gateway.gatewayDefaultSite.id); } ;


      self.output.header($("SKU"));
      self.output.nameValue($("Name"), gateway.sku.name, 2);
      self.output.nameValue($("Tier"), gateway.sku.tier, 2);
      self.output.nameValue($("Capacity"), gateway.sku.capacity, 2);

      var vpnConfig = gateway.vpnClientConfiguration;
      if (vpnConfig) {
        if ((vpnConfig.vpnClientAddressPool.addressPrefixes.length > 0)) {
          self.output.header($("Address Prefixes"));
          self.output.list(vpnConfig.vpnClientAddressPool.addressPrefixes, 2); } ;

        if ((vpnConfig.vpnClientRootCertificates.length > 0)) {
          self.output.header($("Root certificates"));
          vpnConfig.vpnClientRootCertificates.forEach(function(rootCert) {
            self.output.nameValue($("Name"), rootCert.name, 2);
            self.output.nameValue($("Provisioning state"), rootCert.provisioningState, 2); }); } ;


        if ((vpnConfig.vpnClientRevokedCertificates.length > 0)) {
          self.output.header($("Revoked certificates"));
          vpnConfig.vpnClientRevokedCertificates.forEach(function(revokedCert) {
            self.output.nameValue($("Name"), revokedCert.name, 2);
            self.output.nameValue($("Provisioning state"), revokedCert.provisioningState, 2);
            self.output.nameValue($("Thumbprint"), revokedCert.thumbprint, 2); }); } ; } ;




      self.output.header($("IP configurations"));
      gateway.ipConfigurations.forEach(function(ipConfig) {
        self.output.nameValue($("Id"), ipConfig.id, 2);
        self.output.nameValue($("Name"), ipConfig.name, 2);
        self.output.nameValue($("Provisioning state"), ipConfig.provisioningState, 2);
        self.output.nameValue($("Private IP allocation method"), ipConfig.privateIPAllocationMethod, 2);
        self.output.nameValue($("Private IP address"), ipConfig.privateIPAddress, 2);
        self.output.nameValue($("Public IP address id"), ipConfig.publicIPAddress.id, 2);
        self.output.nameValue($("Subnet id"), ipConfig.subnet.id, 2);
        self.output.data(""); }); }); },




  _showConnection: function(connection, resourceGroupName, connectionName) {
    var self = this;

    self.interaction.formatOutput(connection, function(connection) {
      if ((connection === null)) {
        self.output.warn(util.format($("A gateway connection with name \"%s\" not found in the resource group \"%s\""), connectionName, resourceGroupName));
        return; } ;


      self.output.nameValue($("Id"), connection.id);
      self.output.nameValue($("Name"), connection.name);
      self.output.nameValue($("Type"), connection.type);
      self.output.nameValue($("Location"), connection.location);
      self.output.nameValue($("Provisioning state"), connection.provisioningState);
      self.output.nameValue($("Tags"), tagUtils.getTagsInfo(connection.tags));
      self.output.nameValue($("Connection type"), connection.connectionType);
      self.output.nameValue($("Routing weight"), connection.routingWeight);
      self.output.nameValue($("Shared key"), connection.sharedKey);
      self.output.nameValue($("Virtual network gateway 1"), connection.virtualNetworkGateway1.id);
      if (connection.virtualNetworkGateway2) {
        self.output.nameValue($("Virtual network gateway 2"), connection.virtualNetworkGateway2.id); } ;

      if (connection.localNetworkGateway2) {
        self.output.nameValue($("Local network gateway 2"), connection.localNetworkGateway2.id); } ;

      self.output.header($("Bytes transferred"));
      self.output.nameValue($("Egress"), connection.egressBytesTransferred, 2);
      self.output.nameValue($("Ingress"), connection.ingressBytesTransferred, 2); }); }});




module.exports = VirtualNetworkGateway;
