Example #1
0
        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);
            }
        }
        protected override void BeginProcessing()
        {
            base.BeginProcessing();
            ValidateParameters();

            if (MyInvocation.BoundParameters.ContainsKey(nameof(Environment)))
            {
                GraphSettings settings = this.GetContextSettings();
                if (!settings.TryGetEnvironment(Environment, out environment))
                {
                    throw new PSInvalidOperationException(string.Format(ErrorConstants.Message.InvalidEnvironment, Environment));
                }
            }
            else
            {
                environment = GraphEnvironment.BuiltInEnvironments[GraphEnvironmentConstants.EnvironmentName.Global];
            }
        }
        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 ShouldLoadSettingsFromConfiguredDataStore()
        {
            GraphSession.Initialize(() => new GraphSession());

            // Arrange
            GraphSession.Instance.DataStore = new MockDataStore();
            string settingsContent = @"{
  ""EnvironmentTable"": {
    ""MyNewCloud"": {
      ""Name"": ""MyNewCloud"",
      ""AzureADEndpoint"": ""https://login.MyNewCloud.com"",
      ""GraphEndpoint"": ""https://graph.MyNewCloud.com"",
      ""Type"": ""User-defined""
    },
    ""TrialCloud"": {
      ""Name"": ""MyNewCloud"",
      ""AzureADEndpoint"": ""https://login.TrialCloud.com"",
      ""GraphEndpoint"": ""https://graph.TrialCloud.com"",
      ""Type"": ""User-defined""
    }
  }
}";

            GraphSession.Instance.DataStore.WriteFile(Constants.SettingFilePath, settingsContent);

            // Act
            // Loads settings from disk store.
            GraphSettings settings = new GraphSettings(ProtectedFileProvider.CreateFileProvider(Constants.SettingFilePath, FileProtection.SharedRead));

            settings.TryGetEnvironment("MyNewCloud", out IGraphEnvironment loadedEnvironment);

            // Assert
            Assert.NotNull(loadedEnvironment);
            // 5 built-in + 2 user-defined
            Assert.Equal(7, settings.Environments.Count());
            Assert.Equal("https://login.MyNewCloud.com", loadedEnvironment.AzureADEndpoint);
            Assert.Equal("https://graph.MyNewCloud.com", loadedEnvironment.GraphEndpoint);
            Assert.Equal(GraphEnvironmentConstants.EnvironmentType.UserDefined, loadedEnvironment.Type);

            GraphSession.Reset();
        }