Skip to content

Commit 1bbcd8c

Browse files
authored
Merge pull request #344 from Azure/micongjie
Added new cmdlets for Action Groups
2 parents e3869ff + 77d6826 commit 1bbcd8c

33 files changed

+2405
-52
lines changed

src/ResourceManager/Insights/AzureRM.Insights.psd1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ 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+
'New-AzureRmActionGroupReceiver', 'Set-AzureRmActionGroup', 'Get-AzureRmActionGroup', 'Remove-AzureRmActionGroup'
88+
8789

8890
# Variables to export from this module
8991
# VariablesToExport = @()

src/ResourceManager/Insights/ChangeLog.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,16 @@
1818
- Additional information about change #1
1919
-->
2020
## Current Release
21-
21+
* New cmdlet Set-AzureRmActionGroup
22+
- A new cmdlet to create a new or update an existing action group.
23+
* New cmdlet Get-AzureRmActionGroup
24+
- A new cmdlet to retrieve one or more action groups.
25+
- The action groups can be retrieved by name, resource group, or subscription.
26+
* New cmdlet Remove-AzureRmActionGroup
27+
- A new cmdlet to remove one action group.
28+
* New cmdlet New-AzureRmActionGroupReceiver
29+
- A new cmdlet to create an new action group receiver in memory.
30+
2231
## Version 3.3.1
2332

