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















var util = require("util");
var utils = require("../../../util/utils");
var $ = utils.getLocaleString;
var constants = require("./constants");
var VNetUtil = require("../../../util/vnet.util");

function Nsg(cli, networkManagementClient) {
  this.networkManagementClient = networkManagementClient;
  this.vnetUtil = new VNetUtil();
  this.output = cli.output;
  this.interaction = cli.interaction;};


__.extend(Nsg.prototype, {
  create: function create__1(nsgName, location, options, _) { var self, nsgProfile, progress, __this = this; var __frame = { name: "create__1", line: 31 }; return __func(_, this, arguments, create__1, 3, __frame, function __$create__1() { self = __this;

      nsgProfile = {
        name: nsgName,
        location: location };


      if (options.label) { nsgProfile.label = options.label; } ;

      progress = self.interaction.progress(util.format($("Creating a network security group \"%s\""), nsgName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$create__1() {

            return self.networkManagementClient.networkSecurityGroups.create(nsgProfile, __cb(_, __frame, 11, 57, function __$create__1() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$create__1() {

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

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


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

      progress = self.interaction.progress($("Getting the network security groups"));

      groups = null; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$list__2() {

            return self.networkManagementClient.networkSecurityGroups.list(__cb(_, __frame, 6, 66, function ___(__0, __1) { groups = __1; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$list__2() {

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


          self.interaction.formatOutput(groups.networkSecurityGroups, function(data) {
            if ((data.length === 0)) {
              self.output.warn($("No network security groups found")); }
             else {
              self.output.table(data, function(row, nsg) {
                row.cell($("Name"), nsg.name);
                row.cell($("Location"), nsg.location);
                row.cell($("Label"), (nsg.label || "")); }); } ; }); _(); }); }); }); },





  show: function show__3(nsgName, options, _) { var self, nsg, __this = this; var __frame = { name: "show__3", line: 73 }; return __func(_, this, arguments, show__3, 2, __frame, function __$show__3() { self = __this;

      return self.get(nsgName, true, __cb(_, __frame, 2, 19, function ___(__0, __1) { nsg = __1;

        if (nsg) {
          self.interaction.formatOutput(nsg, function(nsg) {
            self.output.nameValue($("Name"), nsg.name);
            self.output.nameValue($("Location"), nsg.location);
            self.output.nameValue($("Label"), nsg.label);

            if ((nsg.rules.length > 0)) {
              self.output.header($("Security group rules"));
              self.output.table(nsg.rules, function(row, rule) {
                row.cell($("Name"), rule.name);
                row.cell($("Source IP"), rule.sourceAddressPrefix);
                row.cell($("Source Port"), rule.sourcePortRange);
                row.cell($("Destination IP"), rule.destinationAddressPrefix);
                row.cell($("Destination Port"), rule.destinationPortRange);
                row.cell($("Protocol"), rule.protocol);
                row.cell($("Type"), rule.type);
                row.cell($("Action"), rule.action);
                row.cell($("Priority"), rule.priority);
                row.cell($("Default"), (rule.isDefault || "false")); }); } ; }); }



         else {
          if (self.output.format().json) {
            self.output.json({ }); }
           else {
            self.output.warn(util.format($("A network security group with name \"%s\" not found"), nsgName)); } ; } ; _(); }, true)); }); },




  delete: function delete__4(nsgName, options, _) { var self, progress, __this = this; var __frame = { name: "delete__4", line: 108 }; return __func(_, this, arguments, delete__4, 2, __frame, function __$delete__4() { self = __this; return (function __$delete__4(_) {

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



          progress = self.interaction.progress(util.format($("Deleting network security group \"%s\""), nsgName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$delete__4() {

                return self.networkManagementClient.networkSecurityGroups.deleteMethod(nsgName, __cb(_, __frame, 8, 57, function __$delete__4() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$delete__4() {

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



  get: function get__5(nsgName, withRules, _) { var self, detailLevel, progress, nsg, __this = this; var __frame = { name: "get__5", line: 122 }; return __func(_, this, arguments, get__5, 2, __frame, function __$get__5() { self = __this;

      detailLevel = null;
      if (withRules) { detailLevel = "Full"; } ;
      progress = self.interaction.progress(util.format($("Looking up the network security group \"%s\""), nsgName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$get__5() { return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$get__5() {

                  return self.networkManagementClient.networkSecurityGroups.get(nsgName, detailLevel, __cb(_, __frame, 6, 67, function ___(__0, __1) { nsg = __1;
                    return _(null, nsg); }, true)); }); })(function ___(e, __result) { __catch(function __$get__5() { if (e) {

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

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

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



  createRule: function createRule__6(nsgName, ruleName, options, _) { var self, ruleProfile, nsg, rule, progress, __this = this; var __frame = { name: "createRule__6", line: 140 }; return __func(_, this, arguments, createRule__6, 3, __frame, function __$createRule__6() { self = __this;

      ruleProfile = self._parseSecurityRule(options, true);

      return self.get(nsgName, false, __cb(_, __frame, 4, 19, function ___(__0, __1) { nsg = __1;
        if (!nsg) {
          return _(new Error(util.format($("A network security group with name \"%s\" not found"), nsgName))); } ;


        rule = self._findSecurityRule(nsg, ruleName);
        if (rule) {
          return _(new Error(util.format($("A network security rule with name \"%s\" already exists in the network security group \"%s\""), ruleName, nsgName))); } ;


        progress = self.interaction.progress(util.format($("Creating a network security rule \"%s\""), ruleName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$createRule__6() {

              return self.networkManagementClient.networkSecurityGroups.setRule(nsgName, ruleName, ruleProfile, __cb(_, __frame, 16, 57, function __$createRule__6() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$createRule__6() {

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

            return self.showRule(nsgName, ruleName, options, __cb(_, __frame, 20, 9, function __$createRule__6() { _(); }, true)); }); }); }, true)); }); },


  setRule: function setRule__7(nsgName, ruleName, options, _) { var self, ruleProfile, nsg, rule, progress, __this = this; var __frame = { name: "setRule__7", line: 163 }; return __func(_, this, arguments, setRule__7, 3, __frame, function __$setRule__7() { self = __this;

      ruleProfile = self._parseSecurityRule(options, false);

      return self.get(nsgName, true, __cb(_, __frame, 4, 19, function ___(__0, __1) { nsg = __1;
        if (!nsg) {
          return _(new Error(util.format($("A network security group with name \"%s\" not found"), nsgName))); } ;


        rule = self._findSecurityRule(nsg, ruleName);
        if (!rule) {
          return _(new Error(util.format($("A network security rule with name \"%s\" not found in the security group \"%s\""), ruleName, nsgName))); } ;


        if (options.protocol) { rule.protocol = ruleProfile.protocol; } ;
        if (options.sourceAddressPrefix) { rule.sourceAddressPrefix = ruleProfile.sourceAddressPrefix; } ;
        if (options.sourcePortRange) { rule.sourcePortRange = ruleProfile.sourcePortRange; } ;
        if (options.destinationAddressPrefix) { rule.destinationAddressPrefix = ruleProfile.destinationAddressPrefix; } ;
        if (options.destinationPortRange) { rule.destinationPortRange = ruleProfile.destinationPortRange; } ;
        if (options.action) { rule.action = ruleProfile.action; } ;
        if (options.priority) { rule.priority = ruleProfile.priority; } ;
        if (options.type) { rule.type = ruleProfile.type; } ;

        progress = self.interaction.progress(util.format($("Setting a network security rule \"%s\""), ruleName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$setRule__7() {

              return self.networkManagementClient.networkSecurityGroups.setRule(nsgName, ruleName, rule, __cb(_, __frame, 25, 57, function __$setRule__7() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$setRule__7() {

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

            return self.showRule(nsgName, ruleName, options, __cb(_, __frame, 29, 9, function __$setRule__7() { _(); }, true)); }); }); }, true)); }); },


  listRules: function listRules__8(nsgName, options, _) { var self, nsg, __this = this; var __frame = { name: "listRules__8", line: 195 }; return __func(_, this, arguments, listRules__8, 2, __frame, function __$listRules__8() { self = __this;

      return self.get(nsgName, true, __cb(_, __frame, 2, 19, function ___(__0, __1) { nsg = __1;
        if (!nsg) {
          return _(new Error(util.format($("A network security group with name \"%s\" not found"), nsgName))); } ;


        self.interaction.formatOutput(nsg.rules, function(data) {
          if ((data.length === 0)) {
            self.output.warn($("No rules found")); }
           else {
            self.output.table(data, function(row, rule) {
              row.cell($("Name"), rule.name);
              row.cell($("Source address prefix"), rule.sourceAddressPrefix);
              row.cell($("Source port range"), rule.sourcePortRange);
              row.cell($("Destination address prefix"), rule.destinationAddressPrefix);
              row.cell($("Destination port range"), rule.destinationPortRange);
              row.cell($("Protocol"), rule.protocol);
              row.cell($("Type"), rule.type);
              row.cell($("Action"), rule.action);
              row.cell($("Priority"), rule.priority); }); } ; }); _(); }, true)); }); },





  showRule: function showRule__9(nsgName, ruleName, options, _) { var self, nsg, rule, __this = this; var __frame = { name: "showRule__9", line: 221 }; return __func(_, this, arguments, showRule__9, 3, __frame, function __$showRule__9() { self = __this;

      return self.get(nsgName, constants.nsg.levelDef, __cb(_, __frame, 2, 19, function ___(__0, __1) { nsg = __1;

        if (!nsg) {
          return _(new Error(util.format($("A network security group with name \"%s\" not found"), nsgName))); } ;


        rule = self._findSecurityRule(nsg, ruleName);
        if (!rule) {
          return _(new Error(util.format($("A network security rule with name \"%s\" not found in the security group \"%s\""), ruleName, nsgName))); } ;


        self.interaction.formatOutput(rule, function(rule) {
          if ((rule === null)) {
            self.output.warn(util.format($("A network security rule with name \"%s\" not found in the security group \"%s\""), ruleName, nsgName)); }
           else {
            self.output.nameValue($("Name"), rule.name);
            self.output.nameValue($("State"), rule.state);
            self.output.nameValue($("Source address prefix"), rule.sourceAddressPrefix);
            self.output.nameValue($("Source port range"), rule.sourcePortRange);
            self.output.nameValue($("Destination address prefix"), rule.destinationAddressPrefix);
            self.output.nameValue($("Destination port range"), rule.destinationPortRange);
            self.output.nameValue($("Protocol"), rule.protocol);
            self.output.nameValue($("Type"), rule.type);
            self.output.nameValue($("Action"), rule.action);
            self.output.nameValue($("Priority"), rule.priority); } ; }); _(); }, true)); }); },




  deleteRule: function deleteRule__10(nsgName, ruleName, options, _) { var self, nsg, rule, progress, __this = this; var __frame = { name: "deleteRule__10", line: 252 }; return __func(_, this, arguments, deleteRule__10, 3, __frame, function __$deleteRule__10() { self = __this;

      return self.get(nsgName, constants.nsg.levelDef, __cb(_, __frame, 2, 19, function ___(__0, __2) { nsg = __2;
        if (!nsg) {
          return _(new Error(util.format($("A network security group with name \"%s\" not found"), nsgName))); } ;


        rule = self._findSecurityRule(nsg, ruleName);
        if (!rule) {
          return _(new Error(util.format($("A network security group rule with name \"%s\" not found in the security group \"%s\""), ruleName, nsgName))); } ; return (function __$deleteRule__10(_) {


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



            progress = self.interaction.progress(util.format($("Deleting network security rule \"%s\""), ruleName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$deleteRule__10() {

                  return self.networkManagementClient.networkSecurityGroups.deleteRule(nsgName, ruleName, __cb(_, __frame, 18, 57, function __$deleteRule__10() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$deleteRule__10() {

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



  _validateAddressPrefix: function(prefix, paramName) {
    var self = this;
    if (((prefix === "*") || (prefix === "\"*\""))) {
      return prefix; } ;

    try {
      var res = utils.verifyParamExistsInCollection(constants.nsg.prefix, prefix, paramName);
      return res;
    } catch (e) {
      var ipValidationResult = self.vnetUtil.parseIPv4Cidr(prefix);
      if (ipValidationResult.error) {
        throw new Error(util.format($("%s parameter must be in CIDR format. Asterisk, INTERNET, VIRTUAL_NETWORK, AZURE_LOADBALANCER can be used also."), paramName)); } ;

      return ipValidationResult.ipv4Cidr; }; },



  _validatePortRange: function(port, paramName) {
    if ((((port === "*") || (port === "\"*\"")) || !isNaN(port))) {
      return port; } ;

    var rangePattern = /^[\d]+\s*-\s*[\d]+$/;
    var match = rangePattern.test(port);
    if (!match) {
      throw new Error(util.format($("%s parameter must be a valid port or port range between %s and %s. Asterisk can be used also. Example: 80, 80-81, *"), paramName, constants.nsg.portMin, constants.nsg.portMax)); } ;


    return port; },


  _parseSecurityRule: function(options, useDefaults) {
    var self = this;
    var rule = { };

    if (options.protocol) {
      if (((options.protocol === "*") || (options.protocol === "\"*\""))) {
        rule.protocol = options.protocol; }
       else {
        rule.protocol = utils.verifyParamExistsInCollection(constants.nsg.protocols, options.protocol, "--protocol"); } ; }

     else if (useDefaults) {
      self.output.warn(util.format($("Using default protocol: %s"), constants.nsg.protocols[0]));
      rule.protocol = constants.nsg.protocols[0]; }  ;


    if (options.sourcePortRange) {
      rule.sourcePortRange = self._validatePortRange(options.sourcePortRange, "--source-port-range"); }
     else if (useDefaults) {
      self.output.warn(util.format($("Using default source port range: %s"), constants.nsg.portDef));
      rule.sourcePortRange = constants.nsg.portDef; }  ;


    if (options.destinationPortRange) {
      rule.destinationPortRange = self._validatePortRange(options.destinationPortRange, "--destination-port-range"); }
     else if (useDefaults) {
      self.output.warn(util.format($("Using default destination port range: %s"), constants.nsg.portDef));
      rule.destinationPortRange = constants.nsg.portDef; }  ;


    if (options.sourceAddressPrefix) {
      rule.sourceAddressPrefix = self._validateAddressPrefix(options.sourceAddressPrefix, "--source-address-prefix"); }
     else if (useDefaults) {
      self.output.warn(util.format($("Using default source address prefix: %s"), constants.nsg.prefixDef));
      rule.sourceAddressPrefix = constants.nsg.prefixDef; }  ;


    if (options.destinationAddressPrefix) {
      rule.destinationAddressPrefix = self._validateAddressPrefix(options.destinationAddressPrefix, "--destination-address-prefix"); }
     else if (useDefaults) {
      self.output.warn(util.format($("Using default destination address prefix: %s"), constants.nsg.prefixDef));
      rule.destinationAddressPrefix = constants.nsg.prefixDef; }  ;


    if (options.action) {
      rule.action = utils.verifyParamExistsInCollection(constants.nsg.action, options.action, "--action"); }
     else if (useDefaults) {
      self.output.warn(util.format($("Using default action: %s"), constants.nsg.action[0]));
      rule.action = constants.nsg.action[0]; }  ;


    if (options.priority) {
      var priority = utils.parseInt(options.priority);
      if (((isNaN(priority) || (priority < constants.nsg.priorityMin)) || (priority > constants.nsg.priorityMax))) {
        throw new Error(util.format($("--priority must be an integer between %s and %s"), constants.nsg.priorityMin, constants.nsg.priorityMax)); } ;

      rule.priority = priority; }
     else if (useDefaults) {
      self.output.warn(util.format($("Using default priority: %s"), constants.nsg.priorityMin));
      rule.priority = constants.nsg.priorityMin; }  ;


    if (options.type) {
      rule.type = utils.verifyParamExistsInCollection(constants.nsg.type, options.type, "--type"); }
     else if (useDefaults) {
      self.output.warn(util.format($("Using default type: %s"), constants.nsg.type[0]));
      rule.type = constants.nsg.type[0]; }  ;


    return rule; },


  _findSecurityRule: function(nsg, ruleName) {
    return utils.findFirstCaseIgnore(nsg.rules, { name: ruleName }); }});



module.exports = Nsg;
