2.4.3 (latest) 2.4.2 2.3.1 2.2.2 Reference documentation and code samples for the Google Cloud Platform environment queries class Google::Cloud::Env.

### Google Cloud hosting environment

This library provides access to information about the application's
hosting environment if it is running on Google Cloud Platform. You can
use this library to determine which Google Cloud product is hosting your
application (e.g. App Engine, Kubernetes Engine), information about the
Google Cloud project hosting the application, information about the
virtual machine instance, authentication information, and so forth.

#### Usage

Obtain an instance of the environment info with:

`ruby
require "google/cloud/env"
env = Google::Cloud.env
`

Then you can interrogate any fields using methods on the object.

`ruby
if env.app_engine?
# App engine specific logic
end
`

Any item that does not apply to the current environment will return nil.
For example:

`ruby
unless env.app_engine?
service = env.app_engine_service_id # => nil
end
`

## Inherits

- Object

## Methods

### .get

```ruby
def self.get() -> Google::Cloud::Env
```
Returns the global instance of [Env](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env.md.txt "Google::Cloud::Env (class)").
**Returns**

- ([Google::Cloud::Env](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env.md.txt))

### #app_engine?

```ruby
def app_engine?() -> boolean
```
Determine whether the application is running on Google App Engine.
**Returns**

- (boolean)

### #app_engine_flexible?

```ruby
def app_engine_flexible?() -> boolean
```
Determine whether the application is running on Google App Engine
Flexible Environment.
**Returns**

- (boolean)

### #app_engine_memory_mb

```ruby
def app_engine_memory_mb() -> Integer, nil
```
Returns the amount of memory reserved for the current App Engine
instance, or `nil` if the current code is not running in App Engine.
**Returns**

- (Integer, nil)

### #app_engine_service_id

```ruby
def app_engine_service_id() -> String, nil
```
**Aliases**

