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

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


__.extend(PublicIp.prototype, {



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


      parameters = {
        name: publicIpName,
        location: options.location };


      parameters = self._parsePublicIP(parameters, options, true);

      return self.get(resourceGroupName, publicIpName, __cb(_, __frame, 10, 24, function ___(__0, __1) { publicIp = __1;
        if (publicIp) {
          return _(new Error(util.format($("A public ip address with name \"%s\" already exists in the resource group \"%s\""), publicIpName, resourceGroupName))); } ;


        progress = self.interaction.progress(util.format($("Creating public ip address \"%s\""), publicIpName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$create__1() {

              return self.networkManagementClient.publicIPAddresses.createOrUpdate(resourceGroupName, publicIpName, parameters, __cb(_, __frame, 17, 64, function ___(__0, __2) { publicIp = __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._showPublicIP(publicIp, resourceGroupName, publicIpName); _(); }); }); }, true)); }); },


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


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


        publicIp = self._parsePublicIP(publicIp, options, false);

        progress = self.interaction.progress(util.format($("Updating public ip address \"%s\""), publicIpName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$set__2() {

              return self.networkManagementClient.publicIPAddresses.createOrUpdate(resourceGroupName, publicIpName, publicIp, __cb(_, __frame, 12, 64, function ___(__0, __2) { publicIp = __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._showPublicIP(publicIp, resourceGroupName, publicIpName); _(); }); }); }, true)); }); },


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

      return self.get(resourceGroupName, publicIpName, __cb(_, __frame, 2, 24, function ___(__0, __1) { publicIp = __1;

        self._showPublicIP(publicIp, resourceGroupName, publicIpName); _(); }, true)); }); },


  delete: function delete__4(resourceGroupName, publicIpName, options, _) { var self, publicIp, progress, __this = this; var __frame = { name: "delete__4", line: 86 }; return __func(_, this, arguments, delete__4, 3, __frame, function __$delete__4() { self = __this;


      return self.get(resourceGroupName, publicIpName, __cb(_, __frame, 3, 24, function ___(__0, __2) { publicIp = __2;
        if (!publicIp) {
          return _(new Error(util.format($("A public ip address with name \"%s\" not found in the resource group \"%s\""), publicIpName, resourceGroupName))); } ; return (function __$delete__4(_) {


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



            progress = self.interaction.progress(util.format($("Deleting public ip address \"%s\""), publicIpName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$delete__4() {

                  return self.networkManagementClient.publicIPAddresses.deleteMethod(resourceGroupName, publicIpName, __cb(_, __frame, 14, 53, 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)); }, true)); }); },



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


      publicIPs = null;
      progress = self.interaction.progress($("Getting the public ip addresses")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$list__5() { return (function __$list__5(__then) {


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

                return self.networkManagementClient.publicIPAddresses.listAll(__cb(_, __frame, 10, 67, function ___(__0, __2) { publicIPs = __2; __then(); }, true)); } ; })(function __$list__5() { _(null, null, true); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$list__5() {


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


          self.interaction.formatOutput(publicIPs, function(publicIPs) {
            if ((publicIPs.length === 0)) {
              self.output.warn($("No public ip addresses found")); }
             else {
              self.output.table(publicIPs, function(row, publicIp) {
                row.cell($("Name"), publicIp.name);
                row.cell($("Location"), publicIp.location);
                var resInfo = resourceUtils.getResourceInformation(publicIp.id);
                row.cell($("Resource group"), resInfo.resourceGroup);
                row.cell($("Provisioning state"), publicIp.provisioningState);
                row.cell($("Allocation method"), publicIp.publicIPAllocationMethod);
                if (publicIp.publicIPAddressVersion) {
                  row.cell($("IP version"), publicIp.publicIPAddressVersion); } ;

                row.cell($("IP Address"), (publicIp.ipAddress || ""));
                row.cell($("Idle timeout, minutes"), (publicIp.idleTimeoutInMinutes || ""));
                var fqdn = (publicIp.dnsSettings ? publicIp.dnsSettings.fqdn : "");
                row.cell($("FQDN"), fqdn); }); } ; }); _(); }); }); }); },





  get: function get__6(resourceGroupName, publicIpName, _) { var self, progress, publicIP, __this = this; var __frame = { name: "get__6", line: 145 }; return __func(_, this, arguments, get__6, 2, __frame, function __$get__6() { self = __this;

      progress = self.interaction.progress(util.format($("Looking up the public ip \"%s\""), publicIpName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$get__6() { return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$get__6() {

                  return self.networkManagementClient.publicIPAddresses.get(resourceGroupName, publicIpName, null, __cb(_, __frame, 4, 68, function ___(__0, __1) { publicIP = __1;
                    return _(null, publicIP); }, 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() { _(); }); }); }); },






  _parsePublicIP: function(publicIp, options, useDefaults) {
    var self = this;

    if (options.idleTimeout) {
      publicIp.idleTimeoutInMinutes = validation.isInt(options.idleTimeout, { }, "--idle-timeout"); }
     else if (useDefaults) {
      publicIp.idleTimeoutInMinutes = utils.takeDefault(self.output, constants.publicIp.defTimeout, "--idle-timeout"); }  ;


    if (options.allocationMethod) {
      publicIp.publicIPAllocationMethod = validation.isIn(options.allocationMethod, constants.publicIp.allocation, "--allocation-method"); }
     else if (useDefaults) {
      publicIp.publicIPAllocationMethod = utils.takeDefault(self.output, constants.publicIp.allocation[0], "--allocation-method"); }  ;


    if (options.domainNameLabel) {
      if (utils.argHasValue(options.domainNameLabel)) {
        if (!publicIp.dnsSettings) { publicIp.dnsSettings = { }; };
        publicIp.dnsSettings.domainNameLabel = options.domainNameLabel; }
       else {
        delete publicIp.dnsSettings; } ; } ;



    if (options.reverseFqdn) {
      if (!publicIp.dnsSettings) { publicIp.dnsSettings = { }; };
      publicIp.dnsSettings.reverseFqdn = options.reverseFqdn; } ;


    if (options.ipVersion) {
      publicIp.publicIPAddressVersion = validation.isIn(options.ipVersion, constants.publicIp.version, "--ip-version"); }
     else if (useDefaults) {
      publicIp.publicIPAddressVersion = utils.takeDefault(self.output, constants.publicIp.version[0], "--ip-version"); }  ;


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



    return publicIp; },


  _showPublicIP: function(publicIp, resourceGroupName, publicIpName) {
    var self = this;

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


      self.output.nameValue($("Id"), publicIp.id);
      self.output.nameValue($("Name"), publicIp.name);
      self.output.nameValue($("Type"), publicIp.type);
      self.output.nameValue($("Location"), publicIp.location);
      self.output.nameValue($("Provisioning state"), publicIp.provisioningState);
      self.output.nameValue($("Tags"), tagUtils.getTagsInfo(publicIp.tags));
      self.output.nameValue($("Allocation method"), publicIp.publicIPAllocationMethod);
      self.output.nameValue($("IP version"), publicIp.publicIPAddressVersion);
      self.output.nameValue($("Idle timeout in minutes"), publicIp.idleTimeoutInMinutes);
      self.output.nameValue($("IP Address"), publicIp.ipAddress);
      if (publicIp.ipConfiguration) {
        self.output.nameValue($("IP configuration id"), publicIp.ipConfiguration.id); } ;

      if (publicIp.dnsSettings) {
        self.output.nameValue($("Domain name label"), publicIp.dnsSettings.domainNameLabel);
        self.output.nameValue($("FQDN"), publicIp.dnsSettings.fqdn); } ; }); }});







module.exports = PublicIp;
