Skip to content

Commit d455130

Browse files
authored
Merge pull request #8853 from shpimpal/SQRCmdlets
SQR (Scheduled Query Rule) PS cmdlets and tests implementation
2 parents d43a228 + db6a1bc commit d455130

File tree

52 files changed

+8582
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+8582
-4
lines changed

src/Monitor/Monitor.Test/Monitor.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
<ItemGroup>
1414
<PackageReference Include="Microsoft.Azure.Management.Monitor" Version="0.22.0-preview" />
15+
<PackageReference Include="Microsoft.Azure.Management.ApplicationInsights" Version="0.2.0-preview" />
1516
</ItemGroup>
1617

1718
<ItemGroup>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.ServiceManagement.Common.Models;
16+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
17+
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
18+
using Xunit;
19+
20+
namespace Microsoft.Azure.Commands.Insights.Test.ScenarioTests
21+
{
22+
public class ScheduledQueryRulesTests : RMTestBase
23+
{
24+
public XunitTracingInterceptor _logger;
25+
26+
public ScheduledQueryRulesTests(Xunit.Abstractions.ITestOutputHelper output)
27+
{
28+
_logger = new XunitTracingInterceptor(output);
29+
XunitTracingInterceptor.AddToContext(_logger);
30+
}
31+
32+
[Fact]
33+
[Trait(Category.AcceptanceType, Category.CheckIn)]
34+
public void TestNewGetUpdateSetRemoveScheduledQueryRule()
35+
{
36+
TestsController.NewInstance.RunPsTest(_logger, "Test-NewGetUpdateSetRemoveScheduledQueryRule");
37+
}
38+
39+
[Fact]
40+
[Trait(Category.AcceptanceType, Category.CheckIn)]
41+
public void TestPipingRemoveSetUpdateScheduledQueryRule()
42+
{
43+
TestsController.NewInstance.RunPsTest(_logger, "Test-PipingRemoveSetUpdateScheduledQueryRule");
44+
}
45+
}
46+
}
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
# ----------------------------------------------------------------------------------
2+
#
3+
# Copyright Microsoft Corporation
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# ----------------------------------------------------------------------------------
14+
15+
# Setup
16+
function Test-setup
17+
{
18+
$global:ruleName = Get-ResourceName
19+
$global:resourceGroupName = Get-ResourceGroupName
20+
$global:location = Get-ProviderLocation("microsoft.insights")
21+
$global:description = "SQR log alert rule"
22+
$global:severity = "2"
23+
$global:throttlingInMin = "5"
24+
$global:enabled = 1
25+
$global:emailSubject = "SQR Log alert trigger notification"
26+
$global:customWebhookPayload = "{}"
27+
$global:thresholdOperator = "GreaterThan"
28+
$global:threshold = 5
29+
$global:metricTriggerType = "Total"
30+
$global:metricTriggerColumn = "timestamp"
31+
$global:frequencyInMin = 5
32+
$global:timeWindowInMin = 5
33+
$global:query = "traces | summarize AggregatedValue = count() by bin(timestamp, 5m)"
34+
35+
# Create resource group
36+
New-AzResourceGroup -Name $resourceGroupName -Location $location -Force
37+
38+
$appInsightsResourceName = Get-ResourceName
39+
40+
# Create the App Insights Resource
41+
$appInsightsResource = New-AzureRmApplicationInsights `
42+
-Name $appInsightsResourceName `
43+
-ResourceGroupName $resourceGroupName `
44+
-Location $location
45+
46+
# Create Action group
47+
$actionGroupName = Get-ResourceName
48+
$email = New-AzActionGroupReceiver -Name 'user1' -EmailReceiver -EmailAddress '[email protected]'
49+
$newActionGroup = Set-AzureRmActionGroup -Name $actionGroupName -ResourceGroup $resourceGroupName -ShortName ASTG -Receiver $email
50+
$actionGroupResource = New-AzActionGroup -ActionGroupId $newActionGroup.Id
51+
$global:actionGroup = @($newActionGroup.Id)
52+
53+
$global:subscription = (Get-AzureRmContext).Subscription
54+
$global:authorizedResources = "/subscriptions/" + $subscription + "/resourceGroups/" + $resourceGroupName + "/providers/microsoft.insights/components/" + $appInsightsResourceName
55+
$global:dataSourceId = $authorizedResources
56+
$global:queryType = "ResultCount"
57+
$global:metricTriggerThreshold = 10
58+
$global:metricTriggerThresholdOperator = "GreaterThan"
59+
60+
$global:tags = @{}
61+
}
62+
63+
function Verify-ScheduledQueryRule($scheduledQueryRule)
64+
{
65+
Assert-NotNull $scheduledQueryRule
66+
67+
Assert-NotNull $scheduledQueryRule.Source
68+
Assert-NotNull $scheduledQueryRule.Schedule
69+
70+
Assert-NotNull $scheduledQueryRule.Action
71+
Assert-NotNull $scheduledQueryRule.Action.Trigger
72+
Assert-NotNull $scheduledQueryRule.Action.Trigger.MetricTrigger
73+
Assert-NotNull $scheduledQueryRule.Action.AznsAction
74+
75+
Assert-AreEqual $scheduledQueryRule.Name $ruleName
76+
#Assert-AreEqual $scheduledQueryRule.Location $location
77+
Assert-AreEqual $scheduledQueryRule.Description $description
78+
79+
Assert-AreEqual $scheduledQueryRule.Action.Severity $severity
80+
Assert-AreEqual $scheduledQueryRule.Action.ThrottlingInMin $throttlingInMin
81+
82+
Assert-AreEqual $scheduledQueryRule.Action.Trigger.Threshold $threshold
83+
Assert-AreEqual $scheduledQueryRule.Action.Trigger.ThresholdOperator $thresholdOperator
84+
85+
Assert-AreEqual $scheduledQueryRule.Action.Trigger.MetricTrigger.Threshold $metricTriggerThreshold
86+
Assert-AreEqual $scheduledQueryRule.Action.Trigger.MetricTrigger.ThresholdOperator $metricTriggerThresholdOperator
87+
Assert-AreEqual $scheduledQueryRule.Action.Trigger.MetricTrigger.MetricTriggerType $metricTriggerType
88+
Assert-AreEqual $scheduledQueryRule.Action.Trigger.MetricTrigger.MetricColumn $metricTriggerColumn
89+
90+
Assert-AreEqual $scheduledQueryRule.Action.AznsAction.ActionGroup $actionGroup
91+
Assert-AreEqual $scheduledQueryRule.Action.AznsAction.EmailSubject $emailSubject
92+
Assert-AreEqual $scheduledQueryRule.Action.AznsAction.CustomWebhookPayload $customWebhookPayload
93+
94+
Assert-AreEqual $scheduledQueryRule.Schedule.FrequencyInMinutes $frequencyInMin
95+
Assert-AreEqual $scheduledQueryRule.Schedule.TimeWindowInMinutes $timeWindowInMin
96+
97+
Assert-AreEqual $scheduledQueryRule.Source.Query $query
98+
Assert-AreEqual $scheduledQueryRule.Source.DataSourceId $dataSourceId
99+
Assert-AreEqual $scheduledQueryRule.Source.AuthorizedResources $authorizedResources
100+
Assert-AreEqual $scheduledQueryRule.Source.QueryType $queryType
101+
102+
}
103+
104+
<#
105+
.SYNOPSIS
106+
107+
#>
108+
function Test-NewGetUpdateSetRemoveScheduledQueryRule
109+
{
110+
Write-Debug "Starting Test-NewGetUpdateSetRemoveScheduledQueryRule"
111+
Test-setup
112+
try
113+
{
114+
$aznsActionGroup = New-AzScheduledQueryRuleAznsActionGroup -ActionGroup $actionGroup -EmailSubject $emailSubject -CustomWebhookPayload $customWebhookPayload
115+
116+
$metricTrigger = New-AzScheduledQueryRuleLogMetricTrigger -ThresholdOperator $metricTriggerthresholdOperator -Threshold $metricTriggerThreshold -MetricTriggerType $metricTriggerType -MetricColumn $metricTriggerColumn
117+
118+
$triggerCondition = New-AzScheduledQueryRuleTriggerCondition -ThresholdOperator $thresholdOperator -Threshold $threshold -MetricTrigger $metricTrigger
119+
120+
$alertingAction = New-AzScheduledQueryRuleAlertingAction -AznsAction $aznsActionGroup -Severity $severity -ThrottlingInMinutes $throttlingInMin -Trigger $triggerCondition
121+
122+
$schedule = New-AzScheduledQueryRuleSchedule -FrequencyInMinutes $frequencyInMin -TimeWindowInMinutes $timeWindowInMin
123+
124+
$source = New-AzScheduledQueryRuleSource -Query $query -DataSourceId $dataSourceId -AuthorizedResource $authorizedResources -QueryType $queryType
125+
126+
$scheduledQueryRule = New-AzScheduledQueryRule -Location $location -Name $ruleName -ResourceGroupName $resourceGroupName -Action $alertingAction -Source $source -Enabled $enabled -Description $description -Schedule $schedule -Tag $tags
127+
128+
Verify-ScheduledQueryRule $scheduledQueryRule
129+
130+
Write-Debug " ****** Getting the Scheduled Query Rule by name"
131+
$retrieved = Get-AzScheduledQueryRule -ResourceGroup $resourceGroupName -Name $ruleName
132+
Assert-NotNull $retrieved
133+
Assert-AreEqual 1 $retrieved.Length
134+
Verify-ScheduledQueryRule $retrieved[0]
135+
136+
Write-Debug " ****** Getting the Scheduled Query Rule by subscriptionId"
137+
$retrieved = Get-AzScheduledQueryRule
138+
Assert-NotNull $retrieved
139+
140+
Write-Debug " ****** Getting the Scheduled Query Rule by resource group"
141+
$retrieved = Get-AzScheduledQueryRule -ResourceGroupName $resourceGroupName
142+
Assert-NotNull $retrieved
143+
Assert-AreEqual 1 $retrieved.Length
144+
Verify-ScheduledQueryRule $retrieved[0]
145+
146+
# testing Set-* cmdlet with same parameters as they were setup, as it is similar to New-*
147+
148+
Write-Debug " ****** Updating Scheduled Query Rule by name (PUT semantics)"
149+
$updated = Set-AzScheduledQueryRule -Location $location -Name $ruleName -ResourceGroupName $resourceGroupName -Action $alertingAction -Source $source -Enabled 1 -Description $description -Schedule $schedule -Tag $tags
150+
Verify-ScheduledQueryRule $scheduledQueryRule
151+
152+
Write-Debug " ****** Updating Scheduled Query Rule by resource Id (PUT semantics)"
153+
$updated = Set-AzScheduledQueryRule -ResourceId $scheduledQueryRule.Id -Location $location -Action $alertingAction -Source $source -Enabled 1 -Description $description -Schedule $schedule -Tag $tags
154+
Verify-ScheduledQueryRule $scheduledQueryRule
155+
156+
Write-Debug " ****** Updating Scheduled Query Rule by InputObject (PUT semantics)"
157+
$updated = Set-AzScheduledQueryRule -InputObject $scheduledQueryRule -Location $location -Action $alertingAction -Source $source -Enabled 1 -Description $description -Schedule $schedule -Tag $tags
158+
Verify-ScheduledQueryRule $scheduledQueryRule
159+
160+
Write-Debug " ****** Updating Scheduled Query Rule by name (PATCH semantics)"
161+
$updated = Update-AzScheduledQueryRule -ResourceGroupName $resourceGroupName -Name $ruleName -Enabled 0
162+
Verify-ScheduledQueryRule $updated
163+
Assert-AreEqual $updated.Enabled false
164+
165+
Write-Debug " ****** Updating Scheduled Query Rule by resource Id (PATCH semantics)"
166+
$updated = Update-AzScheduledQueryRule -ResourceId $scheduledQueryRule.Id -Enabled 0
167+
Verify-ScheduledQueryRule $updated
168+
Assert-AreEqual $updated.Enabled false
169+
170+
Write-Debug " ****** Updating Scheduled Query Rule by InputObject (PATCH semantics)"
171+
$updated = Update-AzScheduledQueryRule -InputObject $scheduledQueryRule -Enabled 0
172+
Verify-ScheduledQueryRule $updated
173+
Assert-AreEqual $updated.Enabled false
174+
175+
Write-Debug " ****** Removing Scheduled Query Rule by name"
176+
Remove-AzScheduledQueryRule -ResourceGroup $resourceGroupName -Name $ruleName
177+
178+
Write-Debug " ****** Removing Scheduled Query Rule by resource Id"
179+
Remove-AzScheduledQueryRule -ResourceId $scheduledQueryRule.Id
180+
181+
Write-Debug " ****** Removing Scheduled Query Rule by InputObject"
182+
Remove-AzScheduledQueryRule -InputObject $scheduledQueryRule
183+
}
184+
catch
185+
{
186+
#Write-Debug $_
187+
throw $_
188+
}
189+
finally
190+
{
191+
# Cleanup
192+
Clean-ResourceGroup($resourceGroupName)
193+
}
194+
}
195+
196+
function Test-PipingRemoveSetUpdateScheduledQueryRule
197+
{
198+
Write-Debug "Starting Test-PipingRemoveSetUpdateScheduledQueryRule"
199+
Test-setup
200+
try
201+
{
202+
$aznsActionGroup = New-AzScheduledQueryRuleAznsActionGroup -ActionGroup $actionGroup -EmailSubject $emailSubject -CustomWebhookPayload $customWebhookPayload
203+
204+
$metricTrigger = New-AzScheduledQueryRuleLogMetricTrigger -ThresholdOperator $metricTriggerthresholdOperator -Threshold $metricTriggerThreshold -MetricTriggerType $metricTriggerType -MetricColumn $metricTriggerColumn
205+
206+
$triggerCondition = New-AzScheduledQueryRuleTriggerCondition -ThresholdOperator $thresholdOperator -Threshold $threshold -MetricTrigger $metricTrigger
207+
208+
$alertingAction = New-AzScheduledQueryRuleAlertingAction -AznsAction $aznsActionGroup -Severity $severity -ThrottlingInMinutes $throttlingInMin -Trigger $triggerCondition
209+
210+
$schedule = New-AzScheduledQueryRuleSchedule -FrequencyInMinutes $frequencyInMin -TimeWindowInMinutes $timeWindowInMin
211+
212+
$source = New-AzScheduledQueryRuleSource -Query $query -DataSourceId $dataSourceId -AuthorizedResource $authorizedResources -QueryType $queryType
213+
214+
$scheduledQueryRule = New-AzScheduledQueryRule -Location $location -Name $ruleName -ResourceGroupName $resourceGroupName -Action $alertingAction -Source $source -Enabled $enabled -Description $description -Schedule $schedule -Tag $tags
215+
216+
Verify-ScheduledQueryRule $scheduledQueryRule
217+
$resourceId = $scheduledQueryRule.Id
218+
219+
Write-Debug " ****** Updating Scheduled Query Rule by name"
220+
$retrieved = Get-AzScheduledQueryRule -ResourceGroup $resourceGroupName -Name $ruleName | Update-AzScheduledQueryRule -Enabled 0
221+
222+
Verify-ScheduledQueryRule $retrieved
223+
Assert-AreEqual $retrieved.Enabled false
224+
225+
$retrieved = Get-AzScheduledQueryRule -ResourceGroup $resourceGroupName -Name $ruleName | Set-AzScheduledQueryRule
226+
Verify-ScheduledQueryRule $retrieved
227+
228+
Write-Debug " ****** Updating Scheduled Query Rule by Resource Id"
229+
$retrieved = Get-AzScheduledQueryRule -ResourceId $resourceId | Update-AzScheduledQueryRule -Enabled 1
230+
Assert-AreEqual $retrieved.Enabled true
231+
Verify-ScheduledQueryRule $retrieved
232+
233+
$retrieved = Get-AzScheduledQueryRule -ResourceId $resourceId | Set-AzScheduledQueryRule
234+
Verify-ScheduledQueryRule $retrieved
235+
236+
Write-Debug " ****** Removing Scheduled Query Rule by name"
237+
$retrieved = Get-AzScheduledQueryRule -ResourceGroup $resourceGroupName -Name $ruleName | Remove-AzScheduledQueryRule
238+
Assert-Null $retrieved
239+
240+
$scheduledQueryRule = New-AzScheduledQueryRule -Location $location -Name $ruleName -ResourceGroupName $resourceGroupName -Action $alertingAction -Source $source -Enabled $enabled -Description $description -Schedule $schedule -Tag $tags
241+
Verify-ScheduledQueryRule $scheduledQueryRule
242+
243+
Write-Debug " ****** Removing Scheduled Query Rule by Resource Id"
244+
$retrieved = Get-AzScheduledQueryRule -ResourceId $resourceId | Remove-AzScheduledQueryRule
245+
Assert-Null $retrieved
246+
247+
$scheduledQueryRule = New-AzScheduledQueryRule -Location $location -Name $ruleName -ResourceGroupName $resourceGroupName -Action $alertingAction -Source $source -Enabled $enabled -Description $description -Schedule $schedule -Tag $tags
248+
Verify-ScheduledQueryRule $scheduledQueryRule
249+
Write-Debug " ****** Removing Scheduled Query Rules in ResourceGroup"
250+
$retrieved = Get-AzScheduledQueryRule -ResourceGroupName $resourceGroupName | Remove-AzScheduledQueryRule
251+
Assert-Null $retrieved
252+
253+
#commenting for now to prevent deleting all alert rules in the subscription
254+
#Write-Debug " ****** Removing Scheduled Query Rules in the current subscription"
255+
#Get-AzScheduledQueryRule | Remove-AzScheduledQueryRule
256+
257+
}
258+
catch
259+
{
260+
#Write-Debug $_
261+
throw $_
262+
}
263+
finally
264+
{
265+
Clean-ResourceGroup($resourceGroupName)
266+
}
267+
}
268+

src/Monitor/Monitor.Test/ScenarioTests/TestsController.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.Azure.Management.Internal.Resources;
1717
using Microsoft.Azure.Management.Monitor;
1818
using Microsoft.Azure.Management.Storage.Version2017_10_01;
19+
using Microsoft.Azure.Management.ApplicationInsights.Management;
1920
using Microsoft.Azure.Test.HttpRecorder;
2021
using Microsoft.WindowsAzure.Commands.ScenarioTest;
2122
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
@@ -38,6 +39,8 @@ public sealed class TestsController : RMTestBase
3839

3940
public IMonitorManagementClient MonitorManagementClient { get; private set; }
4041

42+
public ApplicationInsightsManagementClient ApplicationInsightsClient { get; private set; }
43+
4144
public static TestsController NewInstance => new TestsController();
4245

4346
public TestsController()
@@ -92,7 +95,8 @@ public void RunPsTestWorkflow(
9295
"ScenarioTests\\Common.ps1",
9396
"ScenarioTests\\" + callingClassName + ".ps1",
9497
"AzureRM.Storage.ps1",
95-
"AzureRM.Resources.ps1");
98+
"AzureRM.Resources.ps1",
99+
_helper.GetRMModulePath("AzureRM.ApplicationInsights.psd1"));
96100

97101
try
98102
{
@@ -124,18 +128,21 @@ private void SetupManagementClients(RestTestFramework.MockContext context)
124128
this.MonitorManagementClient = this.GetInsightsManagementClient(context: context, env: environment);
125129
ResourceManagementClient = this.GetResourceManagementClient(context: context, env: environment);
126130
StorageManagementClient = this.GetStorageManagementClient(context: context, env: environment);
131+
ApplicationInsightsClient = this.GetApplicationInsightsManagementClient(context: context, env: environment);
127132
}
128133
else if (HttpMockServer.Mode == HttpRecorderMode.Playback)
129134
{
130135
this.MonitorManagementClient = this.GetInsightsManagementClient(context: context, env: null);
131136
ResourceManagementClient = this.GetResourceManagementClient(context: context, env: null);
132137
StorageManagementClient = this.GetStorageManagementClient(context: context, env: null);
138+
ApplicationInsightsClient = GetApplicationInsightsManagementClient(context, env:null);
133139
}
134140

135141
_helper.SetupManagementClients(
136142
ResourceManagementClient,
137143
this.MonitorManagementClient,
138-
StorageManagementClient);
144+
StorageManagementClient,
145+
this.ApplicationInsightsClient);
139146
}
140147

141148
private ResourceManagementClient GetResourceManagementClient(RestTestFramework.MockContext context, RestTestFramework.TestEnvironment env)
@@ -158,5 +165,12 @@ private StorageManagementClient GetStorageManagementClient(RestTestFramework.Moc
158165
? context.GetServiceClient<StorageManagementClient>(currentEnvironment: env)
159166
: context.GetServiceClient<StorageManagementClient>();
160167
}
168+
169+
private ApplicationInsightsManagementClient GetApplicationInsightsManagementClient(RestTestFramework.MockContext context, RestTestFramework.TestEnvironment env)
170+
{
171+
return env != null
172+
? context.GetServiceClient<ApplicationInsightsManagementClient>(currentEnvironment: env)
173+
: context.GetServiceClient<ApplicationInsightsManagementClient>();
174+
}
161175
}
162176
}

0 commit comments

Comments
 (0)