Skip to content

Commit aa50349

Browse files
authored
Merge pull request Azure#4474 from markcowl/abstractnet
Telemetry fixes, including restricted environments coverage
2 parents 6b82726 + f29e700 commit aa50349

File tree

20 files changed

+414
-708
lines changed

20 files changed

+414
-708
lines changed

setup/azurecmdfiles.wxi

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -932,9 +932,6 @@
932932
<Component Id="cmp6FA8F4C10D6E2F9D68A204B460AF71FE" Guid="*">
933933
<File Id="filA6898D50D80F756D8D4823D9D198700F" KeyPath="yes" Source="$(var.sourceDir)\ResourceManager\AzureResourceManager\AzureRM.ContainerRegistry\Newtonsoft.Json.dll" />
934934
</Component>
935-
<Component Id="cmp84A35F715EEA9EA92E697F6F16DA0551" Guid="*">
936-
<File Id="fil0A0E5309D6B8FB80AD11CFF5AD7B8E98" KeyPath="yes" Source="$(var.sourceDir)\ResourceManager\AzureResourceManager\AzureRM.ContainerRegistry\ResourceManagerStartup.ps1" />
937-
</Component>
938935
<Component Id="cmp20310052D0C97B0D3FF2569D32B059E2" Guid="*">
939936
<File Id="filA27432D947F8D24670028256C293BB88" KeyPath="yes" Source="$(var.sourceDir)\ResourceManager\AzureResourceManager\AzureRM.ContainerRegistry\System.Net.Http.Extensions.dll" />
940937
</Component>
@@ -2768,9 +2765,6 @@
27682765
<Component Id="cmp2BE59FD587F7E44466F41BF3189A9262" Guid="*">
27692766
<File Id="filB2A8EEA5653E4935802251294A7B9F18" KeyPath="yes" Source="$(var.sourceDir)\ResourceManager\AzureResourceManager\AzureRM.Resources\Newtonsoft.Json.dll" />
27702767
</Component>
2771-
<Component Id="cmp44074831AFB81C156E7ED6A30C9965D5" Guid="*">
2772-
<File Id="fil243D872BC193D82929CE3811BE0B4014" KeyPath="yes" Source="$(var.sourceDir)\ResourceManager\AzureResourceManager\AzureRM.Resources\ResourceManagerStartup.ps1" />
2773-
</Component>
27742768
<Component Id="cmpA534986DA2E43D7E50E9A6E219478266" Guid="*">
27752769
<File Id="filE2D02576DA0A4EEF0E8E1E2CFB76279A" KeyPath="yes" Source="$(var.sourceDir)\ResourceManager\AzureResourceManager\AzureRM.Resources\System.Net.Http.Extensions.dll" />
27762770
</Component>
@@ -5646,7 +5640,6 @@
56465640
<ComponentRef Id="cmp06C4EBF261D1884EA04708917383FFEC" />
56475641
<ComponentRef Id="cmp999FBA5405AD96F6312C0F17EB0C70C4" />
56485642
<ComponentRef Id="cmp6FA8F4C10D6E2F9D68A204B460AF71FE" />
5649-
<ComponentRef Id="cmp84A35F715EEA9EA92E697F6F16DA0551" />
56505643
<ComponentRef Id="cmp20310052D0C97B0D3FF2569D32B059E2" />
56515644
<ComponentRef Id="cmp72D0BC3F70A05243347A44BD2C5EBE05" />
56525645
<ComponentRef Id="cmp75C3D72B9C46E1B9D3F51CE3D29F6F00" />
@@ -6242,7 +6235,6 @@
62426235
<ComponentRef Id="cmpD9DA964D9A49738CFC38CD27734BA2D4" />
62436236
<ComponentRef Id="cmpD80193A27EAF0B72702BBA3F066DD099" />
62446237
<ComponentRef Id="cmp2BE59FD587F7E44466F41BF3189A9262" />
6245-
<ComponentRef Id="cmp44074831AFB81C156E7ED6A30C9965D5" />
62466238
<ComponentRef Id="cmpA534986DA2E43D7E50E9A6E219478266" />
62476239
<ComponentRef Id="cmp6D34FB8F9A9678D4511D4BB8E0800104" />
62486240
<ComponentRef Id="cmp890B1E2923E26CE2A15A652293EA92BE" />

src/Common/Commands.Common/AzureDataCmdlet.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,21 @@ protected override void SaveDataCollectionProfile()
102102
}
103103

