Пример #1
0
        public void ShouldParseVcapApplication()
        {
            var vcapApp = PcfSerializer.ParseVcapApplication(SampleVcapApplication);

            vcapApp.application_id.Should().Be("aaaae2ba-e841-1211-94e6-00a319bc2560");
            vcapApp.application_name.Should().Be("helloworld");
            vcapApp.application_uris.Count.Should().Be(1);
            vcapApp.application_uris.Single().Should().Be("helloworld.cfapps.io");
            vcapApp.application_version.Should().Be("eec414fe-4016-40ae-bd70-da2e564c0a90");
            vcapApp.cf_api.Should().Be("https://api.run.pivotal.io");

            vcapApp.limits.disk.Should().Be(64);
            vcapApp.limits.fds.Should().Be(16384);
            vcapApp.limits.mem.Should().Be(64);

            vcapApp.name.Should().Be("helloworld");
            vcapApp.space_id.Should().Be("aaa0b011-ed21-4934-c1721-b07207b9f9a1");
            vcapApp.space_name.Should().Be("development");
            vcapApp.uris.Count.Should().Be(1);
            vcapApp.uris.Single().Should().Be("helloworld.cfapps.io");
            vcapApp.version.Should().Be("eec414fe-4016-40ae-bd70-da2e564c0a90");
        }
        internal static PcfEnvironment Init()
        {
            IPEndPoint cfInstanceAddr = null;
            IPAddress  cfInstanceIp   = null;
            int?       cfInstancePort = null;

            var instanceIp   = Environment.GetEnvironmentVariable("CF_INSTANCE_IP");
            var instancePort = Environment.GetEnvironmentVariable("CF_INSTANCE_PORT");

            if (!string.IsNullOrEmpty(instancePort) && !string.IsNullOrEmpty(instanceIp))
            {
                cfInstanceIp   = IPAddress.Parse(instanceIp);
                cfInstancePort = int.Parse(instancePort);
                cfInstanceAddr = new IPEndPoint(cfInstanceIp, cfInstancePort.Value);
            }

            IPAddress cfInternalIp       = null;
            var       instanceInternalIp = Environment.GetEnvironmentVariable("CF_INSTANCE_INTERNAL_IP");

            if (!string.IsNullOrEmpty(instanceInternalIp))
            {
                cfInstanceIp = IPAddress.Parse(instanceInternalIp);
            }

            var cfInstanceGuid  = Environment.GetEnvironmentVariable("CF_INSTANCE_GUID");
            var instanceIndex   = Environment.GetEnvironmentVariable("CF_INSTANCE_INDEX");
            int?cfInstanceIndex = null;

            if (!string.IsNullOrEmpty(instanceIndex) && int.TryParse(instanceIndex, out var index))
            {
                cfInstanceIndex = index;
            }

            var cfHome = Environment.GetEnvironmentVariable("HOME");
            var lang   = Environment.GetEnvironmentVariable("LANG");
            var pwd    = Environment.GetEnvironmentVariable("PWD");
            var tmpdir = Environment.GetEnvironmentVariable("TMPDIR");
            var user   = Environment.GetEnvironmentVariable("USER");

            var strPortMappings = Environment.GetEnvironmentVariable("CF_INSTANCE_PORTS");
            IReadOnlyList <PortMapping> mappings = new List <PortMapping>();

            if (!string.IsNullOrEmpty(strPortMappings))
            {
                mappings = PcfSerializer.ParsePcfPorts(strPortMappings);
            }

            VcapApplication vcapApplication     = null;
            var             jsonVcapApplication = Environment.GetEnvironmentVariable("VCAP_APPLICATION");

            if (!string.IsNullOrEmpty(jsonVcapApplication))
            {
                vcapApplication = PcfSerializer.ParseVcapApplication(jsonVcapApplication)
                                  .ToVcapApplication();
            }

            var strPort = Environment.GetEnvironmentVariable("PORT");
            int?port    = null;

            if (!string.IsNullOrEmpty(strPort) && int.TryParse(strPort, out var intPort))
            {
                port = intPort;
            }

            return(new PcfEnvironment(cfInstanceAddr, cfInstanceGuid, cfInstanceIndex, cfInstanceIp, cfInstancePort,
                                      mappings, cfHome, lang, port, pwd, tmpdir, user, vcapApplication, cfInternalIp));
        }