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


















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

var $ = utils.getLocaleString;

exports.init = function(cli) {
  var log = cli.output;
  var withProgress = cli.interaction.withProgress.bind(cli.interaction);

  var policy = cli.category("policy").description($("Commands to manage your policies on ARM Resources."));

  var definition = policy.category("definition").description($("Commands to manage your policy definitions."));


  definition.command("create [name] [policy]").description($("Creates a new policy definition")).usage("[options] <name> <policy>").option("-n --name <name>", $("the policy definition name")).fileRelatedOption("-p --policy <policy>", $("the rule for policy definition. This should be a path to a file name containing the rule. More information here: https://azure.microsoft.com/en-us/documentation/articles/resource-manager-policy/")).option("--policy-string <policyString>", $("a JSON-formatted string containing the policy rule.")).option("-d --display-name <display-name>", $("the display name for policy definition.")).option("--description <description>", $("the description for policy definition.")).option("--subscription <subscription>", $("the subscription identifier")).execute(function __1(name, policy, options, _) { var subscription, client, policyContent, policyDefinitionParameters, policyDefinition; var __frame = { name: "__1", line: 45 }; return __func(_, this, arguments, __1, 3, __frame, function __$__1() {









      if (!name) {
        return _(null, cli.missingArgument("name")); } ;

      if ((!policy && !options.policyString)) {
        return _(new Error("Please specify one of the required parameters: --policy or --policy-string.")); } ;


      if ((policy && options.policyString)) {
        return _(new Error("Please specify either --policy or --policy-string, not both.")); } ;


      subscription = profile.current.getSubscription(options.subscription);
      client = utils.createPolicyClient(subscription);
      policyContent = (policy ? jsonlint.parse(utils.stripBOM(fs.readFileSync(policy))) : jsonlint.parse(options.policyString));

      policyDefinitionParameters = {
        policyRule: policyContent };


      if (options.displayName) {
        policyDefinitionParameters.displayName = options.displayName; } ;

      if (options.description) {
        policyDefinitionParameters.description = options.description; } ;


      policyDefinition = null; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__1() {

            return withProgress(util.format($("Creating policy definition %s"), name), function __1(log, _) { var __frame = { name: "__1", line: 75 }; return __func(_, this, arguments, __1, 1, __frame, function __$__1() {

                return client.policyDefinitions.createOrUpdate(name, policyDefinitionParameters, __cb(_, __frame, 1, 42, _, true)); }); }, __cb(_, __frame, 29, 27, function ___(__0, __2) { policyDefinition = __2; __then(); }, true)); }); })(function ___(ex, __result) { __catch(function __$__1() { if (ex) {


              return _(new Error(util.format($("Policy definition creation failed: %s"), ex))); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, function __$__1() {


          cli.interaction.formatOutput(policyDefinition, function(data) {
            if (data) {
              displayAPolicyDefinition(data, log); } ; }); _(); }); }); }); });




  definition.command("set [name]").description($("Updates a policy definition")).usage("[options] <name>").option("-n --name <name>", $("the policy definition name")).fileRelatedOption("-p --policy <policy>", $("the rule for policy definition. This should be a path to a file name containing the rule. More information here: https://azure.microsoft.com/en-us/documentation/articles/resource-manager-policy/")).option("--policy-string <policyString>", $("a JSON-formatted string containing the policy rule.")).option("-d --display-name <display-name>", $("the display name for policy definition.")).option("--description <description>", $("the description for policy definition.")).option("--subscription <subscription>", $("the subscription identifier")).execute(function __2(name, options, _) { var subscription, client, policyDefinition, policyDefinitionParameters, policyContent, policyDefinitionUpdated; var __frame = { name: "__2", line: 98 }; return __func(_, this, arguments, __2, 2, __frame, function __$__2() {









      if (!name) {
        return _(null, cli.missingArgument("name")); } ;


      if ((policy && options.policyString)) {
        return _(new Error("Please specify either --policy or --policy-string, not both.")); } ;


      subscription = profile.current.getSubscription(options.subscription);
      client = utils.createPolicyClient(subscription);

      policyDefinition = null; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__2() {

            return withProgress(util.format($("Getting existing policy definition %s"), name), function __1(log, _) { var __frame = { name: "__1", line: 113 }; return __func(_, this, arguments, __1, 1, __frame, function __$__1() {

                return client.policyDefinitions.get(name, null, __cb(_, __frame, 1, 42, _, true)); }); }, __cb(_, __frame, 14, 27, function ___(__0, __3) { policyDefinition = __3; __then(); }, true)); }); })(function ___(ex, __result) { __catch(function __$__2() { if (ex) {


              return _(new Error(util.format($("Policy definition get failed: %s"), ex))); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, function __$__2() {


          policyDefinitionParameters = {
            policyRule: policyDefinition.policyRule,
            displayName: (options.displayName ? options.displayName : policyDefinition.displayName),
            description: (options.description ? options.description : policyDefinition.description) };



          if (options.policy) {
            policyContent = jsonlint.parse(utils.stripBOM(fs.readFileSync(options.policy))); } else {
            if (options.policyString) {
              policyContent = jsonlint.parse(options.policyString); } ; } ;


          if (policyContent) {
            policyDefinitionParameters.policyRule = policyContent; } ;


          policyDefinitionUpdated = null; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__2() {

                return withProgress(util.format($("Updating policy definition %s"), name), function __2(log, _) { var __frame = { name: "__2", line: 140 }; return __func(_, this, arguments, __2, 1, __frame, function __$__2() {

                    return client.policyDefinitions.createOrUpdate(name, policyDefinitionParameters, __cb(_, __frame, 1, 42, _, true)); }); }, __cb(_, __frame, 41, 34, function ___(__0, __4) { policyDefinitionUpdated = __4; __then(); }, true)); }); })(function ___(ex, __result) { __catch(function __$__2() { if (ex) {


                  return _(new Error(util.format($("Policy definition update failed: %s"), ex))); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, function __$__2() {


              cli.interaction.formatOutput(policyDefinitionUpdated, function(data) {
                if (data) {
                  displayAPolicyDefinition(data, log); } ; }); _(); }); }); }); }); }); });




  definition.command("list").description($("Lists all the policy definitions in the subscription.")).option("--subscription <subscription>", $("the subscription identifier")).execute(function __3(options, _) { var subscription, client, policyDefinitions; var __frame = { name: "__3", line: 157 }; return __func(_, this, arguments, __3, 1, __frame, function __$__3() {



      subscription = profile.current.getSubscription(options.subscription);
      client = utils.createPolicyClient(subscription);

      policyDefinitions = null; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__3() {

            return withProgress($("Listing all policy definitions"), function __1(log, _) { var __frame = { name: "__1", line: 164 }; return __func(_, this, arguments, __1, 1, __frame, function __$__1() {

                return client.policyDefinitions.list(null, __cb(_, __frame, 1, 42, _, true)); }); }, __cb(_, __frame, 6, 28, function ___(__0, __2) { policyDefinitions = __2; __then(); }, true)); }); })(function ___(ex, __result) { __catch(function __$__3() { if (ex) {


              return _(new Error(util.format($("Policy definition list failed: %s"), ex))); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, function __$__3() {


          cli.interaction.formatOutput(policyDefinitions, function(data) {
            if (data) {
              for (var i = 0; (i < data.length); i++) {
                displayAPolicyDefinition(data[i], log);
                log.data(""); }; } ; }); _(); }); }); }); });





  definition.command("show [name]").description($("Shows a policy definition")).usage("[options] <name>").option("-n --name <name>", $("the policy definition name")).option("--subscription <subscription>", $("the subscription identifier")).execute(function __4(name, options, _) { var subscription, client, policyDefinition; var __frame = { name: "__4", line: 186 }; return __func(_, this, arguments, __4, 2, __frame, function __$__4() {





      if (!name) {
        return _(null, cli.missingArgument("name")); } ;


      subscription = profile.current.getSubscription(options.subscription);
      client = utils.createPolicyClient(subscription);

      policyDefinition = null; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__4() {

            return withProgress(util.format($("Getting policy definition %s"), name), function __1(log, _) { var __frame = { name: "__1", line: 197 }; return __func(_, this, arguments, __1, 1, __frame, function __$__1() {

                return client.policyDefinitions.get(name, null, __cb(_, __frame, 1, 42, _, true)); }); }, __cb(_, __frame, 10, 27, function ___(__0, __2) { policyDefinition = __2; __then(); }, true)); }); })(function ___(ex, __result) { __catch(function __$__4() { if (ex) {


              return _(new Error(util.format($("Policy definition get failed: %s"), ex))); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, function __$__4() {


          cli.interaction.formatOutput(policyDefinition, function(data) {
            if (data) {
              displayAPolicyDefinition(data, log); } ; }); _(); }); }); }); });




  definition.command("delete [name]").description($("Deletes a policy definition")).usage("[options] <name>").option("-n --name <name>", $("the policy definition name")).option("-q, --quiet", $("quiet mode (do not ask for delete confirmation)")).option("--subscription <subscription>", $("the subscription identifier")).execute(function __5(name, options, _) { var subscription, client, progress; var __frame = { name: "__5", line: 217 }; return __func(_, this, arguments, __5, 2, __frame, function __$__5() {






      if (!name) {
        return _(null, cli.missingArgument("name")); } ; return (function __$__5(_) {


        var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return cli.interaction.confirm(util.format($("Delete policy definition %s? [y/n] "), name), __cb(_, __frame, 5, 45, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -216, 17, function ___(__0, __2) { return (function __$__5(__then) { if (__2) { return _(null); } else { __then(); } ; })(function __$__5() {



          subscription = profile.current.getSubscription(options.subscription);
          client = utils.createPolicyClient(subscription);

          progress = cli.interaction.progress(util.format($("Deleting policy definition %s"), name)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__5() {

                return client.policyDefinitions.deleteMethod(name, __cb(_, __frame, 14, 33, function __$__5() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__5() {

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



  function displayAPolicyDefinition(policyDefinition, log) {
    log.data($("PolicyName:            "), policyDefinition.name);
    log.data($("PolicyDefinitionId:    "), policyDefinition.id);
    log.data($("PolicyType:            "), policyDefinition.policyType);
    log.data($("DisplayName:           "), policyDefinition.displayName);
    log.data($("Description:           "), policyDefinition.description);
    log.data($("PolicyRule:            "), policyDefinition.policyRule); };};
