Skip to content

Commit bc68f11

Browse files
committed
[Insights] Adding ActivityLogAlerts cmdlets inclding documentation and tests.
1 parent 8f95ec6 commit bc68f11

32 files changed

+3210
-2
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,114 @@
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+
TestExecutionHelpers.SetUpSessionAndProfile();
44+
//ServiceManagemenet.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagemenet.Common.Models.XunitTracingInterceptor(output));
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.Tag = new Dictionary<string, string> { { "key2", "value2" } };
84+
cmdlet.ExecuteCmdlet();
85+
86+
Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
87+
Assert.Equal("alert1", this.name);
88+
Assert.NotNull(this.body);
89+
Assert.False(this.body.Enabled);
90+
Assert.NotNull(this.body.Tags);
91+
92+
cmdlet.Tag = null;
93+
cmdlet.ExecuteCmdlet();
94+
95+
Assert.NotNull(this.body);
96+
Assert.False(this.body.Enabled);
97+
Assert.Null(this.body.Tags);
98+
99+
ActivityLogAlertResource resource = new ActivityLogAlertResource(location: "Global", scopes: null, condition: null, name: "alert1", actions: null, id: "//subscriptions/32323/resourcegroups/" + Utilities.ResourceGroup)
100+
{
101+
Enabled = true
102+
};
103+
104+
cmdlet.InputObject = new OutputClasses.PSActivityLogAlertResource(resource);
105+
cmdlet.ExecuteCmdlet();
106+
107+
Assert.NotNull(this.body);
108+
Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
109+
Assert.Equal("alert1", this.name);
110+
Assert.False(this.body.Enabled);
111+
Assert.Null(this.body.Tags);
112+
}
113+
}
114+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
TestExecutionHelpers.SetUpSessionAndProfile();
44+
//ServiceManagemenet.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagemenet.Common.Models.XunitTracingInterceptor(output));
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.Tag = new Dictionary<string, string>{ { "key2", "value2" } };
84+
cmdlet.ExecuteCmdlet();
85+
86+
Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
87+
Assert.Equal("alert1", this.name);
88+
Assert.NotNull(this.body);
89+
Assert.True(this.body.Enabled);
90+
Assert.NotNull(this.body.Tags);
91+
92+
cmdlet.Tag = null;
93+
cmdlet.ExecuteCmdlet();
94+
95+
Assert.NotNull(this.body);
96+
Assert.True(this.body.Enabled);
97+
Assert.Null(this.body.Tags);
98+
99+
ActivityLogAlertResource resource = new ActivityLogAlertResource(location: "Global", scopes: null, condition: null, name: "alert1", actions: null, id: "//subscriptions/32323/resourcegroups/" + Utilities.ResourceGroup)
100+
{
101+
Enabled = false
102+
};
103+
104+
cmdlet.InputObject = new OutputClasses.PSActivityLogAlertResource(resource);
105+
cmdlet.ExecuteCmdlet();
106+
107+
Assert.NotNull(this.body);
108+
Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
109+
Assert.Equal("alert1", this.name);
110+
Assert.True(this.body.Enabled);
111+
Assert.Null(this.body.Tags);
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)