Skip to content

Commit 35ef33c

Browse files
author
Mahak Jain
committed
Enrich information for Action Rule
1 parent 77d47cd commit 35ef33c

File tree

8 files changed

+198
-12
lines changed

8 files changed

+198
-12
lines changed

src/AlertsManagement/AlertsManagement.Test/SessionRecords/Microsoft.Azure.Commands.AlertsManagement.Test.ScenarioTests.ActionRuleTests/TestGetActionRulesFilteredByParameters.json

Lines changed: 10 additions & 10 deletions
Large diffs are not rendered by default.

src/AlertsManagement/AlertsManagement/ActionRuleCommands/SetAzureActionRule.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,78 @@ protected override void ProcessRecordInternal()
397397

398398
break;
399399
case ByInputObjectParameterSet:
400+
ExtractedInfo info = CommonUtils.ExtractFromActionRuleResourceId(InputObject.Id);
401+
switch (InputObject.ActionRuleType)
402+
{
403+
case "ActionGroup":
404+
// Create Action Rule
405+
ActionRule actionGroupARFromInputObject = new ActionRule(
406+
location: "Global",
407+
tags: new Dictionary<string, string>(),
408+
properties: new ActionGroup(
409+
scope: JsonConvert.DeserializeObject<Scope>(InputObject.Scope),
410+
conditions: JsonConvert.DeserializeObject<Conditions>(InputObject.Conditions),
411+
actionGroupId: InputObject.ActionGroupId,
412+
description: InputObject.Description,
413+
status: InputObject.Status
414+
)
415+
);
416+
417+
actionRule = new PSActionRule(this.AlertsManagementClient.ActionRules.CreateUpdateWithHttpMessagesAsync(
418+
resourceGroupName: info.ResourceGroupName, actionRuleName: info.Resource, actionRule: actionGroupARFromInputObject).Result.Body);
419+
break;
420+
421+
case "Suppression":
422+
SuppressionConfig configFromInputObject = new SuppressionConfig(recurrenceType: InputObject.SuppressionConfig.RecurrenceType);
423+
if (InputObject.SuppressionConfig.RecurrenceType != "Always")
424+
{
425+
configFromInputObject.Schedule = new SuppressionSchedule(
426+
startDate: InputObject.SuppressionConfig.StartDate,
427+
endDate: InputObject.SuppressionConfig.EndDate,
428+
startTime: InputObject.SuppressionConfig.StartTime,
429+
endTime: InputObject.SuppressionConfig.EndTime
430+
);
431+
432+
if (ReccurentValue.Length > 0)
433+
{
434+
configFromInputObject.Schedule.RecurrenceValues = InputObject.SuppressionConfig.RecurrenceValues;
435+
}
436+
}
437+
438+
// Create Action Rule
439+
ActionRule suppressionARFromInputObject = new ActionRule(
440+
location: "Global",
441+
tags: new Dictionary<string, string>(),
442+
properties: new Suppression(
443+
scope: JsonConvert.DeserializeObject<Scope>(InputObject.Scope),
444+
conditions: JsonConvert.DeserializeObject<Conditions>(InputObject.Conditions),
445+
description: InputObject.Description,
446+
status: InputObject.Status,
447+
suppressionConfig: configFromInputObject
448+
)
449+
);
450+
451+
actionRule = new PSActionRule(this.AlertsManagementClient.ActionRules.CreateUpdateWithHttpMessagesAsync(
452+
resourceGroupName: info.ResourceGroupName, actionRuleName: info.Resource, actionRule: suppressionARFromInputObject).Result.Body);
453+
break;
454+
455+
case "Diagnostics":
456+
// Create Action Rule
457+
ActionRule diagnosticsARFromInputObject = new ActionRule(
458+
location: "Global",
459+
tags: new Dictionary<string, string>(),
460+
properties: new Diagnostics(
461+
scope: JsonConvert.DeserializeObject<Scope>(InputObject.Scope),
462+
conditions: JsonConvert.DeserializeObject<Conditions>(InputObject.Conditions),
463+
description: InputObject.Description,
464+
status: InputObject.Status
465+
)
466+
);
467+
468+
actionRule = new PSActionRule(this.AlertsManagementClient.ActionRules.CreateUpdateWithHttpMessagesAsync(
469+
resourceGroupName: info.ResourceGroupName, actionRuleName: info.Resource, actionRule: diagnosticsARFromInputObject).Result.Body);
470+
break;
471+
}
400472
break;
401473
}
402474

src/AlertsManagement/AlertsManagement/AlertsManagement.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,19 @@
1010
<PackageReference Include="Microsoft.Azure.Management.AlertsManagement" Version="0.9.1-preview" />
1111
</ItemGroup>
1212

13+
<ItemGroup>
14+
<Compile Update="Properties\Resources.Designer.cs">
15+
<DesignTime>True</DesignTime>
16+
<AutoGen>True</AutoGen>
17+
<DependentUpon>Resources.resx</DependentUpon>
18+
</Compile>
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<EmbeddedResource Update="Properties\Resources.resx">
23+
<Generator>ResXFileCodeGenerator</Generator>
24+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
25+
</EmbeddedResource>
26+
</ItemGroup>
27+
1328
</Project>

src/AlertsManagement/AlertsManagement/Commons/CommonUtils.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using System.Management.Automation;
16+
using Microsoft.Azure.PowerShell.Cmdlets.AlertsManagement.Properties;
1617

