diff --git a/PrtgAPI.Tests.IntegrationTests/ActionTests/NotificationTriggerTests.cs b/PrtgAPI.Tests.IntegrationTests/ActionTests/NotificationTriggerTests.cs index f95c2055..f1eb7542 100644 --- a/PrtgAPI.Tests.IntegrationTests/ActionTests/NotificationTriggerTests.cs +++ b/PrtgAPI.Tests.IntegrationTests/ActionTests/NotificationTriggerTests.cs @@ -148,13 +148,13 @@ public void Action_NotificationTrigger_AddWithCustomization_Threshold() [TestMethod] public void Action_NotificationTrigger_CreateFromExistingTrigger_State() { - AddRemoveTriggerFromExisting(TriggerType.State, trigger => new StateTriggerParameters(Settings.Device, trigger, ModifyAction.Add)); + AddRemoveTriggerFromExisting(TriggerType.State, trigger => new StateTriggerParameters(Settings.Device, trigger)); } [TestMethod] public void Action_NotificationTrigger_CreateFromExistingTrigger_Threshold_Device() { - AddRemoveTriggerFromExisting(TriggerType.Threshold, trigger => new ThresholdTriggerParameters(Settings.Device, trigger, ModifyAction.Add)); + AddRemoveTriggerFromExisting(TriggerType.Threshold, trigger => new ThresholdTriggerParameters(Settings.Device, trigger)); } [TestMethod] @@ -180,7 +180,7 @@ public void Action_NotificationTrigger_CreateFromExistingTrigger_Threshold_Senso //Clone the trigger - var newParameters = new ThresholdTriggerParameters(Settings.ChannelSensor, triggers.First(), ModifyAction.Add); + var newParameters = new ThresholdTriggerParameters(Settings.ChannelSensor, triggers.First()); client.AddNotificationTrigger(newParameters); @@ -199,19 +199,19 @@ public void Action_NotificationTrigger_CreateFromExistingTrigger_Threshold_Senso [TestMethod] public void Action_NotificationTrigger_CreateFromExistingTrigger_Speed() { - AddRemoveTriggerFromExisting(TriggerType.Speed, trigger => new SpeedTriggerParameters(Settings.Device, trigger, ModifyAction.Add)); + AddRemoveTriggerFromExisting(TriggerType.Speed, trigger => new SpeedTriggerParameters(Settings.Device, trigger)); } [TestMethod] public void Action_NotificationTrigger_CreateFromExistingTrigger_Volume() { - AddRemoveTriggerFromExisting(TriggerType.Volume, trigger => new VolumeTriggerParameters(Settings.Device, trigger, ModifyAction.Add)); + AddRemoveTriggerFromExisting(TriggerType.Volume, trigger => new VolumeTriggerParameters(Settings.Device, trigger)); } [TestMethod] public void Action_NotificationTrigger_CreateFromExistingTrigger_Change() { - AddRemoveTriggerFromExisting(TriggerType.Change, trigger => new ChangeTriggerParameters(Settings.Device, trigger, ModifyAction.Add)); + AddRemoveTriggerFromExisting(TriggerType.Change, trigger => new ChangeTriggerParameters(Settings.Device, trigger)); } private void AddRemoveTriggerFromExisting(TriggerType triggerType, Func getParameters) diff --git a/PrtgAPI.Tests.IntegrationTests/PowerShell/Edit-NotificationTriggerProperty.Tests.ps1 b/PrtgAPI.Tests.IntegrationTests/PowerShell/Edit-NotificationTriggerProperty.Tests.ps1 index 90147296..075fecb2 100644 --- a/PrtgAPI.Tests.IntegrationTests/PowerShell/Edit-NotificationTriggerProperty.Tests.ps1 +++ b/PrtgAPI.Tests.IntegrationTests/PowerShell/Edit-NotificationTriggerProperty.Tests.ps1 @@ -42,6 +42,30 @@ Describe "Edit-NotificationTriggerProperty_IT" { { $trigger | Edit-NotificationTriggerProperty Channel "blah" } | Should Throw "Object of type 'System.String' cannot be converted to type 'PrtgAPI.TriggerChannel'" } + It "can assign an enum value" { + $trigger = Get-Device -Id (Settings Device) | Get-NotificationTrigger -Type State -Inherited $false + $trigger.Count | Should Be 1 + + $trigger | Edit-NotificationTriggerProperty State Warning + + $newTrigger = Get-Device -Id (Settings Device) | Get-NotificationTrigger -Type State -Inherited $false + + $newTrigger.Threshold | Should Be Warning + + #todo: should we maybe make state public? + } + + It "throws editing an inherited notification trigger" { + $device = Get-Device -Id (Settings Device) + + $trigger = @($device | Get-Trigger | where Inherited -EQ $true) + + $trigger.Count | Should Be 1 + $trigger.Threshold | Should Be "Down" + + { $trigger | Edit-TriggerProperty State Warning } | Should Throw "this trigger is inherited" + } + It "throws setting an Channel TriggerChannel on a device" { $device = Get-Device -Id (Settings Device) @@ -71,7 +95,7 @@ Describe "Edit-NotificationTriggerProperty_IT" { } finally { - $trigger | Remove-Trigger + $trigger | Remove-Trigger -Force } } @@ -96,7 +120,7 @@ Describe "Edit-NotificationTriggerProperty_IT" { } finally { - $trigger | Remove-Trigger + $trigger | Remove-Trigger -Force } } } \ No newline at end of file diff --git a/PrtgAPI.Tests.UnitTests/ObjectTests/NotificationTriggerParameterTests.cs b/PrtgAPI.Tests.UnitTests/ObjectTests/NotificationTriggerParameterTests.cs index 7dd40ebe..7dfa65de 100644 --- a/PrtgAPI.Tests.UnitTests/ObjectTests/NotificationTriggerParameterTests.cs +++ b/PrtgAPI.Tests.UnitTests/ObjectTests/NotificationTriggerParameterTests.cs @@ -323,7 +323,7 @@ private void TriggerParameters_AllProperties_HaveValues(TriggerParameters parame public void StateTriggerParameters_Create_FromExistingTrigger() { var trigger = GetMultipleItems().First(t => t.Type == TriggerType.State); - var parameters = new StateTriggerParameters(1234, trigger, ModifyAction.Add); + var parameters = new StateTriggerParameters(1234, trigger); TriggerParameters_Create_FromExistingTrigger(trigger, parameters); } @@ -332,7 +332,7 @@ public void StateTriggerParameters_Create_FromExistingTrigger() public void ChangeTriggerParameters_Create_FromExistingTrigger() { var trigger = GetMultipleItems().First(t => t.Type == TriggerType.Change); - var parameters = new ChangeTriggerParameters(1234, trigger, ModifyAction.Add); + var parameters = new ChangeTriggerParameters(1234, trigger); TriggerParameters_Create_FromExistingTrigger(trigger, parameters); } @@ -341,7 +341,7 @@ public void ChangeTriggerParameters_Create_FromExistingTrigger() public void VolumeTriggerParameters_Create_FromExistingTrigger() { var trigger = GetMultipleItems().First(t => t.Type == TriggerType.Volume); - var parameters = new VolumeTriggerParameters(1234, trigger, ModifyAction.Add); + var parameters = new VolumeTriggerParameters(1234, trigger); TriggerParameters_Create_FromExistingTrigger(trigger, parameters); } @@ -350,7 +350,7 @@ public void VolumeTriggerParameters_Create_FromExistingTrigger() public void SpeedTriggerParameters_Create_FromExistingTrigger() { var trigger = GetMultipleItems().First(t => t.Type == TriggerType.Speed); - var parameters = new SpeedTriggerParameters(1234, trigger, ModifyAction.Add); + var parameters = new SpeedTriggerParameters(1234, trigger); TriggerParameters_Create_FromExistingTrigger(trigger, parameters); } @@ -359,7 +359,7 @@ public void SpeedTriggerParameters_Create_FromExistingTrigger() public void ThresholdTriggerParameters_Create_FromExistingTrigger() { var trigger = GetMultipleItems().First(t => t.Type == TriggerType.Threshold); - var parameters = new ThresholdTriggerParameters(1234, trigger, ModifyAction.Add); + var parameters = new ThresholdTriggerParameters(1234, trigger); TriggerParameters_Create_FromExistingTrigger(trigger, parameters); } @@ -368,7 +368,7 @@ public void ThresholdTriggerParameters_Create_FromExistingTrigger() [ExpectedException(typeof(ArgumentNullException))] public void TriggerParameters_Create_FromNullTrigger() { - var parameters = new StateTriggerParameters(1234, null, ModifyAction.Add); + var parameters = new StateTriggerParameters(1234, null); } [TestMethod] @@ -376,7 +376,7 @@ public void TriggerParameters_Create_FromNullTrigger() public void TriggerParameters_Create_FromInvalidTriggerType() { var trigger = GetMultipleItems().First(t => t.Type == TriggerType.State); - var parameters = new ChangeTriggerParameters(1234, trigger, ModifyAction.Add); + var parameters = new ChangeTriggerParameters(1234, trigger); } private void TriggerParameters_Create_FromExistingTrigger(NotificationTrigger trigger, TriggerParameters parameters) diff --git a/PrtgAPI.Tests.UnitTests/PowerShell/New-NotificationTriggerParameter.Tests.ps1 b/PrtgAPI.Tests.UnitTests/PowerShell/New-NotificationTriggerParameter.Tests.ps1 index 10f33d51..11c37163 100644 --- a/PrtgAPI.Tests.UnitTests/PowerShell/New-NotificationTriggerParameter.Tests.ps1 +++ b/PrtgAPI.Tests.UnitTests/PowerShell/New-NotificationTriggerParameter.Tests.ps1 @@ -26,9 +26,14 @@ Describe "New-NotificationTriggerParameter" { } It "can create EditFrom parameter set" { - $triggers.Count | Should Be 1 + $device2 = Run Device { Get-Device } + $device2.Id = 0 + + $trigger = Run NotificationTrigger { $device2 | Get-NotificationTrigger -Type State } + + $trigger.Count | Should Be 1 - $parameters = $triggers | New-TriggerParameter + $parameters = $trigger | New-TriggerParameter $parameters.Action | Should Be Edit } @@ -66,4 +71,10 @@ Describe "New-NotificationTriggerParameter" { { $parameters.Channel = "Banana" } | Should Throw "type must be convertable" } + + It "throws creating parameters from an inherited trigger" { + $triggers.Count | Should Be 1 + + { $triggers | New-TriggerParameter } | Should Throw "trigger is inherited" + } } \ No newline at end of file diff --git a/PrtgAPI/Objects/NotificationTrigger.cs b/PrtgAPI/Objects/NotificationTrigger.cs index 220ae461..d06b904c 100644 --- a/PrtgAPI/Objects/NotificationTrigger.cs +++ b/PrtgAPI/Objects/NotificationTrigger.cs @@ -152,7 +152,7 @@ public string Unit private int? threshold; /// - /// Value threshold required before this notification is activated. + /// Value threshold or object state required before this notification is activated. /// Applies to: Threshold, Speed, Volume Triggers /// [DataMember(Name = "threshold")] diff --git a/PrtgAPI/Parameters/ObjectManipulation/Triggers/ChangeTriggerParameters.cs b/PrtgAPI/Parameters/ObjectManipulation/Triggers/ChangeTriggerParameters.cs index 84eebcf0..8dbcac59 100644 --- a/PrtgAPI/Parameters/ObjectManipulation/Triggers/ChangeTriggerParameters.cs +++ b/PrtgAPI/Parameters/ObjectManipulation/Triggers/ChangeTriggerParameters.cs @@ -18,19 +18,26 @@ public ChangeTriggerParameters(int objectId) : base(TriggerType.Change, objectId /// /// Initializes a new instance of the class for editing an existing notification trigger. /// - /// The object ID the trigger is applied to. + /// The object ID the trigger is applied to. Note: if the trigger is inherited, the ParentId should be specified. /// The sub ID of the trigger on its parent object. public ChangeTriggerParameters(int objectId, int triggerId) : base(TriggerType.Change, objectId, triggerId, ModifyAction.Edit) { } /// - /// Initializes a new instance of the class for editing or creating a new trigger from an existing . + /// Initializes a new instance of the class for creating a new trigger from an existing . /// /// The object ID the trigger will apply to. /// The notification trigger whose properties should be used. - /// Whether these parameters will create a new trigger or edit an existing one. - public ChangeTriggerParameters(int objectId, NotificationTrigger sourceTrigger, ModifyAction action) : base(TriggerType.Change, objectId, sourceTrigger, action) + public ChangeTriggerParameters(int objectId, NotificationTrigger sourceTrigger) : base(TriggerType.Change, objectId, sourceTrigger, ModifyAction.Add) + { + } + + /// + /// Initializes a new instance of the class for editing an existing . + /// + /// The notification trigger whose properties should be used. + public ChangeTriggerParameters(NotificationTrigger sourceTrigger) : base(TriggerType.Change, sourceTrigger.ObjectId, sourceTrigger, ModifyAction.Edit) { } } diff --git a/PrtgAPI/Parameters/ObjectManipulation/Triggers/SpeedTriggerParameters.cs b/PrtgAPI/Parameters/ObjectManipulation/Triggers/SpeedTriggerParameters.cs index 53ee67b2..bb27386d 100644 --- a/PrtgAPI/Parameters/ObjectManipulation/Triggers/SpeedTriggerParameters.cs +++ b/PrtgAPI/Parameters/ObjectManipulation/Triggers/SpeedTriggerParameters.cs @@ -102,30 +102,34 @@ public SpeedTriggerParameters(int objectId) : base(TriggerType.Speed, objectId, /// /// Initializes a new instance of the class for editing an existing notification trigger. /// - /// The object ID the trigger is applied to. + /// The object ID the trigger is applied to. Note: if the trigger is inherited, the ParentId should be specified. /// The sub ID of the trigger on its parent object. public SpeedTriggerParameters(int objectId, int triggerId) : base(TriggerType.Speed, objectId, triggerId, ModifyAction.Edit) { } /// - /// Initializes a new instance of the class for editing or creating a new trigger from an existing . + /// Initializes a new instance of the class for creating a new trigger from an existing . /// /// The object ID the trigger will apply to. /// The notification trigger whose properties should be used. - /// Whether these parameters will create a new trigger or edit an existing one. - public SpeedTriggerParameters(int objectId, NotificationTrigger sourceTrigger, ModifyAction action) : base(TriggerType.Speed, objectId, sourceTrigger, action) + public SpeedTriggerParameters(int objectId, NotificationTrigger sourceTrigger) : base(TriggerType.Speed, objectId, sourceTrigger, ModifyAction.Add) + { + OffNotificationAction = sourceTrigger.OffNotificationAction; + Channel = sourceTrigger.Channel; + Latency = sourceTrigger.Latency; + Condition = sourceTrigger.Condition; + Threshold = sourceTrigger.ThresholdInternal; + UnitTime = sourceTrigger.UnitTime; + UnitSize = sourceTrigger.UnitSize; + } + + /// + /// Initializes a new instance of the class for editing an existing . + /// + /// The notification trigger whose properties should be used. + public SpeedTriggerParameters(NotificationTrigger sourceTrigger) : base(TriggerType.Speed, sourceTrigger.ObjectId, sourceTrigger, ModifyAction.Edit) { - if (action == ModifyAction.Add) - { - OffNotificationAction = sourceTrigger.OffNotificationAction; - Channel = sourceTrigger.Channel; - Latency = sourceTrigger.Latency; - Condition = sourceTrigger.Condition; - Threshold = sourceTrigger.ThresholdInternal; - UnitTime = sourceTrigger.UnitTime; - UnitSize = sourceTrigger.UnitSize; - } } } } diff --git a/PrtgAPI/Parameters/ObjectManipulation/Triggers/StateTriggerParameters.cs b/PrtgAPI/Parameters/ObjectManipulation/Triggers/StateTriggerParameters.cs index 4ba528cb..74658e09 100644 --- a/PrtgAPI/Parameters/ObjectManipulation/Triggers/StateTriggerParameters.cs +++ b/PrtgAPI/Parameters/ObjectManipulation/Triggers/StateTriggerParameters.cs @@ -89,29 +89,33 @@ public StateTriggerParameters(int objectId) : base(TriggerType.State, objectId, /// /// Initializes a new instance of the class for editing an existing notification trigger. /// - /// The object ID the trigger is applied to. + /// The object ID the trigger is applied to. Note: if the trigger is inherited, the ParentId should be specified. /// The sub ID of the trigger on its parent object. public StateTriggerParameters(int objectId, int triggerId) : base(TriggerType.State, objectId, triggerId, ModifyAction.Edit) { } /// - /// Initializes a new instance of the class for editing or creating a new trigger from an existing . + /// Initializes a new instance of the class for creating a new trigger from an existing . /// /// The object ID the trigger will apply to. /// The notification trigger whose properties should be used. - /// Whether these parameters will create a new trigger or edit an existing one. - public StateTriggerParameters(int objectId, NotificationTrigger sourceTrigger, ModifyAction action) : base(TriggerType.State, objectId, sourceTrigger, action) + public StateTriggerParameters(int objectId, NotificationTrigger sourceTrigger) : base(TriggerType.State, objectId, sourceTrigger, ModifyAction.Add) + { + OffNotificationAction = sourceTrigger.OffNotificationAction; + EscalationNotificationAction = sourceTrigger.EscalationNotificationAction; + Latency = sourceTrigger.Latency; + EscalationLatency = sourceTrigger.EscalationLatency; + RepeatInterval = sourceTrigger.RepeatInterval; + State = sourceTrigger.StateTrigger; + } + + /// + /// Initializes a new instance of the class for editing an existing . + /// + /// The notification trigger whose properties should be used. + public StateTriggerParameters(NotificationTrigger sourceTrigger) : base(TriggerType.State, sourceTrigger.ObjectId, sourceTrigger, ModifyAction.Edit) { - if (action == ModifyAction.Add) - { - OffNotificationAction = sourceTrigger.OffNotificationAction; - EscalationNotificationAction = sourceTrigger.EscalationNotificationAction; - Latency = sourceTrigger.Latency; - EscalationLatency = sourceTrigger.EscalationLatency; - RepeatInterval = sourceTrigger.RepeatInterval; - State = sourceTrigger.StateTrigger; - } } } } diff --git a/PrtgAPI/Parameters/ObjectManipulation/Triggers/ThresholdTriggerParameters.cs b/PrtgAPI/Parameters/ObjectManipulation/Triggers/ThresholdTriggerParameters.cs index 7b4caa90..43be000a 100644 --- a/PrtgAPI/Parameters/ObjectManipulation/Triggers/ThresholdTriggerParameters.cs +++ b/PrtgAPI/Parameters/ObjectManipulation/Triggers/ThresholdTriggerParameters.cs @@ -87,28 +87,32 @@ public ThresholdTriggerParameters(int objectId) : base(TriggerType.Threshold, ob /// /// Initializes a new instance of the class for editing an existing notification trigger. /// - /// The object ID the trigger is applied to. + /// The object ID the trigger is applied to. Note: if the trigger is inherited, the ParentId should be specified. /// The sub ID of the trigger on its parent object. public ThresholdTriggerParameters(int objectId, int triggerId) : base(TriggerType.Threshold, objectId, triggerId, ModifyAction.Edit) { } /// - /// Initializes a new instance of the class for editing or creating a new trigger from an existing . + /// Initializes a new instance of the class for creating a new trigger from an existing . /// /// The object ID the trigger will apply to. /// The notification trigger whose properties should be used. - /// Whether these parameters will create a new trigger or edit an existing one. - public ThresholdTriggerParameters(int objectId, NotificationTrigger sourceTrigger, ModifyAction action) : base(TriggerType.Threshold, objectId, sourceTrigger, action) + public ThresholdTriggerParameters(int objectId, NotificationTrigger sourceTrigger) : base(TriggerType.Threshold, objectId, sourceTrigger, ModifyAction.Add) + { + OffNotificationAction = sourceTrigger.OffNotificationAction; + Latency = sourceTrigger.Latency; + Threshold = sourceTrigger.ThresholdInternal; + Condition = sourceTrigger.Condition; + Channel = sourceTrigger.Channel; + } + + /// + /// Initializes a new instance of the class for editing an existing . + /// + /// The notification trigger to modify. + public ThresholdTriggerParameters(NotificationTrigger sourceTrigger) : base(TriggerType.Threshold, sourceTrigger.ObjectId, sourceTrigger, ModifyAction.Edit) { - if (action == ModifyAction.Add) - { - OffNotificationAction = sourceTrigger.OffNotificationAction; - Latency = sourceTrigger.Latency; - Threshold = sourceTrigger.ThresholdInternal; - Condition = sourceTrigger.Condition; - Channel = sourceTrigger.Channel; - } } } } diff --git a/PrtgAPI/Parameters/ObjectManipulation/Triggers/TriggerParameters.cs b/PrtgAPI/Parameters/ObjectManipulation/Triggers/TriggerParameters.cs index f08aee6a..daba0ee2 100644 --- a/PrtgAPI/Parameters/ObjectManipulation/Triggers/TriggerParameters.cs +++ b/PrtgAPI/Parameters/ObjectManipulation/Triggers/TriggerParameters.cs @@ -106,6 +106,11 @@ protected TriggerParameters(TriggerType type, int objectId, NotificationTrigger { OnNotificationAction = sourceTrigger.OnNotificationAction; } + else + { + if (sourceTrigger.Inherited) + throw new InvalidOperationException($"Cannot modify NotificationTrigger '{sourceTrigger.OnNotificationAction}' applied to object ID {sourceTrigger.ObjectId} as this trigger is inherited from object ID {sourceTrigger.ParentId}. To modify this trigger, retrieve it from its parent object"); + } } /// diff --git a/PrtgAPI/Parameters/ObjectManipulation/Triggers/VolumeTriggerParameters.cs b/PrtgAPI/Parameters/ObjectManipulation/Triggers/VolumeTriggerParameters.cs index 0dca530c..200803fa 100644 --- a/PrtgAPI/Parameters/ObjectManipulation/Triggers/VolumeTriggerParameters.cs +++ b/PrtgAPI/Parameters/ObjectManipulation/Triggers/VolumeTriggerParameters.cs @@ -65,32 +65,36 @@ public VolumeTriggerParameters(int objectId) : base(TriggerType.Volume, objectId /// /// Initializes a new instance of the class for editing an existing notification trigger. /// - /// The object ID the trigger is applied to. + /// The object ID the trigger is applied to. Note: if the trigger is inherited, the ParentId should be specified. /// The sub ID of the trigger on its parent object. public VolumeTriggerParameters(int objectId, int triggerId) : base(TriggerType.Volume, objectId, triggerId, ModifyAction.Edit) { } /// - /// Initializes a new instance of the class for editing or creating a new trigger from an existing . + /// Initializes a new instance of the class for creating a new trigger from an existing . /// /// The object ID the trigger will apply to. /// The notification trigger whose properties should be used. - /// Whether these parameters will create a new trigger or edit an existing one. - public VolumeTriggerParameters(int objectId, NotificationTrigger sourceTrigger, ModifyAction action) : base(TriggerType.Volume, objectId, sourceTrigger, action) + public VolumeTriggerParameters(int objectId, NotificationTrigger sourceTrigger) : base(TriggerType.Volume, objectId, sourceTrigger, ModifyAction.Add) { - if (action == ModifyAction.Add) - { - Channel = sourceTrigger.Channel; - Period = sourceTrigger.Period; + Channel = sourceTrigger.Channel; + Period = sourceTrigger.Period; - if (sourceTrigger.UnitSize == null) - UnitSize = null; - else - UnitSize = sourceTrigger.UnitSize.ToString().ToEnum(); + if (sourceTrigger.UnitSize == null) + UnitSize = null; + else + UnitSize = sourceTrigger.UnitSize.ToString().ToEnum(); - Threshold = sourceTrigger.ThresholdInternal; - } + Threshold = sourceTrigger.ThresholdInternal; + } + + /// + /// Initializes a new instance of the class for editing an existing . + /// + /// The notification trigger to modify. + public VolumeTriggerParameters(NotificationTrigger sourceTrigger) : base(TriggerType.Volume, sourceTrigger.ObjectId, sourceTrigger, ModifyAction.Edit) + { } } } diff --git a/PrtgAPI/PowerShell/Base/PrtgOperationCmdlet.cs b/PrtgAPI/PowerShell/Base/PrtgOperationCmdlet.cs index a72339b6..9d74f9f6 100644 --- a/PrtgAPI/PowerShell/Base/PrtgOperationCmdlet.cs +++ b/PrtgAPI/PowerShell/Base/PrtgOperationCmdlet.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Reflection; namespace PrtgAPI.PowerShell.Base @@ -69,6 +70,20 @@ protected object ParseValueIfRequired(PropertyInfo property, object value) } } } + else + { + //Try and parse the value if the property type is an Enum or Nullable Enum + var type = property.PropertyType; + + if (!type.IsEnum) + type = Nullable.GetUnderlyingType(property.PropertyType); + + if (type?.IsEnum == true) + { + if (Enum.GetNames(type).Any(e => e.ToLower() == value?.ToString().ToLower())) + return Enum.Parse(type, value.ToString(), true); + } + } return value; } diff --git a/PrtgAPI/PowerShell/Cmdlets/ObjectManipulation/EditNotificationTriggerProperty.cs b/PrtgAPI/PowerShell/Cmdlets/ObjectManipulation/EditNotificationTriggerProperty.cs index 86e8bbfb..9149b30b 100644 --- a/PrtgAPI/PowerShell/Cmdlets/ObjectManipulation/EditNotificationTriggerProperty.cs +++ b/PrtgAPI/PowerShell/Cmdlets/ObjectManipulation/EditNotificationTriggerProperty.cs @@ -43,19 +43,19 @@ protected override void ProcessRecordEx() switch (Trigger.Type) { case TriggerType.Change: - SetProperty(new ChangeTriggerParameters(Trigger.ObjectId, Trigger.SubId)); + SetProperty(new ChangeTriggerParameters(Trigger)); break; case TriggerType.Speed: - SetProperty(new SpeedTriggerParameters(Trigger.ObjectId, Trigger.SubId)); + SetProperty(new SpeedTriggerParameters(Trigger)); break; case TriggerType.State: - SetProperty(new StateTriggerParameters(Trigger.ObjectId, Trigger.SubId)); + SetProperty(new StateTriggerParameters(Trigger)); break; case TriggerType.Threshold: - SetProperty(new ThresholdTriggerParameters(Trigger.ObjectId, Trigger.SubId)); + SetProperty(new ThresholdTriggerParameters(Trigger)); break; case TriggerType.Volume: - SetProperty(new VolumeTriggerParameters(Trigger.ObjectId, Trigger.SubId)); + SetProperty(new VolumeTriggerParameters(Trigger)); break; default: throw new NotImplementedException($"Handler of trigger type '{Trigger.Type}' is not implemented."); diff --git a/PrtgAPI/PowerShell/Cmdlets/ObjectManipulation/NewNotificationTriggerParameter.cs b/PrtgAPI/PowerShell/Cmdlets/ObjectManipulation/NewNotificationTriggerParameter.cs index daa93b51..a538ae3d 100644 --- a/PrtgAPI/PowerShell/Cmdlets/ObjectManipulation/NewNotificationTriggerParameter.cs +++ b/PrtgAPI/PowerShell/Cmdlets/ObjectManipulation/NewNotificationTriggerParameter.cs @@ -1,5 +1,6 @@ using System; using System.Management.Automation; +using System.Reflection; using PrtgAPI.Parameters; namespace PrtgAPI.PowerShell.Cmdlets @@ -122,68 +123,55 @@ public class NewNotificationTriggerParameter : PSCmdlet /// protected override void ProcessRecord() { - ModifyAction? action = null; - - //we need to write tests for all the different valid and invalid argument scenarios and try and break it - - switch (ParameterSetName) - { - case "Add": - case "AddFrom": - action = ModifyAction.Add; - break; - case "Edit": - case "EditFrom": - action = ModifyAction.Edit; - break; - default: - throw new NotImplementedException(); - } - - //need to update the trigger constructor to allow edit mode. also need to update readme - //write some unit/integration tests. the unit tests will just - - //var action = ParameterSetName == "Edit" ? ModifyAction.Edit : ModifyAction.Add; - if (Source != null) Type = Source.Type; switch (Type) { case TriggerType.Change: - WriteObject(CreateParameters(action.Value)); + WriteObject(CreateParameters()); break; case TriggerType.Speed: - WriteObject(CreateParameters(action.Value)); + WriteObject(CreateParameters()); break; case TriggerType.State: - WriteObject(CreateParameters(action.Value)); + WriteObject(CreateParameters()); break; case TriggerType.Threshold: - WriteObject(CreateParameters(action.Value)); + WriteObject(CreateParameters()); break; case TriggerType.Volume: - WriteObject(CreateParameters(action.Value)); + WriteObject(CreateParameters()); break; default: throw new NotImplementedException($"Handler of trigger type '{Type}' is not implemented."); } } - private T CreateParameters(ModifyAction action) + private T CreateParameters() { - if (Source != null) + try { - if (Id != null) - return (T) Activator.CreateInstance(typeof (T), Id, Source, action); //Add from an existing notification trigger + if (Source != null) + { + if (Id != null) + return (T)Activator.CreateInstance(typeof(T), Id, Source); //Add from an existing notification trigger + else + return (T)Activator.CreateInstance(typeof(T), Source); //Edit from an existing notification trigger + } + + if (TriggerId != null) + return (T)Activator.CreateInstance(typeof(T), Id, TriggerId); //Edit a notification trigger else - return (T) Activator.CreateInstance(typeof (T), Source.ObjectId, Source.SubId); //Edit from an existing notification trigger + return (T)Activator.CreateInstance(typeof(T), Id); //Create a new notification trigger + } + catch(TargetInvocationException ex) + { + if (ex.InnerException != null) + throw ex.InnerException; + + throw; } - - if (TriggerId != null) - return (T) Activator.CreateInstance(typeof (T), Id, TriggerId); //Edit a notification trigger - else - return (T) Activator.CreateInstance(typeof (T), Id); //Create a new notification trigger } } } diff --git a/PrtgAPI/Properties/AssemblyFileVersion.cs b/PrtgAPI/Properties/AssemblyFileVersion.cs index 08bbb3b8..d8c923fb 100644 --- a/PrtgAPI/Properties/AssemblyFileVersion.cs +++ b/PrtgAPI/Properties/AssemblyFileVersion.cs @@ -1,6 +1,6 @@ //0 //7 -//-1 +//0 //0 // // This code was generated by a tool. Any changes made manually will be lost