/*** 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 insightsWebtestAlertsRulesCommand = cli.category("insights").category("alerts").category("rule").category("webtest").description($("Creates webtest alerts rules"));


  insightsWebtestAlertsRulesCommand.command("set <ruleName> <location> <resourceGroup> <windowSize> <failedLocationCount> <metricName> <webtestResourceUri>").description($("Create a webtest alert rule.")).usage("[options] <ruleName> <location> <resourceGroup> <windowSize> <failedLocationCount> <metricName> <webtestResourceUri>").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("-m --metricName <metricName>", $("The metric name.")).option("--windowSize <windowSize>", $("The time window size. Expected format hh:mm:ss.")).option("-f --failedLocationCount <failedLocationCount>", $("The failed location count.")).option("-i --webtestResourceUri <webtestResourceUri>", $("The webtest resource Id.")).option("-y --metricNamespace <metricNamespace>", $("The metric namespace.")).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, failedLocationCount, metricName, webtestResourceUri, options, _) { var __frame = { name: "__1", line: 51 }; return __func(_, this, arguments, __1, 8, __frame, function __$__1() {





















      return insightsWebtestAlertsRulesCommand._prepareAndExecuteSet(ruleName, location, resourceGroup, windowSize, failedLocationCount, metricName, webtestResourceUri, options, __cb(_, __frame, 1, 40, function __$__1() { _(); }, true)); }); });



  insightsWebtestAlertsRulesCommand._prepareAndExecuteSet = function insightsWebtestAlertsRulesCommand__prepareAndExecuteSet__2(ruleName, location, resourceGroup, windowSize, failedLocationCount, metricName, webtestResourceUri, options, _) { var client, parameters, __this = this; var __frame = { name: "insightsWebtestAlertsRulesCommand__prepareAndExecuteSet__2", line: 56 }; return __func(_, this, arguments, insightsWebtestAlertsRulesCommand__prepareAndExecuteSet__2, 8, __frame, function __$insightsWebtestAlertsRulesCommand__prepareAndExecuteSet__2() {
      log.silly(ruleName);
      log.silly(location);
      log.silly(resourceGroup);
      log.silly(windowSize);
      log.silly(failedLocationCount);
      log.silly(metricName);
      log.silly(webtestResourceUri);
      log.silly(util.inspect(options));

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


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


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


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


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


      client = insightsUtils.createInsightsManagementClient(log, options);
      parameters = __this._createSdkCallParameters(ruleName, location, windowSize, failedLocationCount, metricName, webtestResourceUri, options);

      log.silly(util.format("Parameters: %s", util.inspect(parameters)));

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


  insightsWebtestAlertsRulesCommand._createLocationThresholdRuleCondition = function(windowSize, failedLocationCount, metricName, webtestResourceUri, options) {
    if (windowSize) {
      windowSize = insightsUtils.validateTimeSpan(windowSize); }
     else {
      windowSize = insightsUtils.defaultWindowSize; } ;


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

      failedLocationCount: failedLocationCount,
      windowSize: windowSize,
      type: "Microsoft.Azure.Management.Insights.Models.LocationThresholdRuleCondition" }; };



  insightsWebtestAlertsRulesCommand._createSdkCallParameters = function(ruleName, location, windowSize, failedLocationCount, metricName, webtestResourceUri, options) {
    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 condition = this._createLocationThresholdRuleCondition(windowSize, failedLocationCount, metricName, webtestResourceUri, options);
    var parameters = {
      location: location,
      properties: {
        name: ruleName,
        isEnabled: !options.disabled,
        description: (((__.isUndefined(options.description) || __.isNull(options.description))) ? "" : options.description),
        lastUpdatedTime: new Date(),
        condition: condition,
        actions: internalActions },

      tags: { } };


    parameters.tags[("hidden-link:" + webtestResourceUri)] = "Resource";

    return parameters; };


  insightsWebtestAlertsRulesCommand._executeSetCmd = function insightsWebtestAlertsRulesCommand__executeSetCmd__3(client, ruleName, resourceGroup, parameters, options, _) { var progress, response; var __frame = { name: "insightsWebtestAlertsRulesCommand__executeSetCmd__3", line: 143 }; return __func(_, this, arguments, insightsWebtestAlertsRulesCommand__executeSetCmd__3, 5, __frame, function __$insightsWebtestAlertsRulesCommand__executeSetCmd__3() {
      progress = cli.interaction.progress(util.format($("Creating or updating a webtest alert rule \"%s\""), ruleName));
      response = null; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$insightsWebtestAlertsRulesCommand__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 __$insightsWebtestAlertsRulesCommand__executeSetCmd__3() {

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


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