static IEnumerable GetEnvVars()

in edge-agent/src/Microsoft.Azure.Devices.Edge.Agent.Edgelet/commands/CreateOrUpdateCommand.cs [99:200]


        static IEnumerable<EnvVar> GetEnvVars(
            IDictionary<string, EnvVal> moduleEnvVars,
            IModuleIdentity identity,
            IConfigSource configSource,
            string edgeDeviceHostname,
            Option<string> parentEdgeHostname)
        {
            List<EnvVar> envVars = moduleEnvVars.Select(m => new EnvVar(m.Key, m.Value.Value)).ToList();

            // Inject the connection details as an environment variable
            if (identity.Credentials is IdentityProviderServiceCredentials creds)
            {
                if (!string.IsNullOrWhiteSpace(creds.ProviderUri))
                {
                    envVars.Add(new EnvVar(Constants.EdgeletWorkloadUriVariableName, creds.ProviderUri));
                }

                if (!string.IsNullOrWhiteSpace(creds.AuthScheme))
                {
                    envVars.Add(new EnvVar(Constants.EdgeletAuthSchemeVariableName, creds.AuthScheme));
                }

                if (!string.IsNullOrWhiteSpace(creds.ModuleGenerationId))
                {
                    envVars.Add(new EnvVar(Constants.EdgeletModuleGenerationIdVariableName, creds.ModuleGenerationId));
                }
            }

            if (!string.IsNullOrWhiteSpace(identity.IotHubHostname))
            {
                envVars.Add(new EnvVar(Constants.IotHubHostnameVariableName, identity.IotHubHostname));
            }

            // In nested edge scenario, EdgeAgent and EdgeHub will use parent edge as upstream gateway,
            // Other modules use current edge hub as upstream gateway and have parent edge hostname in environment variable.
            if (identity.ModuleId.Equals(Constants.EdgeAgentModuleIdentityName) || identity.ModuleId.Equals(Constants.EdgeHubModuleIdentityName))
            {
                envVars.Add(new EnvVar(Constants.EdgeDeviceHostNameKey, edgeDeviceHostname));
                parentEdgeHostname.ForEach(value => envVars.Add(new EnvVar(Constants.GatewayHostnameVariableName, value)));
            }
            else
            {
                envVars.Add(new EnvVar(Constants.GatewayHostnameVariableName, edgeDeviceHostname));
                parentEdgeHostname.ForEach(value => envVars.Add(new EnvVar(Constants.ParentEdgeHostnameVariableName, value)));
            }

            if (!string.IsNullOrWhiteSpace(identity.DeviceId))
            {
                envVars.Add(new EnvVar(Constants.DeviceIdVariableName, identity.DeviceId));
            }

            if (!string.IsNullOrWhiteSpace(identity.ModuleId))
            {
                envVars.Add(new EnvVar(Constants.ModuleIdVariableName, identity.ModuleId));
            }

            if (!envVars.Exists(e => e.Key == Logger.RuntimeLogLevelEnvKey))
            {
                envVars.Add(new EnvVar(Logger.RuntimeLogLevelEnvKey, Logger.GetLogLevel().ToString()));
            }

            configSource.Configuration.GetValue<string>(Constants.UpstreamProtocolKey).ToUpstreamProtocol().ForEach(
                u =>
                {
                    if (!envVars.Any(e => e.Key.Equals(Constants.UpstreamProtocolKey, StringComparison.OrdinalIgnoreCase)))
                    {
                        envVars.Add(new EnvVar(Constants.UpstreamProtocolKey, u.ToString()));
                    }
                });

            if (identity.ModuleId.Equals(Constants.EdgeAgentModuleIdentityName))
            {
                string managementUri = configSource.Configuration.GetValue<string>(Constants.EdgeletManagementUriVariableName);
                if (!string.IsNullOrEmpty(managementUri))
                {
                    envVars.Add(new EnvVar(Constants.EdgeletManagementUriVariableName, managementUri));
                }

                string networkId = configSource.Configuration.GetValue<string>(Constants.NetworkIdKey);
                if (!string.IsNullOrEmpty(networkId))
                {
                    envVars.Add(new EnvVar(Constants.NetworkIdKey, networkId));
                }

                string workloadListenMnt = configSource.Configuration.GetValue<string>(Constants.EdgeletWorkloadListenMntUriVariableName);
                if (!string.IsNullOrEmpty(workloadListenMnt))
                {
                    envVars.Add(new EnvVar(Constants.EdgeletWorkloadListenMntUriVariableName, workloadListenMnt));
                }

                envVars.Add(new EnvVar(Constants.ModeKey, Constants.IotedgedMode));
            }

            // Set the edgelet's api version
            string apiVersion = configSource.Configuration.GetValue<string>(Constants.EdgeletApiVersionVariableName);
            if (!string.IsNullOrEmpty(apiVersion))
            {
                envVars.Add(new EnvVar(Constants.EdgeletApiVersionVariableName, apiVersion));
            }

            return envVars;
        }