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

function AppGateways(cli, networkManagementClient) {
  this.interaction = cli.interaction;
  this.networkManagementClient = networkManagementClient;
  this.output = cli.output;
  this.publicIpCrud = new PublicIp(cli, networkManagementClient);
  this.subnetCrud = new Subnet(cli, networkManagementClient);
  this.vnetUtil = new VNetUtil();};


__.extend(AppGateways.prototype, {
  create: function create__1(resourceGroup, appGatewayName, location, options, _) { var self, appGateway, subnetId, subnet, parameters, frontendIpID, frontendPortID, poolID, settingsID, listenerID, capacity, data, certID, progress, createdAppGateway, __this = this; var __frame = { name: "create__1", line: 39 }; return __func(_, this, arguments, create__1, 4, __frame, function __$create__1() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __1) { appGateway = __1;
        if (appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" already exists in resource group \"%s\""), appGatewayName, resourceGroup))); } ; return (function __$create__1(__then) {


          if (options.subnetId) {
            subnetId = options.subnetId; __then(); } else {

            return self.subnetCrud.get(resourceGroup, options.vnetName, options.subnetName, __cb(_, __frame, 10, 35, function ___(__0, __2) { subnet = __2;
              if (!subnet) {
                return _(new Error(util.format($("Subnet \"%s\" not found in virtual network \"%s\""), options.subnetName, options.vnetName))); } ;

              subnetId = subnet.id; __then(); }, true)); } ; })(function __$create__1() {


          parameters = self._setDefaultAttributes(options);
          self.subscriptionId = self._getSubscriptionId(options);
          frontendIpID = self._generateResourceId(resourceGroup, appGatewayName, "frontendIPConfigurations", parameters.frontendIpName);
          frontendPortID = self._generateResourceId(resourceGroup, appGatewayName, "frontendPorts", parameters.frontendPortName);
          poolID = self._generateResourceId(resourceGroup, appGatewayName, "backendAddressPools", parameters.addressPoolName);
          settingsID = self._generateResourceId(resourceGroup, appGatewayName, "backendHttpSettingsCollection", parameters.httpSettingsName);
          listenerID = self._generateResourceId(resourceGroup, appGatewayName, "httpListeners", parameters.httpListenerName);

          appGateway = {
            name: appGatewayName,
            location: location,
            gatewayIPConfigurations: [{
              name: parameters.gatewayIpName,
              subnet: { id: subnetId } },],

            frontendPorts: [{
              name: parameters.frontendPortName,
              port: parseInt(parameters.frontendPort) },],

            backendAddressPools: [{
              name: parameters.addressPoolName,
              backendAddresses: self._parseDnsServers(options),
              backendIPConfiguration: [] },],

            backendHttpSettingsCollection: [{
              name: parameters.httpSettingsName,
              protocol: parameters.httpSettingsProtocol,
              port: parseInt(parameters.httpSettingsPort),
              cookieBasedAffinity: parameters.httpSettingsCookieBasedAffinity },],

            httpListeners: [{
              name: parameters.httpListenerName,
              frontendIPConfiguration: { id: frontendIpID },
              frontendPort: { id: frontendPortID },
              protocol: parameters.httpListenerProtocol },],

            requestRoutingRules: [{
              name: parameters.routingRuleName,
              ruleType: parameters.routingRuleType,
              backendAddressPool: { id: poolID },
              backendHttpSettings: { id: settingsID },
              httpListener: { id: listenerID } },] };


          if (parameters.skuName) {
            utils.verifyParamExistsInCollection(constants.appGateway.sku.name, parameters.skuName, "--sku-name");
            appGateway.sku = {
              name: parameters.skuName }; } ;


          if (options.skuTier) {
            utils.verifyParamExistsInCollection(constants.appGateway.sku.tier, parameters.skuTier, "--sku-tier");
            appGateway.sku.tier = parameters.skuTier; } ;

          if (options.capacity) {
            capacity = parseInt(options.capacity);
            if (((capacity >= constants.appGateway.sku.capacity[0]) && (capacity <= constants.appGateway.sku.capacity[1]))) {
              appGateway.sku.capacity = capacity; }
             else {
              return _(new Error(util.format($("Given %s \"%s\" is invalid, supported values are: [%s]"), "--capacity", options.capacity, constants.appGateway.sku.capacity))); } ; } ;



          appGateway.frontendIPConfigurations = [];
          return self._parseFrontendIp(resourceGroup, appGatewayName, parameters.frontendIpName, parameters, __cb(_, __frame, 81, 50, function ___(__0, __3) { appGateway.frontendIPConfigurations.push(__3);

            if (parameters.certFile) {
              appGateway.sslCertificates = [];
              data = fs.readFileSync(parameters.certFile);
              appGateway.sslCertificates.push({
                name: parameters.certName,
                password: parameters.certPassword,
                data: data.toString("base64") });

              certID = self._generateResourceId(resourceGroup, appGatewayName, "sslCertificates", parameters.certName);
              appGateway.httpListeners[0].sslCertificate = {
                id: certID }; } ;



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


            progress = self.interaction.progress(util.format($("Creating configuration for an application gateway \"%s\""), appGatewayName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$create__1() {


                  return self.networkManagementClient.applicationGateways.createOrUpdate(resourceGroup, appGatewayName, appGateway, __cb(_, __frame, 106, 75, function ___(__0, __4) { createdAppGateway = __4; _(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._showAppGateway(createdAppGateway); _(); }); }); }, true)); }); }, true)); }); },


  set: function set__2(resourceGroup, appGatewayName, options, _) { var self, appGateway, capacity, __this = this; var __frame = { name: "set__2", line: 152 }; return __func(_, this, arguments, set__2, 3, __frame, function __$set__2() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __1) { appGateway = __1;
        if (!appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" not found in resource group \"%s\""), appGatewayName, resourceGroup))); } ;


        if (options.skuName) {
          utils.verifyParamExistsInCollection(constants.appGateway.sku.name, options.skuName, "--sku-name");
          appGateway.sku.name = options.skuName; } ;

        if (options.skuTier) {
          utils.verifyParamExistsInCollection(constants.appGateway.sku.tier, options.skuTier, "--sku-tier");
          appGateway.sku.tier = options.skuTier; } ;

        if (options.capacity) {
          capacity = parseInt(options.capacity);
          if (((capacity >= constants.appGateway.sku.capacity[0]) && (capacity <= constants.appGateway.sku.capacity[1]))) {
            appGateway.sku.capacity = capacity; }
           else {
            return _(new Error(util.format($("Given %s \"%s\" is invalid, supported values are: [%s]"), "--capacity", options.capacity, constants.appGateway.sku.capacity))); } ; } ;


        if (utils.argHasValue(options.tags)) {
          tagUtils.appendTags(appGateway, options); } ;


        self.output.warn("Application gateway set command is a long-running process. It may take up to 15-20 minutes to complete.");
        return self._setAppGateway(resourceGroup, appGatewayName, appGateway, options, __cb(_, __frame, 28, 9, function __$set__2() { _(); }, true)); }, true)); }); },


  get: function get__3(resourceGroup, appGatewayName, _) { var self, appGateway, progress, __this = this; var __frame = { name: "get__3", line: 183 }; return __func(_, this, arguments, get__3, 2, __frame, function __$get__3() { self = __this;


      progress = self.interaction.progress(util.format($("Looking up an application gateway \"%s\""), appGatewayName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$get__3() { return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$get__3() {

                  return self.networkManagementClient.applicationGateways.get(resourceGroup, appGatewayName, null, __cb(_, __frame, 5, 68, function ___(__0, __1) { appGateway = __1; __then(); }, true)); }); })(function ___(error, __result) { __catch(function __$get__3() { if (error) {

                    if ((error.statusCode === 404)) {
                      appGateway = null; } ; __then(); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, function __$get__3() { _(null, null, true); }); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$get__3() {


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

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


  list: function list__4(options, _) { var self, appGateways, progress, __this = this; var __frame = { name: "list__4", line: 199 }; return __func(_, this, arguments, list__4, 1, __frame, function __$list__4() { self = __this; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$list__4() { return (function __$list__4(__then) {




              if (options.resourceGroup) {
                progress = self.interaction.progress(util.format($("Looking up application gateways in resource group \"%s\""), options.resourceGroup));
                return self.networkManagementClient.applicationGateways.list(options.resourceGroup, options, __cb(_, __frame, 7, 71, function ___(__0, __1) { appGateways = __1; __then(); }, true)); } else {

                progress = self.interaction.progress($("Looking up application gateways in all resource groups"));
                return self.networkManagementClient.applicationGateways.listAll(options, __cb(_, __frame, 10, 71, function ___(__0, __2) { appGateways = __2; __then(); }, true)); } ; })(function __$list__4() { _(null, null, true); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$list__4() {


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

          if (!appGateways) {
            self.output.warn(util.format($("No application gateways found in resource group \"%s\""), options.resourceGroup)); } ;


          self.interaction.formatOutput(appGateways, function(data) {
            if ((data.length === 0)) {
              self.output.warn(util.format($("No application gateways found in resource group \"%s\""), options.resourceGroup)); }
             else {
              self.output.table(data, function(row, gateway) {
                row.cell($("Name"), gateway.name);
                row.cell($("Provisioning state"), gateway.provisioningState);
                row.cell($("Location"), gateway.location);
                var resource = resourceUtils.getResourceInformation(gateway.id);
                row.cell($("Resource group"), resource.resourceGroup); }); } ; }); _(); }); }); }); },





  show: function show__5(resourceGroup, appGatewayName, options, _) { var self, appGateway, __this = this; var __frame = { name: "show__5", line: 233 }; return __func(_, this, arguments, show__5, 3, __frame, function __$show__5() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __1) { appGateway = __1;

        if (appGateway) {
          self._showAppGateway(appGateway); }
         else {
          self.output.warn(util.format($("An application gateway with name \"%s\" not found in resource group \"%s\""), appGatewayName, resourceGroup)); } ; _(); }, true)); }); },



  delete: function delete__6(resourceGroup, appGatewayName, options, _) { var self, appGateway, progress, __this = this; var __frame = { name: "delete__6", line: 244 }; return __func(_, this, arguments, delete__6, 3, __frame, function __$delete__6() { self = __this;


      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 3, 26, function ___(__0, __2) { appGateway = __2;
        if (!appGateway) {
          self.output.warn(util.format($("An application gateway with name \"%s\" not found in resource group \"%s\""), appGatewayName, resourceGroup)); return _(null); } ; return (function __$delete__6(_) {


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



            progress = self.interaction.progress(util.format($("Deleting an application gateway \"%s\""), appGatewayName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$delete__6() { return (function __$delete__6(__then) {

                    if (options.nowait) {
                      return self.networkManagementClient.applicationGateways.beginDeleteMethod(resourceGroup, appGatewayName, options, __cb(_, __frame, 15, 57, __then, true)); } else {

                      return self.networkManagementClient.applicationGateways.deleteMethod(resourceGroup, appGatewayName, options, __cb(_, __frame, 17, 57, __then, true)); } ; })(function __$delete__6() { _(null, null, true); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$delete__6() {


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



  start: function start__7(resourceGroup, appGatewayName, options, _) { var self, appGateway, progress, __this = this; var __frame = { name: "start__7", line: 268 }; return __func(_, this, arguments, start__7, 3, __frame, function __$start__7() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __1) { appGateway = __1;
        if (!appGateway) {
          return _(new Error(util.format($("An application gateway with name \"%s\" not found in resource group \"%s\""), appGatewayName, resourceGroup))); } ;



        self.output.warn("Application gateway start command is a long-running process. It may take up to 15-20 minutes to complete.");

        progress = self.interaction.progress(util.format($("Starting an application gateway \"%s\""), appGatewayName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$start__7() { return (function __$start__7(__then) {

                if (options.nowait) {
                  return self.networkManagementClient.applicationGateways.beginStart(resourceGroup, appGatewayName, options, __cb(_, __frame, 13, 57, __then, true)); } else { __then(); } ; })(function __$start__7() {

                return self.networkManagementClient.applicationGateways.start(resourceGroup, appGatewayName, options, __cb(_, __frame, 15, 55, function __$start__7() { _(null, null, true); }, true)); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$start__7() {

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



  stop: function stop__8(resourceGroup, appGatewayName, options, _) { var self, appGateway, progress, __this = this; var __frame = { name: "stop__8", line: 289 }; return __func(_, this, arguments, stop__8, 3, __frame, function __$stop__8() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __1) { appGateway = __1;
        if (!appGateway) {
          return _(new Error(util.format($("An application gateway with name \"%s\" not found in resource group \"%s\""), appGatewayName, resourceGroup))); } ;



        progress = self.interaction.progress(util.format($("Stopping an application gateway \"%s\""), appGatewayName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$stop__8() {

              return self.networkManagementClient.applicationGateways.stop(resourceGroup, appGatewayName, null, __cb(_, __frame, 10, 55, function __$stop__8() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$stop__8() {

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



  addSsl: function addSsl__9(resourceGroup, appGatewayName, certName, options, _) { var self, appGateway, certificateObject, data, __this = this; var __frame = { name: "addSsl__9", line: 305 }; return __func(_, this, arguments, addSsl__9, 4, __frame, function __$addSsl__9() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __1) { appGateway = __1;
        if (!appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" not found in resource group \"%s\""), appGatewayName, resourceGroup))); } ;


        if (utils.stringIsNullOrEmpty(options.certFile)) {
          return _(new Error($("--cert-file parameter must not be empty"))); } ;


        if (utils.stringIsNullOrEmpty(options.certPassword)) {
          return _(new Error($("--cert-password parameter must not be empty"))); } ;


        certificateObject = { password: options.certPassword, name: certName }; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$addSsl__9() {


              data = fs.readFileSync(options.certFile); __then(); }); })(function ___(e, __result) { __catch(function __$addSsl__9() { if (e) {

                if ((e.code === "ENONT")) {
                  return _(new Error(util.format($("File \"%s\" not found"), options.certFile))); } ; __then(); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, function __$addSsl__9() {


            certificateObject.data = data.toString("base64");
            appGateway.sslCertificates.push(certificateObject);
            return self._setAppGateway(resourceGroup, appGatewayName, appGateway, options, __cb(_, __frame, 26, 9, function __$addSsl__9() { _(); }, true)); }); }); }, true)); }); },


  removeSsl: function removeSsl__10(resourceGroup, appGatewayName, certName, options, _) { var self, appGateway, index, __this = this; var __frame = { name: "removeSsl__10", line: 334 }; return __func(_, this, arguments, removeSsl__10, 4, __frame, function __$removeSsl__10() { self = __this;


      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 3, 26, function ___(__0, __2) { appGateway = __2;
        if (!appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" not found in resource group \"%s\""), appGatewayName, resourceGroup))); } ;


        index = utils.indexOfCaseIgnore(appGateway.sslCertificates, { name: certName }); return (function __$removeSsl__10(__then) {
          if ((index !== -1)) { return (function __$removeSsl__10(_) {
              var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return self.interaction.confirm(util.format($("Delete an ssl certificate \"%s\"? [y/n] "), certName), __cb(_, __frame, 10, 46, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -333, 17, function ___(__0, __3) { return (function __$removeSsl__10(__then) { if (__3) { return _(null); } else { __then(); } ; })(function __$removeSsl__10() {


                appGateway.sslCertificates.splice(index, 1);
                return self._setAppGateway(resourceGroup, appGatewayName, appGateway, options, __cb(_, __frame, 14, 11, __then, true)); }); }, true)); } else {

            return _(new Error(util.format($("SSL certificate with name \"%s\" not found for an application gateway \"%s\""), certName, appGatewayName))); } ; })(_); }, true)); }); },



  addFrontendIp: function addFrontendIp__11(resourceGroup, appGatewayName, frontendIpName, options, _) { var self, appGateway, frontendIp, __this = this; var __frame = { name: "addFrontendIp__11", line: 354 }; return __func(_, this, arguments, addFrontendIp__11, 4, __frame, function __$addFrontendIp__11() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __1) { appGateway = __1;
        if (!appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" not found in resource group \"%s\""), appGatewayName, resourceGroup))); } ;


        frontendIp = utils.findFirstCaseIgnore(appGateway.frontendIPConfigurations, { name: frontendIpName });
        if (frontendIp) {
          return _(new Error(util.format($("A frontend ip with name \"%s\" already exists for an application gateway \"%s\""), frontendIpName, appGatewayName))); } ;


        return self._parseFrontendIp(resourceGroup, appGatewayName, frontendIpName, options, __cb(_, __frame, 12, 22, function ___(__0, __2) { frontendIp = __2;
          appGateway.frontendIPConfigurations.push(frontendIp);
          return self._setAppGateway(resourceGroup, appGatewayName, appGateway, options, __cb(_, __frame, 14, 9, function __$addFrontendIp__11() { _(); }, true)); }, true)); }, true)); }); },


  removeFrontendIp: function removeFrontendIp__12(resourceGroup, appGatewayName, frontendIpName, options, _) { var self, appGateway, index, __this = this; var __frame = { name: "removeFrontendIp__12", line: 371 }; return __func(_, this, arguments, removeFrontendIp__12, 4, __frame, function __$removeFrontendIp__12() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __2) { appGateway = __2;
        if (!appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" not found in resource group \"%s\""), appGatewayName, resourceGroup))); } ;


        index = utils.indexOfCaseIgnore(appGateway.frontendIPConfigurations, { name: frontendIpName });
        if ((index === -1)) {
          return _(new Error(util.format($("A frontend ip with name \"%s\" not found for an application gateway \"%s\""), frontendIpName, appGatewayName))); } ; return (function __$removeFrontendIp__12(_) {


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


            appGateway.frontendIPConfigurations.splice(index, 1);
            return self._setAppGateway(resourceGroup, appGatewayName, appGateway, options, __cb(_, __frame, 16, 9, function __$removeFrontendIp__12() { _(); }, true)); }); }, true)); }, true)); }); },


  addFrontendPort: function addFrontendPort__13(resourceGroup, appGatewayName, frontendPortName, options, _) { var self, appGateway, frontendPort, __this = this; var __frame = { name: "addFrontendPort__13", line: 390 }; return __func(_, this, arguments, addFrontendPort__13, 4, __frame, function __$addFrontendPort__13() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __1) { appGateway = __1;
        if (!appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" not found in resource group \"%s\""), appGatewayName, resourceGroup))); } ;


        frontendPort = utils.findFirstCaseIgnore(appGateway.frontendPorts, { name: frontendPortName });
        if (frontendPort) {
          return _(new Error(util.format($("A frontend port with name \"%s\" already exists for an application gateway \"%s\""), frontendPortName, appGatewayName))); } ;

        if (((options.port < constants.appGateway.settings.port[0]) || (options.port > constants.appGateway.settings.port[1]))) {
          return _(new Error(util.format($("A frontend port value must be in range [%s]"), constants.appGateway.settings.port))); } ;

        frontendPort = {
          name: frontendPortName,
          port: parseInt(options.port) };


        self.output.warn("Application gateway add new frontend port command is a long-running process. It may take up to 15-20 minutes to complete.");
        appGateway.frontendPorts.push(frontendPort);
        return self._setAppGateway(resourceGroup, appGatewayName, appGateway, options, __cb(_, __frame, 21, 9, function __$addFrontendPort__13() { _(); }, true)); }, true)); }); },


  removeFrontendPort: function removeFrontendPort__14(resourceGroup, appGatewayName, frontendPortName, options, _) { var self, appGateway, index, __this = this; var __frame = { name: "removeFrontendPort__14", line: 414 }; return __func(_, this, arguments, removeFrontendPort__14, 4, __frame, function __$removeFrontendPort__14() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __2) { appGateway = __2;
        if (!appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" not found in resource group \"%s\""), appGatewayName, resourceGroup))); } ;


        index = utils.indexOfCaseIgnore(appGateway.frontendPorts, { name: frontendPortName });
        if ((index === -1)) {
          return _(new Error(util.format($("Frontend port with name \"%s\" not found for an application gateway \"%s\""), frontendPortName, appGatewayName))); } ; return (function __$removeFrontendPort__14(_) {


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



            self.output.warn("Application gateway remove frontend port command is a long-running process. It may take up to 15-20 minutes to complete.");
            appGateway.frontendPorts.splice(index, 1);
            return self._setAppGateway(resourceGroup, appGatewayName, appGateway, options, __cb(_, __frame, 18, 9, function __$removeFrontendPort__14() { _(); }, true)); }); }, true)); }, true)); }); },


  addBackendAddressPool: function addBackendAddressPool__15(resourceGroup, appGatewayName, poolName, options, _) { var self, dnsServers, appGateway, pool, addressPool, __this = this; var __frame = { name: "addBackendAddressPool__15", line: 435 }; return __func(_, this, arguments, addBackendAddressPool__15, 4, __frame, function __$addBackendAddressPool__15() { self = __this;

      dnsServers = self._parseDnsServers(options);
      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 3, 26, function ___(__0, __1) { appGateway = __1;
        if (!appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" not found in resource group \"%s\""), appGatewayName, resourceGroup))); } ;


        pool = utils.findFirstCaseIgnore(appGateway.backendAddressPools, { name: poolName }); return (function __$addBackendAddressPool__15(__then) {
          if (pool) {
            return _(new Error(util.format($("A backend address pool with name \"%s\" already exists in application gateway \"%s\""), poolName, appGatewayName))); } else {

            addressPool = {
              name: poolName,
              backendAddresses: dnsServers };

            appGateway.backendAddressPools.push(addressPool);

            return self._setAppGateway(resourceGroup, appGatewayName, appGateway, options, __cb(_, __frame, 18, 11, __then, true)); } ; })(_); }, true)); }); },



  removeBackendAddressPool: function removeBackendAddressPool__16(resourceGroup, appGatewayName, poolName, options, _) { var self, appGateway, index, __this = this; var __frame = { name: "removeBackendAddressPool__16", line: 457 }; return __func(_, this, arguments, removeBackendAddressPool__16, 4, __frame, function __$removeBackendAddressPool__16() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __2) { appGateway = __2;
        if (!appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" not found in resource group \"%s\""), appGatewayName, resourceGroup))); } ;


        index = utils.indexOfCaseIgnore(appGateway.backendAddressPools, { name: poolName }); return (function __$removeBackendAddressPool__16(__then) {
          if ((index !== -1)) { return (function __$removeBackendAddressPool__16(_) {
              var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return self.interaction.confirm(util.format($("Delete a backend address pool \"%s\"? [y/n] "), poolName), __cb(_, __frame, 9, 46, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -456, 17, function ___(__0, __3) { return (function __$removeBackendAddressPool__16(__then) { if (__3) { return _(null); } else { __then(); } ; })(function __$removeBackendAddressPool__16() {


                appGateway.backendAddressPools.splice(index, 1);
                return self._setAppGateway(resourceGroup, appGatewayName, appGateway, options, __cb(_, __frame, 13, 11, __then, true)); }); }, true)); } else {

            return _(new Error(util.format($("A backend address pool with name \"%s\" not found for an application gateway \"%s\""), poolName, appGatewayName))); } ; })(_); }, true)); }); },



  addHttpSettings: function addHttpSettings__17(resourceGroup, appGatewayName, httpSettingsName, options, _) { var self, appGateway, httpSettings, settings, __this = this; var __frame = { name: "addHttpSettings__17", line: 476 }; return __func(_, this, arguments, addHttpSettings__17, 4, __frame, function __$addHttpSettings__17() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __1) { appGateway = __1;
        if (!appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" not found in resource group \"%s\""), appGatewayName, resourceGroup))); } ;


        httpSettings = self._parseHttpSettings(httpSettingsName, options, true);
        settings = utils.findFirstCaseIgnore(appGateway.backendHttpSettingsCollection, { name: httpSettingsName }); return (function __$addHttpSettings__17(__then) {
          if (settings) {
            return _(new Error(util.format($("A http settings with name \"%s\" already exists for an application gateway \"%s\""), httpSettingsName, appGatewayName))); } else {

            appGateway.backendHttpSettingsCollection.push(httpSettings);
            return self._setAppGateway(resourceGroup, appGatewayName, appGateway, options, __cb(_, __frame, 13, 11, __then, true)); } ; })(_); }, true)); }); },



  removeHttpSettings: function removeHttpSettings__18(resourceGroup, appGatewayName, httpSettingsName, options, _) { var self, appGateway, index, __this = this; var __frame = { name: "removeHttpSettings__18", line: 493 }; return __func(_, this, arguments, removeHttpSettings__18, 4, __frame, function __$removeHttpSettings__18() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __2) { appGateway = __2;
        if (!appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" not found in resource group \"%s\""), appGatewayName, resourceGroup))); } ;


        index = utils.indexOfCaseIgnore(appGateway.backendHttpSettingsCollection, { name: httpSettingsName }); return (function __$removeHttpSettings__18(__then) {
          if ((index !== -1)) { return (function __$removeHttpSettings__18(_) {
              var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return self.interaction.confirm(util.format($("Delete an http settings \"%s\"? [y/n] "), httpSettingsName), __cb(_, __frame, 9, 46, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -492, 17, function ___(__0, __3) { return (function __$removeHttpSettings__18(__then) { if (__3) { return _(null); } else { __then(); } ; })(function __$removeHttpSettings__18() {


                appGateway.backendHttpSettingsCollection.splice(index, 1);
                return self._setAppGateway(resourceGroup, appGatewayName, appGateway, options, __cb(_, __frame, 13, 11, __then, true)); }); }, true)); } else {

            return _(new Error(util.format($("An http settings with name \"%s\" not found for an application gateway \"%s\""), httpSettingsName, appGatewayName))); } ; })(_); }, true)); }); },



  addHttpListener: function addHttpListener__19(resourceGroup, appGatewayName, httpListenerName, options, _) { var self, appGateway, httpListener, frontendIp, frontendPort, protocol, sslCert, __this = this; var __frame = { name: "addHttpListener__19", line: 512 }; return __func(_, this, arguments, addHttpListener__19, 4, __frame, function __$addHttpListener__19() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __1) { appGateway = __1;
        if (!appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" not found in resource group \"%s\""), appGatewayName, resourceGroup))); } ;


        if ((!appGateway.httpListeners || !appGateway.httpListeners.length)) {
          appGateway.httpListeners = []; } ;


        if (utils.findFirstCaseIgnore(appGateway.httpListeners, { name: httpListenerName })) {
          return _(new Error(util.format($("An http listener with name \"%s\" already exists for an application gateway \"%s\""), httpListenerName, appGatewayName))); } ;


        httpListener = {
          name: httpListenerName,
          protocol: constants.appGateway.settings.protocol[0] };


        if (options.frontendIpName) {
          frontendIp = utils.findFirstCaseIgnore(appGateway.frontendIPConfigurations, { name: options.frontendIpName });
          if (!frontendIp) {
            return _(new Error(util.format($("Frontend ip with name \"%s\" not found for an application gateway \"%s\""), options.frontendIpName, appGatewayName))); } ;

          httpListener.frontendIPConfiguration = frontendIp; } ;


        if (options.frontendPortName) {
          frontendPort = utils.findFirstCaseIgnore(appGateway.frontendPorts, { name: options.frontendPortName });
          if (!frontendPort) {
            return _(new Error(util.format($("Frontend port with name \"%s\" not found for an application gateway \"%s\""), options.frontendPortName, appGatewayName))); } ;

          httpListener.frontendPort = frontendPort; } ;


        if (options.protocol) {
          protocol = options.protocol.toLowerCase();
          if (((protocol === "https") && !options.sslCert)) {
            return _(new Error($("--ssl-cert parameter is required, when \"--protocol Https\" parameter is specified"))); } ;

          if (((protocol !== "http") && (protocol != "https"))) {
            return _(new Error(util.format($("\"%s\" protocol is not valid. Valid values are [%s]"), constants.appGateway.httpListener.protocol))); } ;

          httpListener.protocol = utils.capitalizeFirstLetter(protocol); } ;


        if (options.sslCert) {
          sslCert = utils.findFirstCaseIgnore(appGateway.sslCertificates, { name: options.sslCert });
          if (!sslCert) {
            return _(new Error(util.format($("SSL certificate with name \"%s\" not found for an application gateway \"%s\""), options.sslCert, appGatewayName))); } ;

          httpListener.sslCertificate = sslCert; } ;


        self.output.warn("Application gateway add new http listener command is a long-running process. It may take up to 15-20 minutes to complete.");
        appGateway.httpListeners.push(httpListener);
        return self._setAppGateway(resourceGroup, appGatewayName, appGateway, options, __cb(_, __frame, 57, 9, function __$addHttpListener__19() { _(); }, true)); }, true)); }); },


  removeHttpListener: function removeHttpListener__20(resourceGroup, appGatewayName, httpListenerName, options, _) { var self, appGateway, index, __this = this; var __frame = { name: "removeHttpListener__20", line: 572 }; return __func(_, this, arguments, removeHttpListener__20, 4, __frame, function __$removeHttpListener__20() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __2) { appGateway = __2;
        if (!appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" not found in resource group \"%s\""), appGatewayName, resourceGroup))); } ;


        index = utils.indexOfCaseIgnore(appGateway.httpListeners, { name: httpListenerName });
        if ((index === -1)) {
          return _(new Error(util.format($("Http listener with name \"%s\" not found for an application gateway \"%s\""), httpListenerName, appGatewayName))); } ; return (function __$removeHttpListener__20(_) {


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



            self.output.warn("Application gateway remove http listener command is a long-running process. It may take up to 15-20 minutes to complete.");
            appGateway.httpListeners.splice(index, 1);
            return self._setAppGateway(resourceGroup, appGatewayName, appGateway, options, __cb(_, __frame, 18, 9, function __$removeHttpListener__20() { _(); }, true)); }); }, true)); }, true)); }); },


  addRequestRoutingRule: function addRequestRoutingRule__21(resourceGroup, appGatewayName, ruleName, options, _) { var self, appGateway, backendHttpSettings, httpListener, backendAddressPool, rule, __this = this; var __frame = { name: "addRequestRoutingRule__21", line: 593 }; return __func(_, this, arguments, addRequestRoutingRule__21, 4, __frame, function __$addRequestRoutingRule__21() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __1) { appGateway = __1;
        if (!appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" not found"), appGatewayName))); } ;


        if (utils.findFirstCaseIgnore(appGateway.requestRoutingRules, { name: ruleName })) {
          return _(new Error(util.format($("A request routing rule with name \"%s\" already exists in application gateway \"%s\""), ruleName, appGatewayName))); } ;


        backendHttpSettings = utils.findFirstCaseIgnore(appGateway.backendHttpSettingsCollection, { name: options.httpSettingsName });
        if (!backendHttpSettings) {
          return _(new Error(util.format($("A backend http settings with name \"%s\" not found in application gateway \"%s\""), options.httpSettingsName, appGatewayName))); } ;


        httpListener = utils.findFirstCaseIgnore(appGateway.httpListeners, { name: options.httpListenerName });
        if (!httpListener) {
          return _(new Error(util.format($("Http listener with name \"%s\" not found for an application gateway \"%s\""), options.httpListenerName, appGatewayName))); } ;


        backendAddressPool = utils.findFirstCaseIgnore(appGateway.backendAddressPools, { name: options.addressPoolName });
        if (!backendAddressPool) {
          return _(new Error(util.format($("Address pool with name \"%s\" not found for an application gateway \"%s\""), options.addressPoolName, appGatewayName))); } ;


        rule = {
          name: ruleName,
          type: constants.appGateway.routingRule.type[0],
          backendHttpSettings: backendHttpSettings,
          httpListener: httpListener,
          backendAddressPool: backendAddressPool };


        if (options.type) {
          utils.verifyParamExistsInCollection(constants.appGateway.routingRule.type, options.type, "--type");
          rule.type = options.type; } ;


        appGateway.requestRoutingRules.push(rule);
        return self._setAppGateway(resourceGroup, appGatewayName, appGateway, options, __cb(_, __frame, 40, 9, function __$addRequestRoutingRule__21() { _(); }, true)); }, true)); }); },


  removeRequestRoutingRule: function removeRequestRoutingRule__22(resourceGroup, appGatewayName, ruleName, options, _) { var self, appGateway, index, __this = this; var __frame = { name: "removeRequestRoutingRule__22", line: 636 }; return __func(_, this, arguments, removeRequestRoutingRule__22, 4, __frame, function __$removeRequestRoutingRule__22() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __2) { appGateway = __2;
        if (!appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" not found"), appGatewayName))); } ;


        index = utils.indexOfCaseIgnore(appGateway.requestRoutingRules, { name: ruleName });
        if ((index === -1)) {
          return _(new Error(util.format($("An request routing rule with name \"%s\" not found in application gateway \"%s\""), ruleName, appGatewayName))); } ; return (function __$removeRequestRoutingRule__22(_) {


          var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return self.interaction.confirm(util.format($("Delete request routing rule \"%s\"? [y/n] "), ruleName), __cb(_, __frame, 12, 44, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -635, 17, function ___(__0, __3) { return (function __$removeRequestRoutingRule__22(__then) { if (__3) { return _(null); } else { __then(); } ; })(function __$removeRequestRoutingRule__22() {


            appGateway.requestRoutingRules.splice(index, 1);
            return self._setAppGateway(resourceGroup, appGatewayName, appGateway, options, __cb(_, __frame, 16, 9, function __$removeRequestRoutingRule__22() { _(); }, true)); }); }, true)); }, true)); }); },


  addProbe: function addProbe__23(resourceGroup, appGatewayName, probeName, options, _) { var self, appGateway, probe, __this = this; var __frame = { name: "addProbe__23", line: 655 }; return __func(_, this, arguments, addProbe__23, 4, __frame, function __$addProbe__23() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __1) { appGateway = __1;
        if (!appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" not found"), appGatewayName))); } ;


        if (utils.findFirstCaseIgnore(appGateway.probes, { name: probeName })) {
          return _(new Error(util.format($("A probe with name \"%s\" already exists in application gateway \"%s\""), probeName, appGatewayName))); } ;


        probe = self._parseProbe(resourceGroup, appGatewayName, probeName, options);
        appGateway.probes.push(probe);
        return self._setAppGateway(resourceGroup, appGatewayName, appGateway, options, __cb(_, __frame, 13, 9, function __$addProbe__23() { _(); }, true)); }, true)); }); },


  removeProbe: function removeProbe__24(resourceGroup, appGatewayName, probeName, options, _) { var self, appGateway, index, __this = this; var __frame = { name: "removeProbe__24", line: 671 }; return __func(_, this, arguments, removeProbe__24, 4, __frame, function __$removeProbe__24() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __2) { appGateway = __2;
        if (!appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" not found"), appGatewayName))); } ;


        index = utils.indexOfCaseIgnore(appGateway.probes, { name: probeName });
        if ((index === -1)) {
          return _(new Error(util.format($("A probe with name \"%s\" not found in application gateway \"%s\""), probeName, appGatewayName))); } ; return (function __$removeProbe__24(_) {


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


            appGateway.probes.splice(index, 1);
            return self._setAppGateway(resourceGroup, appGatewayName, appGateway, options, __cb(_, __frame, 16, 9, function __$removeProbe__24() { _(); }, true)); }); }, true)); }, true)); }); },


  addUrlPathMap: function addUrlPathMap__25(resourceGroup, appGatewayName, urlPathMapName, options, _) { var self, appGateway, backendHttpSettings, backendAddressPool, urlPathMap, __this = this; var __frame = { name: "addUrlPathMap__25", line: 690 }; return __func(_, this, arguments, addUrlPathMap__25, 4, __frame, function __$addUrlPathMap__25() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __1) { appGateway = __1;
        if (!appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" not found"), appGatewayName))); } ;


        if (utils.findFirstCaseIgnore(appGateway.urlPathMaps, { name: urlPathMapName })) {
          return _(new Error(util.format($("An url path map with name \"%s\" already exists in application gateway \"%s\""), urlPathMapName, appGatewayName))); } ;


        backendHttpSettings = utils.findFirstCaseIgnore(appGateway.backendHttpSettingsCollection, { name: options.httpSettingsName });
        if (!backendHttpSettings) {
          return _(new Error(util.format($("A backend http settings with name \"%s\" not found in application gateway \"%s\""), options.httpSettingsName, appGatewayName))); } ;


        backendAddressPool = utils.findFirstCaseIgnore(appGateway.backendAddressPools, { name: options.addressPoolName });
        if (!backendAddressPool) {
          return _(new Error(util.format($("Address pool with name \"%s\" not found for an application gateway \"%s\""), options.addressPoolName, appGatewayName))); } ;


        urlPathMap = {
          name: urlPathMapName,
          defaultBackendAddressPool: {
            id: backendAddressPool.id },

          defaultBackendHttpSettings: {
            id: backendHttpSettings.id },

          pathRules: [{
            name: options.ruleName,
            paths: [options.path,],
            backendAddressPool: { id: backendAddressPool.id },
            backendHttpSettings: { id: backendHttpSettings.id } },] };



        appGateway.urlPathMaps.push(urlPathMap);
        return self._setAppGateway(resourceGroup, appGatewayName, appGateway, options, __cb(_, __frame, 38, 9, function __$addUrlPathMap__25() { _(); }, true)); }, true)); }); },


  removeUrlPathMap: function removeUrlPathMap__26(resourceGroup, appGatewayName, urlPathMapName, options, _) { var self, appGateway, index, __this = this; var __frame = { name: "removeUrlPathMap__26", line: 731 }; return __func(_, this, arguments, removeUrlPathMap__26, 4, __frame, function __$removeUrlPathMap__26() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __2) { appGateway = __2;
        if (!appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" not found"), appGatewayName))); } ;


        index = utils.indexOfCaseIgnore(appGateway.urlPathMaps, { name: urlPathMapName });
        if ((index === -1)) {
          return _(new Error(util.format($("An url path map with name \"%s\" not found in application gateway \"%s\""), urlPathMapName, appGatewayName))); } ; return (function __$removeUrlPathMap__26(_) {


          var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return self.interaction.confirm(util.format($("Delete url path map \"%s\"? [y/n] "), urlPathMapName), __cb(_, __frame, 12, 44, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -730, 17, function ___(__0, __3) { return (function __$removeUrlPathMap__26(__then) { if (__3) { return _(null); } else { __then(); } ; })(function __$removeUrlPathMap__26() {


            appGateway.urlPathMaps.splice(index, 1);
            return self._setAppGateway(resourceGroup, appGatewayName, appGateway, options, __cb(_, __frame, 16, 9, function __$removeUrlPathMap__26() { _(); }, true)); }); }, true)); }, true)); }); },


  addMapRule: function addMapRule__27(resourceGroup, appGatewayName, ruleName, options, _) { var self, appGateway, urlPathMap, backendHttpSettings, backendAddressPool, rule, __this = this; var __frame = { name: "addMapRule__27", line: 750 }; return __func(_, this, arguments, addMapRule__27, 4, __frame, function __$addMapRule__27() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __1) { appGateway = __1;
        if (!appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" not found"), appGatewayName))); } ;


        urlPathMap = utils.findFirstCaseIgnore(appGateway.urlPathMaps, { name: options.urlPathMapName });
        if (!urlPathMap) {
          return _(new Error(util.format($("An url path map with name \"%s\" not found in application gateway \"%s\""), options.urlPathMapName, appGatewayName))); } ;


        if (utils.findFirstCaseIgnore(urlPathMap.pathRules, { name: ruleName })) {
          return _(new Error(util.format($("A rule with name \"%s\" already exists in url path map \"%s\""), ruleName, options.urlPathMapName))); } ;


        backendHttpSettings = utils.findFirstCaseIgnore(appGateway.backendHttpSettingsCollection, { name: options.httpSettingsName });
        if (!backendHttpSettings) {
          return _(new Error(util.format($("A backend http settings with name \"%s\" not found in application gateway \"%s\""), options.httpSettingsName, appGatewayName))); } ;


        backendAddressPool = utils.findFirstCaseIgnore(appGateway.backendAddressPools, { name: options.addressPoolName });
        if (!backendAddressPool) {
          return _(new Error(util.format($("Address pool with name \"%s\" not found for an application gateway \"%s\""), options.addressPoolName, appGatewayName))); } ;


        rule = {
          name: ruleName,
          paths: [options.path,],
          backendAddressPool: { id: backendAddressPool.id },
          backendHttpSettings: { id: backendHttpSettings.id } };

        urlPathMap.pathRules.push(rule);
        return self._setAppGateway(resourceGroup, appGatewayName, appGateway, options, __cb(_, __frame, 33, 9, function __$addMapRule__27() { _(); }, true)); }, true)); }); },


  removeMapRule: function removeMapRule__28(resourceGroup, appGatewayName, ruleName, options, _) { var self, appGateway, urlPathMap, index, __this = this; var __frame = { name: "removeMapRule__28", line: 786 }; return __func(_, this, arguments, removeMapRule__28, 4, __frame, function __$removeMapRule__28() { self = __this;

      return self.get(resourceGroup, appGatewayName, __cb(_, __frame, 2, 26, function ___(__0, __2) { appGateway = __2;
        if (!appGateway) {
          return _(new Error(util.format($("Application gateway \"%s\" not found"), appGatewayName))); } ;


        urlPathMap = utils.findFirstCaseIgnore(appGateway.urlPathMaps, { name: options.urlPathMapName });
        if (!urlPathMap) {
          return _(new Error(util.format($("An url path map with name \"%s\" not found in application gateway \"%s\""), options.urlPathMapName, appGatewayName))); } ;


        index = utils.indexOfCaseIgnore(urlPathMap.pathRules, { name: ruleName });
        if ((index === -1)) {
          return _(new Error(util.format($("A rule with name \"%s\" not found in url path map \"%s\""), ruleName, options.urlPathMapName))); } ; return (function __$removeMapRule__28(_) {


          var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return self.interaction.confirm(util.format($("Delete url path map rule \"%s\"? [y/n] "), ruleName), __cb(_, __frame, 17, 44, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -785, 17, function ___(__0, __3) { return (function __$removeMapRule__28(__then) { if (__3) { return _(null); } else { __then(); } ; })(function __$removeMapRule__28() {


            urlPathMap.pathRules.splice(index, 1);
            return self._setAppGateway(resourceGroup, appGatewayName, appGateway, options, __cb(_, __frame, 21, 9, function __$removeMapRule__28() { _(); }, true)); }); }, true)); }, true)); }); },


  _generateResourceId: function(resourceGroup, appGatewayName, resourceType, resourceName) {
    var id = "";
    id += "/subscriptions/";
    id += encodeURIComponent(this.subscriptionId);
    id += "/resourceGroups/";
    id += encodeURIComponent(resourceGroup);
    id += "/providers/";
    id += "Microsoft.Network";
    id += "/applicationGateways/";
    id += encodeURIComponent(appGatewayName);
    id += util.format($("/%s"), resourceType);
    id += util.format($("/%s"), resourceName);
    return id; },


  _getAttributeNames: function(list) {
    var namesString = "[";
    var counter = 0;
    list.forEach(function(item) {
      if ((counter > 0)) {
        namesString += ", "; } ;

      namesString += item.name;
      counter++; });

    namesString += "]";
    return namesString; },


  _getSubscriptionId: function(options) {
    var subscription = profile.current.getSubscription(options.subscription);
    var client = utils.createResourceClient(subscription);
    return client.credentials.subscriptionId; },


  _parseDnsServers: function(options) {
    var self = this;

    var ipAddresses = options.servers.split(",");
    var dnsServers = [];

    ipAddresses.forEach(function(address) {
      var ipValidationResult = self.vnetUtil.parseIPv4(address);
      if (ipValidationResult.error) {
        var dnsValidationResult = self.vnetUtil.isValidDns(address);
        if ((dnsValidationResult === false)) {
          throw new Error(util.format($("Address \"%s\" is not valid IPv4 or DNS name"), address)); } ; } ;


      var dns = { ipAddress: address };
      dnsServers.push(dns); });


    return dnsServers; },


  _parseFrontendIp: function _parseFrontendIp__29(resourceGroup, appGatewayName, frontendIpName, options, _) { var self, frontendIp, ipValidationResult, publicIp, subnet, __this = this; var __frame = { name: "_parseFrontendIp__29", line: 866 }; return __func(_, this, arguments, _parseFrontendIp__29, 4, __frame, function __$_parseFrontendIp__29() { self = __this;

      frontendIp = {
        id: self._generateResourceId(resourceGroup, appGatewayName, "frontendIPConfigurations", frontendIpName),
        name: frontendIpName,
        privateIPAllocationMethod: constants.appGateway.frontendIp.privateIPAllocationMethod[0] };


      if (options.staticIpAddress) {
        ipValidationResult = self.vnetUtil.parseIPv4(options.staticIpAddress);
        if (ipValidationResult.error) {
          return _(new Error(util.format($("IPv4 %s static ip address is not valid"), options.staticIpAddress))); } ;

        frontendIp.privateIPAddress = options.staticIpAddress; } ;


      if (options.publicIpId) {
        frontendIp.publicIPAddress = options.publicIpId;
        return _(null, frontendIp); } ; return (function __$_parseFrontendIp__29(__then) {


        if (options.publicIpName) {
          return self.publicIpCrud.get(resourceGroup, options.publicIpName, __cb(_, __frame, 22, 39, function ___(__0, __1) { publicIp = __1;
            if (!publicIp) {
              return _(new Error(util.format($("Public IP \"%s\" not found in resource group \"%s\""), options.publicIpName, resourceGroup))); } ;

            frontendIp.publicIPAddress = { id: publicIp.id };
            return _(null, frontendIp); }, true)); } else { __then(); } ; })(function __$_parseFrontendIp__29() { return (function __$_parseFrontendIp__29(__then) {


          if ((options.vnetName && options.subnetName)) {
            return self.subnetCrud.get(resourceGroup, options.vnetName, options.subnetName, __cb(_, __frame, 31, 35, function ___(__0, __2) { subnet = __2;
              if (!subnet) {
                return _(new Error(util.format($("Subnet \"%s\" not found in virtual network \"%s\" resource group \"%s\""), options.subnetName, options.vnetName, resourceGroup))); } ;

              frontendIp.subnet = { id: subnet.id };
              return _(null, frontendIp); }, true)); } else { __then(); } ; })(function __$_parseFrontendIp__29() {


          if (options.subnetId) {
            frontendIp.subnet = { id: options.subnetId }; } ;


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


  _parseHttpSettings: function(httpSettingsName, options, useDefaults) {
    var self = this;

    var httpSettings = {
      name: httpSettingsName };


    if (options.protocol) {
      var protocol = utils.verifyParamExistsInCollection(constants.appGateway.settings.protocol, options.protocol, "--protocol");

      httpSettings.protocol = utils.capitalizeFirstLetter(protocol); }
     else if (useDefaults) {
      self.output.warn(util.format($("Using default protocol: %s"), constants.appGateway.settings.protocol[0]));
      httpSettings.protocol = constants.appGateway.settings.protocol[0]; }  ;


    if (options.port) {
      var portAsInt = utils.parseInt(options.port);
      if (((isNaN(portAsInt) || (portAsInt < constants.appGateway.settings.port[0])) || (portAsInt > constants.appGateway.settings.port[1]))) {
        throw new Error(util.format($("port parameter must be an integer in range %s"), utils.toRange(constants.appGateway.settings.port))); } ;


      httpSettings.port = portAsInt; } ;


    if (options.cookieBasedAffinity) {
      var cookieBasedAffinity = utils.verifyParamExistsInCollection(constants.appGateway.settings.affinity, options.cookieBasedAffinity, "--cookie-based-affinity");

      httpSettings.cookieBasedAffinity = utils.capitalizeFirstLetter(cookieBasedAffinity); }
     else if (useDefaults) {
      self.output.warn(util.format($("Using default cookie based affinity: %s"), constants.appGateway.settings.affinity[0]));
      httpSettings.cookieBasedAffinity = constants.appGateway.settings.affinity[0]; }  ;


    return httpSettings; },


  _parseProbe: function(resourceGroup, appGatewayName, probeName, options) {
    var self = this;
    var probe = {
      name: probeName };

    if (options.protocol) {
      var protocol = utils.verifyParamExistsInCollection(constants.appGateway.settings.protocol, options.protocol, "--protocol");

      probe.protocol = utils.capitalizeFirstLetter(protocol); } ;

    if (options.port) {
      if (((options.port < constants.appGateway.settings.port[0]) || (options.port > constants.appGateway.settings.port[1]))) {
        throw new Error(util.format($("A frontend port value must be in range [%s]"), constants.appGateway.settings.port)); } ;

      probe.port = options.port; } ;

    if ((options.hostName && options.path)) {
      probe.host = options.hostName;
      probe.path = options.path; }
     else {
      self.output.warn(util.format($("Using default probe URL: %s%s"), constants.appGateway.probe.host, constants.appGateway.probe.path));
      probe.host = constants.appGateway.probe.host;
      probe.path = constants.appGateway.probe.path; } ;

    if (options.interval) {
      probe.interval = parseInt(options.interval);
      if (isNaN(probe.interval)) {
        throw new Error($("Parameter --interval must be a number")); } ; }

     else {
      self.output.warn(util.format($("Using default interval: %s"), constants.appGateway.probe.interval));
      probe.interval = constants.appGateway.probe.interval; } ;

    if (options.timeout) {
      probe.timeout = parseInt(options.timeout);
      if (isNaN(probe.timeout)) {
        throw new Error($("Parameter --timeout must be a number")); } ; }

     else {
      self.output.warn(util.format($("Using default timeout: %s"), constants.appGateway.probe.timeout));
      probe.timeout = constants.appGateway.probe.timeout; } ;

    if (options.unhealthyThreshold) {
      probe.unhealthyThreshold = parseInt(options.unhealthyThreshold);
      if (isNaN(probe.unhealthyThreshold)) {
        throw new Error($("Parameter --unhealthy-threshold must be a number")); } ; }

     else {
      self.output.warn(util.format($("Using default unhealthy threshold: %s"), constants.appGateway.probe.unhealthyThreshold));
      probe.unhealthyThreshold = constants.appGateway.probe.unhealthyThreshold; } ;

    return probe; },


  _setAppGateway: function _setAppGateway__30(resourceGroup, appGatewayName, appGateway, options, _) { var self, progress, updatedAppGateway, __this = this; var __frame = { name: "_setAppGateway__30", line: 1003 }; return __func(_, this, arguments, _setAppGateway__30, 4, __frame, function __$_setAppGateway__30() { self = __this;


      progress = self.interaction.progress(util.format($("Setting long-running configuration for an application gateway %s"), appGatewayName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$_setAppGateway__30() { return (function __$_setAppGateway__30(__then) {


              if (options.nowait) {
                return self.networkManagementClient.applicationGateways.beginCreateOrUpdate(resourceGroup, appGatewayName, appGateway, null, __cb(_, __frame, 7, 77, function ___(__0, __1) { updatedAppGateway = __1; __then(); }, true)); } else {

                return self.networkManagementClient.applicationGateways.createOrUpdate(resourceGroup, appGatewayName, appGateway, null, __cb(_, __frame, 9, 77, function ___(__0, __2) { updatedAppGateway = __2; __then(); }, true)); } ; })(function __$_setAppGateway__30() { _(null, null, true); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$_setAppGateway__30() {


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

          self._showAppGateway(updatedAppGateway); _(); }); }); }); },


  _setDefaultAttributes: function(options) {
    var self = this;
    if (options.certFile) {
      if (options.httpListenerProtocol) {


        if ((options.httpListenerProtocol.toLowerCase() !== constants.appGateway.httpListener.protocol[1])) {
          throw new Error($("--http-listener-protocol parameter must be Https")); } ; }

       else {
        options.httpListenerProtocol = constants.appGateway.httpListener.protocol[1];
        self.output.warn(util.format($("Using default http listener protocol: %s"), options.httpListenerProtocol)); } ;

      options.certName = "cert01";
      if (utils.stringIsNullOrEmpty(options.certFile)) {
        throw new Error($("--cert-file parameter must not be empty")); } ;

      if (utils.stringIsNullOrEmpty(options.certPassword)) {
        throw new Error($("--cert-password parameter must not be empty")); } ; } ;


    if (!options.gatewayIpName) {
      options.gatewayIpName = "ipConfig01";
      self.output.warn(util.format($("Using default gateway ip name: %s"), options.gatewayIpName)); } ;

    if (!options.skuName) {
      options.skuName = constants.appGateway.sku.name[0];
      self.output.warn(util.format($("Using default sku name: %s"), options.skuName)); } ;

    if (!options.skuTier) {
      options.skuTier = constants.appGateway.sku.tier[0];
      self.output.warn(util.format($("Using default sku tier: %s"), options.skuTier)); } ;

    if (!options.capacity) {
      options.capacity = constants.appGateway.sku.capacity[0];
      self.output.warn(util.format($("Using default sku capacity: %s"), options.capacity)); }
     else {
      if (((options.capacity < constants.appGateway.sku.capacity[0]) || (options.capacity > constants.appGateway.sku.capacity[1]))) {
        throw new Error(util.format($("Application gateway instance count must be in range \"[%s]\""), constants.appGateway.sku.capacity)); } ; } ;


    if (!options.frontendIpName) {
      options.frontendIpName = "frontendIp01";
      self.output.warn(util.format($("Using default frontend ip name: %s"), options.frontendIpName)); } ;

    if (!options.frontendPortName) {
      options.frontendPortName = "frontendPort01";
      self.output.warn(util.format($("Using default frontend port name: %s"), options.frontendPortName)); } ;

    if (!options.frontendPort) {
      options.frontendPort = (options.certFile ? constants.appGateway.settings.defHttpsPort : constants.appGateway.settings.defHttpPort);
      self.output.warn(util.format($("Using default frontend port: %s"), options.frontendPort)); } ;

    if (!options.addressPoolName) {
      options.addressPoolName = constants.appGateway.pool.name;
      self.output.warn(util.format($("Using default address pool name: %s"), options.addressPoolName)); } ;

    if (!options.httpSettingsName) {
      options.httpSettingsName = constants.appGateway.settings.name;
      self.output.warn(util.format($("Using default http settings name: %s"), options.httpSettingsName)); } ;

    if (!options.httpSettingsProtocol) {
      options.httpSettingsProtocol = constants.appGateway.settings.protocol[0];
      self.output.warn(util.format($("Using default http settings protocol: %s"), options.httpSettingsProtocol)); } ;

    if (!options.httpSettingsPort) {
      options.httpSettingsPort = constants.appGateway.settings.defHttpPort;
      self.output.warn(util.format($("Using default http settings port: %s"), options.httpSettingsPort)); } ;

    if (!options.httpSettingsCookieBasedAffinity) {
      options.httpSettingsCookieBasedAffinity = constants.appGateway.settings.affinity[0];
      self.output.warn(util.format($("Using default http settings cookie based affinity: %s"), options.httpSettingsCookieBasedAffinity)); } ;

    if (!options.httpListenerName) {
      options.httpListenerName = "listener01";
      self.output.warn(util.format($("Using default http listener name: %s"), options.httpListenerName)); } ;

    if (!options.routingRuleName) {
      options.routingRuleName = "rule01";
      self.output.warn(util.format($("Using default request routing rule name: %s"), options.routingRuleName)); } ;

    if (!options.routingRuleType) {
      options.routingRuleType = constants.appGateway.routingRule.type[0];
      self.output.warn(util.format($("Using default request routing rule type: %s"), options.routingRuleType)); } ;

    return options; },


  _showAppGateway: function(appGateway) {
    var self = this;
    self.interaction.formatOutput(appGateway, function(appGateway) {
      self.output.nameValue($("Id"), appGateway.id);
      self.output.nameValue($("Name"), appGateway.name);
      self.output.nameValue($("Location"), appGateway.location);
      self.output.nameValue($("Provisioning state"), appGateway.provisioningState);
      self.output.nameValue($("Sku"), appGateway.sku.name);

      var resource = resourceUtils.getResourceInformation(appGateway.id);
      self.output.nameValue($("Resource Group"), resource.resourceGroup);
      self.output.nameValue($("Tags"), tagUtils.getTagsInfo(appGateway.tags));
      self.output.nameValue($("Gateway IP configations"), self._getAttributeNames(appGateway.gatewayIPConfigurations));
      self.output.nameValue($("SSL cerificates"), self._getAttributeNames(appGateway.sslCertificates));
      self.output.nameValue($("Frontend ip configurations"), self._getAttributeNames(appGateway.frontendIPConfigurations));
      self.output.nameValue($("Frontend ports"), self._getAttributeNames(appGateway.frontendPorts));
      self.output.nameValue($("Backend address pools"), self._getAttributeNames(appGateway.backendAddressPools));
      self.output.nameValue($("Backend http settings"), self._getAttributeNames(appGateway.backendHttpSettingsCollection));
      self.output.nameValue($("Http listeners"), self._getAttributeNames(appGateway.httpListeners));
      self.output.nameValue($("Request routing rules"), self._getAttributeNames(appGateway.requestRoutingRules));
      self.output.nameValue($("Probes"), self._getAttributeNames(appGateway.probes));
      self.output.nameValue($("Url Path Maps"), self._getAttributeNames(appGateway.urlPathMaps));

      self.output.nameValue($("GatewayIpConfigurationText"), JSON.stringify(appGateway.gatewayIPConfigurations, null, 4));
      self.output.nameValue($("SslCertificateText"), JSON.stringify(appGateway.sslCertificates, null, 4));
      self.output.nameValue($("FrontendIpConfigurationText"), JSON.stringify(appGateway.frontendIPConfigurations, null, 4));
      self.output.nameValue($("FrontendPortText"), JSON.stringify(appGateway.frontendPorts, null, 4));
      self.output.nameValue($("BackendAddressPoolText"), JSON.stringify(appGateway.backendAddressPools, null, 4));
      self.output.nameValue($("BackendHttpSettingsText"), JSON.stringify(appGateway.backendHttpSettingsCollection, null, 4));
      self.output.nameValue($("HttpListenersText"), JSON.stringify(appGateway.httpListeners, null, 4));
      self.output.nameValue($("RequestRoutingRulesText"), JSON.stringify(appGateway.requestRoutingRules, null, 4));
      self.output.nameValue($("SkuText"), JSON.stringify(appGateway.sku, null, 4));
      self.output.nameValue($("ProbesText"), JSON.stringify(appGateway.probes, null, 4));
      self.output.nameValue($("UrlPathMapsText"), JSON.stringify(appGateway.urlPathMaps, null, 4)); }); }});




module.exports = AppGateways;