1718
namespace Microsoft.Azure.Commands.AlertsManagement
1819
{
@@ -36,7 +37,7 @@ public static string GetIdFromARMResourceId(string resourceId)
3637
}
3738
else
3839
{
39-
throw new PSArgumentException(string.Format("{0} is not a valid Id.", resourceId));
40+
throw new PSArgumentException(string.Format(Resources.InvalidId, resourceId));
4041
}
4142
}
4243

@@ -57,7 +58,7 @@ public static ExtractedInfo ExtractFromActionRuleResourceId(string resourceId)
5758
}
5859
else
5960
{
60-
throw new PSArgumentException(string.Format("{0} is not a valid ResourceId.", resourceId));
61+
throw new PSArgumentException(string.Format(Resources.InvalidResourceId, resourceId));
6162
}
6263
}
6364
}

src/AlertsManagement/AlertsManagement/OutputModels/PSActionRule.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System;
1616
using Newtonsoft.Json;
1717
using Microsoft.Azure.Management.AlertsManagement.Models;
18+
using Microsoft.WindowsAzure.Commands.Common.Attributes;
1819

1920
namespace Microsoft.Azure.Commands.AlertsManagement.OutputModels
2021
{
@@ -43,6 +44,21 @@ public PSActionRule(ActionRule rule)
4344
LastModifiedBy = rule.Properties.LastModifiedBy;
4445
Scope = JsonConvert.SerializeObject(rule.Properties.Scope);
4546
Conditions = JsonConvert.SerializeObject(rule.Properties.Conditions);
47+
ActionRuleType = rule.Properties.GetType().Name;
48+
49+
switch (ActionRuleType)
50+
{
51+
case "Suppression":
52+
Suppression suppression = (Suppression)rule.Properties;
53+
SuppressionConfig = new PSSuppressionConfig(suppression.SuppressionConfig);
54+
break;
55+
case "ActionGroup":
56+
ActionGroup actionGroup = (ActionGroup)rule.Properties;
57+
ActionGroupId = actionGroup.ActionGroupId;
58+
break;
59+
case "Diagnostics":
60+
break;
61+
}
4662
}
4763

4864
public string Id { get; }
@@ -64,5 +80,17 @@ public PSActionRule(ActionRule rule)
6480
public DateTime? LastModifiedAt { get; }
6581

6682
public string LastModifiedBy { get; }
83+
84+
public string ActionRuleType { get; }
85+
86+
public string ActionGroupId { get; }
87+
88+
[Ps1Xml(Label = "RecurrenceType", Target = ViewControl.List, ScriptBlock = "$_.SuppressionConfig.RecurrenceType")]
89+
[Ps1Xml(Label = "StartDate", Target = ViewControl.List, ScriptBlock = "$_.SuppressionConfig.StartDate")]
90+
[Ps1Xml(Label = "StartTime", Target = ViewControl.List, ScriptBlock = "$_.SuppressionConfig.StartTime")]
91+
[Ps1Xml(Label = "EndDate", Target = ViewControl.List, ScriptBlock = "$_.SuppressionConfig.EndDate")]
92+
[Ps1Xml(Label = "EndTime", Target = ViewControl.List, ScriptBlock = "$_.SuppressionConfig.EndTime")]
93+
[Ps1Xml(Label = "RecurrenceValues", Target = ViewControl.List, ScriptBlock = "$_.SuppressionConfig.RecurrenceValues.ToString()")]
94+
public PSSuppressionConfig SuppressionConfig { get; }
6795
}
6896
}
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 System;
16+
using Newtonsoft.Json;
17+
using Microsoft.Azure.Management.AlertsManagement.Models;
18+
using System.Collections.Generic;
19+
20+
namespace Microsoft.Azure.Commands.AlertsManagement.OutputModels
21+
{
22+
public class PSSuppressionConfig
23+
{
24+
public PSSuppressionConfig(SuppressionConfig config)
25+
{
26+
RecurrenceType = config?.RecurrenceType;
27+
StartDate = config?.Schedule?.StartDate;
28+
EndDate = config?.Schedule?.EndDate;
29+
StartTime = config?.Schedule?.StartTime;
30+
EndTime = config?.Schedule?.EndTime;
31+
RecurrenceValues = config?.Schedule?.RecurrenceValues;
32+
}
33+
34+
public string RecurrenceType { get; }
35+
36+
public string StartDate { get; set; }
37+
38+
public string EndDate { get; set; }
39+
40+
public string StartTime { get; set; }
41+
42+
public string EndTime { get; set; }
43+
44+
public IList<int?> RecurrenceValues { get; set; }
45+
}
46+
}

src/AlertsManagement/AlertsManagement/Properties/Resources.Designer.cs

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AlertsManagement/AlertsManagement/Properties/Resources.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@
123123
<data name="IncorrectActionRuleType_Exception" xml:space="preserve">
124124
<value>Incorrect Action Rule Type for given set of parameters. Use '{0}' type for this parameter set.</value>
125125
</data>
126+
<data name="InvalidId" xml:space="preserve">
127+
<value>'{0}' is not a valid Id.</value>
128+
</data>
129+
<data name="InvalidResourceId" xml:space="preserve">
130+
<value>'{0}' is not a valid ResourceId.</value>
131+
</data>
126132
<data name="RemoveActionRule_Action" xml:space="preserve">
127133
<value>Remove action rule</value>
128134
</data>

0 commit comments

Comments
 (0)