Execute Google Cloud CLI Command

Overview

The Execute Google Cloud CLI Command action enables users to execute Google Cloud CLI commands directly from a bot workflow, providing streamlined access to Google Cloud resources. Users can input Google Cloud CLI commands manually or leverage AI to generate commands tailored to specific tasks.

Prerequisites

Linux Agent:

  1. Agent Configuration:
    Ensure the Linux Agent is configured. Refer to the Agent Installation Guide.
  2. Google Cloud SDK Installation: The Google Cloud SDK (gcloud) must be installed on the agent machine.
  3. Configuration Profile: A configuration profile must be created in the agent's config.yaml file, with GOOGLE_APPLICATION_CREDENTIALS set to the path of the Google service account JSON key file.
    1. Installation Guide: Refer to the Google Cloud SDK installation guide for installation details.

How to Use This Action?

To use the Execute Google Cloud CLI Command action, follow these steps:

  • In the bot workflow, navigate to Linux Actions and select Library.
    Navigate to Linux Action

  • Search for or locate the Execute Google Cloud CLI Command action and drag it into your workflow.

  • Select the Linux integration connected to the agent configured with the Google Cloud CLI.

  • Under the Parameters section, enter the required values as described in the Parameter Details section.

  • In the gcloud_command field, you can:

    1. Manually Enter the Google Cloud CLI command you want to execute.
      Gcloud Command Parameter
    2. Generate with AI: Click "Generate with AI" and input a prompt, such as "retrieve all storage buckets."
      Gcloud Command AI
  • Save or update the bot, then click Run to execute the bot or the Run button inside the action node. After execution, view results in the execution details.

Parameter Details

ParameterRequiredDescription
gcloud_commandYesThe Google Cloud CLI command to execute.
script_modeYesSpecifies whether the Google Cloud CLI command is executed as a script (combination of multiple commands) or as individual commands. Set to True for script execution, or False for individual command execution. Defaults to False. NOTE: Check format limitations in Limitations.
gcp_config_profileYesSpecifies the profile for loading Google Cloud credentials from config.yaml. This profile must include GOOGLE_APPLICATION_CREDENTIALS, pointing to the service account key path. For more information on the agent configuration file, refer to the Agent Installation Guide.
run_as_userYesSpecifies the user under which the command runs (defaults to "ubuntu"). Set this to another user if needed. Ensure gcloud SDK is configured for the specified user.
execution_timeoutYesMaximum time (in seconds) allowed for command execution, defaulting to 300 seconds.

Using Handlebar

You can use the Handlebar capabilities to reference values dynamically from the output of a previous node. This enables the creation of flexible and reusable commands within your workflow.

Referencing Particular Values

For example, to stop a virtual machine in Google Cloud, you can use the following Google Cloud CLI command:

gcloud compute instances stop --project <project-id> --zone <zone> --instance <instance-name>

If a previous node returns the project-id, zone, and instance-name, you can reference those values as follows:

gcloud compute instances stop --project {{$.nodes.list_gcp_instances[0].project_id}} --zone {{$.nodes.list_gcp_instances[0].zone}} --instance {{$.nodes.list_gcp_instances[0].instance_name}}

Note: To reference values from previous nodes, type $$ to view and select the desired data dynamically.

Explanation

In this example:

  1. The project-id, zone, and instance-name values are retrieved dynamically from the output of the list_gcp_instances node.
  2. The Handlebar syntax ({{}}) is used to reference attributes from the node's output.
  3. The index [0] specifies the first instance in the list of instances returned by the node.

The resulting command dynamically substitutes the placeholders with actual values. For example:

gcloud compute instances stop --project my-project --zone us-central1-a --instance my-instance

Limitations

  1. Outputs: This action does not process JSON output by default. Users must include flags like --format=json to ensure the output is in JSON format.
    1. Script mode: Not recommended if you intend to use data from the current CLI node as input for the next node in the workflow.
    2. Non-script mode: The output may not always be in JSON format. This depends on the behavior of the gcloud CLI, as certain commands do not produce JSON outputs by default unless explicitly requested.

Example Use Case

Scenario: Listing Storage Buckets and Their Details

As a cloud administrator, you manage Google Cloud storage buckets across projects. You want a quick, organized list of all storage buckets in a project to review metadata like location, creation date, and labels.

Command to List All Storage Buckets with Details: Use the following command to retrieve a structured JSON list of all storage buckets in a specified project:

gcloud storage buckets list --project=my-gcp-project-id