/*** 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 fs = require("fs");
















var path = require("path");
var util = require("util");
var async = require("async");

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

var $ = utils.getLocaleString;

exports.init = function(cli) {
  var certificates = cli.category("service").category("cert").description($("Commands to manage your Cloud Services certificates"));


  var log = cli.output;

  function getCertificate(serviceName, thumbprint, thumbprintAlgorithm, options, _) { var service, certificate, progress, params, certificates; var __frame = { name: "getCertificate", line: 33 }; return __func(_, this, arguments, getCertificate, 4, __frame, function __$getCertificate() {
      service = utils.createComputeClient(profile.current.getSubscription(options.subscription));
      certificate = null;


      progress = cli.interaction.progress($("Getting cloud service certificate")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$getCertificate() {

            params = { serviceName: serviceName, thumbprint: thumbprint, thumbprintAlgorithm: thumbprintAlgorithm };
            return service.serviceCertificates.get(params, __cb(_, __frame, 8, 48, function ___(__0, __1) { certificate = __1; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$getCertificate() {

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


          certificates = [certificate,];
          return _(null, certificates); }); }); }); };


  function getCertificates(serviceName, options, _) { var service, certificates, progress, hostedServices, serviceCertificates, certificate; var __frame = { name: "getCertificates", line: 50 }; return __func(_, this, arguments, getCertificates, 2, __frame, function __$getCertificates() {
      service = utils.createComputeClient(profile.current.getSubscription(options.subscription));

      certificates = []; return (function __$getCertificates(__then) {


        if (!serviceName) {

          progress = cli.interaction.progress($("Getting cloud services")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$getCertificates() {

                return service.hostedServices.list(__cb(_, __frame, 10, 48, function ___(__0, __1) { hostedServices = __1.hostedServices; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$getCertificates() {

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


                if (hostedServices) {
                  progress = cli.interaction.progress($("Getting cloud service certificates")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$getCertificates() {

                        return async.map(hostedServices, function(hostedService, callback) {
                          service.serviceCertificates.list(hostedService.serviceName, function(err, result) {
                            if (err) { return callback(err); } ;

                            for (var certificate in result.certificates) {
                              result.certificates[certificate].serviceName = hostedService.serviceName; };


                            callback(err, result.certificates); }); }, __cb(_, __frame, 18, 42, function ___(__0, __2) { serviceCertificates = __2;



                          certificates = certificates.concat.apply(certificates, serviceCertificates); _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$getCertificates() {

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



          progress = cli.interaction.progress($("Getting cloud service certificates")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$getCertificates() {

                return service.serviceCertificates.list(serviceName, __cb(_, __frame, 38, 51, function ___(__0, __3) { certificates = __3.certificates; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$getCertificates() {

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


              for (certificate in certificates) {
                certificates[certificate].serviceName = serviceName; }; __then(); }); }); } ; })(function __$getCertificates() {



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


  certificates.command("list").description($("List Azure certificates")).option("--serviceName <serviceName>", $("the cloud service name")).option("-s, --subscription <id>", $("the subscription id")).execute(function __1(options, _) { var certificates; var __frame = { name: "__1", line: 105 }; return __func(_, this, arguments, __1, 1, __frame, function __$__1() {




      return getCertificates(options.serviceName, options, __cb(_, __frame, 1, 25, function ___(__0, __1) { certificates = __1;

        if ((certificates && (certificates.length > 0))) {
          cli.interaction.formatOutput(certificates, function(outputData) {
            log.table(outputData, function(row, item) {
              row.cell($("Service Name"), item.serviceName);
              row.cell($("Thumbprint"), item.thumbprint);
              row.cell($("Algorithm"), item.thumbprintAlgorithm); }); }); }


         else {
          log.info($("No certificates defined")); } ; _(); }, true)); }); });



  certificates.command("get").description($("Get Azure certificate")).option("--service-name <serviceName>", $("the cloud service name")).option("--thumbprint <thumbprint>", $("the thumbprint")).option("--thumbprint-algorithm <thumbprint-algorithm>", $("the thumbprint algorithm")).option("-s, --subscription <id>", $("the subscription id")).execute(function __2(options, _) { var certificate; var __frame = { name: "__2", line: 127 }; return __func(_, this, arguments, __2, 1, __frame, function __$__2() {






      return getCertificate(options.serviceName, options.thumbprint, options.thumbprintAlgorithm, options, __cb(_, __frame, 1, 24, function ___(__0, __1) { certificate = __1;

        if ((certificate && (certificate.length > 0))) {
          cli.interaction.formatOutput(certificate, function(outputData) {
            log.table(outputData, function(row, item) {
              row.cell($("Service Name"), item.serviceName);
              row.cell($("Thumbprint"), item.thumbprint);
              row.cell($("Algorithm"), item.thumbprintAlgorithm); }); }); }


         else {
          log.info($("No certificate defined")); } ; _(); }, true)); }); });



  certificates.command("create <dns-name> <file> [password]").usage("[options] <dns-name> <file> [password]").description($("Upload certificate")).option("-s, --subscription <id>", $("the subscription id")).execute(function __3(dnsName, file, password, options, _) { var dnsPrefix, service, certificateOptions, progress; var __frame = { name: "__3", line: 147 }; return __func(_, this, arguments, __3, 4, __frame, function __$__3() {




      dnsPrefix = utils.getDnsPrefix(dnsName);

      service = utils.createComputeClient(profile.current.getSubscription(options.subscription));

      if ((path.extname(file) !== ".pfx")) {
        return _(new Error("Certificates need to have a pfx extension")); } ;


      certificateOptions = {
        password: password,
        certificateFormat: path.extname(file).split(".")[1],
        data: fs.readFileSync(file) };


      progress = cli.interaction.progress($("Creating certificate")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__3() {


            return service.serviceCertificates.create(dnsPrefix, certificateOptions, __cb(_, __frame, 18, 36, 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(_, function __$__3() { _(); }); }); }); });



  certificates.command("delete <thumbprint>").description($("Delete certificate")).option("-d, --dns-name <name>", $("indicates to only look for certs for this DNS name")).option("-q, --quiet", $("quiet mode, do not ask for delete confirmation")).option("-s, --subscription <id>", $("the subscription id")).execute(function __4(thumbprint, options, _) { var service, dnsPrefix, certificates, progress; var __frame = { name: "__4", line: 176 }; return __func(_, this, arguments, __4, 2, __frame, function __$__4() {





      service = utils.createComputeClient(profile.current.getSubscription(options.subscription));


      if (options.dnsName) {
        dnsPrefix = utils.getDnsPrefix(options.dnsName); } ;


      return getCertificates(dnsPrefix, options, __cb(_, __frame, 8, 25, function ___(__0, __2) { certificates = __2;

        certificates = certificates.filter(function(certificate) {
          return utils.ignoreCaseEquals(certificate.thumbprint, thumbprint); }); return (function __$__4(__then) {


          if ((certificates && (certificates.length > 0))) { return (function __$__4(_) {
              var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return cli.interaction.confirm(util.format($("Delete certificate with thumbprint %s? [y/n] "), thumbprint), __cb(_, __frame, 15, 47, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -175, 18, function ___(__0, __3) { return (function __$__4(__then) { if (__3) { return _(null); } else { __then(); } ; })(function __$__4() {



                progress = cli.interaction.progress($("Deleting certificates")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__4() {


                      return async.each(certificates, function(certificate, callback) {
                        service.serviceCertificates.deleteMethod(certificate, callback); }, __cb(_, __frame, 22, 16, 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); }); }); }, true)); } else {


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