Skip to content

Commit d724d35

Browse files
authored
Merge pull request #283 from AuxMon/ActivityLogAlerts
Adding new Activity Log Alerts cmdlets and updating/improving the test in general.
2 parents 39f3d2c + 6b84dbb commit d724d35

File tree

65 files changed

+4144
-537
lines changed

Some content is hidden

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

65 files changed

+4144
-537
lines changed

src/ResourceManager/Insights/AzureRM.Insights.psd1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ CmdletsToExport = 'Get-AzureRmUsage', 'Get-AzureRmMetricDefinition',
8383
'Add-AzureRmMetricAlertRule', 'Add-AzureRmLogAlertRule',
8484
'Add-AzureRmWebtestAlertRule', 'Get-AzureRmAlertHistory',
8585
'Get-AzureRmAlertRule', 'New-AzureRmAlertRuleEmail',
86-
'New-AzureRmAlertRuleWebhook', 'Remove-AzureRmAlertRule'
86+
'New-AzureRmAlertRuleWebhook', 'Remove-AzureRmAlertRule',
87+
'Set-AzureRmActivityLogAlert', 'Get-AzureRmActivityLogAlert',
88+
'New-AzureRmActionGroup', 'New-AzureRmActivityLogAlertCondition',
89+
'Enable-AzureRmActivityLogAlert', 'Disable-AzureRmActivityLogAlert',
90+
'Remove-AzureRmActivityLogAlert'
8791

8892
# Variables to export from this module
8993
# VariablesToExport = @()

src/ResourceManager/Insights/ChangeLog.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@
1818
- Additional information about change #1
1919
-->
2020
## Current Release
21+
* New cmdlet Disable-AzureRmActivityLogAlert
22+
- A new cmdlet to disable an existing activity log alert.
23+
- Optionally the Tags are settable with this cmdlet too.
24+
* New cmdlet Enable-AzureRmActivityLogAlert
25+
- A new cmdlet to enable an existing activity log alert.
26+
- Optionally the Tags are settable with this cmdlet too.
27+
* New cmdlet Get-AzureRmActivityLogAlert
28+
- A new cmdlet to retrieve one or more activity log alerts.
29+
- The alerts can be retrieved by name, resource group, or subscription.
30+
* New cmdlet New-AzureRmActionGroup
31+
- A new cmdlet to create an ActionGroup object in memory (no request involved.)
32+
* New cmdlet New-AzureRmActivityLogAlertCondition
33+
- A new cmdlet to create an activity log alert leaf condition object in memory (no request involved.)
34+
* New cmdlet Set-AzureRmActivityLogAlert
35+
- A new cmdlet to create or update an activity log alert.
36+
* New cmdlet Remove-AzureRmActivityLogAlert
37+
- A new cmdlet to remove one activity log alert.
2138