- [#app_engine_service_name](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env.md.txt#Google__Cloud__Env_app_engine_service_name_instance_)
Returns the name of the running App Engine service, or `nil` if the
current code is not running in App Engine.
**Returns**

- (String, nil)

### #app_engine_service_name

```ruby
def app_engine_service_name() -> String, nil
```
**Alias Of** : [#app_engine_service_id](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env.md.txt#Google__Cloud__Env_app_engine_service_id_instance_) Returns the name of the running App Engine service, or `nil` if the
current code is not running in App Engine.
**Returns**

- (String, nil)

### #app_engine_service_version

```ruby
def app_engine_service_version() -> String, nil
```
Returns the version of the running App Engine service, or `nil` if the
current code is not running in App Engine.
**Returns**

- (String, nil)

### #app_engine_standard?

```ruby
def app_engine_standard?() -> boolean
```
Determine whether the application is running on Google App Engine
Standard Environment.
**Returns**

- (boolean)

### #cloud_shell?

```ruby
def cloud_shell?() -> boolean
```
Determine whether the application is running on Google Cloud Shell.
**Returns**

- (boolean)

### #compute_engine?

```ruby
def compute_engine?() -> boolean
```
Determine whether the application is running on Google Compute Engine.


<br />

Note that most other products (e.g. App Engine, Kubernetes Engine,
Cloud Shell) themselves use Compute Engine under the hood, so this
method will return true for all the above products. If you want to
determine whether the application is running on a "raw" Compute Engine
VM without using a higher level hosting product, use
[#raw_compute_engine?](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env.md.txt#Google__Cloud__Env_raw_compute_engine__instance_ "Google::Cloud::Env#raw_compute_engine? (method)").
**Returns**

- (boolean)

### #compute_metadata

```ruby
def compute_metadata() -> Google::Cloud::Env::ComputeMetadata
```
The compute metadata access object. Use this to make direct calls to
compute metadata or configure how metadata server queries are done, or
to mock out the metadata server for testing.
**Returns**

- ([Google::Cloud::Env::ComputeMetadata](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env-ComputeMetadata.md.txt))

### #compute_smbios

```ruby
def compute_smbios() -> Google::Cloud::Env::ComputeSMBIOS
```
The compute SMBIOS access object. Use this to make direct queries for
compute SMBIOS information, or to mock out the SMBIOS for testing.
**Returns**

- ([Google::Cloud::Env::ComputeSMBIOS](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env-ComputeSMBIOS.md.txt))

### #container_engine?

```ruby
def container_engine?() -> boolean
```
**Alias Of** : [#kubernetes_engine?](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env.md.txt#Google__Cloud__Env_kubernetes_engine?_instance_) Determine whether the application is running on Google Kubernetes
Engine (GKE).
**Returns**

- (boolean)

### #container_engine_cluster_name

```ruby
def container_engine_cluster_name() -> String, nil
```
**Alias Of** : [#kubernetes_engine_cluster_name](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env.md.txt#Google__Cloud__Env_kubernetes_engine_cluster_name_instance_) Returns the name of the Kubernetes Engine cluster hosting the
application, or `nil` if the current code is not running in
Kubernetes Engine.
**Returns**

- (String, nil)

### #container_engine_namespace_id

```ruby
def container_engine_namespace_id() -> String, nil
```
**Alias Of** : [#kubernetes_engine_namespace_id](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env.md.txt#Google__Cloud__Env_kubernetes_engine_namespace_id_instance_) Returns the name of the Kubernetes Engine namespace hosting the
application, or `nil` if the current code is not running in
Kubernetes Engine.
**Returns**

- (String, nil)

### #ensure_metadata

```ruby
def ensure_metadata(timeout: nil) -> :confirmed
```
Assert that the Metadata Server should be present, and wait for a
confirmed connection to ensure it is up. In general, this will run at
most [ComputeMetadata::DEFAULT_WARMUP_TIME](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env-ComputeMetadata.md.txt#Google__Cloud__Env__ComputeMetadata__DEFAULT_WARMUP_TIME "Google::Cloud::Env::ComputeMetadata::DEFAULT_WARMUP_TIME (constant)") seconds to wait out the
expected maximum warmup time, but a shorter timeout can be provided.


<br />

This method is useful call during application initialization to wait
for the Metadata Server to warm up and ensure that subsequent lookups
should succeed.
**Parameter**

- **timeout** (Numeric, nil) *(defaults to: nil)* --- a timeout in seconds, or nil to wait until we have conclusively decided one way or the other.
**Returns**

- (:confirmed) --- if we were able to confirm connection.
**Raises**

- ([MetadataServerNotResponding](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env-MetadataServerNotResponding.md.txt)) --- if we were unable to confirm connection with the Metadata Server, either because the timeout expired or because the server seems to be down

### #file_system

```ruby
def file_system() -> Google::Cloud::Env::FileSystem
```
The variables access object. Use this to make direct queries for
information from the file system, or to mock out the file system for
testing.
**Returns**

- ([Google::Cloud::Env::FileSystem](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env-FileSystem.md.txt))

### #initialize

```ruby
def initialize() -> Env
```
Create a new instance of the environment information.
Most clients should not need to call this directly. Obtain a singleton
instance of the information from `Google::Cloud.env`.
**Returns**

- ([Env](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env.md.txt)) --- a new instance of Env

### #instance_attribute

```ruby
def instance_attribute(key) -> String, nil
```
Returns the value of the given instance attribute for the VM instance
hosting the application, or `nil` if the given key does not exist or
application is not running on Google Cloud.
**Parameter**

- **key** (String) --- Attribute key to look up.
**Returns**

- (String, nil)

### #instance_attribute_keys

```ruby
def instance_attribute_keys() -> Array<String>, nil
```
Returns an array (which may be empty) of all attribute keys present
for the VM instance hosting the application, or `nil` if the
application is not running on Google Cloud.
**Returns**

- (Array\<String\>, nil)

### #instance_description

```ruby
def instance_description() -> String, nil
```
Returns the description field (which may be the empty string) of the
VM instance hosting the application, or `nil` if the application is
not running on Google Cloud.
**Returns**

- (String, nil)

### #instance_machine_type

```ruby
def instance_machine_type() -> String, nil
```
Returns the machine type of the VM instance hosting the application,
or `nil` if the application is not running on Google Cloud.
**Returns**

- (String, nil)

### #instance_name

```ruby
def instance_name() -> String, nil
```
Returns the name of the VM instance hosting the application, or `nil`
if the application is not running on Google Cloud.
**Returns**

- (String, nil)

### #instance_tags

```ruby
def instance_tags() -> Array<String>, nil
```
Returns an array (which may be empty) of all tags set on the VM
instance hosting the application, or `nil` if the application is not
running on Google Cloud.
**Returns**

- (Array\<String\>, nil)

### #instance_zone

```ruby
def instance_zone() -> String, nil
```
Returns the zone (for example "`us-central1-c`") in which the instance
hosting the application lives. Returns `nil` if the application is
not running on Google Cloud.
**Returns**

- (String, nil)

### #knative?

```ruby
def knative?() -> boolean
```
Determine whether the application is running on a Knative-based
hosting platform, such as Cloud Run or Cloud Functions.
**Returns**

- (boolean)

### #knative_service_id

```ruby
def knative_service_id() -> String, nil
```
**Aliases**

- [#knative_service_name](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env.md.txt#Google__Cloud__Env_knative_service_name_instance_)
Returns the name of the running Knative service, or `nil` if the
current code is not running on Knative.
**Returns**

- (String, nil)

### #knative_service_name

```ruby
def knative_service_name() -> String, nil
```
**Alias Of** : [#knative_service_id](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env.md.txt#Google__Cloud__Env_knative_service_id_instance_) Returns the name of the running Knative service, or `nil` if the
current code is not running on Knative.
**Returns**

- (String, nil)

### #knative_service_revision

```ruby
def knative_service_revision() -> String, nil
```
Returns the revision of the running Knative service, or `nil` if the
current code is not running on Knative.
**Returns**

- (String, nil)

### #kubernetes_engine?

```ruby
def kubernetes_engine?() -> boolean
```
**Aliases**

- [#container_engine?](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env.md.txt#Google__Cloud__Env_container_engine?_instance_)
Determine whether the application is running on Google Kubernetes
Engine (GKE).
**Returns**

- (boolean)

### #kubernetes_engine_cluster_name

```ruby
def kubernetes_engine_cluster_name() -> String, nil
```
**Aliases**

- [#container_engine_cluster_name](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env.md.txt#Google__Cloud__Env_container_engine_cluster_name_instance_)
Returns the name of the Kubernetes Engine cluster hosting the
application, or `nil` if the current code is not running in
Kubernetes Engine.
**Returns**

- (String, nil)

### #kubernetes_engine_namespace_id

```ruby
def kubernetes_engine_namespace_id() -> String, nil
```
**Aliases**

- [#container_engine_namespace_id](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env.md.txt#Google__Cloud__Env_container_engine_namespace_id_instance_)
Returns the name of the Kubernetes Engine namespace hosting the
application, or `nil` if the current code is not running in
Kubernetes Engine.
**Returns**

- (String, nil)

### #logging_agent_expected?

```ruby
def logging_agent_expected?() -> boolean
```
Determine whether the application is running in an environment where a
Google Cloud logging agent is expected to be running. In such an
environment, we expect that the standard output and error streams are
likely to be parsed by the logging agent and log entries are written to
the Google Cloud Logging service.
**Returns**

- (boolean)

### #lookup_metadata

```ruby
def lookup_metadata(type, entry, query: nil) -> String, nil
```
Retrieve info from the Google Compute Engine Metadata Service.
Returns `nil` if the given data is not present.
**Parameters**

- **type** (String) --- Type of metadata to look up. Currently supported values are "project" and "instance".
- **entry** (String) --- Metadata entry path to look up.
- **query** (Hash{String =\> String}) *(defaults to: nil)* --- Any additional query parameters to send with the request.
**Returns**

- (String) --- the data
- (nil) --- if there is no data for the specified type and entry
**Raises**

- ([MetadataServerNotResponding](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env-MetadataServerNotResponding.md.txt)) --- if the Metadata Server is not responding. This could either be because the metadata service is not present in the current environment, or if it is expected to be present but is overloaded or has not finished initializing.

### #lookup_metadata_response

```ruby
def lookup_metadata_response(type, entry, query: nil) -> Google::Cloud::Env::ComputeMetadata::Response
```
Retrieve an HTTP response from the Google Compute Engine Metadata
Service. Returns a [ComputeMetadata::Response](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env-ComputeMetadata-Response.md.txt "Google::Cloud::Env::ComputeMetadata::Response (class)") with a status code,
data, and headers. The response could be 200 for success, 404 if the
given entry is not present, or other HTTP result code for authorization
or other errors.
**Parameters**

- **type** (String) --- Type of metadata to look up. Currently supported values are "project" and "instance".
- **entry** (String) --- Metadata entry path to look up.
- **query** (Hash{String =\> String}) *(defaults to: nil)* --- Any additional query parameters to send with the request.
**Returns**

- ([Google::Cloud::Env::ComputeMetadata::Response](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env-ComputeMetadata-Response.md.txt)) --- the response
**Raises**

- ([MetadataServerNotResponding](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env-MetadataServerNotResponding.md.txt)) --- if the Metadata Server is not responding. This could either be because the metadata service is not present in the current environment, or if it is expected to be present but is overloaded or has not finished initializing.

### #metadata?

```ruby
def metadata?() -> boolean
```
Determine whether the Google Compute Engine Metadata Service is running.


<br />

This method is conservative. It may block for a short period (up to
about 1.5 seconds) while attempting to contact the server, but if this
fails, this method will return false, even though it is possible that a
future call could succeed. In particular, this might happen in
environments where there is a warmup time for the Metadata Server.
Early calls before the Server has warmed up may return false, while
later calls return true.
**Returns**

- (boolean)

### #numeric_project_id

```ruby
def numeric_project_id() -> Integer, nil
```
Returns the unique numeric ID of the project hosting the application,
or `nil` if the application is not running on Google Cloud.


<br />

Caveat: this method does not work and returns `nil` on CloudShell.
**Returns**

- (Integer, nil)

### #project_id

```ruby
def project_id() -> String, nil
```
Returns the unique string ID of the project hosting the application,
or `nil` if the application is not running on Google Cloud.
**Returns**

- (String, nil)

### #raw_compute_engine?

```ruby
def raw_compute_engine?() -> boolean
```
Determine whether the application is running on "raw" Google Compute
Engine without using a higher level hosting product such as App
Engine or Kubernetes Engine.
**Returns**

- (boolean)

### #variables

```ruby
def variables() -> Google::Cloud::Env::Variables
```
The variables access object. Use this to make direct queries for
environment variable information, or to mock out environment variables
for testing.
**Returns**

- ([Google::Cloud::Env::Variables](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-env/latest/Google-Cloud-Env-Variables.md.txt))

## Constants

### VERSION

**value:** "2.4.3".freeze  
Library version