104104
string fileFullPath = Path.Combine(AzureSession.Instance.ProfileDirectory, AzurePSDataCollectionProfile.DefaultFileName);
105-
var contents = JsonConvert.SerializeObject(_dataCollectionProfile);
106-
if (!AzureSession.Instance.DataStore.DirectoryExists(AzureSession.Instance.ProfileDirectory))
105+
try
107106
{
108-
AzureSession.Instance.DataStore.CreateDirectory(AzureSession.Instance.ProfileDirectory);
107+
var contents = JsonConvert.SerializeObject(_dataCollectionProfile);
108+
if (!AzureSession.Instance.DataStore.DirectoryExists(AzureSession.Instance.ProfileDirectory))
109+
{
110+
AzureSession.Instance.DataStore.CreateDirectory(AzureSession.Instance.ProfileDirectory);
111+
}
112+
113+
AzureSession.Instance.DataStore.WriteFile(fileFullPath, contents);
114+
WriteWarning(string.Format(Resources.DataCollectionSaveFileInformation, fileFullPath));
115+
}
116+
catch
117+
{
118+
// do not throw if the profile cannot be written
109119
}
110-
AzureSession.Instance.DataStore.WriteFile(fileFullPath, contents);
111-
WriteWarning(string.Format(Resources.DataCollectionSaveFileInformation, fileFullPath));
112120
}
113121

114122
protected override void SetDataCollectionProfileIfNotExists()

