Example #1
0
        public void ConfigureCloudFoundryService_ThrowsIfConfigurtionNull()
        {
            IServiceCollection services = new ServiceCollection();
            IConfigurationRoot config   = null;

            Assert.Throws <ArgumentNullException>(() => CloudFoundryServiceCollectionExtensions.ConfigureCloudFoundryService <MySqlServiceOption>(services, config, "foobar"));
        }
Example #2
0
        public void AddCloudFoundryActuators_ThrowsOnNull_Config()
        {
            IServiceCollection services2 = new ServiceCollection();

            var ex = Assert.Throws <ArgumentNullException>(() => CloudFoundryServiceCollectionExtensions.AddCloudFoundryActuators(services2, null));

            Assert.Equal("config", ex.ParamName);
        }
Example #3
0
        public void AddCloudFoundryActuators_ThrowsOnNull_Services()
        {
            var config = new ConfigurationBuilder().Build();

            var ex = Assert.Throws <ArgumentNullException>(() => CloudFoundryServiceCollectionExtensions.AddCloudFoundryActuators(null, config));

            Assert.Equal("services", ex.ParamName);
        }
Example #4
0
        public void ConfigureCloudFoundryService_BadServiceName()
        {
            IServiceCollection services = new ServiceCollection();
            var config = new ConfigurationBuilder().Build();

            Assert.Throws <ArgumentException>(() => CloudFoundryServiceCollectionExtensions.ConfigureCloudFoundryService <MySqlServiceOption>(services, config, null));
            Assert.Throws <ArgumentException>(() => CloudFoundryServiceCollectionExtensions.ConfigureCloudFoundryService <MySqlServiceOption>(services, config, string.Empty));
        }
        public void ConfigureCloudFoundryService_ThrowsIfServiceCollectionNull()
        {
            // Arrange
            IServiceCollection services = null;
            IConfigurationRoot config   = null;

            // Act and Assert
            Assert.Throws <ArgumentNullException>(() => CloudFoundryServiceCollectionExtensions.ConfigureCloudFoundryService <MySqlServiceOption>(services, config, "foobar"));
        }
Example #6
0
        public void ConfigureCloudFoundryOptions_ThrowsIfConfigurtionNull()
        {
            IServiceCollection services = new ServiceCollection();
            IConfigurationRoot config   = null;

            var ex = Assert.Throws <ArgumentNullException>(() => CloudFoundryServiceCollectionExtensions.ConfigureCloudFoundryOptions(services, config));

            Assert.Contains(nameof(config), ex.Message);
        }
        public void ConfigureCloudFoundryOptions_ThrowsIfServiceCollectionNull()
        {
            // Arrange
            IServiceCollection services = null;
            IConfigurationRoot config   = null;

            // Act and Assert
            var ex = Assert.Throws <ArgumentNullException>(() => CloudFoundryServiceCollectionExtensions.ConfigureCloudFoundryOptions(services, config));

            Assert.Contains(nameof(services), ex.Message);
        }
        public void AddCloudFoundryJwtAuthentication_AddsRequiredServices()
        {
            ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
            var config = configurationBuilder.Build();

            var services = new ServiceCollection();

            CloudFoundryServiceCollectionExtensions.AddCloudFoundryJwtAuthentication(services, config);
            var provider = services.BuildServiceProvider();
            var iopts    = provider.GetService(typeof(IOptions <OAuthServiceOptions>)) as IOptions <OAuthServiceOptions>;

            Assert.NotNull(iopts);
        }
        public void AddCloudFoundryJwtAuthentication_ThowsForNulls()
        {
            // Arrange
            IServiceCollection services = null;
            IConfiguration     config   = null;

            // Act and Assert
            var ex = Assert.Throws <ArgumentNullException>(() => CloudFoundryServiceCollectionExtensions.AddCloudFoundryJwtAuthentication(services, config));

            Assert.Contains(nameof(services), ex.Message);
            var ex2 = Assert.Throws <ArgumentNullException>(() => CloudFoundryServiceCollectionExtensions.AddCloudFoundryJwtAuthentication(new ServiceCollection(), config));

            Assert.Contains(nameof(config), ex2.Message);
        }
        public void AddCloudFoundry_AddsConfigurationAsService()
        {
            // Arrange
            var services = new ServiceCollection();

            // Act and Assert
            var builder = new ConfigurationBuilder().AddCloudFoundry();
            var config  = builder.Build();

            CloudFoundryServiceCollectionExtensions.AddCloudFoundry(services, config);

            var service = services.BuildServiceProvider().GetService <IConfigurationRoot>();

            Assert.NotNull(service);
        }