2239
## Version 3.3.1
2340

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 System.Collections.Generic;
16+
using Microsoft.Azure.Management.Monitor.Management.Models;
17+
18+
namespace Microsoft.Azure.Commands.Insights.Test.ActivityLogAlerts
19+
{
20+
public static class ActivityLogAlertsUtilities
21+
{
22+
public static ActivityLogAlertActionGroup CreateActionGroup(string id, Dictionary<string, string> webhooks)
23+
{
24+
return new ActivityLogAlertActionGroup
25+
{
26+
ActionGroupId = id,
27+
WebhookProperties = webhooks
28+
};
29+
}
30+
31+
public static ActivityLogAlertLeafCondition CreateLeafCondition(string field, string equals)
32+
{
33+
return new ActivityLogAlertLeafCondition
34+
{
35+
Field = field,
36+
Equals = equals
37+
};
38+
}
39+
40+
public static ActivityLogAlertResource CreateActivityLogAlertResource(string location, string name)
41+
{
42+
return new ActivityLogAlertResource(
43+
location: location,
44+
name: name,
45+
condition: new ActivityLogAlertAllOfCondition(
46+
new List<ActivityLogAlertLeafCondition>
47+
{
48+
CreateLeafCondition("field", "equals")
49+
}),
50+
scopes: new List<string> { "scope1" },
51+
actions: new ActivityLogAlertActionList(
52+
new List<ActivityLogAlertActionGroup>
53+
{
54+
CreateActionGroup(
55+
id: "actionGrp1",
56+
webhooks: new Dictionary<string, string>())
57+
}));
58+
}
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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 System.Collections.Generic;
16+
using Microsoft.Rest.Azure;
17+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
18+
using Moq;
19+
using System.Management.Automation;
20+
using System.Threading;
21+
using System.Threading.Tasks;
22+
using Xunit;
23+
using Microsoft.Azure.Commands.ScenarioTest;
24+
using Microsoft.Azure.Commands.Insights.ActivityLogAlert;
25+
using Microsoft.Azure.Management.Monitor.Management;
26+
using Microsoft.Azure.Management.Monitor.Management.Models;
27+
28+
namespace Microsoft.Azure.Commands.Insights.Test.ActivityLogAlerts
29+
{
30+
public class DisableAzureRmActivityLogAlertTests
31+
{
32+
private readonly DisableAzureRmActivityLogAlertCommand cmdlet;
33+
private readonly Mock<MonitorManagementClient> monitorClientMock;
34+
private readonly Mock<IActivityLogAlertsOperations> insightsOperationsMock;
35+
private Mock<ICommandRuntime> commandRuntimeMock;
36+
private AzureOperationResponse<ActivityLogAlertResource> response;
37+
private string resourceGroup;
38+
private string name;
39+
private ActivityLogAlertPatchBody body;
40+
41+
public DisableAzureRmActivityLogAlertTests(Xunit.Abstractions.ITestOutputHelper output)
42+
{
43+
ServiceManagemenet.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagemenet.Common.Models.XunitTracingInterceptor(output));
44+
TestExecutionHelpers.SetUpSessionAndProfile();
45+
insightsOperationsMock = new Mock<IActivityLogAlertsOperations>();
46+
monitorClientMock = new Mock<MonitorManagementClient>();
47+
commandRuntimeMock = new Mock<ICommandRuntime>();
48+
cmdlet = new DisableAzureRmActivityLogAlertCommand()
49+
{
50+
CommandRuntime = commandRuntimeMock.Object,
51+
MonitorManagementClient = monitorClientMock.Object
52+
};
53+
54+
response = new AzureOperationResponse<ActivityLogAlertResource>()
55+
{
56+
Body = ActivityLogAlertsUtilities.CreateActivityLogAlertResource(location: "westus", name: "alert1")
57+
};
58+
59+
insightsOperationsMock.Setup(f => f.UpdateWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ActivityLogAlertPatchBody>(), It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>()))
60+
.Returns(Task.FromResult<AzureOperationResponse<ActivityLogAlertResource>>(response))
61+
.Callback((string r, string n, ActivityLogAlertPatchBody b, Dictionary<string, List<string>> headers, CancellationToken t) =>
62+
{
63+
this.resourceGroup = r;
64+
this.name = n;
65+
this.body = b;
66+
});
67+
68+
monitorClientMock.SetupGet(f => f.ActivityLogAlerts).Returns(this.insightsOperationsMock.Object);
69+
70+
// Setup Confirmation
71+
commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>())).Returns(true);
72+
commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
73+
commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(true);
74+
commandRuntimeMock.Setup(f => f.ShouldContinue(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
75+
}
76+
77+
[Fact]
78+
[Trait(Category.AcceptanceType, Category.CheckIn)]
79+
public void DisableActivityLogAlertCommandParametersProcessing()
80+
{
81+
cmdlet.ResourceGroupName = Utilities.ResourceGroup;
82+
cmdlet.Name = "alert1";
83+
cmdlet.ExecuteCmdlet();
84+
85+
Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
86+
Assert.Equal("alert1", this.name);
87+
Assert.NotNull(this.body);
88+
Assert.False(this.body.Enabled);
89+
Assert.Null(this.body.Tags);
90+
91+
cmdlet.ExecuteCmdlet();
92+
93+
Assert.NotNull(this.body);
94+
Assert.False(this.body.Enabled);
95+
Assert.Null(this.body.Tags);
96+
97+
ActivityLogAlertResource resource = new ActivityLogAlertResource(location: "Global", scopes: null, condition: null, name: "andy0307rule", actions: null, id: "//subscriptions/07c0b09d-9f69-4e6e-8d05-f59f67299cb2/resourceGroups/Default-ActivityLogAlerts/providers/microsoft.insights/activityLogAlerts/andy0307rule")
98+
{
99+
Enabled = true
100+
};
101+
102+
cmdlet.InputObject = new OutputClasses.PSActivityLogAlertResource(resource);
103+
cmdlet.ExecuteCmdlet();
104+
105+
Assert.NotNull(this.body);
106+
Assert.Equal("Default-ActivityLogAlerts", this.resourceGroup);
107+
Assert.Equal("andy0307rule", this.name);
108+
Assert.False(this.body.Enabled);
109+
Assert.Null(this.body.Tags);
110+
111+
cmdlet.InputObject = null;
112+
cmdlet.ResourceId = "/subscriptions/07c0b09d-9f69-4e6e-8d05-f59f67299cb2/resourceGroups/Default-ActivityLogAlerts/providers/microsoft.insights/activityLogAlerts/andy0307rule";
113+
cmdlet.ExecuteCmdlet();
114+
Assert.NotNull(this.body);
115+
Assert.Equal("Default-ActivityLogAlerts", this.resourceGroup);
116+
Assert.Equal("andy0307rule", this.name);
117+
Assert.False(this.body.Enabled);
118+
Assert.Null(this.body.Tags);
119+
}
120+
}
121+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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 System.Collections.Generic;
16+
using Microsoft.Rest.Azure;
17+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
18+
using Moq;
19+
using System.Management.Automation;
20+
using System.Threading;
21+
using System.Threading.Tasks;
22+
using Xunit;
23+
using Microsoft.Azure.Commands.ScenarioTest;
24+
using Microsoft.Azure.Commands.Insights.ActivityLogAlert;
25+
using Microsoft.Azure.Management.Monitor.Management;
26+
using Microsoft.Azure.Management.Monitor.Management.Models;
27+
28+
namespace Microsoft.Azure.Commands.Insights.Test.ActivityLogAlerts
29+
{
30+
public class EnableAzureRmActivityLogAlertTests
31+
{
32+
private readonly EnableAzureRmActivityLogAlertCommand cmdlet;
33+
private readonly Mock<MonitorManagementClient> monitorClientMock;
34+
private readonly Mock<IActivityLogAlertsOperations> insightsOperationsMock;
35+
private Mock<ICommandRuntime> commandRuntimeMock;
36+
private AzureOperationResponse<ActivityLogAlertResource> response;
37+
private string resourceGroup;
38+
private string name;
39+
private ActivityLogAlertPatchBody body;
40+
41+
public EnableAzureRmActivityLogAlertTests(Xunit.Abstractions.ITestOutputHelper output)
42+
{
43+
ServiceManagemenet.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagemenet.Common.Models.XunitTracingInterceptor(output));
44+
TestExecutionHelpers.SetUpSessionAndProfile();
45+
insightsOperationsMock = new Mock<IActivityLogAlertsOperations>();
46+
monitorClientMock = new Mock<MonitorManagementClient>();
47+
commandRuntimeMock = new Mock<ICommandRuntime>();
48+
cmdlet = new EnableAzureRmActivityLogAlertCommand()
49+
{
50+
CommandRuntime = commandRuntimeMock.Object,
51+
MonitorManagementClient = monitorClientMock.Object
52+
};
53+
54+
response = new AzureOperationResponse<ActivityLogAlertResource>()
55+
{
56+
Body = ActivityLogAlertsUtilities.CreateActivityLogAlertResource(location: "westus", name: "alert1")
57+
};
58+
59+
insightsOperationsMock.Setup(f => f.UpdateWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ActivityLogAlertPatchBody>(), It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>()))
60+
.Returns(Task.FromResult<AzureOperationResponse<ActivityLogAlertResource>>(response))
61+
.Callback((string r, string n, ActivityLogAlertPatchBody b, Dictionary<string, List<string>> headers, CancellationToken t) =>
62+
{
63+
this.resourceGroup = r;
64+
this.name = n;
65+
this.body = b;
66+
});
67+
68+
monitorClientMock.SetupGet(f => f.ActivityLogAlerts).Returns(this.insightsOperationsMock.Object);
69+
70+
// Setup Confirmation
71+
commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>())).Returns(true);
72+
commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
73+
commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(true);
74+
commandRuntimeMock.Setup(f => f.ShouldContinue(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
75+
}
76+
77+
[Fact]
78+
[Trait(Category.AcceptanceType, Category.CheckIn)]
79+
public void EnableActivityLogAlertCommandParametersProcessing()
80+
{
81+
cmdlet.ResourceGroupName = Utilities.ResourceGroup;
82+
cmdlet.Name = "alert1";
83+
cmdlet.ExecuteCmdlet();
84+
85+
Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
86+
Assert.Equal("alert1", this.name);
87+
Assert.NotNull(this.body);
88+
Assert.True(this.body.Enabled);
89+
Assert.Null(this.body.Tags);
90+
91+
cmdlet.ExecuteCmdlet();
92+
93+
Assert.NotNull(this.body);
94+
Assert.True(this.body.Enabled);
95+
Assert.Null(this.body.Tags);
96+
97+
ActivityLogAlertResource resource = new ActivityLogAlertResource(location: "Global", scopes: null, condition: null, name: "andy0307rule", actions: null, id: "//subscriptions/07c0b09d-9f69-4e6e-8d05-f59f67299cb2/resourceGroups/Default-ActivityLogAlerts/providers/microsoft.insights/activityLogAlerts/andy0307rule")
98+
{
99+
Enabled = false
100+
};
101+
102+
cmdlet.InputObject = new OutputClasses.PSActivityLogAlertResource(resource);
103+
cmdlet.ExecuteCmdlet();
104+
105+
Assert.NotNull(this.body);
106+
Assert.Equal("Default-ActivityLogAlerts", this.resourceGroup);
107+
Assert.Equal("andy0307rule", this.name);
108+
Assert.True(this.body.Enabled);
109+
Assert.Null(this.body.Tags);
110+
111+
cmdlet.InputObject = null;
112+
cmdlet.ResourceId = "/subscriptions/07c0b09d-9f69-4e6e-8d05-f59f67299cb2/resourceGroups/Default-ActivityLogAlerts/providers/microsoft.insights/activityLogAlerts/andy0307rule";
113+
cmdlet.ExecuteCmdlet();
114+
Assert.NotNull(this.body);
115+
Assert.Equal("Default-ActivityLogAlerts", this.resourceGroup);
116+
Assert.Equal("andy0307rule", this.name);
117+
Assert.True(this.body.Enabled);
118+
Assert.Null(this.body.Tags);
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)