/*** 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,__tryCatch=__rt.__tryCatch; var util = require("util");















var profile = require("../../util/profile");
var utils = require("../../util/utils");

var $ = utils.getLocaleString;

exports.init = function(cli) {
  var internallb = cli.category("service").category("internal-load-balancer").description($("Commands to manage internal load balancers for your Cloud Service Deployment"));


  var log = cli.output;

  internallb.command("list [serviceName]").description($("List internal load balancers for a cloud service deployment")).usage("[options] [serviceName]").option("-r, --serviceName <serviceName>", $("the cloud service name")).option("-s, --subscription <id>", $("the subscription id")).execute(function __1(serviceName, options, _) { var computeClient, progress, deployment, loadBalancers; var __frame = { name: "__1", line: 33 }; return __func(_, this, arguments, __1, 2, __frame, function __$__1() {





      return cli.interaction.promptIfNotGiven($("Cloud Service name: "), serviceName, __cb(_, __frame, 1, 36, function ___(__0, __1) { serviceName = __1;

        computeClient = utils.createComputeClient(profile.current.getSubscription(options.subscription));
        progress = cli.interaction.progress($("Getting cloud service deployment")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__1() {


              return computeClient.deployments.getBySlot(serviceName, "Production", __cb(_, __frame, 7, 47, function ___(__0, __2) { deployment = __2; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__1() {

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


            loadBalancers = (deployment.loadBalancers ? deployment.loadBalancers : []);
            cli.interaction.formatOutput(loadBalancers, function(outputData) {
              if ((outputData.length === 0)) {
                log.info($("No internal load balancers defined")); }
               else {
                log.table(outputData, function(row, item) {
                  var getfrontendIpConfigProperty = function(propertyName) {
                    if (!item.frontendIPConfiguration) {
                      return ""; } ;


                    if (!item.frontendIPConfiguration[propertyName]) {
                      return ""; } ;


                    return item.frontendIPConfiguration[propertyName]; };


                  row.cell($("Name"), item.name);
                  row.cell($("Type"), getfrontendIpConfigProperty("type"));
                  row.cell($("SubnetName"), getfrontendIpConfigProperty("subnetName"));
                  row.cell($("StaticVirtualNetworkIPAddress"), getfrontendIpConfigProperty("staticVirtualNetworkIPAddress")); }); } ; }); _(); }); }); }, true)); }); });





  internallb.command("add [serviceName] [internalLBName]").usage("[options] [serviceName] [internalLBName]").description($("Add an internal load balancer for a cloud service deployment")).option("-r, --serviceName <serviceName>", $("the cloud service name")).option("-n, --internalLBName <internalLBName>", $("the internal load balancer name")).option("-t, --subnet-name <name>", $("the name of the subnet in the virtual network that the load balancer uses")).option("-a, --static-virtualnetwork-ipaddress <ip-address>", $("specific virtual IP address that the load balancer uses from the subnet in the virtual network")).option("-s, --subscription <id>", $("the subscription id")).execute(function __2(serviceName, internalLBName, options, _) { var computeClient, progress, deployment, internalLoadBalancerConfig; var __frame = { name: "__2", line: 80 }; return __func(_, this, arguments, __2, 3, __frame, function __$__2() {








      return cli.interaction.promptIfNotGiven($("Cloud Service name: "), serviceName, __cb(_, __frame, 1, 36, function ___(__0, __1) { serviceName = __1;
        return cli.interaction.promptIfNotGiven($("Internal Load Balancer name: "), internalLBName, __cb(_, __frame, 2, 39, function ___(__0, __2) { internalLBName = __2;

          computeClient = utils.createComputeClient(profile.current.getSubscription(options.subscription));
          progress = cli.interaction.progress($("Getting cloud service deployment")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__2() {


                return computeClient.deployments.getBySlot(serviceName, "Production", __cb(_, __frame, 8, 47, function ___(__0, __3) { deployment = __3; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__2() {

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


              internalLoadBalancerConfig = {
                name: internalLBName,
                frontendIPConfiguration: {
                  type: "Private" } };



              if (options.staticVirtualnetworkIpaddress) {
                if (!options.subnetName) {
                  return _(new Error($("--subnet-name is required when --static-virtualnetwork-ipaddress is specified"))); } ;


                internalLoadBalancerConfig.frontendIPConfiguration.staticVirtualNetworkIPAddress = options.staticVirtualnetworkIpaddress; } ;


              if (options.subnetName) {
                internalLoadBalancerConfig.frontendIPConfiguration.subnetName = options.subnetName; } ;


              progress = cli.interaction.progress($("Adding internal load balancer")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__2() {

                    return computeClient.loadBalancers.create(serviceName, deployment.name, internalLoadBalancerConfig, __cb(_, __frame, 34, 36, function __$__2() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__2() {

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



  internallb.command("delete [serviceName] [internalLBName]").usage("[options] [serviceName] [internalLBName]").description($("Delete an internal load balancer for a cloud service deployment")).option("-r, --serviceName <serviceName>", $("the cloud service name")).option("-n, --internalLBName <internalLBName>", $("the internal load balancer name")).option("-q, --quiet", $("quiet mode, do not ask for delete confirmation")).option("-s, --subscription <id>", $("the subscription id")).execute(function __3(serviceName, internalLBName, options, _) { var computeClient, progress, deployment; var __frame = { name: "__3", line: 127 }; return __func(_, this, arguments, __3, 3, __frame, function __$__3() {







      return cli.interaction.promptIfNotGiven($("Cloud Service name: "), serviceName, __cb(_, __frame, 1, 36, function ___(__0, __2) { serviceName = __2;
        return cli.interaction.promptIfNotGiven($("Internal Load Balancer name: "), internalLBName, __cb(_, __frame, 2, 39, function ___(__0, __3) { internalLBName = __3;

          computeClient = utils.createComputeClient(profile.current.getSubscription(options.subscription));
          progress = cli.interaction.progress($("Getting cloud service deployment")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__3() {


                return computeClient.deployments.getBySlot(serviceName, "Production", __cb(_, __frame, 8, 47, function ___(__0, __4) { deployment = __4; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__3() {

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


              loadBalancer = findLoadBalancer(deployment.loadBalancers, internalLBName); return (function __$__3(__then) {
                if (loadBalancer) { return (function __$__3(_) {
                    var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return cli.interaction.confirm(util.format($("Delete internal load balancer with name %s? [y/n] "), internalLBName), __cb(_, __frame, 15, 47, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -126, 18, function ___(__0, __5) { return (function __$__3(__then) { if (__5) { return _(null); } else { __then(); } ; })(function __$__3() {



                      progress = cli.interaction.progress($("Deleting internal load balancer")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__3() {

                            return computeClient.loadBalancers.deleteMethod(serviceName, deployment.name, loadBalancer.name, __cb(_, __frame, 21, 38, function __$__3() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__3() {

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


                  log.info($("No matching internal load balancer defined")); __then(); } ; })(_); }); }); }, true)); }, true)); }); });



  internallb.command("set [serviceName] [internalLBName]").description($("Update internal load balancer of a cloud service deployment")).option("-r, --serviceName <serviceName>", $("the cloud service name")).option("-n, --internalLBName <internalLBName>", $("the internal load balancer name")).option("-t, --subnet-name <name>", $("the name of the subnet in the virtual network that the load balancer uses")).option("-a, --static-virtualnetwork-ipaddress <ip-address>", $("specific virtual IP address that the load balancer uses from the subnet in the virtual network")).option("-d, --remove-subnet", $("remove subnet entry if it is already defined for the internal load balancer")).option("-e, --remove-vnetip", $("remove virtual network IP address if it is already defined for the internal load balancer")).option("-s, --subscription <id>", $("the subscription id")).execute(function __4(serviceName, internalLBName, options, _) { var computeClient, progress, deployment; var __frame = { name: "__4", line: 166 }; return __func(_, this, arguments, __4, 3, __frame, function __$__4() {










      if ((((!options.subnetName && !options.staticVirtualnetworkIpaddress) && !options.removeSubnet) && !options.removeVnetip)) {
        return _(new Error($("one of the optional argument --subnet-name, --static-virtualnetwork-ipaddress, --remove-subnet or --remove-vnetip is required"))); } ;


      if (options.removeSubnet) {
        if ((options.subnetName || options.staticVirtualnetworkIpaddress)) {
          return _(new Error($("--subnet-name or --static-virtualnetwork-ipaddress are not allowed when --remove-subnet is specified"))); } ; } ;



      if (options.removeVnetip) {
        if (options.staticVirtualnetworkIpaddress) {
          return _(new Error($("--static-virtualnetwork-ipaddress not allowed when --remove-vnetip is specified"))); } ; } ;



      return cli.interaction.promptIfNotGiven($("Cloud Service name: "), serviceName, __cb(_, __frame, 18, 36, function ___(__0, __1) { serviceName = __1;
        return cli.interaction.promptIfNotGiven($("Internal Load Balancer name: "), internalLBName, __cb(_, __frame, 19, 39, function ___(__0, __2) { internalLBName = __2;

          computeClient = utils.createComputeClient(profile.current.getSubscription(options.subscription));
          progress = cli.interaction.progress($("Getting cloud service deployment")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__4() {



                return computeClient.deployments.getBySlot(serviceName, "Production", __cb(_, __frame, 26, 47, function ___(__0, __3) { deployment = __3; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__4() {

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


              loadBalancer = findLoadBalancer(deployment.loadBalancers, internalLBName); return (function __$__4(__then) {

                if (loadBalancer) {
                  if (options.removeSubnet) {
                    loadBalancer.frontendIPConfiguration.subnetName = null;
                    loadBalancer.frontendIPConfiguration.staticVirtualNetworkIPAddress = null; } ;


                  if (options.removeVnetip) {
                    loadBalancer.frontendIPConfiguration.staticVirtualNetworkIPAddress = null; } ;


                  if (options.subnetName) {
                    loadBalancer.frontendIPConfiguration.subnetName = options.subnetName; } ;


                  if (options.staticVirtualnetworkIpaddress) {
                    if (!loadBalancer.frontendIPConfiguration.subnetName) {
                      return _(new Error($("no existing subnet defined for this internal load balancer, --subnet-name is required to set static virtual network ip address"))); } ;


                    loadBalancer.frontendIPConfiguration.staticVirtualNetworkIPAddress = options.staticVirtualnetworkIpaddress; } ;

                  progress = cli.interaction.progress($("Updating internal load balancer")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__4() {

                        return computeClient.loadBalancers.update(serviceName, deployment.name, loadBalancer.name, loadBalancer, __cb(_, __frame, 56, 38, function __$__4() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__4() {

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


                  log.info($("No matching internal load balancer defined")); __then(); } ; })(_); }); }); }, true)); }, true)); }); });



  function findLoadBalancer(loadBalancers, internalLBName) {
    loadBalancers = (loadBalancers ? loadBalancers : []);
    var loadBalancer;
    for (var j = 0; (j < loadBalancers.length); j++) {
      if (utils.ignoreCaseEquals(loadBalancers[j].name, internalLBName)) {
        loadBalancer = loadBalancers[j];
        break; } ; };


    return loadBalancer; };};