Example #11
0
        public void AddCloudFoundryActuators_ThrowsOnNulls()
        {
            // Arrange
            IServiceCollection services  = null;
            IServiceCollection services2 = new ServiceCollection();
            IConfigurationRoot config    = null;
            IConfigurationRoot config2   = new ConfigurationBuilder().Build();

            // Act and Assert
            var ex = Assert.Throws <ArgumentNullException>(() => CloudFoundryServiceCollectionExtensions.AddCloudFoundryActuators(services, config));

            Assert.Contains(nameof(services), ex.Message);
            var ex2 = Assert.Throws <ArgumentNullException>(() => CloudFoundryServiceCollectionExtensions.AddCloudFoundryActuators(services2, config));

            Assert.Contains(nameof(config), ex2.Message);
        }
        public void ConfigureCloudFoundryOptions_ConfiguresCloudFoundryOptions()
        {
            // Arrange
            var services = new ServiceCollection();

            // Act and Assert
            var builder = new ConfigurationBuilder().AddCloudFoundry();
            var config  = builder.Build();

            CloudFoundryServiceCollectionExtensions.ConfigureCloudFoundryOptions(services, config);

            var serviceProvider = services.BuildServiceProvider();
            var app             = serviceProvider.GetService <IOptions <CloudFoundryApplicationOptions> >();

            Assert.NotNull(app.Value);
            var service = serviceProvider.GetService <IOptions <CloudFoundryServicesOptions> >();

            Assert.NotNull(service.Value);
        }
Example #13
0
        public void ConfigureCloudFoundryOptions_ConfiguresCloudFoundryOptions()
        {
            var services = new ServiceCollection();

            Environment.SetEnvironmentVariable("VCAP_APPLICATION", @"{ ""cf_api"": ""https://api.run.pcfone.io"", ""limits"": { ""fds"": 16384 }, ""application_name"": ""foo"", ""application_uris"": [ ""foo-unexpected-serval-iy.apps.pcfone.io"" ], ""name"": ""foo"", ""space_name"": ""playground"", ""space_id"": ""f03f2ab0-cf33-416b-999c-fb01c1247753"", ""organization_id"": ""d7afe5cb-2d42-487b-a415-f47c0665f1ba"", ""organization_name"": ""pivot-thess"", ""uris"": [ ""foo-unexpected-serval-iy.apps.pcfone.io"" ], ""users"": null, ""application_id"": ""f69a6624-7669-43e3-a3c8-34d23a17e3db"" }");

            var builder = new ConfigurationBuilder().AddCloudFoundry();
            var config  = builder.Build();

            CloudFoundryServiceCollectionExtensions.ConfigureCloudFoundryOptions(services, config);

            var serviceProvider = services.BuildServiceProvider();
            var app             = serviceProvider.GetService <IOptions <CloudFoundryApplicationOptions> >();

            Assert.NotNull(app.Value);
            Assert.Equal("foo", app.Value.ApplicationName);
            Assert.Equal("playground", app.Value.SpaceName);
            var service = serviceProvider.GetService <IOptions <CloudFoundryServicesOptions> >();

            Assert.NotNull(service.Value);
        }
        public void ConfigureCloudFoundryServices_ConfiguresServices()
        {
            // Arrange
            var configJson = @"
                {
                    ""vcap"": {
                        ""services"" : {
                            ""p-mysql"": [{
                                ""name"": ""mySql1"",
                                ""label"": ""p-mysql"",
                                ""tags"": [
                                    ""mysql"",
                                    ""relational""
                                ],
                                ""plan"": ""100mb-dev"",
                                ""credentials"": {
                                    ""hostname"": ""192.168.0.97"",
                                    ""port"": 3306,
                                    ""name"": ""cf_0f5dda44_e678_4727_993f_30e6d455cc31"",
                                    ""username"": ""9vD0Mtk3wFFuaaaY"",
                                    ""password"": ""Cjn4HsAiKV8sImst"",
                                    ""uri"": ""mysql://*****:*****@192.168.0.97:3306/cf_0f5dda44_e678_4727_993f_30e6d455cc31?reconnect=true"",
                                    ""jdbcUrl"": ""jdbc:mysql://192.168.0.97:3306/cf_0f5dda44_e678_4727_993f_30e6d455cc31?user=9vD0Mtk3wFFuaaaY&password=Cjn4HsAiKV8sImst""
                                }
                            },
                            {
                                ""name"": ""mySql2"",
                                ""label"": ""p-mysql"",
                                ""tags"": [
                                    ""mysql"",
                                    ""relational""
                                ],
                                ""plan"": ""100mb-dev"",
                                ""credentials"": {
                                    ""hostname"": ""192.168.0.97"",
                                    ""port"": 3306,
                                    ""name"": ""cf_0f5dda44_e678_4727_993f_30e6d455cc31"",
                                    ""username"": ""9vD0Mtk3wFFuaaaY"",
                                    ""password"": ""Cjn4HsAiKV8sImst"",
                                    ""uri"": ""mysql://*****:*****@192.168.0.97:3306/cf_0f5dda44_e678_4727_993f_30e6d455cc31?reconnect=true"",
                                    ""jdbcUrl"": ""jdbc:mysql://192.168.0.97:3306/cf_0f5dda44_e678_4727_993f_30e6d455cc31?user=9vD0Mtk3wFFuaaaY&password=Cjn4HsAiKV8sImst""
                                }
                            }]
                        }
                    }
                }";
            var memStream  = CloudFoundryConfigurationProvider.GetMemoryStream(configJson);
            var jsonSource = new JsonStreamConfigurationSource(memStream);
            var builder    = new ConfigurationBuilder().Add(jsonSource);
            var config     = builder.Build();
            var services   = new ServiceCollection();

            services.AddOptions();

            // Act and Assert
            CloudFoundryServiceCollectionExtensions.ConfigureCloudFoundryServices <MySqlServiceOption>(services, config, "p-mysql");

            var serviceProvider = services.BuildServiceProvider();
            var snapShot        = serviceProvider.GetRequiredService <IOptionsSnapshot <MySqlServiceOption> >();
            var monitor         = serviceProvider.GetRequiredService <IOptionsMonitor <MySqlServiceOption> >();

            var snapOpt1 = snapShot.Get("mySql1");
            var monOpt1  = monitor.Get("mySql1");

            Assert.NotNull(snapOpt1);
            Assert.NotNull(monOpt1);

            Assert.Equal("mySql1", snapOpt1.Name);
            Assert.Equal("p-mysql", snapOpt1.Label);
            Assert.Equal("mySql1", monOpt1.Name);
            Assert.Equal("p-mysql", monOpt1.Label);

            var snapOpt2 = snapShot.Get("mySql2");
            var monOpt2  = monitor.Get("mySql2");

            Assert.NotNull(snapOpt2);
            Assert.NotNull(monOpt2);

            Assert.Equal("mySql2", snapOpt2.Name);
            Assert.Equal("p-mysql", snapOpt2.Label);
            Assert.Equal("mySql2", monOpt2.Name);
            Assert.Equal("p-mysql", monOpt2.Label);
        }
