Skip to content

Commit d237671

Browse files
GetAlertRuleTemplates updates
1 parent 968f387 commit d237671

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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;
16+
using System.Management.Automation;
17+
using Microsoft.Azure.Commands.SecurityInsights;
18+
using Microsoft.Azure.Commands.SecurityInsights.Common;
19+
using Microsoft.Azure.Commands.SecurityInsights.Models.AlertRuleTemplates;
20+
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
21+
using System.Linq;
22+
using Microsoft.Azure.Management.SecurityInsights;
23+
24+
namespace Microsoft.Azure.Commands.SecurityInsights.Cmdlets.AlertRulesTemplates
25+
{
26+
[Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "SentinelAlertRuleTemplate", DefaultParameterSetName = ParameterSetNames.WorkspaceScope), OutputType(typeof(PSSentinelAlertRuleTemplate))]
27+
public class GetAlertRuleTemplate : SecurityInsightsCmdletBase
28+
{
29+
private const int MaxAlertRulesToFetch = 1500;
30+
31+
[Parameter(ParameterSetName = ParameterSetNames.WorkspaceScope, Mandatory = true, HelpMessage = ParameterHelpMessages.ResourceGroupName)]
32+
[Parameter(ParameterSetName = ParameterSetNames.AlertRuleTemplateId, Mandatory = true, HelpMessage = ParameterHelpMessages.ResourceGroupName)]
33+
[ResourceGroupCompleter]
34+
[ValidateNotNullOrEmpty]
35+
public string ResourceGroupName { get; set; }
36+
37+
[Parameter(ParameterSetName = ParameterSetNames.WorkspaceScope, Mandatory = true, HelpMessage = ParameterHelpMessages.WorkspaceName)]
38+
[Parameter(ParameterSetName = ParameterSetNames.AlertRuleTemplateId, Mandatory = true, HelpMessage = ParameterHelpMessages.WorkspaceName)]
39+
[ValidateNotNullOrEmpty]
40+
public string WorkspaceName { get; set; }
41+
42+
[Parameter(ParameterSetName = ParameterSetNames.AlertRuleTemplateId, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = ParameterHelpMessages.AlertRuleTemplateId)]
43+
[ValidateNotNullOrEmpty]
44+
public string AlertRuleTemplateId { get; set; }
45+
46+
[Parameter(ParameterSetName = ParameterSetNames.ResourceId, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = ParameterHelpMessages.ResourceId)]
47+
[ValidateNotNullOrEmpty]
48+
public string ResourceId { get; set; }
49+
50+
public override void ExecuteCmdlet()
51+
{
52+
int numberOfFetchedAlertRuleTemplates = 0;
53+
string nextLink = null;
54+
switch (ParameterSetName)
55+
{
56+
case ParameterSetNames.WorkspaceScope:
57+
var alertruletemplate = SecurityInsightsClient.AlertRuleTemplates.List(ResourceGroupName, WorkspaceName);
58+
59+
int alertruletemplatecount = alertruletemplate.Count();
60+
WriteObject(alertruletemplate.ConvertToPSType(), enumerateCollection: true);
61+
numberOfFetchedAlertRuleTemplates += alertruletemplatecount;
62+
nextLink = alertruletemplate?.NextPageLink;
63+
while (!string.IsNullOrWhiteSpace(nextLink) && numberOfFetchedAlertRuleTemplates < MaxAlertRulesToFetch)
64+
{
65+
alertruletemplate = SecurityInsightsClient.AlertRuleTemplates.ListNext(alertruletemplate.NextPageLink);
66+
alertruletemplatecount = alertruletemplate.Count();
67+
WriteObject(alertruletemplate.ConvertToPSType(), enumerateCollection: true);
68+
numberOfFetchedAlertRuleTemplates += alertruletemplatecount;
69+
nextLink = alertruletemplate?.NextPageLink;
70+
}
71+
break;
72+
case ParameterSetNames.AlertRuleTemplateId:
73+
var alertruletemplate2 = SecurityInsightsClient.AlertRuleTemplates.Get(ResourceGroupName, WorkspaceName, AlertRuleTemplateId);
74+
WriteObject(alertruletemplate2.ConvertToPSType(), enumerateCollection: false);
75+
break;
76+
case ParameterSetNames.ResourceId:
77+
alertruletemplate2 = SecurityInsightsClient.AlertRuleTemplates.Get(ResourceGroupName, WorkspaceName, AzureIdUtilities.GetResourceName(ResourceId));
78+
WriteObject(alertruletemplate2.ConvertToPSType(), enumerateCollection: false);
79+
break;
80+
default:
81+
throw new PSInvalidOperationException();
82+
}
83+
}
84+
}
85+
}

src/SecurityInsights/SecurityInsights/Common/ParameterHelpMessages.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public static class ParameterHelpMessages
5555
public const string TriggerThreshold = "Alert Rule Trigger Threshold.";
5656
#endregion
5757

58+
#region
59+
public const string AlertRuleTemplateId = "Template Alert Rule Id.";
60+
#endregion
61+
5862
#region Bookmarks
5963
public const string BookmarkId = "Bookmark Id,";
6064
public const string RelationName = "Bookmark Relation Name.";

src/SecurityInsights/SecurityInsights/Common/ParameterSetNames.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public static class ParameterSetNames
3535

3636
#endregion
3737

38+
#region AlertRuleTemplates
39+
public const string AlertRuleTemplateId = "AlertRuleTemplateId";
40+
#endregion
41+
3842
#region Bookmarks
3943
public const string BookmarkId = "BookmarkId.";
4044
#endregion

0 commit comments

Comments
 (0)