/*** 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,__tryCatch=__rt.__tryCatch; var __ = require("underscore");

















var util = require("util");
var utils = require("../../../util/utils");
var insightsUtils = require("./insights.utils");

var $ = utils.getLocaleString;

exports.init = function(cli) {
  var log = cli.output;
  var insightsMetricAlertsRulesCommand = cli.category("insights").category("alerts").category("rule").category("metric").description($("Creates metric-based alerts rules"));


  insightsMetricAlertsRulesCommand.command("set <ruleName> <location> <resourceGroup> <windowSize> <operator> <threshold> <targetResourceId> <metricName> <timeAggregationOperator>").description($("Create or set a metric alert rule.")).usage("[options] <ruleName> <location> <resourceGroup> <windowSize> <operator> <threshold> <targetResourceId> <metricName> <timeAggregationOperator>").option("-x --disable", $("Flag to disable the rule.")).option("-s --subscription <subscription>", $("The subscription identifier.")).option("-n --ruleName <ruleName>", $("The name of the rule.")).option("-d --description <description>", $("The description of the rule.")).option("-l --location <location>", $("The location.")).option("-g --resourceGroup <resourceGroup>", $("The resource group.")).option("--windowSize <windowSize>", $("The time window size. Expected format hh:mm:ss.")).option("-o --operator <operator>", $("The condition operator: GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual. Value is case insensitive.")).option("-a --threshold <threshold>", $("The threshold.")).option("-i --targetResourceId <targetResourceId>", $("The target resource Id.")).option("-m --metricName <metricName>", $("The metric name.")).option("--timeAggregationOperator <timeAggregationOperator>", $("The time aggregation operator: Average, Minimum, Maximum, Total. Value is case insensitve.")).option("-z --actions <actions>", $("The list of alert rule actions. The list must be a json object (string) of an array. Example: \"[{\\\"customEmails\\\":[\\\"gu@ms.com\\\"]},{\\\"serviceUri\\\":\\\"http://foo.com\\\",\\\"properties\\\":[{\\\"key\\\":\\\"key1\\\",\\\"value\\\":\\\"value1\\\"},{\\\"key\\\":\\\"value1\\\",\\\"value\\\":\\\"key2\\\"}]}]")).execute(function __1(ruleName, location, resourceGroup, windowSize, operator, threshold, targetResourceId, metricName, timeAggregationOperator, options, _) { var __frame = { name: "__1", line: 53 }; return __func(_, this, arguments, __1, 10, __frame, function __$__1() {























      return insightsMetricAlertsRulesCommand._prepareAndExecuteSet(ruleName, location, resourceGroup, windowSize, operator, threshold, targetResourceId, metricName, timeAggregationOperator, options, __cb(_, __frame, 1, 39, function __$__1() { _(); }, true)); }); });


  insightsMetricAlertsRulesCommand._prepareAndExecuteSet = function insightsMetricAlertsRulesCommand__prepareAndExecuteSet__2(ruleName, location, resourceGroup, windowSize, operator, threshold, targetResourceId, metricName, timeAggregationOperator, options, _) { var client, parameters, __this = this; var __frame = { name: "insightsMetricAlertsRulesCommand__prepareAndExecuteSet__2", line: 57 }; return __func(_, this, arguments, insightsMetricAlertsRulesCommand__prepareAndExecuteSet__2, 10, __frame, function __$insightsMetricAlertsRulesCommand__prepareAndExecuteSet__2() {
      log.silly(ruleName);
      log.silly(location);
      log.silly(resourceGroup);
      log.silly(windowSize);
      log.silly(operator);
      log.silly(threshold);
      log.silly(targetResourceId);
      log.silly(metricName);
      log.silly(timeAggregationOperator);
      log.silly(util.inspect(options));

      if (!__.isString(ruleName)) {
        cli.missingArgument("ruleName"); } ;


      if (!__.isString(location)) {
        cli.missingArgument("location"); } ;


      if (!__.isString(resourceGroup)) {
        cli.missingArgument("resourceGroup"); } ;


      client = insightsUtils.createInsightsManagementClient(log, options);
      parameters = __this._createSdkCallParameters(ruleName, location, metricName, targetResourceId, operator, threshold, timeAggregationOperator, windowSize, options);

      return __this._executeSetCmd(client, ruleName, resourceGroup, parameters, options, __cb(_, __frame, 27, 9, function __$insightsMetricAlertsRulesCommand__prepareAndExecuteSet__2() { _(); }, true)); }); };


  insightsMetricAlertsRulesCommand._createThresholdRuleCondition = function(metricName, targetResourceId, operator, threshold, timeAggregationOperator, windowSize) {
    if (!__.isString(metricName)) {
      cli.missingArgument("metricName"); } ;


    if (!__.isString(targetResourceId)) {
      cli.missingArgument("targetResourceId"); } ;


    if (!__.isString(operator)) {
      cli.missingArgument("operator"); }
     else {
      var operatorTemp = operator.toLowerCase();
      if (((((operatorTemp != "greaterthan") && (operatorTemp != "greaterthanorequal")) && (operatorTemp != "lessthan")) && (operatorTemp != "lessthanorequal"))) {
        throw new Error(util.format($("Invalid condition operator: %s"), operator)); } ; } ;



    if (!__.isString(threshold)) {
      cli.missingArgument("threshold"); }
     else {
      threshold = parseFloat(threshold); } ;


    if (!__.isString(timeAggregationOperator)) {
      timeAggregationOperator = insightsUtils.defaultTimeAggregationOperator; }
     else {
      var tempOperator = timeAggregationOperator.toLowerCase();
      if (((((tempOperator != "average") && (tempOperator != "minimum")) && (tempOperator != "maximum")) && (tempOperator != "total"))) {
        throw new Error(util.format($("Invalid time aggregation operator: %s"), timeAggregationOperator)); } ; } ;



    if (windowSize) {
      windowSize = insightsUtils.validateTimeSpan(windowSize); }
     else {
      windowSize = insightsUtils.defaultWindowSize; } ;


    return {
      dataSource: {
        metricName: metricName,
        resourceUri: targetResourceId,
        type: "Microsoft.Azure.Management.Insights.Models.RuleMetricDataSource" },

      operator: operator,
      threshold: threshold,
      timeAggregation: timeAggregationOperator,
      windowSize: windowSize,
      type: "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition" }; };



  insightsMetricAlertsRulesCommand._createSdkCallParameters = function(ruleName, location, metricName, targetResourceId, operator, threshold, timeAggregationOperator, windowSize, options) {
    var condition = this._createThresholdRuleCondition(metricName, targetResourceId, operator, threshold, timeAggregationOperator, windowSize);

    var internalActions = [];
    if ((!__.isUndefined(options.actions) && !__.isNull(options.actions))) {
      internalActions = JSON.parse(options.actions);
      log.silly(util.format("Parsed actions: %s", util.inspect(internalActions)));
      if (!__.isArray(internalActions)) {
        throw new Error($("Invalid actions argument: array expected.")); } ; } ;



    var parameters = {
      location: location,
      properties: {
        name: ruleName,
        isEnabled: !options.disabled,
        description: options.description,
        lastUpdatedTime: new Date(),
        condition: condition,
        actions: internalActions },

      tags: { } };


    if (targetResourceId) {
      parameters.tags["$type"] = "Microsoft.WindowsAzure.Management.Common.Storage.CasePreservedDictionary,Microsoft.WindowsAzure.Management.Common.Storage";
      parameters.tags[("hidden-link:" + targetResourceId)] = "Resource"; } ;


    return parameters; };



  insightsMetricAlertsRulesCommand._executeSetCmd = function insightsMetricAlertsRulesCommand__executeSetCmd__3(client, ruleName, resourceGroup, parameters, options, _) { var progress, response; var __frame = { name: "insightsMetricAlertsRulesCommand__executeSetCmd__3", line: 174 }; return __func(_, this, arguments, insightsMetricAlertsRulesCommand__executeSetCmd__3, 5, __frame, function __$insightsMetricAlertsRulesCommand__executeSetCmd__3() {
      progress = cli.interaction.progress(util.format($("Creating or updating a metric alert rule \"%s\""), ruleName));
      response = null; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$insightsMetricAlertsRulesCommand__executeSetCmd__3() {

            return client.alertOperations.createOrUpdateRule(resourceGroup, parameters, __cb(_, __frame, 4, 40, function ___(__0, __1) { response = __1;


              log.silly((!response ? util.inspect(response) : "nothing in response")); _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$insightsMetricAlertsRulesCommand__executeSetCmd__3() {

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


          insightsUtils.formatOutput(cli, log, options, response); _(); }); }); }); };};