Example #15
0
        static void Main(string[] args)
        {
            // Arrange
            var services = new ServiceCollection();

            // Act and Assert
            var builder = new ConfigurationBuilder().AddCloudFoundry();
            var config  = builder.Build();

            CloudFoundryServiceCollectionExtensions.ConfigureCloudFoundryOptions(services, config);



            var serviceProvider = services.BuildServiceProvider();
            var app             = serviceProvider.GetService <IOptions <CloudFoundryApplicationOptions> >();
            var service         = serviceProvider.GetService <IOptions <CloudFoundryServicesOptions> >();

            foreach (var sk in service.Value.Services)
            {
                Console.Out.WriteLine("Service available: " + sk);
            }
            Service[] redisOptionsArray = service.Value.Services["p.redis"];
            var       redisUri          = "";

            if (redisOptionsArray.Length > 0)
            {
                var creds = redisOptionsArray[0].Credentials;
                //foreach (var key in creds.Keys) {
                //    Console.Out.WriteLine(key + " = " + creds[key].Value);
                //}
                redisUri = creds["host"].Value + ":" + creds["port"].Value + ",password="******"password"].Value;
            }
            Service[] rabbitOptionsArray = null;
            try {
                rabbitOptionsArray = service.Value.Services["cloudamqp"];
            } catch (KeyNotFoundException knfe) {
                try {
                    rabbitOptionsArray = service.Value.Services["p.rabbitmq"];
                } catch (KeyNotFoundException knfe2)
                {
                }
            }
            var rabbitUri = "";

            if (rabbitOptionsArray.Length > 0)
            {
                var creds = rabbitOptionsArray[0].Credentials;
                //foreach (var key in creds.Keys) {
                //    Console.Out.WriteLine(key + " = " + creds[key].Value);
                //}
                rabbitUri = creds["uri"].Value;
            }

            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(
                redisUri);
            var factory = new ConnectionFactory()
            {
                Uri = new Uri(
                    rabbitUri
                    , UriKind.Absolute)
            };

            using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    QueueDeclareOk queueOk = channel.QueueDeclare(queue: "adsbposition.live",
                                                                  durable: false,
                                                                  exclusive: false,
                                                                  autoDelete: false,
                                                                  arguments: null);
                    channel.ExchangeDeclare(exchange: "adsb-fan-exchange", type: "topic", durable: true, autoDelete: false);
                    channel.QueueBind(queue: "adsbposition.live", exchange: "adsb-fan-exchange", routingKey: "");

                    channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);

                    Console.WriteLine(" [*] Waiting for messages.");

                    var consumer = new EventingBasicConsumer(channel);
                    consumer.Received += (model, ea) =>
                    {
                        var body    = ea.Body;
                        var message = Encoding.UTF8.GetString(body);
                        Console.WriteLine(" [x] Received {0}", message);

                        //int dots = message.Split('.').Length - 1;
                        //Thread.Sleep(dots * 1000);

                        // parse json to get the aircraft ID (flight json property)
                        PositionData pos = JsonConvert.DeserializeObject <PositionData>(message);

                        // Now send to Redis
                        IDatabase db = redis.GetDatabase();
                        db.StringSet(pos.flight, message);
                        var epoch    = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                        var timeSpan = epoch.Add(TimeSpan.FromSeconds(60 + (pos.timestamp / 1000.0)));
                        db.KeyExpire(pos.flight, timeSpan);

                        Console.WriteLine(" [x] Done");

                        channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
                    };
                    channel.BasicConsume(queue: "adsbposition.live",
                                         autoAck: false,
                                         consumer: consumer);

                    Console.WriteLine(" Press [enter] to exit.");
                    Console.ReadLine();
                }
        }
