From 8490825b4abe6342d0da378181ff50f0fc92ec88 Mon Sep 17 00:00:00 2001 From: lordmilko Date: Thu, 16 Feb 2017 10:01:11 +1100 Subject: [PATCH] -Fixed a bug wherein cmdlets did not check a retry event handler had been added before attempting to remove one -Default RetryDelay is now 0 -Implemented a check that the PassHash returned is a valid PassHash and not a system status message --- .../InfrastructureTests/PrtgClientTests.cs | 9 +++++++++ .../InfrastructureTests/Support/PassHashResponse.cs | 9 ++++++++- PrtgAPI/PowerShell/Base/PrtgCmdlet.cs | 2 +- PrtgAPI/PowerShell/Resources/PrtgAPI.Format.ps1xml | 6 ++++++ PrtgAPI/Properties/AssemblyFileVersion.cs | 4 ++-- PrtgAPI/PrtgAPI.csproj | 4 ---- PrtgAPI/PrtgClient.cs | 7 +++++-- PrtgAPI/_Todo.cs | 4 ---- README.md | 3 +-- 9 files changed, 32 insertions(+), 16 deletions(-) diff --git a/PrtgAPI.Tests.UnitTests/InfrastructureTests/PrtgClientTests.cs b/PrtgAPI.Tests.UnitTests/InfrastructureTests/PrtgClientTests.cs index 5c9c964b..10d1af18 100644 --- a/PrtgAPI.Tests.UnitTests/InfrastructureTests/PrtgClientTests.cs +++ b/PrtgAPI.Tests.UnitTests/InfrastructureTests/PrtgClientTests.cs @@ -20,6 +20,15 @@ public void PrtgClient_Constructor_RetrievesPassHash() Assert.IsTrue(client.PassHash == "12345678"); } + [TestMethod] + [ExpectedException(typeof(PrtgRequestException))] + public void PrtgClient_Constructor_CantRetrievePassHash() + { + var webClient = new MockWebClient(new PassHashResponse("PRTG Network Monitor is starting")); + + var client = new PrtgClient("prtg.example.com", "username", "password", AuthMode.Password, webClient); + } + /*private T[] CreateNullResponseItem(T obj) { //var obj = Activator.CreateInstance(typeof (T)); diff --git a/PrtgAPI.Tests.UnitTests/InfrastructureTests/Support/PassHashResponse.cs b/PrtgAPI.Tests.UnitTests/InfrastructureTests/Support/PassHashResponse.cs index 48901bc6..6d8c3b8d 100644 --- a/PrtgAPI.Tests.UnitTests/InfrastructureTests/Support/PassHashResponse.cs +++ b/PrtgAPI.Tests.UnitTests/InfrastructureTests/Support/PassHashResponse.cs @@ -6,11 +6,18 @@ namespace PrtgAPI.Tests.UnitTests.InfrastructureTests.Support { class PassHashResponse : IWebResponse { + private string response; + + public PassHashResponse(string response = "12345678") + { + this.response = response; + } + public HttpStatusCode StatusCode { get; set; } public string GetResponseText(string address) { - return "12345678"; + return response; } public Task GetResponseTextStream(string address) diff --git a/PrtgAPI/PowerShell/Base/PrtgCmdlet.cs b/PrtgAPI/PowerShell/Base/PrtgCmdlet.cs index 97e8ce32..96b21046 100644 --- a/PrtgAPI/PowerShell/Base/PrtgCmdlet.cs +++ b/PrtgAPI/PowerShell/Base/PrtgCmdlet.cs @@ -49,7 +49,7 @@ private void AddEvent(Action item) private void RemoveEvent() { - if (!retryEventRemoved) + if (!retryEventRemoved && retryEventAdded) { lock (lockEvents) { diff --git a/PrtgAPI/PowerShell/Resources/PrtgAPI.Format.ps1xml b/PrtgAPI/PowerShell/Resources/PrtgAPI.Format.ps1xml index c4b1edfb..f0c508ab 100644 --- a/PrtgAPI/PowerShell/Resources/PrtgAPI.Format.ps1xml +++ b/PrtgAPI/PowerShell/Resources/PrtgAPI.Format.ps1xml @@ -421,6 +421,12 @@ PassHash + + RetryCount + + + RetryDelay + diff --git a/PrtgAPI/Properties/AssemblyFileVersion.cs b/PrtgAPI/Properties/AssemblyFileVersion.cs index 4d45ce70..e68ec961 100644 --- a/PrtgAPI/Properties/AssemblyFileVersion.cs +++ b/PrtgAPI/Properties/AssemblyFileVersion.cs @@ -1,7 +1,7 @@ //0 //6 //0 -//13 +//14 // // This code was generated by a tool. Any changes made manually will be lost // the next time this code is regenerated. @@ -9,4 +9,4 @@ using System.Reflection; -[assembly: AssemblyFileVersion("0.6.0.13")] +[assembly: AssemblyFileVersion("0.6.0.14")] diff --git a/PrtgAPI/PrtgAPI.csproj b/PrtgAPI/PrtgAPI.csproj index 2a6f9712..e82ee0e1 100644 --- a/PrtgAPI/PrtgAPI.csproj +++ b/PrtgAPI/PrtgAPI.csproj @@ -34,10 +34,6 @@ - diff --git a/PrtgAPI/PrtgClient.cs b/PrtgAPI/PrtgClient.cs index 3f23be1c..a854196a 100644 --- a/PrtgAPI/PrtgClient.cs +++ b/PrtgAPI/PrtgClient.cs @@ -50,12 +50,12 @@ public partial class PrtgClient /// /// The number of times to retry a request that times out while communicating with PRTG. /// - public int RetryCount { get; set; } = 0; + public int RetryCount { get; set; } /// /// The base delay (in seconds) between retrying a timed out request. Each successive failure of a given request will wait an additional multiple of this value. /// - public int RetryDelay { get; set; } = 1; + public int RetryDelay { get; set; } internal EventHandler retryRequest; @@ -333,6 +333,9 @@ private string GetPassHash(string password) var response = ExecuteRequest(JsonFunction.GetPassHash, parameters); + if(!Regex.Match(response, "^[0-9]+$").Success) + throw new PrtgRequestException($"Could not retrieve PassHash from PRTG Server. PRTG responded '{response}'"); + return response; } diff --git a/PrtgAPI/_Todo.cs b/PrtgAPI/_Todo.cs index 9e7ff69a..a4d9079a 100644 --- a/PrtgAPI/_Todo.cs +++ b/PrtgAPI/_Todo.cs @@ -10,8 +10,6 @@ namespace PrtgAPI Todo ---------------- - //BUG: if you connect while prtg is starting your passhash might be "PRTG Network Monitor is starting" - //maybe we should verify the passhash is all digits. if not, respond with whatever message it responded with //add a test that checks that anything that derives from prtgcmdlet does not override processrecord Project @@ -37,8 +35,6 @@ we can use remove-object on the root prtg object to test this -upload our empty settings file then say dont track it git update-index --assume-unchanged and --no-assume-unchanged - -test failed pester tests generate errors in appveyor - -maybe create a "filter" alias for new-searchfilter? -when we make a request to the ci server to do something when the core is running but the probe isnt, whats the response? diff --git a/README.md b/README.md index 3aa27811..33fd06e6 100644 --- a/README.md +++ b/README.md @@ -327,7 +327,6 @@ C:\> Get-Sensor | Select -First 1 | Get-Channel Name SensorId Id LastValue LimitsEnabled UpperErrorLimit LowerErrorLimit ErrorLimitMessage ---- -------- -- --------- ------------- --------------- --------------- ----------------- -Downtime 3001 -4 Total 3001 0 0.32 % True 95 PANIC!! PANIC!!! Processor 1 3001 1 <1 % False ``` @@ -435,7 +434,7 @@ $trigger = Get-Device | Get-NotificationTrigger *admin* -Inherited $false -Type $parameters = $trigger | New-NotificationTriggerParameter $parameters.Latency = 120 -$parameters | Set-NotificationTrigger +$parameters | Add-NotificationTrigger ``` ### Access Underlying Methods