2433
## Version 3.3.0
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// ---------------------------------------------------------------------------
2+
// ----------------------------------------------------------------------------------
3+
//
4+
// Copyright Microsoft Corporation
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
// ----------------------------------------------------------------------------------
15+
16+
namespace Microsoft.Azure.Commands.Insights.Test.ActionGroups
17+
{
18+
using System.Collections.Generic;
19+
20+
using Microsoft.Azure.Management.Monitor.Management.Models;
21+
22+
public class ActionGroupsUtilities
23+
{
24+
public static EmailReceiver CreateEmailReceiver(
25+
string name,
26+
string emailAddress)
27+
{
28+
return new EmailReceiver { Name = name, EmailAddress = emailAddress };
29+
}
30+
31+
public static SmsReceiver CreateSmsReceiver(
32+
string name,
33+
string phoneNumber)
34+
{
35+
return new SmsReceiver { Name = name, CountryCode = "1", PhoneNumber = phoneNumber };
36+
}
37+
38+
public static WebhookReceiver CreateWebhookReceiver(
39+
string name,
40+
string serviceUri)
41+
{
42+
return new WebhookReceiver { Name = name, ServiceUri = serviceUri };
43+
}
44+
45+
public static ActionGroupResource CreateActionGroupResource(
46+
string name,
47+
string shortName)
48+
{
49+
return new ActionGroupResource(
50+
location: "Global",
51+
enabled: true,
52+
name: name,
53+
groupShortName: shortName,
54+
id:
55+
$"/subscriptions/7de05d20-f39f-44d8-83ca-e7d2f12118b0/resourceGroups/testResourceGroup/providers/microsoft.insights/actionGroups/{name}",
56+
emailReceivers: new List<EmailReceiver> { CreateEmailReceiver("email", "[email protected]") },
57+
smsReceivers: new List<SmsReceiver> { CreateSmsReceiver("sms", "4254251234") },
58+
webhookReceivers: new List<WebhookReceiver> { CreateWebhookReceiver("webhook", "http://test.com") });
59+
}
60+
}
61+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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.Management.Monitor.Management;
16+
using Microsoft.Azure.Management.Monitor.Management.Models;
17+
using Microsoft.Rest.Azure;
18+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
19+
using Moq;
20+
using System.Collections.Generic;
21+
using System.Management.Automation;
22+
using System.Threading;
23+
using System.Threading.Tasks;
24+
using Xunit;
25+
using Microsoft.Azure.Commands.ScenarioTest;
26+
using Microsoft.Azure.Commands.Insights.ActionGroups;
27+
28+
namespace Microsoft.Azure.Commands.Insights.Test.ActionGroups
29+
{
30+
public class GetAzureRmActionGroupTests
31+
{
32+
private readonly GetAzureRmActionGroupCommand cmdlet;
33+
private readonly Mock<MonitorManagementClient> insightsManagementClientMock;
34+
private readonly Mock<IActionGroupsOperations> insightsOperationsMock;
35+
private Mock<ICommandRuntime> commandRuntimeMock;
36+
private AzureOperationResponse<ActionGroupResource> responseSimple;
37+
private AzureOperationResponse<IEnumerable<ActionGroupResource>> responsePage;
38+
private string resourceGroup;
39+
private string name;
40+
41+
public GetAzureRmActionGroupTests(Xunit.Abstractions.ITestOutputHelper output)
42+
{
43+
TestExecutionHelpers.SetUpSessionAndProfile();
44+
insightsOperationsMock = new Mock<IActionGroupsOperations>();
45+
insightsManagementClientMock = new Mock<MonitorManagementClient>();
46+
commandRuntimeMock = new Mock<ICommandRuntime>();
47+
cmdlet = new GetAzureRmActionGroupCommand()
48+
{
49+
CommandRuntime = commandRuntimeMock.Object,
50+
MonitorManagementClient = insightsManagementClientMock.Object
51+
};
52+
53+
ActionGroupResource responseObject = ActionGroupsUtilities.CreateActionGroupResource(name: "actiongroup1", shortName: "ag1");
54+
55+
responseSimple = new AzureOperationResponse<ActionGroupResource>()
56+
{
57+
Body = responseObject
58+
};
59+
60+
responsePage = new AzureOperationResponse<IEnumerable<ActionGroupResource>>()
61+
{
62+
Body = new List<ActionGroupResource> { responseObject }
63+
};
64+
65+
insightsOperationsMock.Setup(f => f.GetWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>()))
66+
.Returns(Task.FromResult<AzureOperationResponse<ActionGroupResource>>(responseSimple))
67+
.Callback((string resourceGrp, string name, Dictionary<string, List<string>> headers, CancellationToken t) =>
68+
{
69+
this.resourceGroup = resourceGrp;
70+
this.name = name;
71+
});
72+
73+
insightsOperationsMock.Setup(f => f.ListByResourceGroupWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>()))
74+
.Returns(Task.FromResult<AzureOperationResponse<IEnumerable<ActionGroupResource>>>(responsePage))
75+
.Callback((string resourceGrp, Dictionary<string, List<string>> headers, CancellationToken t) =>
76+
{
77+
this.resourceGroup = resourceGrp;
78+
});
79+
80+
insightsOperationsMock.Setup(f => f.ListBySubscriptionIdWithHttpMessagesAsync(It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>()))
81+
.Returns(Task.FromResult<AzureOperationResponse<IEnumerable<ActionGroupResource>>>(responsePage))
82+
.Callback((Dictionary<string, List<string>> headers, CancellationToken t) =>
83+
{ });
84+
85+
insightsManagementClientMock.SetupGet(f => f.ActionGroups).Returns(this.insightsOperationsMock.Object);
86+
}
87+
88+
[Fact]
89+
[Trait(Category.AcceptanceType, Category.CheckIn)]
90+
public void GetActionGroupCommandParametersProcessing()
91+
{
92+
// Get by subId
93+
cmdlet.ExecuteCmdlet();
94+
95+
// Get by resource group
96+
cmdlet.ResourceGroupName = Utilities.ResourceGroup;
97+
cmdlet.ExecuteCmdlet();
98+
99+
Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
100+
Assert.Null(this.name);
101+
102+
// Get by name
103+
cmdlet.Name = Utilities.Name;
104+
cmdlet.ExecuteCmdlet();
105+
106+
Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
107+
Assert.Equal(Utilities.Name, this.name);
108+
109+
// Error
110+
cmdlet.ResourceGroupName = " ";
111+
cmdlet.Name = Utilities.Name;
112+
Assert.Throws<PSArgumentException>(() => cmdlet.ExecuteCmdlet());
113+
}
114+
}
115+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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.WindowsAzure.Commands.ScenarioTest;
16+
using Moq;
17+
using System.Management.Automation;
18+
using Xunit;
19+
using Microsoft.Azure.Commands.Insights.ActionGroups;
20+
21+
namespace Microsoft.Azure.Commands.Insights.Test.ActionGroups
22+
{
23+
using System;
24+
25+
using Microsoft.Azure.Commands.Insights.OutputClasses;
26+
using Microsoft.Azure.Management.Monitor.Management.Models;
27+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
28+
29+
public class NewAzureRmActionGroupReceiverTests
30+
{
31+
private Mock<ICommandRuntime> commandRuntimeMock;
32+
33+
public NewAzureRmActionGroupReceiverCommand Cmdlet { get; set; }
34+
35+
public NewAzureRmActionGroupReceiverTests(Xunit.Abstractions.ITestOutputHelper output)
36+
{
37+
commandRuntimeMock = new Mock<ICommandRuntime>();
38+
Cmdlet = new NewAzureRmActionGroupReceiverCommand()
39+
{
40+
CommandRuntime = commandRuntimeMock.Object
41+
};
42+
}
43+
44+
[Fact]
45+
[Trait(Category.AcceptanceType, Category.CheckIn)]
46+
public void NewAzureRmReceiverCommandEmailParametersProcessing()
47+
{
48+
Cmdlet.SetParameterSet("NewEmailReceiver");
49+
Cmdlet.EmailReceiver = true;
50+
Cmdlet.Name = "email1";
51+
Cmdlet.EmailAddress = "[email protected]";
52+
Cmdlet.ExecuteCmdlet();
53+
54+
Func<PSEmailReceiver, bool> verify = r =>
55+
{
56+
Assert.Equal("[email protected]", r.EmailAddress);
57+
Assert.Equal("email1", r.Name);
58+
return true;
59+
};
60+
61+
this.commandRuntimeMock.Verify(o => o.WriteObject(It.Is<PSEmailReceiver>(r => verify(r))), Times.Once);
62+
}
63+
64+
[Fact]
65+
[Trait(Category.AcceptanceType, Category.CheckIn)]
66+
public void NewAzureRmReceiverCommandSmsParametersProcessing()
67+
{
68+
Cmdlet.SetParameterSet("NewSmsReceiver");
69+
Cmdlet.SmsReceiver = true;
70+
Cmdlet.Name = "sms1";
71+
Cmdlet.CountryCode = "1";
72+
Cmdlet.PhoneNumber = "4254251234";
73+
Cmdlet.ExecuteCmdlet();
74+
75+
Func<PSSmsReceiver, bool> verify = r =>
76+
{
77+
Assert.Equal("1", r.CountryCode);
78+
Assert.Equal("4254251234", r.PhoneNumber);
79+
Assert.Equal("sms1", r.Name);
80+
return true;
81+
};
82+
83+
this.commandRuntimeMock.Verify(o => o.WriteObject(It.Is<PSSmsReceiver>(r => verify(r))), Times.Once);
84+
}
85+
86+
[Fact]
87+
[Trait(Category.AcceptanceType, Category.CheckIn)]
88+
public void NewAzureRmReceiverCommandWebhookParametersProcessing()
89+
{
90+
Cmdlet.SetParameterSet("NewWebhookReceiver");
91+
Cmdlet.WebhookReceiver = true;
92+
Cmdlet.Name = "webhook1";
93+
Cmdlet.ServiceUri = "http://test.com";
94+
Cmdlet.ExecuteCmdlet();
95+
96+
Func<PSWebhookReceiver, bool> verify = r =>
97+
{
98+
Assert.Equal("http://test.com", r.ServiceUri);
99+
Assert.Equal("webhook1", r.Name);
100+
return true;
101+
};
102+
103+
this.commandRuntimeMock.Verify(o => o.WriteObject(It.Is<PSWebhookReceiver>(r => verify(r))), Times.Once);
104+
}
105+
}
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.ActionGroups;
25+
using Microsoft.Azure.Management.Monitor.Management;
26+
using Microsoft.Azure.Management.Monitor.Management.Models;
27+
28+
namespace Microsoft.Azure.Commands.Insights.Test.ActionGroups
29+
{
30+
public class RemoveAzureRmActionGroupTests
31+
{
32+
private readonly RemoveAzureRmActionGroupCommand cmdlet;
33+
private readonly Mock<MonitorManagementClient> monitorClientMock;
34+
private readonly Mock<IActionGroupsOperations> insightsOperationsMock;
35+
private Mock<ICommandRuntime> commandRuntimeMock;
36+
private string resourceGroup;
37+
private string name;
38+
39+
public RemoveAzureRmActionGroupTests(Xunit.Abstractions.ITestOutputHelper output)
40+
{
41+
TestExecutionHelpers.SetUpSessionAndProfile();
42+
insightsOperationsMock = new Mock<IActionGroupsOperations>();
43+
monitorClientMock = new Mock<MonitorManagementClient>();
44+
commandRuntimeMock = new Mock<ICommandRuntime>();
45+
cmdlet = new RemoveAzureRmActionGroupCommand()
46+
{
47+
CommandRuntime = commandRuntimeMock.Object,
48+
MonitorManagementClient = monitorClientMock.Object
49+
};
50+
51+
insightsOperationsMock.Setup(f => f.DeleteWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>()))
52+
.Returns(Task.FromResult<Microsoft.Rest.Azure.AzureOperationResponse>(new Microsoft.Rest.Azure.AzureOperationResponse()))
53+
.Callback((string r, string n, Dictionary<string, List<string>> headers, CancellationToken t) =>
54+
{
55+
this.resourceGroup = r;
56+
this.name = n;
57+
});
58+
59+
monitorClientMock.SetupGet(f => f.ActionGroups).Returns(this.insightsOperationsMock.Object);
60+
61+
// Setup Confirmation
62+
commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>())).Returns(true);
63+
commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
64+
commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(true);
65+
commandRuntimeMock.Setup(f => f.ShouldContinue(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
66+
}
67+
68+
[Fact]
69+
[Trait(Category.AcceptanceType, Category.CheckIn)]
70+
public void RemoveActionGroupCommandParametersProcessing()
71+
{
72+
cmdlet.ResourceGroupName = Utilities.ResourceGroup;
73+
cmdlet.Name = "group1";
74+
cmdlet.ExecuteCmdlet();
75+
76+
Assert.Equal(Utilities.ResourceGroup, this.resourceGroup);
77+
Assert.Equal("group1", this.name);
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)