Example #16
0
        public void ConfigureCloudFoundryServices_ConfiguresServices()
        {
            // Arrange
            var configJson = @"
{ 'vcap': {
    'services' : {
            'p-mysql': [
            {
                'name': 'mySql1',
                'label': 'p-mysql',
                'tags': [
                'mysql',
                'relational'
                ],
                'plan': '100mb-dev',
                'credentials': {
                    'hostname': '192.168.0.97',
                    'port': 3306,
                    'name': 'cf_0f5dda44_e678_4727_993f_30e6d455cc31',
                    'username': '******',
                    'password': '******',
                    'uri': 'mysql://*****:*****@192.168.0.97:3306/cf_0f5dda44_e678_4727_993f_30e6d455cc31?reconnect=true',
                    'jdbcUrl': 'jdbc:mysql://192.168.0.97:3306/cf_0f5dda44_e678_4727_993f_30e6d455cc31?user=9vD0Mtk3wFFuaaaY&password=Cjn4HsAiKV8sImst'
                }
            },
            {
                'name': 'mySql2',
                'label': 'p-mysql',
                'tags': [
                'mysql',
                'relational'
                ],
                'plan': '100mb-dev',
                'credentials': {
                    'hostname': '192.168.0.97',
                    'port': 3306,
                    'name': 'cf_0f5dda44_e678_4727_993f_30e6d455cc31',
                    'username': '******',
                    'password': '******',
                    'uri': 'mysql://*****:*****@192.168.0.97:3306/cf_0f5dda44_e678_4727_993f_30e6d455cc31?reconnect=true',
                    'jdbcUrl': 'jdbc:mysql://192.168.0.97:3306/cf_0f5dda44_e678_4727_993f_30e6d455cc31?user=9vD0Mtk3wFFuaaaY&password=Cjn4HsAiKV8sImst'
                }
            }
            ]
        }
    }
}";
            var memStream  = CloudFoundryConfigurationProvider.GetMemoryStream(configJson);
            var jsonSource = new JsonStreamConfigurationSource(memStream);
            var builder    = new ConfigurationBuilder().Add(jsonSource);
            var config     = builder.Build();
            var services   = new ServiceCollection();

            services.AddOptions();

            // Act and Assert
            CloudFoundryServiceCollectionExtensions.ConfigureCloudFoundryServices <MySqlServiceOption>(services, config, "p-mysql");

            var serviceProvider = services.BuildServiceProvider();
            var snapShot        = serviceProvider.GetRequiredService <IOptionsSnapshot <MySqlServiceOption> >();
            var monitor         = serviceProvider.GetRequiredService <IOptionsMonitor <MySqlServiceOption> >();

            var snapOpt1 = snapShot.Get("mySql1");
            var monOpt1  = monitor.Get("mySql1");

            Assert.NotNull(snapOpt1);
            Assert.NotNull(monOpt1);

            Assert.Equal("mySql1", snapOpt1.Name);
            Assert.Equal("p-mysql", snapOpt1.Label);
            Assert.Equal("mySql1", monOpt1.Name);
            Assert.Equal("p-mysql", monOpt1.Label);

            var snapOpt2 = snapShot.Get("mySql2");
            var monOpt2  = monitor.Get("mySql2");

            Assert.NotNull(snapOpt2);
            Assert.NotNull(monOpt2);

            Assert.Equal("mySql2", snapOpt2.Name);
            Assert.Equal("p-mysql", snapOpt2.Label);
            Assert.Equal("mySql2", monOpt2.Name);
            Assert.Equal("p-mysql", monOpt2.Label);
        }