Building Google Cloud Platform Solutions
上QQ阅读APP看书,第一时间看更新

Modifying output and behavior

As we've seen, the gcloud tool (and the broader Google Cloud SDK) offers many configuration properties and global flags to customize user experience and modify behavior. When it comes to scripting, knowing how to apply these configurations and flags can make a big difference on the overall ease of use and reliability of these tools.

Although the customizable default values and behaviors of gcloud configurations are useful for interactive workflows, when it comes to writing scripts, being explicit is usually the way to go. As we mentioned when covering configurations, gcloud offers a global flag in order to specify which configuration to use as well as a special NONE configuration that has no set properties. In many cases, it's a good idea to use these in tandem by appending --configuration=NONE to any command passed to gcloud. This will ensure that no changes to the underlying gcloud configuration interferes with the desired behavior of your scripts.

In addition to managing configuration properties, gcloud offers quite a few flags to modify command output. Understanding these flags is critical to ensure that the right information is captured, and that the results are usable for subsequent actions. The following is a list of commonly applicable flags when automating tasks using gcloud.

 

Because these scripts often run when no one is watching, it's important to make sure that they capture enough information to later effectively diagnose any issues that may have occurred. To this end, it is often a good idea to use the --log-http, --verbosity, and --token flags. Using these flags, scripts can produce highly actionable logs.

In addition to global flags that modify the output of diagnostic data, an important aspect of scripting with a tool such as gcloud is the usefulness of output as actionable data for subsequent commands. By default, command output is provided in a human-readable format. For ingestion by other tools, it's normally ideal to provide a more machine-friendly format. For this purpose, all commands in gcloud accept the --format flag, which is capable of producing most common formats including JSON, CSV, and YAML.

Many operational tasks involve dealing with entire sets of resources. To help with this, gcloud offers several flags to help shape and filter output according to a number of criteria. With flags such as --flatten and --filter, results can be trimmed down to a meaningful subset. In addition, flags such as --limit, --page-size, and --sort-by give users the ability to effectively manage large sets of data.

Flags that modify results are applied in a specific order: --flatten, --sort-by, --filter, then --limit. It's important to keep this in mind as that order is applied regardless of the order you use them.

Most operations in gcloud produce some type of list output. The formatting and filtering capabilities in gcloud are quite expressive. As we'll see, these modifiers offer powerful and complex methods of zeroing in on specific sets of results. For the purpose of exploring these modifiers, a good resource type is services as results are always available and don't require any provisioning. Let's take a look at some of the ways we can modify these results.

The gcloud services command group allows us to view and modify Google Cloud services and APIs from the command line. To see the services available, try running gcloud services list --available. This will output the services in a human-readable format. Now suppose we want to take some action on the services via a Python script. In this case, we probably want to see more detail about the service, and we probably want the results to be in a format that is more easily handled within the script. In this case, we can specify the format to be JSON by running the following command:

gcloud services list --available --format=JSON