Skip to content

Commit a239d68

Browse files
authored
Add a missing file. (#13234)
1 parent 822b212 commit a239d68

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 Microsoft.Azure.PowerShell.Tools.AzPredictor;
16+
using Newtonsoft.Json;
17+
using System;
18+
using System.IO;
19+
20+
namespace Microsoft.Azure.PowerShell.Tools.AzPredictor.Profile
21+
{
22+
// See https://github.com/Azure/azure-powershell-common/blob/master/src/Authentication.Abstractions/AzurePSDataCollectionProfile.cs
23+
24+
/// <summary>
25+
/// The profile about data collection in Azure PowerShell
26+
/// </summary>
27+
public sealed class AzurePSDataCollectionProfile
28+
{
29+
private const string EnvironmentVariableName = "Azure_PS_Data_Collection";
30+
private const string DefaultFileName = "AzurePSDataCollectionProfile.json";
31+
32+
private static AzurePSDataCollectionProfile _instance;
33+
34+
/// <summary>
35+
/// Gets the singleton for <see cref="AzurePSDataCollectionProfile"/>
36+
/// </summary>
37+
public static AzurePSDataCollectionProfile Instance
38+
{
39+
get
40+
{
41+
if (_instance == null)
42+
{
43+
_instance = AzurePSDataCollectionProfile.CreateInstance();
44+
}
45+
46+
return _instance;
47+
}
48+
}
49+
50+
/// <summary>
51+
/// Gets an instance of <see cref="AzurePSDataCollectionProfile"/>
52+
/// </summary>
53+
public AzurePSDataCollectionProfile() : this(false)
54+
{ }
55+
56+
private AzurePSDataCollectionProfile(bool enable)
57+
{
58+
EnableAzureDataCollection = enable;
59+
}
60+
61+
/// <summary>
62+
/// Gets if the data collection is enabled.
63+
/// </summary>
64+
[JsonProperty(PropertyName = "enableAzureDataCollection")]
65+
public bool? EnableAzureDataCollection { get; private set; }
66+
67+
private static AzurePSDataCollectionProfile CreateInstance()
68+
{
69+
// Gets the profile about data collection as in Azure PowerShell.
70+
// See AzurePSDataCollectionProfile Initialize(IAzureSession session) in
71+
// https://github.com/Azure/azure-powershell-common/blob/master/src/Authentication.Abstractions/DataCollectionController.cs
72+
73+
AzurePSDataCollectionProfile result = new AzurePSDataCollectionProfile(true);
74+
75+
try
76+
{
77+
var environmentValue = Environment.GetEnvironmentVariable(AzurePSDataCollectionProfile.EnvironmentVariableName);
78+
79+
if (!string.IsNullOrWhiteSpace(environmentValue) && bool.TryParse(environmentValue, out bool enabled))
80+
{
81+
result.EnableAzureDataCollection = enabled;
82+
}
83+
else
84+
{
85+
var profileDirectory = Path.Combine(
86+
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
87+
AzPredictorConstants.AzureProfileDirectoryName);
88+
string dataPath = Path.Combine(profileDirectory, AzurePSDataCollectionProfile.DefaultFileName);
89+
90+
if (File.Exists(dataPath))
91+
{
92+
string contents = File.ReadAllText(dataPath);
93+
var localResult = JsonConvert.DeserializeObject<AzurePSDataCollectionProfile>(contents);
94+
if (localResult != null && localResult.EnableAzureDataCollection.HasValue)
95+
{
96+
result = localResult;
97+
}
98+
}
99+
}
100+
}
101+
catch
102+
{
103+
// do not throw for i/o or serialization errors
104+
}
105+
106+
return result;
107+
}
108+
}
109+
}

0 commit comments

Comments
 (0)