public AddAzureRmWebtestAlertRuleTests()
        {
            insightsAlertRuleOperationsMock = new Mock<IAlertOperations>();
            insightsManagementClientMock = new Mock<InsightsManagementClient>();
            commandRuntimeMock = new Mock<ICommandRuntime>();
            cmdlet = new AddAzureRmWebtestAlertRuleCommand()
            {
                CommandRuntime = commandRuntimeMock.Object,
                InsightsManagementClient = insightsManagementClientMock.Object
            };

            response = new AzureOperationResponse()
            {
                RequestId = Guid.NewGuid().ToString(),
                StatusCode = HttpStatusCode.OK,
            };

            insightsAlertRuleOperationsMock.Setup(f => f.CreateOrUpdateRuleAsync(It.IsAny<string>(), It.IsAny<RuleCreateOrUpdateParameters>(), It.IsAny<CancellationToken>()))
                .Returns(Task.FromResult<AzureOperationResponse>(response))
                .Callback((string resourceGrp, RuleCreateOrUpdateParameters createOrUpdateParams, CancellationToken t) =>
                {
                    resourceGroup = resourceGrp;
                    createOrUpdatePrms = createOrUpdateParams;
                });

            insightsManagementClientMock.SetupGet(f => f.AlertOperations).Returns(this.insightsAlertRuleOperationsMock.Object);
        }
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Insights.IAlertOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group.
 /// </param>
 /// <param name='parameters'>
 /// Required. The rule to create or update.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static OperationResponse CreateOrUpdateRule(this IAlertOperations operations, string resourceGroupName, RuleCreateOrUpdateParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IAlertOperations)s).CreateOrUpdateRuleAsync(resourceGroupName, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Insights.IAlertOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group.
 /// </param>
 /// <param name='parameters'>
 /// Required. The rule to create or update.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static Task<OperationResponse> CreateOrUpdateRuleAsync(this IAlertOperations operations, string resourceGroupName, RuleCreateOrUpdateParameters parameters)
 {
     return operations.CreateOrUpdateRuleAsync(resourceGroupName, parameters, CancellationToken.None);
 }
Esempio n. 4
0
        private static void CreateOrUpdateAlert(
            InsightsManagementClient insightsClient,
            string resourceGroupName,
            string insightsResourceUri,
            string webTestUri)
        {
            string webTestName = GetNameFromUri(webTestUri);
            string alertName = string.Format("{0}-alert", webTestName);

            var parameters = new RuleCreateOrUpdateParameters
            {
                Location = "East US",
                Properties = new Rule()
                {
                    Name = alertName,
                    Description = string.Empty,
                    IsEnabled = true,
                    LastUpdatedTime = DateTime.UtcNow,
                    Action = new RuleEmailAction
                    {
                        SendToServiceOwners = true
                    },
                    Condition = new LocationThresholdRuleCondition
                    {
                        DataSource = new RuleMetricDataSource
                        {
                            MetricName = "GSMT_AvRaW",
                            ResourceUri = webTestUri
                        },
                        FailedLocationCount = 1,
                        WindowSize = TimeSpan.FromMinutes(5)
                    },
                },
                Tags =
                {
                    { string.Format("hidden-link:{0}", insightsResourceUri), "Resource" },
                    { string.Format("hidden-link:{0}", webTestUri), "Resource" }
                }
            };

            var alertCreateOrUpdateResult = insightsClient.AlertOperations.CreateOrUpdateRuleAsync(
                resourceGroupName,
                parameters).Result;

            if (alertCreateOrUpdateResult.StatusCode != HttpStatusCode.Created && alertCreateOrUpdateResult.StatusCode != HttpStatusCode.OK)
            {
                throw new InvalidOperationException(
                    string.Format("Unable to create resource '{0}' (HTTP Status Code: {1}).",
                    insightsResourceUri,
                    alertCreateOrUpdateResult.StatusCode));
            }
        }
 private void AreEqual(RuleCreateOrUpdateParameters exp, RuleCreateOrUpdateParameters act)
 {
     if (exp != null)
     {
         Assert.Equal(exp.Location, act.Location);
         AreEqual(exp.Properties, act.Properties);
         AreEqual(exp.Tags, act.Tags);
     }
 }
        protected override RuleCreateOrUpdateParameters CreateSdkCallParameters()
        {
            RuleCondition condition = this.CreateRuleCondition();

            WriteVerboseWithTimestamp(string.Format("CreateSdkCallParameters: Creating rule object"));
            var rule = new RuleCreateOrUpdateParameters()
            {
                Location = this.Location,
                Properties = new Rule()
                {
                    Name = this.Name,
                    IsEnabled = !this.DisableRule,
                    Description = this.Description ?? Utilities.GetDefaultDescription("log alert rule"),
                    LastUpdatedTime = DateTime.Now,
                    Condition = condition,
                    Actions = this.Actions,
                },

                // DO NOT REMOVE OR CHANGE the following. The two elements in the Tags are required by other services.
                Tags = new LazyDictionary<string, string>(),
            };

            if (!string.IsNullOrEmpty(this.TargetResourceId))
            {
                rule.Tags.Add("$type", "Microsoft.WindowsAzure.Management.Common.Storage.CasePreservedDictionary,Microsoft.WindowsAzure.Management.Common.Storage");
                rule.Tags.Add("hidden-link:" + this.TargetResourceId, "Resource");
            }

            return rule;
        }