diff --git a/tools/Az.Tools.Predictor/Az.Tools.Predictor/Profile/AzurePSDataCollectionProfile.cs b/tools/Az.Tools.Predictor/Az.Tools.Predictor/Profile/AzurePSDataCollectionProfile.cs new file mode 100644 index 000000000000..a31b7c1d319a --- /dev/null +++ b/tools/Az.Tools.Predictor/Az.Tools.Predictor/Profile/AzurePSDataCollectionProfile.cs @@ -0,0 +1,109 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +using Microsoft.Azure.PowerShell.Tools.AzPredictor; +using Newtonsoft.Json; +using System; +using System.IO; + +namespace Microsoft.Azure.PowerShell.Tools.AzPredictor.Profile +{ + // See https://github.com/Azure/azure-powershell-common/blob/master/src/Authentication.Abstractions/AzurePSDataCollectionProfile.cs + + /// + /// The profile about data collection in Azure PowerShell + /// + public sealed class AzurePSDataCollectionProfile + { + private const string EnvironmentVariableName = "Azure_PS_Data_Collection"; + private const string DefaultFileName = "AzurePSDataCollectionProfile.json"; + + private static AzurePSDataCollectionProfile _instance; + + /// + /// Gets the singleton for + /// + public static AzurePSDataCollectionProfile Instance + { + get + { + if (_instance == null) + { + _instance = AzurePSDataCollectionProfile.CreateInstance(); + } + + return _instance; + } + } + + /// + /// Gets an instance of + /// + public AzurePSDataCollectionProfile() : this(false) + { } + + private AzurePSDataCollectionProfile(bool enable) + { + EnableAzureDataCollection = enable; + } + + /// + /// Gets if the data collection is enabled. + /// + [JsonProperty(PropertyName = "enableAzureDataCollection")] + public bool? EnableAzureDataCollection { get; private set; } + + private static AzurePSDataCollectionProfile CreateInstance() + { + // Gets the profile about data collection as in Azure PowerShell. + // See AzurePSDataCollectionProfile Initialize(IAzureSession session) in + // https://github.com/Azure/azure-powershell-common/blob/master/src/Authentication.Abstractions/DataCollectionController.cs + + AzurePSDataCollectionProfile result = new AzurePSDataCollectionProfile(true); + + try + { + var environmentValue = Environment.GetEnvironmentVariable(AzurePSDataCollectionProfile.EnvironmentVariableName); + + if (!string.IsNullOrWhiteSpace(environmentValue) && bool.TryParse(environmentValue, out bool enabled)) + { + result.EnableAzureDataCollection = enabled; + } + else + { + var profileDirectory = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), + AzPredictorConstants.AzureProfileDirectoryName); + string dataPath = Path.Combine(profileDirectory, AzurePSDataCollectionProfile.DefaultFileName); + + if (File.Exists(dataPath)) + { + string contents = File.ReadAllText(dataPath); + var localResult = JsonConvert.DeserializeObject(contents); + if (localResult != null && localResult.EnableAzureDataCollection.HasValue) + { + result = localResult; + } + } + } + } + catch + { + // do not throw for i/o or serialization errors + } + + return result; + } + } +}