protected override void ProcessRecord() { base.ProcessRecord(); if (ShouldProcess("adding environment", Name)) { if (GraphEnvironment.BuiltInEnvironments.Keys.Any((k) => string.Equals(k, Name, StringComparison.CurrentCultureIgnoreCase))) { throw new InvalidOperationException( ErrorConstants.Message.CannotModifyBuiltInEnvironment.FormatCurrentCulture("add", Name)); } GraphSettings settings = this.GetContextSettings(); if (!settings.TryGetEnvironment(Name, out IGraphEnvironment newEnvironment)) { newEnvironment = new GraphEnvironment { Name = Name }; } newEnvironment.AzureADEndpoint = AzureADEndpoint.GetBaseUrl(); newEnvironment.GraphEndpoint = GraphEndpoint.GetBaseUrl(); newEnvironment.Type = GraphEnvironmentConstants.EnvironmentType.UserDefined; bool isSuccess = settings.TrySetEnvironment(newEnvironment, out IGraphEnvironment mergedEnvironment); // Update environment session object. if (isSuccess && GraphSession.Instance.Environment?.Name == mergedEnvironment?.Name) { GraphSession.Instance.Environment = mergedEnvironment; } WriteObject(mergedEnvironment); } }
public void ShouldRemoveSettingsFromConfiguredDataStore() { GraphSession.Initialize(() => new GraphSession()); // Arrange GraphSession.Instance.DataStore = new MockDataStore(); GraphSettings settings = new GraphSettings(ProtectedFileProvider.CreateFileProvider(Constants.SettingFilePath, FileProtection.SharedRead)); GraphEnvironment myNewCloudEnv = new GraphEnvironment { Name = "MyNewCloud", Type = GraphEnvironmentConstants.EnvironmentType.UserDefined, AzureADEndpoint = "https://login.MyNewCloud.com", GraphEndpoint = "https://graph.MyNewCloud.com" }; GraphEnvironment trialCloudEnv = new GraphEnvironment { Name = "TrialCloud", Type = GraphEnvironmentConstants.EnvironmentType.UserDefined, AzureADEndpoint = "https://login.TrialCloud.com", GraphEndpoint = "https://graph.TrialCloud.com" }; settings.TrySetEnvironment(myNewCloudEnv, out IGraphEnvironment mergedMyNewCloudEnv); settings.TrySetEnvironment(trialCloudEnv, out IGraphEnvironment mergedTrialCloudEnv); // Act settings.RemoveEnvironment(trialCloudEnv.Name); string settingsContent = GraphSession.Instance.DataStore.ReadFileAsText(Constants.SettingFilePath); // Assert Assert.NotEmpty(settingsContent); // 5 built-in + 1 user-defined Assert.Equal(6, settings.Environments.Count()); GraphSession.Reset(); }
protected override void ProcessRecord() { base.ProcessRecord(); if (ShouldProcess("updating environment", Name)) { if (GraphEnvironment.BuiltInEnvironments.Keys.Any((k) => string.Equals(k, Name, StringComparison.CurrentCultureIgnoreCase))) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Cannot change built-in environment {0}.", Name)); } GraphSettings settings = this.GetContextSettings(); if (!settings.TryGetEnvironment(Name, out IGraphEnvironment newEnvironment)) { newEnvironment = new GraphEnvironment { Name = Name }; } if (MyInvocation.BoundParameters.ContainsKey(nameof(AzureADEndpoint))) { newEnvironment.AzureADEndpoint = AzureADEndpoint.GetBaseUrl(); } if (MyInvocation.BoundParameters.ContainsKey(nameof(GraphEndpoint))) { newEnvironment.GraphEndpoint = GraphEndpoint.GetBaseUrl(); } ValidateEnvironment(newEnvironment); newEnvironment.Type = GraphEnvironmentConstants.EnvironmentType.UserDefined; bool isSuccess = settings.TrySetEnvironment(newEnvironment, out IGraphEnvironment mergedEnvironment); // Update environment session object. if (isSuccess && GraphSession.Instance.Environment?.Name == mergedEnvironment?.Name) { GraphSession.Instance.Environment = mergedEnvironment; } WriteObject(mergedEnvironment); } }
public void ShouldSaveSettingsToConfiguredDataStore() { GraphSession.Initialize(() => new GraphSession()); // Arrange GraphSession.Instance.DataStore = new MockDataStore(); GraphSettings settings = new GraphSettings(ProtectedFileProvider.CreateFileProvider(Constants.SettingFilePath, FileProtection.SharedRead)); GraphEnvironment userDefinedEnv = new GraphEnvironment { Name = "TestCloud", Type = GraphEnvironmentConstants.EnvironmentType.UserDefined, AzureADEndpoint = "https://tester.com", GraphEndpoint = "https://tester.com" }; string expectedSettingsContent = @"{ ""EnvironmentTable"": { ""TestCloud"": { ""Name"": ""TestCloud"", ""AzureADEndpoint"": ""https://tester.com"", ""GraphEndpoint"": ""https://tester.com"", ""Type"": ""User-defined"" } } }"; // Act // Saves settings to disk store. settings.TrySetEnvironment(userDefinedEnv, out IGraphEnvironment savedEnvironment); string settingsContent = GraphSession.Instance.DataStore.ReadFileAsText(Constants.SettingFilePath).Substring(1).TrimEnd(new[] { '\0' }); // Assert Assert.NotEmpty(settingsContent); Assert.Equal(expectedSettingsContent, settingsContent); GraphSession.Reset(); }