Skip to content

Commit b114a0e

Browse files
committed
Merge pull request #188 from AuxMon/dev
BUG: 3410517 [Event Service][PowerShell] cmdlets (e.g. Get-AzureSubscrip...
2 parents 65a4c3a + 071c258 commit b114a0e

File tree

6 files changed

+1444
-1318
lines changed

6 files changed

+1444
-1318
lines changed

src/ResourceManager/Insights/Commands.Insights.Test/Commands.Insights.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<HintPath>..\..\..\packages\Microsoft.Azure.Common.2.0.2\lib\net45\Microsoft.Azure.Common.NetFramework.dll</HintPath>
5959
</Reference>
6060
<Reference Include="Microsoft.Azure.Insights">
61-
<HintPath>..\..\..\packages\Microsoft.Azure.Insights.0.7.0-preview\lib\net45\Microsoft.Azure.Insights.dll</HintPath>
61+
<HintPath>..\..\..\packages\Microsoft.Azure.Insights.0.7.1-preview\lib\net45\Microsoft.Azure.Insights.dll</HintPath>
6262
</Reference>
6363
<Reference Include="Microsoft.Azure.Test.Framework, Version=1.0.5513.27084, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6464
<SpecificVersion>False</SpecificVersion>

src/ResourceManager/Insights/Commands.Insights.Test/Events/Utilities.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,15 @@ public static void ExecuteVerifications(EventCmdletBase cmdlet, Mock<IEventOpera
214214
Assert.True(string.Equals(PSEventDataNoDetails.SelectedFieldsForQuery, selected, StringComparison.OrdinalIgnoreCase), "Incorrect selected clause without optional parameters");
215215

216216
// Calling with only start date
217-
cmdlet.StartTime = startDate.ToString("O");
217+
cmdlet.StartTime = startDate;
218218
cmdlet.ExecuteCmdlet();
219219

220220
VerifyFilterIsUsable(filter: filter);
221221
VerifyStartDateInFilter(filter: filter, startDate: startDate);
222222
VerifyConditionInFilter(filter: filter, field: requiredFieldName, value: requiredFieldValue);
223223

224224
// Calling with only start and end date
225-
cmdlet.EndTime = startDate.AddSeconds(2).ToString("O");
225+
cmdlet.EndTime = startDate.AddSeconds(2);
226226
cmdlet.ExecuteCmdlet();
227227

228228
VerifyFilterIsUsable(filter: filter);
@@ -245,6 +245,18 @@ public static void ExecuteVerifications(EventCmdletBase cmdlet, Mock<IEventOpera
245245

246246
VerifyDetailedOutput(cmdlet: cmdlet, selected: ref selected);
247247
VerifyContinuationToken(response: response, insinsightsEventOperationsMockightsClientMock: insinsightsEventOperationsMockightsClientMock, cmdlet: cmdlet);
248+
249+
// Execute negative tests
250+
cmdlet.StartTime = DateTime.Now.AddSeconds(1);
251+
Assert.Throws<ArgumentException>(() => cmdlet.ExecuteCmdlet());
252+
253+
cmdlet.StartTime = DateTime.Now.Subtract(TimeSpan.FromSeconds(20));
254+
cmdlet.EndTime = DateTime.Now.Subtract(TimeSpan.FromSeconds(21));
255+
Assert.Throws<ArgumentException>(() => cmdlet.ExecuteCmdlet());
256+
257+
cmdlet.StartTime = DateTime.Now.Subtract(TimeSpan.FromDays(30));
258+
cmdlet.EndTime = DateTime.Now.Subtract(TimeSpan.FromDays(14));
259+
Assert.Throws<ArgumentException>(() => cmdlet.ExecuteCmdlet());
248260
}
249261
}
250262
}

src/ResourceManager/Insights/Commands.Insights/Commands.Insights.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<HintPath>..\..\..\packages\Microsoft.Azure.Common.2.0.2\lib\net45\Microsoft.Azure.Common.NetFramework.dll</HintPath>
5959
</Reference>
6060
<Reference Include="Microsoft.Azure.Insights">
61-
<HintPath>..\..\..\packages\Microsoft.Azure.Insights.0.7.0-preview\lib\net45\Microsoft.Azure.Insights.dll</HintPath>
61+
<HintPath>..\..\..\packages\Microsoft.Azure.Insights.0.7.1-preview\lib\net45\Microsoft.Azure.Insights.dll</HintPath>
6262
</Reference>
6363
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=2.12.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6464
<SpecificVersion>False</SpecificVersion>