src/Common/Commands.Common/AzurePSCmdlet.cs

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ protected string PSVersion
7373
get
7474
{
7575
if (string.IsNullOrEmpty(_psVersion))
76-
{
77-
if(this.Host != null)
76+
{
77+
if (this.Host != null)
7878
{
7979
_psVersion = this.Host.Version.ToString();
8080
}
@@ -153,19 +153,33 @@ protected static void InitializeDataCollectionProfile()
153153
// If it exists, remove the old AzureDataCollectionProfile.json file
154154
string oldFileFullPath = Path.Combine(AzurePowerShell.ProfileDirectory,
155155
AzurePSDataCollectionProfile.OldDefaultFileName);
156-
if (AzureSession.Instance.DataStore.FileExists(oldFileFullPath))
156+
try
157+
{
158+
if (AzureSession.Instance.DataStore.FileExists(oldFileFullPath))
159+
{
160+
AzureSession.Instance.DataStore.DeleteFile(oldFileFullPath);
161+
}
162+
}
163+
catch
157164
{
158-
AzureSession.Instance.DataStore.DeleteFile(oldFileFullPath);
165+
// do not throw if the old file cannot be deleted
159166
}
160167

161168
// Try and read from the new AzurePSDataCollectionProfile.json file
162169
string fileFullPath = Path.Combine(AzurePowerShell.ProfileDirectory,
163170
AzurePSDataCollectionProfile.DefaultFileName);
164-
if (AzureSession.Instance.DataStore.FileExists(fileFullPath))
171+
try
172+
{
173+
if (AzureSession.Instance.DataStore.FileExists(fileFullPath))
174+
{
175+
string contents = AzureSession.Instance.DataStore.ReadFileAsText(fileFullPath);
176+
_dataCollectionProfile =
177+
JsonConvert.DeserializeObject<AzurePSDataCollectionProfile>(contents);
178+
}
179+
}
180+
catch
165181
{
166-
string contents = AzureSession.Instance.DataStore.ReadFileAsText(fileFullPath);
167-
_dataCollectionProfile =
168-
JsonConvert.DeserializeObject<AzurePSDataCollectionProfile>(contents);
182+
// do not throw if the data collection profile cannot be serialized
169183
}
170184
}
171185

@@ -284,7 +298,7 @@ protected virtual void TearDownDebuggingTraces()
284298

285299
protected virtual void SetupHttpClientPipeline()
286300
{
287-
AzureSession.Instance.ClientFactory.UserAgents.Add(new ProductInfoHeaderValue(ModuleName, string.Format("v{0}", ModuleVersion)));
301+
AzureSession.Instance.ClientFactory.UserAgents.Add(new ProductInfoHeaderValue(ModuleName, string.Format("v{0}", ModuleVersion)));
288302
AzureSession.Instance.ClientFactory.UserAgents.Add(new ProductInfoHeaderValue(PSVERSION, string.Format("v{0}", PSVersion)));
289303

290304
AzureSession.Instance.ClientFactory.AddHandler(
@@ -472,41 +486,48 @@ private void FlushDebugMessages(bool record = false)
472486

473487
private void RecordDebugMessages()
474488
{
475-
// Create 'ErrorRecords' folder under profile directory, if not exists
476-
if (string.IsNullOrEmpty(_errorRecordFolderPath)
477-
|| !Directory.Exists(_errorRecordFolderPath))
489+
try
478490
{
479-
_errorRecordFolderPath = Path.Combine(AzurePowerShell.ProfileDirectory,
480-
"ErrorRecords");
481-
Directory.CreateDirectory(_errorRecordFolderPath);
482-
}
491+
// Create 'ErrorRecords' folder under profile directory, if not exists
492+
if (string.IsNullOrEmpty(_errorRecordFolderPath)
493+
|| !Directory.Exists(_errorRecordFolderPath))
494+
{
495+
_errorRecordFolderPath = Path.Combine(AzurePowerShell.ProfileDirectory,
496+
"ErrorRecords");
497+
Directory.CreateDirectory(_errorRecordFolderPath);
498+
}
483499

484-
CommandInfo cmd = this.MyInvocation.MyCommand;
500+
CommandInfo cmd = this.MyInvocation.MyCommand;
485501

486-
string filePrefix = cmd.Name;
487-
string timeSampSuffix = DateTime.Now.ToString(_fileTimeStampSuffixFormat);
488-
string fileName = filePrefix + "_" + timeSampSuffix + ".log";
489-
string filePath = Path.Combine(_errorRecordFolderPath, fileName);
502+
string filePrefix = cmd.Name;
503+
string timeSampSuffix = DateTime.Now.ToString(_fileTimeStampSuffixFormat);
504+
string fileName = filePrefix + "_" + timeSampSuffix + ".log";
505+
string filePath = Path.Combine(_errorRecordFolderPath, fileName);
490506

491-
StringBuilder sb = new StringBuilder();
492-
sb.Append("Module : ").AppendLine(cmd.ModuleName);
493-
sb.Append("Cmdlet : ").AppendLine(cmd.Name);
507+
StringBuilder sb = new StringBuilder();
508+
sb.Append("Module : ").AppendLine(cmd.ModuleName);
509+
sb.Append("Cmdlet : ").AppendLine(cmd.Name);
494510

495-
sb.AppendLine("Parameters");
496-
foreach (var item in this.MyInvocation.BoundParameters)
497-
{
498-
sb.Append(" -").Append(item.Key).Append(" : ");
499-
sb.AppendLine(item.Value == null ? "null" : item.Value.ToString());
500-
}
511+
sb.AppendLine("Parameters");
512+
foreach (var item in this.MyInvocation.BoundParameters)
513+
{
514+
sb.Append(" -").Append(item.Key).Append(" : ");
515+
sb.AppendLine(item.Value == null ? "null" : item.Value.ToString());
516+
}
517+
518+
sb.AppendLine();
501519

502-
sb.AppendLine();
520+
foreach (var content in DebugMessages)
521+
{
522+
sb.AppendLine(content);
523+
}
503524

504-
foreach (var content in DebugMessages)
525+
AzureSession.Instance.DataStore.WriteFile(filePath, sb.ToString());
526+
}
527+
catch
505528
{
506-
sb.AppendLine(content);
529+
// do not throw an error if recording debug messages fails
507530
}
508-
509-
AzureSession.Instance.DataStore.WriteFile(filePath, sb.ToString());
510531
}
511532

512533
/// <summary>

src/Common/Commands.Common/Commands.Common.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
<Compile Include="Serialization\LegacyAzureSubscription.cs" />
129129
<Compile Include="Serialization\ModelConversionExtensions.cs" />
130130
<Compile Include="Utilities\FileUtilities.cs" />
131+
<Compile Include="Utilities\INetworkHelper.cs" />
131132
<Compile Include="Utilities\JsonUtilities.cs" />
132133
<Compile Include="AzureDataCmdlet.cs" />
133134
<Compile Include="AzurePSCmdlet.cs" />
@@ -148,6 +149,7 @@
148149
<Compile Include="Utilities\ConversionUtilities.cs" />
149150
<Compile Include="DebugStreamTraceListener.cs" />
150151
<Compile Include="Utilities\GeneralUtilities.cs" />
152+
<Compile Include="Utilities\NetworkHelper.cs" />
151153
<Compile Include="Utilities\PowerShellUtilities.cs" />
152154
<Compile Include="RecordingTracingInterceptor.cs" />
153155
<Compile Include="ClientCreatedArgs.cs" />

0 commit comments

Comments
 (0)