| 1 | // Copyright (C) MongoDB, Inc. 2023-present. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | // not use this file except in compliance with the License. You may obtain |
| 5 | // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | |
| 7 | package driverutilscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/ |
| 8 | |
| 9 | import ( |
| 10 | "osscip-go gomod github.com/golang/go/src go1.18 os/" |
| 11 | "stringsscip-go gomod github.com/golang/go/src go1.18 strings/" |
| 12 | ) |
| 13 | |
| 14 | const AwsLambdaPrefixscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/AwsLambdaPrefix. = "AWS_Lambda_" |
| 15 | |
| 16 | const ( |
| 17 | // FaaS environment variable names |
| 18 | |
| 19 | // EnvVarAWSExecutionEnv is the AWS Execution environment variable. |
| 20 | EnvVarAWSExecutionEnvscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarAWSExecutionEnv. = "AWS_EXECUTION_ENV" |
| 21 | // EnvVarAWSLambdaRuntimeAPI is the AWS Lambda runtime API variable. |
| 22 | EnvVarAWSLambdaRuntimeAPIscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarAWSLambdaRuntimeAPI. = "AWS_LAMBDA_RUNTIME_API" |
| 23 | // EnvVarFunctionsWorkerRuntime is the functions worker runtime variable. |
| 24 | EnvVarFunctionsWorkerRuntimescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarFunctionsWorkerRuntime. = "FUNCTIONS_WORKER_RUNTIME" |
| 25 | // EnvVarKService is the K Service variable. |
| 26 | EnvVarKServicescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarKService. = "K_SERVICE" |
| 27 | // EnvVarFunctionName is the function name variable. |
| 28 | EnvVarFunctionNamescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarFunctionName. = "FUNCTION_NAME" |
| 29 | // EnvVarVercel is the Vercel variable. |
| 30 | EnvVarVercelscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarVercel. = "VERCEL" |
| 31 | // EnvVarK8s is the K8s variable. |
| 32 | EnvVarK8sscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarK8s. = "KUBERNETES_SERVICE_HOST" |
| 33 | ) |
| 34 | |
| 35 | const ( |
| 36 | // FaaS environment variable names |
| 37 | |
| 38 | // EnvVarAWSRegion is the AWS region variable. |
| 39 | EnvVarAWSRegionscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarAWSRegion. = "AWS_REGION" |
| 40 | // EnvVarAWSLambdaFunctionMemorySize is the AWS Lambda function memory size variable. |
| 41 | EnvVarAWSLambdaFunctionMemorySizescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarAWSLambdaFunctionMemorySize. = "AWS_LAMBDA_FUNCTION_MEMORY_SIZE" |
| 42 | // EnvVarFunctionMemoryMB is the function memory in megabytes variable. |
| 43 | EnvVarFunctionMemoryMBscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarFunctionMemoryMB. = "FUNCTION_MEMORY_MB" |
| 44 | // EnvVarFunctionTimeoutSec is the function timeout in seconds variable. |
| 45 | EnvVarFunctionTimeoutSecscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarFunctionTimeoutSec. = "FUNCTION_TIMEOUT_SEC" |
| 46 | // EnvVarFunctionRegion is the function region variable. |
| 47 | EnvVarFunctionRegionscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarFunctionRegion. = "FUNCTION_REGION" |
| 48 | // EnvVarVercelRegion is the Vercel region variable. |
| 49 | EnvVarVercelRegionscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarVercelRegion. = "VERCEL_REGION" |
| 50 | ) |
| 51 | |
| 52 | const ( |
| 53 | // FaaS environment names used by the client |
| 54 | |
| 55 | // EnvNameAWSLambda is the AWS Lambda environment name. |
| 56 | EnvNameAWSLambdascip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvNameAWSLambda. = "aws.lambda" |
| 57 | // EnvNameAzureFunc is the Azure Function environment name. |
| 58 | EnvNameAzureFuncscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvNameAzureFunc. = "azure.func" |
| 59 | // EnvNameGCPFunc is the Google Cloud Function environment name. |
| 60 | EnvNameGCPFuncscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvNameGCPFunc. = "gcp.func" |
| 61 | // EnvNameVercel is the Vercel environment name. |
| 62 | EnvNameVercelscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvNameVercel. = "vercel" |
| 63 | ) |
| 64 | |
| 65 | // GetFaasEnvName parses the FaaS environment variable name and returns the |
| 66 | // corresponding name used by the client. If none of the variables or variables |
| 67 | // for multiple names are populated the client.env value MUST be entirely |
| 68 | // omitted. When variables for multiple "client.env.name" values are present, |
| 69 | // "vercel" takes precedence over "aws.lambda"; any other combination MUST cause |
| 70 | // "client.env" to be entirely omitted. |
| 71 | func GetFaasEnvNamescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/GetFaasEnvName().() string { |
| 72 | envVars := []string{ |
| 73 | EnvVarAWSExecutionEnvscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarAWSExecutionEnv., |
| 74 | EnvVarAWSLambdaRuntimeAPIscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarAWSLambdaRuntimeAPI., |
| 75 | EnvVarFunctionsWorkerRuntimescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarFunctionsWorkerRuntime., |
| 76 | EnvVarKServicescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarKService., |
| 77 | EnvVarFunctionNamescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarFunctionName., |
| 78 | EnvVarVercelscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarVercel., |
| 79 | } |
| 80 | |
| 81 | // If none of the variables are populated the client.env value MUST be |
| 82 | // entirely omitted. |
| 83 | names := make(map[string]struct{}) |
| 84 | |
| 85 | for _, envVar := range envVars { |
| 86 | val := osscip-go gomod github.com/golang/go/src go1.18 os/.Getenvscip-go gomod github.com/golang/go/src go1.18 os/Getenv().(envVar) |
| 87 | if val == "" { |
| 88 | continue |
| 89 | } |
| 90 | |
| 91 | var name string |
| 92 | |
| 93 | switch envVar { |
| 94 | case EnvVarAWSExecutionEnvscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarAWSExecutionEnv.: |
| 95 | if !stringsscip-go gomod github.com/golang/go/src go1.18 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.18 strings/HasPrefix().(val, AwsLambdaPrefixscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/AwsLambdaPrefix.) { |
| 96 | continue |
| 97 | } |
| 98 | |
| 99 | name = EnvNameAWSLambdascip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvNameAWSLambda. |
| 100 | case EnvVarAWSLambdaRuntimeAPIscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarAWSLambdaRuntimeAPI.: |
| 101 | name = EnvNameAWSLambdascip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvNameAWSLambda. |
| 102 | case EnvVarFunctionsWorkerRuntimescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarFunctionsWorkerRuntime.: |
| 103 | name = EnvNameAzureFuncscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvNameAzureFunc. |
| 104 | case EnvVarKServicescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarKService., EnvVarFunctionNamescip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarFunctionName.: |
| 105 | name = EnvNameGCPFuncscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvNameGCPFunc. |
| 106 | case EnvVarVercelscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvVarVercel.: |
| 107 | // "vercel" takes precedence over "aws.lambda". |
| 108 | delete(names, EnvNameAWSLambdascip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvNameAWSLambda.) |
| 109 | |
| 110 | name = EnvNameVercelscip-go gomod go.mongodb.org/mongo-driver . `go.mongodb.org/mongo-driver/internal/driverutil`/EnvNameVercel. |
| 111 | } |
| 112 | |
| 113 | names[name] = struct{}{} |
| 114 | if len(names) > 1 { |
| 115 | // If multiple names are populated the client.env value |
| 116 | // MUST be entirely omitted. |
| 117 | names = nil |
| 118 | |
| 119 | break |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | for name := range names { |
| 124 | return name |
| 125 | } |
| 126 | |
| 127 | return "" |
| 128 | } |