/*** Generated by streamline 0.10.17 (callbacks) - DO NOT EDIT ***/ var __rt=require('streamline/lib/callbacks/runtime').runtime(__filename, false),__func=__rt.__func,__cb=__rt.__cb,__catch=__rt.__catch,__tryCatch=__rt.__tryCatch; var __ = require("underscore");















var util = require("util");
var utils = require("../../../util/utils");
var constants = require("./constants");
var validation = require("../../../util/validation");
var $ = utils.getLocaleString;
var tagUtils = require("../tag/tagUtils");
var resourceUtils = require("../resource/resourceUtils");

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


__.extend(RouteTable.prototype, {



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


      parameters = {
        location: options.location };


      parameters = self._parseRouteTable(parameters, options);

      return self.get(resourceGroupName, routeTableName, __cb(_, __frame, 9, 26, function ___(__0, __1) { routeTable = __1;
        if (routeTable) {
          return _(new Error(util.format($("A Route Table with name \"%s\" already exists in the resource group \"%s\""), routeTableName, resourceGroupName))); } ;


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

              return self.networkManagementClient.routeTables.createOrUpdate(resourceGroupName, routeTableName, parameters, __cb(_, __frame, 16, 60, function ___(__0, __2) { routeTable = __2; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$create__1() {

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

            self._showRouteTable(routeTable, resourceGroupName, routeTableName); _(); }); }); }, true)); }); },


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


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


        routeTable = self._parseRouteTable(routeTable, options);

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

              return self.networkManagementClient.routeTables.createOrUpdate(resourceGroupName, routeTableName, routeTable, __cb(_, __frame, 12, 60, function ___(__0, __2) { routeTable = __2; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$set__2() {

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

            self._showRouteTable(routeTable, resourceGroupName, routeTableName); _(); }); }); }, true)); }); },


  show: function show__3(resourceGroupName, routeTableName, options, _) { var self, routeTable, __this = this; var __frame = { name: "show__3", line: 77 }; return __func(_, this, arguments, show__3, 3, __frame, function __$show__3() { self = __this;

      return self.get(resourceGroupName, routeTableName, __cb(_, __frame, 2, 26, function ___(__0, __1) { routeTable = __1;

        self.interaction.formatOutput(routeTable, function(routeTable) {
          if (!routeTable) {
            self.output.warn(util.format($("A route table with name \"%s\" not found in the resource group \"%s\""), routeTableName, resourceGroupName)); }
           else {
            self._showRouteTable(routeTable, resourceGroupName, routeTableName); } ; }); _(); }, true)); }); },




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


      progress = self.interaction.progress(("Looking up Route Tables")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$list__4() { return (function __$list__4(__then) {



              if (options.resourceGroup) {
                return self.networkManagementClient.routeTables.list(options.resourceGroup, __cb(_, __frame, 8, 63, function ___(__0, __1) { routeTables = __1; __then(); }, true)); } else {

                return self.networkManagementClient.routeTables.listAll(__cb(_, __frame, 10, 63, function ___(__0, __2) { routeTables = __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() {


          self.interaction.formatOutput(routeTables, function(routeTables) {
            if ((routeTables.length === 0)) {
              self.output.warn($("No route tables found")); }
             else {
              self.output.table(routeTables, function(row, routeTable) {
                row.cell($("Name"), routeTable.name);
                row.cell($("Location"), routeTable.location);
                var resInfo = resourceUtils.getResourceInformation(routeTable.id);
                row.cell($("Resource group"), resInfo.resourceGroup);
                row.cell($("Routes number"), routeTable.routes.length);
                row.cell($("Subnets number"), (routeTable.subnets ? routeTable.subnets.length : "0")); }); } ; }); _(); }); }); }); },





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

      return self.get(resourceGroupName, routeTableName, __cb(_, __frame, 2, 26, function ___(__0, __2) { routeTable = __2;

        if (!routeTable) {
          return _(new Error(util.format($("Route table \"%s\" not found in the resource group \"%s\""), routeTableName, resourceGroupName))); } ; return (function __$delete__5(_) {


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

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

      progress = self.interaction.progress(util.format($("Looking up Route Table \"%s\""), routeTableName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$get__6() { return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$get__6() {

                  return self.networkManagementClient.routeTables.get(resourceGroupName, routeTableName, null, __cb(_, __frame, 4, 64, function ___(__0, __1) { routeTable = __1;
                    return _(null, routeTable); }, 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() { _(); }); }); }); },






  createRoute: function createRoute__7(resourceGroupName, routeTableName, routeName, options, _) { var self, parameters, routeTable, route, progress, __this = this; var __frame = { name: "createRoute__7", line: 161 }; return __func(_, this, arguments, createRoute__7, 4, __frame, function __$createRoute__7() { self = __this;


      parameters = { };
      parameters = self._parseRoute(parameters, options);

      return self.get(resourceGroupName, routeTableName, __cb(_, __frame, 6, 26, function ___(__0, __1) { routeTable = __1;
        if (!routeTable) {
          return _(new Error(util.format($("A Route Table with name \"%s\" not found in the resource group \"%s\""), routeTableName, resourceGroupName))); } ;


        return self.getRoute(resourceGroupName, routeTableName, routeName, __cb(_, __frame, 11, 21, function ___(__0, __2) { route = __2;
          if (route) {
            return _(new Error(util.format($("A route with name \"%s\" already exists in a route table \"%s\""), routeName, routeTableName))); } ;


          progress = self.interaction.progress(util.format($("Creating route \"%s\" in a route table \"%s\""), routeName, routeTableName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$createRoute__7() {

                return self.networkManagementClient.routes.createOrUpdate(resourceGroupName, routeTableName, routeName, parameters, __cb(_, __frame, 18, 50, function ___(__0, __3) { route = __3; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$createRoute__7() {

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

              self._showRoute(route, routeTableName, routeName); _(); }); }); }, true)); }, true)); }); },


  setRoute: function setRoute__8(resourceGroupName, routeTableName, routeName, options, _) { var self, routeTable, route, progress, __this = this; var __frame = { name: "setRoute__8", line: 186 }; return __func(_, this, arguments, setRoute__8, 4, __frame, function __$setRoute__8() { self = __this;


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


        return self.getRoute(resourceGroupName, routeTableName, routeName, __cb(_, __frame, 8, 21, function ___(__0, __2) { route = __2;
          if (!route) {
            return _(new Error(util.format($("A route with name \"%s\" not found in the Route Table \"%s\""), routeName, routeTableName))); } ;


          route = self._parseRoute(route, options);

          progress = self.interaction.progress(util.format($("Updating route \"%s\" in a route table \"%s\""), routeName, routeTableName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$setRoute__8() {

                return self.networkManagementClient.routes.createOrUpdate(resourceGroupName, routeTableName, routeName, route, __cb(_, __frame, 17, 50, function ___(__0, __3) { route = __3; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$setRoute__8() {

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

              self._showRoute(route, routeTableName, routeName); _(); }); }); }, true)); }, true)); }); },


  listRoutes: function listRoutes__9(resourceGroupName, routeTableName, options, _) { var self, routeTable, progress, routes, __this = this; var __frame = { name: "listRoutes__9", line: 210 }; return __func(_, this, arguments, listRoutes__9, 3, __frame, function __$listRoutes__9() { self = __this;


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


        progress = self.interaction.progress(util.format($("Looking up routes in a Route Table \"%s\""), routeTableName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$listRoutes__9() {



              return self.networkManagementClient.routes.list(resourceGroupName, routeTableName, __cb(_, __frame, 12, 51, function ___(__0, __2) { routes = __2; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$listRoutes__9() {

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


            self.interaction.formatOutput(routes, function(routes) {
              if ((routes.length === 0)) {
                self.output.warn(util.format($("No routes found in the Route Table \"%s\""), routeTableName)); }
               else {
                self.output.table(routes, function(row, route) {
                  row.cell($("Name"), route.name);
                  row.cell($("Address prefix"), route.addressPrefix);
                  row.cell($("Next hop type"), route.nextHopType);
                  row.cell($("Next hop IP address"), (route.nextHopIpAddress || "")); }); } ; }); _(); }); }); }, true)); }); },





  showRoute: function showRoute__10(resourceGroupName, routeTableName, routeName, options, _) { var self, routeTable, route, __this = this; var __frame = { name: "showRoute__10", line: 241 }; return __func(_, this, arguments, showRoute__10, 4, __frame, function __$showRoute__10() { self = __this;


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


        return self.getRoute(resourceGroupName, routeTableName, routeName, __cb(_, __frame, 8, 21, function ___(__0, __2) { route = __2;
          self._showRoute(route, routeTableName, routeName); _(); }, true)); }, true)); }); },


  deleteRoute: function deleteRoute__11(resourceGroupName, routeTableName, routeName, options, _) { var self, routeTable, route, progress, __this = this; var __frame = { name: "deleteRoute__11", line: 253 }; return __func(_, this, arguments, deleteRoute__11, 4, __frame, function __$deleteRoute__11() { self = __this;


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


        return self.getRoute(resourceGroupName, routeTableName, routeName, __cb(_, __frame, 8, 21, function ___(__0, __3) { route = __3;
          if (!route) {
            return _(new Error(util.format($("A route with name \"%s\" not found"), routeName))); } ; return (function __$deleteRoute__11(_) {


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



              progress = self.interaction.progress(util.format($("Deleting route \"%s\""), routeName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$deleteRoute__11() {

                    return self.networkManagementClient.routes.deleteMethod(resourceGroupName, routeTableName, routeName, __cb(_, __frame, 19, 42, function __$deleteRoute__11() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$deleteRoute__11() {

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



  getRoute: function getRoute__12(resourceGroupName, routeTableName, routeName, _) { var self, progress, route, __this = this; var __frame = { name: "getRoute__12", line: 278 }; return __func(_, this, arguments, getRoute__12, 3, __frame, function __$getRoute__12() { self = __this;

      progress = self.interaction.progress(util.format($("Looking up route \"%s\" in route table \"%s\""), routeName, routeTableName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$getRoute__12() { return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$getRoute__12() {

                  return self.networkManagementClient.routes.get(resourceGroupName, routeTableName, routeName, __cb(_, __frame, 4, 54, function ___(__0, __1) { route = __1;
                    return _(null, route); }, true)); }); })(function ___(e, __result) { __catch(function __$getRoute__12() { if (e) {

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

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

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






  _parseRouteTable: function(routeTable, options) {
    if (options.tags) {
      if (utils.argHasValue(options.tags)) {
        tagUtils.appendTags(routeTable, options); }
       else {
        routeTable.tags = { }; } ; } ;



    return routeTable; },


  _parseRoute: function(route, options) {
    if (options.addressPrefix) {
      route.addressPrefix = validation.isCIDR(options.addressPrefix, "--address-prefix"); } ;


    if (options.nextHopIpAddress) {
      route.nextHopIpAddress = validation.isIP(options.nextHopIpAddress, "--next-hop-ip-address"); } ;


    if (options.nextHopType) {
      route.nextHopType = validation.isIn(options.nextHopType, constants.route.nextHopType, "--next-hop-type");
      if (!utils.ignoreCaseEquals(options.nextHopType, constants.route.nextHopType[0])) {
        delete route.nextHopIpAddress; } ; } ;



    return route; },


  _showRouteTable: function(routeTable, resourceGroupName, routeTableName) {
    var self = this;

    self.interaction.formatOutput(routeTable, function(routeTable) {
      if (!routeTable) {
        self.output.warn(util.format($("A route table with name \"%s\" not found in the resource group \"%s\""), routeTableName, resourceGroupName));
        return; } ;


      self.output.nameValue($("Id"), routeTable.id);
      self.output.nameValue($("Name"), routeTable.name);
      self.output.nameValue($("Type"), routeTable.type);
      self.output.nameValue($("Location"), routeTable.location);
      self.output.nameValue($("Provisioning state"), routeTable.provisioningState);
      self.output.nameValue($("Tags"), tagUtils.getTagsInfo(routeTable.tags));

      if ((routeTable.subnets && (routeTable.subnets.length > 0))) {
        self.output.header($("Subnets"));
        routeTable.subnets.forEach(function(subnet) {
          self.output.nameValue($("Id"), subnet.id, 2); }); } ;


      if ((routeTable.routes && (routeTable.routes.length > 0))) {
        self.output.header($("Routes"));
        routeTable.routes.forEach(function(route) {
          self.output.nameValue($("Name"), route.name, 2);
          self.output.nameValue($("Address prefix"), route.addressPrefix, 2);
          self.output.nameValue($("Next hop type"), route.nextHopType, 2);
          self.output.nameValue($("Next hop IP address"), route.nextHopIpAddress, 2);
          self.output.data(""); }); } ; }); },





  _showRoute: function(route, routeTableName, routeName) {
    var self = this;

    self.interaction.formatOutput(route, function(route) {
      if (!route) {
        self.output.warn(util.format($("A route with name \"%s\" not found in table \"%s\""), routeName, routeTableName));
        return; } ;


      self.output.nameValue($("Id"), route.id);
      self.output.nameValue($("Name"), route.name);
      self.output.nameValue($("Provisioning state"), route.provisioningState);
      self.output.nameValue($("Next hop type"), route.nextHopType);
      self.output.nameValue($("Next hop IP address"), route.nextHopIpAddress);
      self.output.nameValue($("Address prefix"), route.addressPrefix); }); }});




module.exports = RouteTable;