src/ResourceManager/Insights/Commands.Insights/EventCmdletBase.cs

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using System;
1616
using System.Collections.Generic;
17+
using System.Globalization;
1718
using System.Linq;
1819
using System.Management.Automation;
1920
using System.Threading;
@@ -27,9 +28,9 @@ namespace Microsoft.Azure.Commands.Insights
2728
/// </summary>
2829
public abstract class EventCmdletBase : InsightsCmdletBase
2930
{
30-
internal static readonly TimeSpan DefaultQueryTimeRange = TimeSpan.FromHours(-1);
31-
32-
internal static int MaxNumberOfReturnedRecords = 100000;
31+
private static readonly TimeSpan MaximumDateDifferenceAllowedInDays = TimeSpan.FromDays(15);
32+
private static readonly TimeSpan DefaultQueryTimeRange = TimeSpan.FromHours(1);
33+
private const int MaxNumberOfReturnedRecords = 100000;
3334

3435
internal const string SubscriptionLevelName = "Query at subscription level";
3536
internal const string ResourceProviderName = "Query on ResourceProvider";
@@ -43,15 +44,13 @@ public abstract class EventCmdletBase : InsightsCmdletBase
4344
/// Gets or sets the starttime parameter of the cmdlet
4445
/// </summary>
4546
[Parameter(ValueFromPipelineByPropertyName = true, HelpMessage = "The startTime of the query")]
46-
[ValidateNotNullOrEmpty]
47-
public string StartTime { get; set; }
47+
public DateTime? StartTime { get; set; }
4848

4949
/// <summary>
5050
/// Gets or sets the endtime parameter of the cmdlet
5151
/// </summary>
5252
[Parameter(ValueFromPipelineByPropertyName = true, HelpMessage = "The endTime of the query")]
53-
[ValidateNotNullOrEmpty]
54-
public string EndTime { get; set; }
53+
public DateTime? EndTime { get; set; }
5554

5655
/// <summary>
5756
/// Gets or sets the status parameter of the cmdlet
@@ -90,39 +89,55 @@ protected string AddConditionIfPResent(string currentQueryFilter, string name, s
9089
}
9190

9291
/// <summary>
93-
/// Process the parameters defined by this class
92+
/// Gets the default query time range
9493
/// </summary>
95-
/// <returns>The query filter with the conditions for general parameters (i.e. defined by this class) added</returns>
96-
private string ProcessGeneralParameters()
94+
/// <returns>The default query time range for this class</returns>
95+
protected virtual TimeSpan GetDefaultQueryTimeRange()
96+
{
97+
return DefaultQueryTimeRange;
98+
}
99+
100+
/// <summary>
101+
/// Validates that the range of dates (start / end) makes sense, it is not to great (less 15 days), and adds the defaul values if needed
102+
/// </summary>
103+
/// <returns>A query filter string with the time conditions</returns>
104+
private string ValidateDateTimeRangeAndAdddefaults()
97105
{
98-
DateTime startTime;
99-
if (string.IsNullOrWhiteSpace(this.StartTime))
106+
// EndTime is optional
107+
DateTime endTime = this.EndTime.HasValue ? this.EndTime.Value : DateTime.Now;
108+
109+
// StartTime is optional
110+
DateTime startTime = this.StartTime.HasValue ? this.StartTime.Value : endTime.Subtract(this.GetDefaultQueryTimeRange());
111+
112+
// Check the value of StartTime
113+
if (startTime > DateTime.Now)
100114
{
101-
// Default to one hour from Now
102-
startTime = DateTime.Now.Subtract(DefaultQueryTimeRange);
115+
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "StartDate {0:o} is invalid. It is later than Now: {1:o}", startTime, DateTime.Now));
103116
}
104-
else if (!DateTime.TryParse(this.StartTime, out startTime))
117+
118+
// Check that the dateTime range makes sense
119+
if (endTime < startTime)
105120
{
106-
throw new ArgumentException("Unable to parse startTime argument");
121+
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "EndDate {0:o} is earlier than StartDate {1:o}", endTime, startTime));
107122
}
108123

109-
string queryFilter;
110-
111-
// EndTime is optional
112-
if (string.IsNullOrWhiteSpace(this.EndTime))
124+
// Validate start and end dates difference is reasonable (<= MaximumDateDifferenceAllowedInDays)
125+
var dateDifference = endTime.Subtract(startTime);
126+
if (dateDifference > MaximumDateDifferenceAllowedInDays)
113127
{
114-
queryFilter = string.Format("eventTimestamp ge '{0:o}'", startTime.ToUniversalTime());
128+
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Start and end dates are too far appart. Separation {0} days. Maximum allowed {1} days", dateDifference.TotalDays, MaximumDateDifferenceAllowedInDays.TotalDays));
115129
}
116-
else
117-
{
118-
DateTime endTime;
119-
if (!DateTime.TryParse(this.EndTime, out endTime))
120-
{
121-
throw new ArgumentException("Unable to parse endTime argument");
122-
}
123130

124-
queryFilter = string.Format("eventTimestamp ge '{0:o}' and eventTimestamp le '{1:o}'", startTime.ToUniversalTime(), endTime.ToUniversalTime());
125-
}
131+
return string.Format("eventTimestamp ge '{0:o}' and eventTimestamp le '{1:o}'", startTime.ToUniversalTime(), endTime.ToUniversalTime());
132+
}
133+
134+
/// <summary>
135+
/// Process the parameters defined by this class
136+
/// </summary>
137+
/// <returns>The query filter with the conditions for general parameters (i.e. defined by this class) added</returns>
138+
private string ProcessGeneralParameters()
139+
{
140+
string queryFilter = this.ValidateDateTimeRangeAndAdddefaults();
126141

127142
// Include the status if present
128143
queryFilter = this.AddConditionIfPResent(queryFilter, "status", this.Status);

0 commit comments

Comments
 (0)